1 | /** |
2 | * Licensed under the Apache License, Version 2.0 (the "License"); |
3 | * you may not use this file except in compliance with the License. |
4 | * You may obtain a copy of the License at |
5 | * |
6 | * http://www.apache.org/licenses/LICENSE-2.0 |
7 | * |
8 | * Unless required by applicable law or agreed to in writing, |
9 | * software distributed under the License is distributed on an |
10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
11 | * KIND, either express or implied. See the License for the |
12 | * specific language governing permissions and limitations |
13 | * under the License. |
14 | */ |
15 | |
16 | package org.wso2.siddhi.query.compiler; |
17 | |
18 | import org.antlr.runtime.ANTLRStringStream; |
19 | import org.antlr.runtime.CommonTokenStream; |
20 | import org.antlr.runtime.tree.CommonTree; |
21 | import org.antlr.runtime.tree.CommonTreeNodeStream; |
22 | import org.wso2.siddhi.query.api.ExecutionPlan; |
23 | import org.wso2.siddhi.query.api.definition.StreamDefinition; |
24 | import org.wso2.siddhi.query.api.query.Query; |
25 | import org.wso2.siddhi.query.compiler.exception.SiddhiPraserException; |
26 | |
27 | import java.util.List; |
28 | |
29 | public class SiddhiCompiler { |
30 | |
31 | // private static SiddhiCompiler compiler = null; |
32 | // |
33 | // private SiddhiCompiler() { |
34 | // } |
35 | // |
36 | // /** |
37 | // * get the SiddhiCompiler instance |
38 | // * |
39 | // * @return the instance of query factory |
40 | // */ |
41 | // public static SiddhiCompiler getInstance() { |
42 | // if (null == compiler) { |
43 | // compiler = new SiddhiCompiler(); |
44 | // } |
45 | // return compiler; |
46 | // } |
47 | |
48 | public static List<ExecutionPlan> parse(String source) throws SiddhiPraserException { |
49 | try { |
50 | SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer(); |
51 | lexer.setCharStream(new ANTLRStringStream(source)); |
52 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
53 | SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens); |
54 | |
55 | SiddhiQLGrammarParser.executionPlan_return r = parser.executionPlan(); |
56 | CommonTree t = (CommonTree) r.getTree(); |
57 | |
58 | CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); |
59 | nodes.setTokenStream(tokens); |
60 | SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes); |
61 | return walker.executionPlan(); |
62 | |
63 | } catch (Throwable e) { |
64 | throw new SiddhiPraserException(e.getMessage(), e); |
65 | } |
66 | } |
67 | |
68 | public static StreamDefinition parseStreamDefinition(String source) throws SiddhiPraserException { |
69 | try { |
70 | SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer(); |
71 | lexer.setCharStream(new ANTLRStringStream(source)); |
72 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
73 | SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens); |
74 | |
75 | SiddhiQLGrammarParser.definitionStream_return r = parser.definitionStream(); |
76 | CommonTree t = (CommonTree) r.getTree(); |
77 | |
78 | CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); |
79 | nodes.setTokenStream(tokens); |
80 | SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes); |
81 | return walker.definitionStream(); |
82 | |
83 | } catch (Throwable e) { |
84 | throw new SiddhiPraserException(e.getMessage(), e); |
85 | } |
86 | } |
87 | |
88 | public static Query parseQuery(String source) throws SiddhiPraserException { |
89 | try { |
90 | SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer(); |
91 | lexer.setCharStream(new ANTLRStringStream(source)); |
92 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
93 | SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens); |
94 | |
95 | SiddhiQLGrammarParser.query_return r = parser.query(); |
96 | CommonTree t = (CommonTree) r.getTree(); |
97 | |
98 | CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); |
99 | nodes.setTokenStream(tokens); |
100 | SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes); |
101 | return walker.query(); |
102 | |
103 | } catch (Throwable e) { |
104 | throw new SiddhiPraserException(e.getMessage(), e); |
105 | } |
106 | } |
107 | |
108 | |
109 | } |