Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ReplayParser.cs
1using System.Collections.Generic;
5
7{
11 public class ReplayParser : IKeyWord
12 {
16 public ReplayParser()
17 {
18 }
19
23 public string KeyWord => "REPLAY";
24
28 public string[] Aliases => null;
29
33 public string[] InternalKeywords => new string[]
34 {
35 "AS",
36 "FROM",
37 "WHERE",
38 "TOP",
39 "OFFSET",
40 "TO",
41 "XML",
42 "JSON",
43 "COUNTERS",
44 "TABLE"
45 };
46
53 public bool TryParse(ScriptParser Parser, out ScriptNode Result)
54 {
55 Result = null;
56
57 List<ScriptNode> Columns;
58 List<ScriptNode> ColumnNames;
59 ScriptNode Top = null;
60 string s;
61
62 s = Parser.PeekNextToken().ToUpper();
63 if (string.IsNullOrEmpty(s))
64 return false;
65
66 while (s == "TOP" || s == "DISTINCT" || s == "GENERIC")
67 {
68 switch (s)
69 {
70 case "TOP":
71 Parser.NextToken();
72 Top = Parser.ParseNoWhiteSpace();
73 break;
74 }
75
76 s = Parser.PeekNextToken().ToUpper();
77 if (string.IsNullOrEmpty(s))
78 return false;
79 }
80
81 if (s == "*")
82 {
83 Parser.NextToken();
84 Columns = null;
85 ColumnNames = null;
86 }
87 else
88 {
89 Columns = new List<ScriptNode>();
90 ColumnNames = new List<ScriptNode>();
91
92 ScriptNode Node;
93 ScriptNode Name;
94
95 while (true)
96 {
97 Node = Parser.ParseNoWhiteSpace();
98
99 Name = null;
100 Parser.SkipWhiteSpace();
101
102 s = Parser.PeekNextToken().ToUpper();
103 if (!string.IsNullOrEmpty(s) && s != "," && s != "FROM")
104 {
105 if (s == "AS")
106 Parser.NextToken();
107
108 Name = Parser.ParseNoWhiteSpace();
109 s = Parser.PeekNextToken();
110 }
111 else if (Node is VariableReference Ref)
112 Name = new ConstantElement(new StringValue(Ref.VariableName), Node.Start, Node.Length, Node.Expression);
113 else if (Node is NamedMember NamedMember)
114 Name = new ConstantElement(new StringValue(NamedMember.Name), Node.Start, Node.Length, Node.Expression);
115
116 Columns.Add(Node);
117 ColumnNames.Add(Name);
118
119 if (s != ",")
120 break;
121
122 Parser.NextToken();
123 }
124 }
125
126 s = Parser.NextToken().ToUpper();
127 if (s != "FROM")
128 return false;
129
130 if (!SelectParser.TryParseSources(Parser, out SourceDefinition Source))
131 return false;
132
133 ScriptNode Where = null;
134
135 s = Parser.PeekNextToken().ToUpper();
136 if (s == "WHERE")
137 {
138 Parser.NextToken();
139 Where = Parser.ParseOrs();
140
141 s = Parser.PeekNextToken().ToUpper();
142 }
143
144 ScriptNode Offset = null;
145
146 if (s == "OFFSET")
147 {
148 Parser.NextToken();
149 Offset = Parser.ParseNoWhiteSpace();
150 s = Parser.PeekNextToken().ToUpper();
151 }
152
153 ScriptNode To = null;
154
155 if (s == "TO")
156 {
157 Parser.NextToken();
158 To = Parser.ParseNoWhiteSpace();
159 }
160
161 Result = new Replay(Columns?.ToArray(), ColumnNames?.ToArray(), Source, Where,
162 Top, Offset, To, Parser.Start, Parser.Length, Parser.Expression);
163
164 return true;
165 }
166
167 }
168}
Represents a constant element value.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Script parser, for custom parsers.
Definition: ScriptParser.cs:10
string PeekNextToken()
Returns the next token to be parsed, without moving the position forward. If at the end of the expres...
string NextToken()
Returns the next token to be parsed, and moves the position forward correspondingly....
int Length
Length of script parsed
Definition: ScriptParser.cs:33
ScriptNode ParseOrs()
Parses ORs.
int Start
Start position in expression
Definition: ScriptParser.cs:28
Expression Expression
Expression being parsed.
Definition: ScriptParser.cs:43
void SkipWhiteSpace()
If current position is whitespace, moves the current position forward to the first non-whitespace cha...
Represents a variable reference.
string[] Aliases
Keyword aliases, if available, null if none.
Definition: ReplayParser.cs:28
string KeyWord
Keyword associated with custom parser.
Definition: ReplayParser.cs:23
string[] InternalKeywords
Any keywords used internally by the custom parser.
Definition: ReplayParser.cs:33
bool TryParse(ScriptParser Parser, out ScriptNode Result)
Tries to parse a script node.
Definition: ReplayParser.cs:53
Executes a REPLAY statement against the ledger.
Definition: Replay.cs:24
Abstract base class for source definitions
Interface for keywords with custom parsing.
Definition: IKeyWord.cs:9