185986d8e2ce0857e63c13dfbbace57a2144441c
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicExprListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.ccsdk.sli.core.sli;
25
26 import java.util.LinkedList;
27 import java.util.List;
28
29 import org.antlr.v4.runtime.tree.TerminalNode;
30 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AddExprContext;
31 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AtomContext;
32 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.CompareExprContext;
33 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ConstantContext;
34 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ExprContext;
35 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.FuncExprContext;
36 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.MultExprContext;
37 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ParenExprContext;
38 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.RelExprContext;
39 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableContext;
40 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableLeadContext;
41 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableTermContext;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class SvcLogicExprListener extends ExprGrammarBaseListener 
46 {
47
48
49
50
51         private static final Logger LOG = LoggerFactory
52                         .getLogger(SvcLogicExprListener.class);
53         
54         private SvcLogicExpression curExpr;
55         //private SvcLogicExpression topExpr;
56         private LinkedList<SvcLogicExpression> exprStack;
57         
58         public SvcLogicExprListener()
59         {
60                 exprStack = new LinkedList<>();
61         }
62         
63         public SvcLogicExpression getParsedExpr()
64         {
65                 return(curExpr);
66         }
67
68         private void pushOperand(SvcLogicExpression operand)
69         {
70                 if (curExpr == null)
71                 {
72                         curExpr = operand;
73                 }
74                 else
75                 {
76                         curExpr.addOperand(operand);
77                 }
78         }
79         
80         private void pushExpr(SvcLogicExpression expr)
81         {
82                 LOG.trace("Pushing expression ["+expr.getClass().getName()+"]");
83                 if (curExpr != null)
84                 {
85                         exprStack.push(curExpr);
86                 }
87                 curExpr = expr;
88         }
89         
90         private void popExpr()
91         {
92                 if (exprStack.isEmpty())
93                 {
94                         //topExpr = curExpr;
95                 }
96                 else
97                 {
98                         SvcLogicExpression lastExpr = curExpr;
99                         curExpr = exprStack.pop();
100                         curExpr.addOperand(lastExpr);
101                 }
102                 
103         }
104         
105         @Override
106         public void enterAtom(AtomContext ctx) {
107                 String atomText = ctx.getText();        
108                 SvcLogicAtom newAtom = new SvcLogicAtom(atomText);
109                 pushExpr(newAtom);
110         }
111
112
113         @Override
114         public void enterMultExpr(MultExprContext ctx) {
115                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
116                 pushExpr(curBinExpr);
117                 
118                 List<TerminalNode> opList = ctx.MULTOP();
119                 
120                 for (TerminalNode nd : opList)
121                 {
122                         curBinExpr.addOperator(nd.getText());
123                 }
124
125         }
126
127         @Override
128         public void exitMultExpr(MultExprContext ctx) {
129                 popExpr();
130         }
131
132         @Override
133         public void exitAtom(AtomContext ctx) {
134                 popExpr();
135         }
136
137         @Override
138         public void enterAddExpr(AddExprContext ctx) {
139                 List<TerminalNode> opList = ctx.ADDOP();
140                 
141
142                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
143                 pushExpr(curBinExpr);
144
145                 
146                 for (TerminalNode nd : opList)
147                 {
148                         curBinExpr.addOperator(nd.getText());
149                 }
150                 
151         }
152
153         @Override
154         public void exitAddExpr(AddExprContext ctx) {           
155                 popExpr();
156         }
157
158         @Override
159         public void enterFuncExpr(FuncExprContext ctx) {
160                 LOG.trace("enterFuncExpr: text = "+ctx.getText());
161                 LOG.trace("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText());
162                 
163                 for (ExprContext expr: ctx.expr())
164                 {
165                         LOG.trace("enterFuncExpr - expr = "+expr.getText());
166                 }
167                 
168
169                 pushExpr(new SvcLogicFunctionCall(ctx.IDENTIFIER().getText()));
170         }
171
172         @Override
173         public void exitFuncExpr(FuncExprContext ctx) { 
174                 popExpr();
175         }
176
177         @Override
178         public void enterParenExpr(ParenExprContext ctx) {
179                 LOG.trace("enterParenExpr: text = "+ctx.getText());
180                 LOG.trace("enterParenExpr: expr = "+ctx.expr().getText());
181         }
182
183         @Override
184         public void exitParenExpr(ParenExprContext ctx) {
185                 LOG.trace("exitParenExpr: text = "+ctx.getText());
186         }
187
188         @Override
189         public void enterRelExpr(RelExprContext ctx) {  
190                 List<TerminalNode> opList = ctx.RELOP();
191                 
192
193                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
194                 pushExpr(curBinExpr);
195
196                 
197                 for (TerminalNode nd : opList)
198                 {
199                         curBinExpr.addOperator(nd.getText());
200                 }
201                 
202         }
203
204         @Override
205         public void exitRelExpr(RelExprContext ctx) {   
206                 popExpr();
207         }
208
209         @Override
210         public void enterCompareExpr(CompareExprContext ctx) {
211                 
212                 TerminalNode nd = ctx.COMPAREOP();
213
214                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
215                 pushExpr(curBinExpr);
216
217                 curBinExpr.addOperator(nd.getText());
218
219         }
220
221         @Override
222         public void exitCompareExpr(CompareExprContext ctx) {
223                 
224                 popExpr();
225         }
226
227
228         
229         @Override 
230         public void enterConstant(ConstantContext ctx) {
231         }
232
233         @Override
234         public void exitConstant(ConstantContext ctx) {
235         }
236
237
238         @Override
239         public void enterVariable(VariableContext ctx) {
240         }
241
242         @Override
243         public void exitVariable(VariableContext ctx) { 
244         }
245         
246
247         @Override
248         public void enterVariableLead(VariableLeadContext ctx) {
249         }
250
251         @Override
252         public void exitVariableLead(VariableLeadContext ctx) {
253         }
254
255         @Override
256         public void enterVariableTerm(VariableTermContext ctx) {                
257                 String name = ctx.getText();
258                 
259                 int subscrStart = name.indexOf("[");
260                 if (subscrStart > -1)
261                 {
262                         name = name.substring(0, subscrStart);
263                 }
264                 SvcLogicVariableTerm vterm = new SvcLogicVariableTerm(name);
265                 pushExpr(vterm);
266         }
267
268         @Override
269         public void exitVariableTerm(VariableTermContext ctx) {
270             popExpr();
271         }
272 }