forked from jdevillard/JmesPath.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJmesPathParser.cs
227 lines (163 loc) · 7.6 KB
/
JmesPathParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using StarodubOleg.GPPG.Runtime;
using DevLab.JmesPath.Tokens;
namespace DevLab.JmesPath
{
partial class JmesPathParser
{
readonly IJmesPathGenerator generator_;
public JmesPathParser(AbstractScanner<ValueType, LexLocation> scanner,
IJmesPathGenerator generator)
: base(scanner)
{
generator_ = generator;
}
#region Implementation
void OnExpression() => generator_.OnExpression();
void ResolveParsingState()
{
// the grammar does not specify this but the canonical
// implementation accepts constructs that consist of
// projections immediately followed by a multi_select_list.
// these constructs should be separated with a T_DOT
// because these really are sub_expressions in disguise.
// when this method is called, upon each successful
// parse of an expression, we need to identify
// whether we are in such a case and insert a T_DOT
// in the token stream if necessary
// only interested in cases where previous construct is a projection
if (!generator_.IsProjection())
return;
var scanner = this.Scanner as JmesPathScanner;
System.Diagnostics.Debug.Assert(scanner != null);
var next = scanner.EnqueueAndReturnInitialToken(NextToken);
NextToken = 0;
if (next == (int)TokenType.T_LBRACKET && IsParsingMultiSelectList())
ResolveSubExpressionToMultiSelectList();
scanner.AddPushbackBufferToQueue();
}
bool IsParsingMultiSelectList()
{
var scanner = this.Scanner as JmesPathScanner;
System.Diagnostics.Debug.Assert(scanner != null);
// this method is called upon each successfull parse of an expression
//
// (T_LBRACKET) T_STAR T_RBRACKET => list_wildcard => bracket_specifier
// (T_LBRACKET) T_STAR ... <any> => hash_wildcard => in the context of a multi_select_list
// (T_LBRACKET) T_COLON => slice_expression => bracket_specifier
// (T_LBRACKET) T_NUMBER => number => bracket_specifier
// (T_LBRACKET) <any> => multi_select_list
var next = scanner.GetAndEnqueue();
switch (next)
{
case (int)TokenType.T_STAR:
{
var lookahead = scanner.GetAndEnqueue();
if (lookahead != (int)TokenType.T_RBRACKET)
return true;
}
break;
case (int)TokenType.T_COLON:
case (int)TokenType.T_NUMBER:
{
// next construct is a bracket specifier
// do nothing
}
break;
// otherwise, resolve to a multi_select_list
default:
return true;
}
return false;
}
void ResolveSubExpressionToMultiSelectList()
{
// next construct is a multi_select_list
// check that the previous construct is a projection
// and prepend a "." to parse as a sub_expression
NextToken = (int)TokenType.T_DOT;
}
#endregion
#region Expressions
void OnSubExpression() => generator_.OnSubExpression();
#region index_expression
void OnIndex(Token token)
{
var number = token as NumberToken;
System.Diagnostics.Debug.Assert(token.Type == TokenType.T_NUMBER);
System.Diagnostics.Debug.Assert(number != null);
var index = (int)number.Value;
generator_.OnIndex(index);
}
void OnFilterProjection() => generator_.OnFilterProjection();
void OnFlattenProjection() => generator_.OnFlattenProjection();
void OnListWildcardProjection() => generator_.OnListWildcardProjection();
void OnIndexExpression() => generator_.OnIndexExpression();
void OnSliceExpression(Token start, Token stop, Token step)
{
System.Diagnostics.Debug.Assert(start == null || start is NumberToken);
System.Diagnostics.Debug.Assert(stop == null || stop is NumberToken);
System.Diagnostics.Debug.Assert(step == null || step is NumberToken);
var startIndex = (int?)start?.Value;
var stopIndex = (int?)stop?.Value;
var stepIndex = (int?)step?.Value;
generator_.OnSliceExpression(startIndex, stopIndex, stepIndex);
}
#endregion
#region comparator_expression
void OnComparisonExpression(Token token)
{
switch (token.Type)
{
case TokenType.T_EQ: generator_.OnComparisonEqual(); break;
case TokenType.T_GE: generator_.OnComparisonGreaterOrEqual(); break;
case TokenType.T_GT: generator_.OnComparisonGreater(); break;
case TokenType.T_LE: generator_.OnComparisonLesserOrEqual(); break;
case TokenType.T_LT: generator_.OnComparisonLesser(); break;
case TokenType.T_NE: generator_.OnComparisonNotEqual(); break;
default:
System.Diagnostics.Debug.Assert(false);
throw new NotSupportedException();
}
}
#endregion
void OnOrExpression() => generator_.OnOrExpression();
void OnAndExpression() => generator_.OnAndExpression();
void OnNotExpression() => generator_.OnNotExpression();
void OnIdentifier(Token token) =>
generator_.OnIdentifier((string)token.Value);
void OnHashWildcardProjection() =>
generator_.OnHashWildcardProjection();
#region multi_select_hash
void PushMultiSelectHash() => generator_.PushMultiSelectHash();
void AddMultiSelectHashExpression() => generator_.AddMultiSelectHashExpression();
void PopMultiSelectHash() => generator_.PopMultiSelectHash();
#endregion
#region multi_select_list
void PushMultiSelectList() => generator_.PushMultiSelectList();
void AddMultiSelectListExpression() => generator_.AddMultiSelectListExpression();
void PopMultiSelectList() => generator_.PopMultiSelectList();
#endregion
void OnLiteralString(Token token) =>
generator_.OnLiteralString((string)((LiteralStringToken)token).Value);
void OnPipeExpression() => generator_.OnPipeExpression();
#region function_expression
void PushFunction() => generator_.PushFunction();
void PopFunction(Token token)
{
System.Diagnostics.Debug.Assert(token.Type == TokenType.T_USTRING);
var name = (string)token.Value;
generator_.PopFunction(name);
}
void AddFunctionArg() => generator_.AddFunctionArg();
void OnExpressionType() => generator_.OnExpressionType();
#endregion
void OnRawString(Token token)
{
var @string = (string)token.Value;
generator_.OnRawString(@string);
}
void OnCurrentNode() => generator_.OnCurrentNode();
#endregion // Expressions
}
}