7
7
package org .elasticsearch .xpack .eql .parser ;
8
8
9
9
import org .antlr .v4 .runtime .ParserRuleContext ;
10
- import org .antlr .v4 .runtime .Token ;
11
- import org .antlr .v4 .runtime .misc .Interval ;
12
10
import org .antlr .v4 .runtime .tree .ParseTree ;
13
- import org .antlr . v4 . runtime . tree . TerminalNode ;
11
+ import org .elasticsearch . xpack . ql . parser . ParserUtils ;
14
12
import org .elasticsearch .xpack .ql .plan .logical .LogicalPlan ;
15
- import org .elasticsearch .xpack .ql .tree .Location ;
16
13
import org .elasticsearch .xpack .ql .tree .Source ;
17
- import org .elasticsearch .xpack .ql .util .Check ;
18
14
19
- import java .util .ArrayList ;
20
15
import java .util .List ;
21
16
22
17
/**
@@ -26,90 +21,19 @@ abstract class AbstractBuilder extends EqlBaseBaseVisitor<Object> {
26
21
27
22
@ Override
28
23
public Object visit (ParseTree tree ) {
29
- Object result = super .visit (tree );
30
- Check .notNull (result , "Don't know how to handle context [{}] with value [{}]" , tree .getClass (), tree .getText ());
31
- return result ;
24
+ return ParserUtils .visit (super ::visit , tree );
32
25
}
33
26
34
- @ SuppressWarnings ("unchecked" )
35
27
protected <T > T typedParsing (ParseTree ctx , Class <T > type ) {
36
- Object result = ctx .accept (this );
37
- if (type .isInstance (result )) {
38
- return (T ) result ;
39
- }
40
-
41
- throw new ParsingException (source (ctx ), "Invalid query '{}'[{}] given; expected {} but found {}" ,
42
- ctx .getText (), ctx .getClass ().getSimpleName (),
43
- type .getSimpleName (), (result != null ? result .getClass ().getSimpleName () : "null" ));
28
+ return ParserUtils .typedParsing (this , ctx , type );
44
29
}
45
30
46
31
protected LogicalPlan plan (ParseTree ctx ) {
47
32
return typedParsing (ctx , LogicalPlan .class );
48
33
}
49
34
50
35
protected List <LogicalPlan > plans (List <? extends ParserRuleContext > ctxs ) {
51
- return visitList (ctxs , LogicalPlan .class );
52
- }
53
-
54
- protected <T > List <T > visitList (List <? extends ParserRuleContext > contexts , Class <T > clazz ) {
55
- List <T > results = new ArrayList <>(contexts .size ());
56
- for (ParserRuleContext context : contexts ) {
57
- results .add (clazz .cast (visit (context )));
58
- }
59
- return results ;
60
- }
61
-
62
- static Source source (ParseTree ctx ) {
63
- if (ctx instanceof ParserRuleContext ) {
64
- return source ((ParserRuleContext ) ctx );
65
- }
66
- return Source .EMPTY ;
67
- }
68
-
69
- static Source source (TerminalNode terminalNode ) {
70
- Check .notNull (terminalNode , "terminalNode is null" );
71
- return source (terminalNode .getSymbol ());
72
- }
73
-
74
- static Source source (ParserRuleContext parserRuleContext ) {
75
- Check .notNull (parserRuleContext , "parserRuleContext is null" );
76
- Token start = parserRuleContext .start ;
77
- Token stop = parserRuleContext .stop != null ? parserRuleContext .stop : start ;
78
- Interval interval = new Interval (start .getStartIndex (), stop .getStopIndex ());
79
- String text = start .getInputStream ().getText (interval );
80
- return new Source (new Location (start .getLine (), start .getCharPositionInLine ()), text );
81
- }
82
-
83
- static Source source (Token token ) {
84
- Check .notNull (token , "token is null" );
85
- String text = token .getInputStream ().getText (new Interval (token .getStartIndex (), token .getStopIndex ()));
86
- return new Source (new Location (token .getLine (), token .getCharPositionInLine ()), text );
87
- }
88
-
89
- Source source (ParserRuleContext begin , ParserRuleContext end ) {
90
- Check .notNull (begin , "begin is null" );
91
- Check .notNull (end , "end is null" );
92
- Token start = begin .start ;
93
- Token stop = end .stop != null ? end .stop : begin .stop ;
94
- Interval interval = new Interval (start .getStartIndex (), stop .getStopIndex ());
95
- String text = start .getInputStream ().getText (interval );
96
- return new Source (new Location (start .getLine (), start .getCharPositionInLine ()), text );
97
- }
98
-
99
- static Source source (TerminalNode begin , ParserRuleContext end ) {
100
- Check .notNull (begin , "begin is null" );
101
- Check .notNull (end , "end is null" );
102
- Token start = begin .getSymbol ();
103
- Token stop = end .stop != null ? end .stop : start ;
104
- String text = start .getInputStream ().getText (new Interval (start .getStartIndex (), stop .getStopIndex ()));
105
- return new Source (new Location (start .getLine (), start .getCharPositionInLine ()), text );
106
- }
107
-
108
- /**
109
- * Retrieves the raw text of the node (without interpreting it as a string literal).
110
- */
111
- static String text (ParseTree node ) {
112
- return node == null ? null : node .getText ();
36
+ return ParserUtils .visitList (this , ctxs , LogicalPlan .class );
113
37
}
114
38
115
39
public static String unquoteString (Source source ) {
@@ -212,11 +136,4 @@ private static void checkForSingleQuotedString(Source source, String text, int i
212
136
"Use double quotes [\" ] to define string literals, not single quotes [']" );
213
137
}
214
138
}
215
-
216
- @ Override
217
- public Object visitTerminal (TerminalNode node ) {
218
- Source source = source (node );
219
- throw new ParsingException (source , "Does not know how to handle {}" , source .text ());
220
- }
221
-
222
139
}
0 commit comments