2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.ccsdk.sli.core.sli;
26 import java.util.LinkedList;
27 import java.util.List;
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;
45 public class SvcLogicExprListener extends ExprGrammarBaseListener
51 private static final Logger LOG = LoggerFactory
52 .getLogger(SvcLogicExprListener.class);
54 private SvcLogicExpression curExpr;
55 //private SvcLogicExpression topExpr;
56 private LinkedList<SvcLogicExpression> exprStack;
58 public SvcLogicExprListener()
60 exprStack = new LinkedList<>();
63 public SvcLogicExpression getParsedExpr()
68 private void pushOperand(SvcLogicExpression operand)
76 curExpr.addOperand(operand);
80 private void pushExpr(SvcLogicExpression expr)
84 exprStack.push(curExpr);
89 private void popExpr()
91 if (exprStack.isEmpty())
97 SvcLogicExpression lastExpr = curExpr;
98 curExpr = exprStack.pop();
99 curExpr.addOperand(lastExpr);
105 public void enterAtom(AtomContext ctx) {
106 String atomText = ctx.getText();
107 SvcLogicAtom newAtom = new SvcLogicAtom(atomText);
113 public void enterMultExpr(MultExprContext ctx) {
114 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
115 pushExpr(curBinExpr);
117 List<TerminalNode> opList = ctx.MULTOP();
119 for (TerminalNode nd : opList)
121 curBinExpr.addOperator(nd.getText());
127 public void exitMultExpr(MultExprContext ctx) {
132 public void exitAtom(AtomContext ctx) {
137 public void enterAddExpr(AddExprContext ctx) {
138 List<TerminalNode> opList = ctx.ADDOP();
141 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
142 pushExpr(curBinExpr);
145 for (TerminalNode nd : opList)
147 curBinExpr.addOperator(nd.getText());
153 public void exitAddExpr(AddExprContext ctx) {
158 public void enterFuncExpr(FuncExprContext ctx) {
159 LOG.trace("enterFuncExpr: text = "+ctx.getText());
160 LOG.trace("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText());
162 for (ExprContext expr: ctx.expr())
164 LOG.trace("enterFuncExpr - expr = "+expr.getText());
168 pushExpr(new SvcLogicFunctionCall(ctx.IDENTIFIER().getText()));
172 public void exitFuncExpr(FuncExprContext ctx) {
177 public void enterParenExpr(ParenExprContext ctx) {
178 LOG.trace("enterParenExpr: text = "+ctx.getText());
179 LOG.trace("enterParenExpr: expr = "+ctx.expr().getText());
183 public void exitParenExpr(ParenExprContext ctx) {
184 LOG.trace("exitParenExpr: text = "+ctx.getText());
188 public void enterRelExpr(RelExprContext ctx) {
189 List<TerminalNode> opList = ctx.RELOP();
192 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
193 pushExpr(curBinExpr);
196 for (TerminalNode nd : opList)
198 curBinExpr.addOperator(nd.getText());
204 public void exitRelExpr(RelExprContext ctx) {
209 public void enterCompareExpr(CompareExprContext ctx) {
211 TerminalNode nd = ctx.COMPAREOP();
213 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
214 pushExpr(curBinExpr);
216 curBinExpr.addOperator(nd.getText());
221 public void exitCompareExpr(CompareExprContext ctx) {
229 public void enterConstant(ConstantContext ctx) {
233 public void exitConstant(ConstantContext ctx) {
238 public void enterVariable(VariableContext ctx) {
242 public void exitVariable(VariableContext ctx) {
247 public void enterVariableLead(VariableLeadContext ctx) {
251 public void exitVariableLead(VariableLeadContext ctx) {
255 public void enterVariableTerm(VariableTermContext ctx) {
256 String name = ctx.getText();
258 int subscrStart = name.indexOf("[");
259 if (subscrStart > -1)
261 name = name.substring(0, subscrStart);
263 SvcLogicVariableTerm vterm = new SvcLogicVariableTerm(name);
268 public void exitVariableTerm(VariableTermContext ctx) {