2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdnc.sli;
24 import java.util.LinkedList;
25 import java.util.List;
27 import org.antlr.v4.runtime.tree.TerminalNode;
28 import org.openecomp.sdnc.sli.ExprGrammarParser.AddExprContext;
29 import org.openecomp.sdnc.sli.ExprGrammarParser.AtomContext;
30 import org.openecomp.sdnc.sli.ExprGrammarParser.CompareExprContext;
31 import org.openecomp.sdnc.sli.ExprGrammarParser.ConstantContext;
32 import org.openecomp.sdnc.sli.ExprGrammarParser.ExprContext;
33 import org.openecomp.sdnc.sli.ExprGrammarParser.FuncExprContext;
34 import org.openecomp.sdnc.sli.ExprGrammarParser.MultExprContext;
35 import org.openecomp.sdnc.sli.ExprGrammarParser.ParenExprContext;
36 import org.openecomp.sdnc.sli.ExprGrammarParser.RelExprContext;
37 import org.openecomp.sdnc.sli.ExprGrammarParser.VariableContext;
38 import org.openecomp.sdnc.sli.ExprGrammarParser.VariableLeadContext;
39 import org.openecomp.sdnc.sli.ExprGrammarParser.VariableTermContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class SvcLogicExprListener extends ExprGrammarBaseListener
49 private static final Logger LOG = LoggerFactory
50 .getLogger(SvcLogicExprListener.class);
52 private SvcLogicExpression curExpr;
53 private SvcLogicExpression topExpr;
54 private LinkedList<SvcLogicExpression> exprStack;
56 public SvcLogicExprListener()
58 exprStack = new LinkedList<SvcLogicExpression>();
61 public SvcLogicExpression getParsedExpr()
66 private void pushOperand(SvcLogicExpression operand)
74 curExpr.addOperand(operand);
78 private void pushExpr(SvcLogicExpression expr)
80 LOG.debug("Pushing expression ["+expr.getClass().getName()+"]");
83 exprStack.push(curExpr);
88 private void popExpr()
90 if (exprStack.isEmpty())
92 LOG.debug("Popping last expression");
97 SvcLogicExpression lastExpr = curExpr;
98 curExpr = exprStack.pop();
99 curExpr.addOperand(lastExpr);
100 LOG.debug("New curExpr is ["+curExpr.getClass().getName()+"]");
106 public void enterAtom(AtomContext ctx) {
108 String atomText = ctx.getText();
110 LOG.debug("enterAtom: text = "+atomText);
113 SvcLogicAtom newAtom = new SvcLogicAtom(atomText);
120 public void enterMultExpr(MultExprContext ctx) {
121 LOG.debug("enterMultExpr: text = "+ctx.getText());
123 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
124 pushExpr(curBinExpr);
126 List<TerminalNode> opList = ctx.MULTOP();
128 for (TerminalNode nd : opList)
130 LOG.debug("enterMultExpr: operator - "+nd.getText());
131 curBinExpr.addOperator(nd.getText());
137 public void exitMultExpr(MultExprContext ctx) {
139 LOG.debug("exitMultExpr: text = "+ctx.getText());
146 public void exitAtom(AtomContext ctx) {
147 LOG.debug("exitAtom: text = "+ctx.getText());
152 public void enterAddExpr(AddExprContext ctx) {
153 LOG.debug("enterAddExpr: text = "+ctx.getText());
154 List<TerminalNode> opList = ctx.ADDOP();
157 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
158 pushExpr(curBinExpr);
161 for (TerminalNode nd : opList)
163 LOG.debug("enterAddExpr: operator - "+nd.getText());
164 curBinExpr.addOperator(nd.getText());
170 public void exitAddExpr(AddExprContext ctx) {
171 LOG.debug("exitAddExpr: text = "+ctx.getText());
177 public void enterFuncExpr(FuncExprContext ctx) {
178 LOG.debug("enterFuncExpr: text = "+ctx.getText());
179 LOG.debug("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText());
181 for (ExprContext expr: ctx.expr())
183 LOG.debug("enterFuncExpr - expr = "+expr.getText());
187 pushExpr(new SvcLogicFunctionCall(ctx.IDENTIFIER().getText()));
191 public void exitFuncExpr(FuncExprContext ctx) {
192 LOG.debug("exitFuncExpr: text = "+ctx.getText());
198 public void enterParenExpr(ParenExprContext ctx) {
199 LOG.debug("enterParenExpr: text = "+ctx.getText());
200 LOG.debug("enterParenExpr: expr = "+ctx.expr().getText());
204 public void exitParenExpr(ParenExprContext ctx) {
205 LOG.debug("exitParenExpr: text = "+ctx.getText());
209 public void enterRelExpr(RelExprContext ctx) {
210 LOG.debug("enterRelExpr: text = "+ctx.getText());
212 List<TerminalNode> opList = ctx.RELOP();
215 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
216 pushExpr(curBinExpr);
219 for (TerminalNode nd : opList)
221 LOG.debug("enterRelExpr: operator - "+nd.getText());
222 curBinExpr.addOperator(nd.getText());
228 public void exitRelExpr(RelExprContext ctx) {
229 LOG.debug("exitRelExpr: text = "+ctx.getText());
235 public void enterCompareExpr(CompareExprContext ctx) {
236 LOG.debug("enterCompareExpr: text = "+ctx.getText());
238 TerminalNode nd = ctx.COMPAREOP();
240 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
241 pushExpr(curBinExpr);
243 LOG.debug("enterCompareExpr: operator - "+nd.getText());
244 curBinExpr.addOperator(nd.getText());
249 public void exitCompareExpr(CompareExprContext ctx) {
250 LOG.debug("exitCompareExpr : text = "+ctx.getText());
258 public void enterConstant(ConstantContext ctx) {
259 LOG.debug("enterConstant: text = "+ctx.getText());
263 public void exitConstant(ConstantContext ctx) {
264 LOG.debug("exitConstant: text = "+ctx.getText());
269 public void enterVariable(VariableContext ctx) {
270 LOG.debug("enterVariable: text = "+ctx.getText());
276 public void exitVariable(VariableContext ctx) {
277 LOG.debug("exitVariable: text ="+ctx.getText());
283 public void enterVariableLead(VariableLeadContext ctx) {
285 LOG.debug("enterVariableLead: text ="+ctx.getText());
291 public void exitVariableLead(VariableLeadContext ctx) {
293 LOG.debug("exitVariableLead: text ="+ctx.getText());
297 public void enterVariableTerm(VariableTermContext ctx) {
298 LOG.debug("enterVariableTerm: text ="+ctx.getText());
300 String name = ctx.getText();
302 int subscrStart = name.indexOf("[");
303 if (subscrStart > -1)
305 name = name.substring(0, subscrStart);
307 SvcLogicVariableTerm vterm = new SvcLogicVariableTerm(name);
312 public void exitVariableTerm(VariableTermContext ctx) {
313 LOG.debug("exitVariableTerm: text="+ctx.getText());