X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=xpath%2Fyang-xpath-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fxpath%2Fimpl%2FAntlrXPathParser.java;h=1c381a4ae836aae1419baca7c5c94e69c0dffcb9;hb=650caaea10614de461155de677540936edba23b1;hp=a7e09afbc40d0accb19b61185afc7e66822a8cf5;hpb=4248f4a3b94775cab515f14a63d2331c9be80e9e;p=yangtools.git diff --git a/xpath/yang-xpath-impl/src/main/java/org/opendaylight/yangtools/yang/xpath/impl/AntlrXPathParser.java b/xpath/yang-xpath-impl/src/main/java/org/opendaylight/yangtools/yang/xpath/impl/AntlrXPathParser.java index a7e09afbc4..1c381a4ae8 100644 --- a/xpath/yang-xpath-impl/src/main/java/org/opendaylight/yangtools/yang/xpath/impl/AntlrXPathParser.java +++ b/xpath/yang-xpath-impl/src/main/java/org/opendaylight/yangtools/yang/xpath/impl/AntlrXPathParser.java @@ -240,15 +240,12 @@ abstract class AntlrXPathParser implements YangXPathParser { abstract QNameStep createStep(YangXPathAxis axis, String prefix, String localName, List predicates); private QNameStep createStep(final YangXPathAxis axis, final QNameContext expr, final List predicates) { - switch (expr.getChildCount()) { - case 1: - return createStep(axis, getChild(expr, NCNameContext.class, 0).getText(), predicates); - case 3: - return createStep(axis, getChild(expr, NCNameContext.class, 0).getText(), + return switch (expr.getChildCount()) { + case 1 -> createStep(axis, getChild(expr, NCNameContext.class, 0).getText(), predicates); + case 3 -> createStep(axis, getChild(expr, NCNameContext.class, 0).getText(), getChild(expr, NCNameContext.class, 2).getText(), predicates); - default: - throw illegalShape(expr); - } + default -> throw illegalShape(expr); + }; } @SuppressWarnings("checkstyle:illegalCatch") @@ -336,18 +333,11 @@ abstract class AntlrXPathParser implements YangXPathParser { // We are mapping functions to RFC7950 YIN namespace, to keep us consistent with type/statement definitions final FunctionNameContext name = getChild(expr, FunctionNameContext.class, 0); - final QName parsed; - switch (name.getChildCount()) { - case 1: - parsed = QName.create(YangConstants.RFC6020_YIN_MODULE, name.getChild(0).getText()); - break; - case 3: - parsed = createQName(name.getChild(0).getText(), name.getChild(2).getText()); - break; - default: - throw illegalShape(name); - } - + final QName parsed = switch (name.getChildCount()) { + case 1 -> QName.create(YangConstants.RFC6020_YIN_MODULE, name.getChild(0).getText()); + case 3 -> createQName(name.getChild(0).getText(), name.getChild(2).getText()); + default -> throw illegalShape(name); + }; final List args = expr.expr().stream().map(this::parseExpr).collect(ImmutableList.toImmutableList()); final YangFunction func = YANG_FUNCTIONS.get(parsed); if (func != null) { @@ -370,8 +360,8 @@ abstract class AntlrXPathParser implements YangXPathParser { private YangLocationPath parseLocationPath(final LocationPathContext expr) { verifyChildCount(expr, 1); final ParseTree first = expr.getChild(0); - if (first instanceof RelativeLocationPathContext) { - return YangLocationPath.relative(parseLocationPathSteps((RelativeLocationPathContext) first)); + if (first instanceof RelativeLocationPathContext relativeLocation) { + return YangLocationPath.relative(parseLocationPathSteps(relativeLocation)); } final AbsoluteLocationPathNorootContext abs = verifyTree(AbsoluteLocationPathNorootContext.class, first); @@ -385,12 +375,8 @@ abstract class AntlrXPathParser implements YangXPathParser { private YangExpr parseMultiplicative(final MultiplicativeExprContext expr) { final ParseTree first = expr.getChild(0); - final YangExpr left; - if (first instanceof UnaryExprNoRootContext) { - left = parseUnary((UnaryExprNoRootContext) first); - } else { - left = YangLocationPath.root(); - } + final YangExpr left = first instanceof UnaryExprNoRootContext unary ? parseUnary(unary) + : YangLocationPath.root(); if (expr.getChildCount() == 1) { return left; } @@ -404,8 +390,8 @@ abstract class AntlrXPathParser implements YangXPathParser { private YangExpr parsePathExpr(final PathExprNoRootContext expr) { final ParseTree first = expr.getChild(0); - if (first instanceof LocationPathContext) { - return parseLocationPath((LocationPathContext) first); + if (first instanceof LocationPathContext location) { + return parseLocationPath(location); } final YangExpr filter = parseFilter(verifyTree(FilterExprContext.class, first)); @@ -431,16 +417,15 @@ abstract class AntlrXPathParser implements YangXPathParser { verifyChildCount(expr, 1); final ParseTree first = expr.getChild(0); - if (first instanceof TerminalNode) { - return parseTerminal((TerminalNode) first); - } - if (first instanceof FunctionCallContext) { - return parseFunctionCall((FunctionCallContext) first); - } - if (first instanceof VariableReferenceContext) { - return YangVariableReferenceExpr.of(parseQName(((VariableReferenceContext) first).qName())); + if (first instanceof TerminalNode terminal) { + return parseTerminal(terminal); + } else if (first instanceof FunctionCallContext function) { + return parseFunctionCall(function); + } else if (first instanceof VariableReferenceContext variable) { + return YangVariableReferenceExpr.of(parseQName(variable.qName())); + } else { + throw illegalShape(first); } - throw illegalShape(first); } private YangExpr parseRelational(final RelationalExprContext expr) { @@ -472,16 +457,15 @@ abstract class AntlrXPathParser implements YangXPathParser { private YangExpr parseTerminal(final TerminalNode term) { final String text = term.getText(); - switch (term.getSymbol().getType()) { - case xpathParser.Literal: + return switch (term.getSymbol().getType()) { + case xpathParser.Literal -> { // We have to strip quotes haveLiteral = true; - return YangLiteralExpr.of(text.substring(1, text.length() - 1)); - case xpathParser.Number: - return mathSupport.createNumber(text); - default: - throw illegalShape(term); - } + yield YangLiteralExpr.of(text.substring(1, text.length() - 1)); + } + case xpathParser.Number -> mathSupport.createNumber(text); + default -> throw illegalShape(term); + }; } private YangExpr parseUnary(final UnaryExprNoRootContext expr) { @@ -492,14 +476,14 @@ abstract class AntlrXPathParser implements YangXPathParser { // Even number of '-' tokens cancel out return ret; } - return ret instanceof YangNumberExpr ? mathSupport.negateNumber((YangNumberExpr) ret) : YangNegateExpr.of(ret); + return ret instanceof YangNumberExpr number ? mathSupport.negateNumber(number) : YangNegateExpr.of(ret); } private YangExpr parseUnion(final UnionExprNoRootContext expr) { final ParseTree first = expr.getChild(0); final YangExpr path; - if (first instanceof PathExprNoRootContext) { - path = parsePathExpr((PathExprNoRootContext) first); + if (first instanceof PathExprNoRootContext noRoot) { + path = parsePathExpr(noRoot); if (expr.getChildCount() == 1) { return path; } @@ -513,14 +497,8 @@ abstract class AntlrXPathParser implements YangXPathParser { // Deduplicate expressions so we do not perform useless unioning final Set expressions = new LinkedHashSet<>(); expressions.add(path); - if (union instanceof YangNaryExpr) { - // If the result is a union expression, integrate it into this expression - final YangNaryExpr nary = (YangNaryExpr) union; - if (nary.getOperator() == YangNaryOperator.UNION) { - expressions.addAll(nary.getExpressions()); - } else { - expressions.add(union); - } + if (union instanceof YangNaryExpr nary && nary.getOperator() == YangNaryOperator.UNION) { + expressions.addAll(nary.getExpressions()); } else { expressions.add(union); } @@ -534,7 +512,7 @@ abstract class AntlrXPathParser implements YangXPathParser { final YangBinaryOperator operator = nextOperator(it); final YangExpr right = parseMultiplicative(nextContext(it, MultiplicativeExprContext.class)); final Optional simple = simplifyNumbers(operator, ret, right); - ret = simple.isPresent() ? simple.get() : operator.exprWith(ret, right); + ret = simple.isPresent() ? simple.orElseThrow() : operator.exprWith(ret, right); } while (it.hasNext()); return ret; @@ -542,9 +520,9 @@ abstract class AntlrXPathParser implements YangXPathParser { private Optional simplifyNumbers(final YangBinaryOperator operator, final YangExpr left, final YangExpr right) { - if (left instanceof YangNumberExpr && right instanceof YangNumberExpr) { + if (left instanceof YangNumberExpr leftNumber && right instanceof YangNumberExpr rightNumber) { // Constant folding on numbers -- precision plays a role here - return mathSupport.tryEvaluate(operator, (YangNumberExpr)left, (YangNumberExpr)right); + return mathSupport.tryEvaluate(operator, leftNumber, rightNumber); } return Optional.empty(); } @@ -568,7 +546,7 @@ abstract class AntlrXPathParser implements YangXPathParser { } final Optional simple = simplifyNumbers(operator, ret, right); - ret = simple.isPresent() ? simple.get() : operator.exprWith(ret, right); + ret = simple.isPresent() ? simple.orElseThrow() : operator.exprWith(ret, right); } while (it.hasNext()); return ret; @@ -580,7 +558,7 @@ abstract class AntlrXPathParser implements YangXPathParser { final YangBinaryOperator operator = nextOperator(it); final YangExpr right = parseAdditive(nextContext(it, AdditiveExprContext.class)); final Optional simple = simplifyNumbers(operator, ret, right); - ret = simple.isPresent() ? simple.get() : operator.exprWith(ret, right); + ret = simple.isPresent() ? simple.orElseThrow() : operator.exprWith(ret, right); } while (it.hasNext()); return ret; @@ -590,14 +568,11 @@ abstract class AntlrXPathParser implements YangXPathParser { if (expr.getChildCount() == 1) { final AbbreviatedStepContext abbrev = getChild(expr, AbbreviatedStepContext.class, 0); verifyChildCount(abbrev, 1); - switch (getTerminalType(abbrev, 0)) { - case xpathParser.DOT: - return YangXPathAxis.SELF.asStep(); - case xpathParser.DOTDOT: - return YangXPathAxis.PARENT.asStep(); - default: - throw illegalShape(abbrev); - } + return switch (getTerminalType(abbrev, 0)) { + case xpathParser.DOT -> YangXPathAxis.SELF.asStep(); + case xpathParser.DOTDOT -> YangXPathAxis.PARENT.asStep(); + default -> throw illegalShape(abbrev); + }; } final int size = verifyAtLeastChildren(expr, 2); @@ -608,50 +583,47 @@ abstract class AntlrXPathParser implements YangXPathParser { final YangXPathAxis axis = parseAxis(getChild(expr, AxisSpecifierContext.class, 0)); final NodeTestContext nodeTest = getChild(expr, NodeTestContext.class, 1); - switch (nodeTest.getChildCount()) { - case 1: + return switch (nodeTest.getChildCount()) { + case 1 -> { final NameTestContext nameChild = getChild(nodeTest, NameTestContext.class, 0); final ParseTree first = nameChild.getChild(0); - if (first instanceof TerminalNode) { - verify(((TerminalNode) first).getSymbol().getType() == xpathParser.MUL); - return axis.asStep(predicates); + if (first instanceof TerminalNode terminal) { + verify(terminal.getSymbol().getType() == xpathParser.MUL); + yield axis.asStep(predicates); } - return createStep(axis, verifyTree(QNameContext.class, first), predicates); - case 3: - return axis.asStep(parseNodeType(nodeTest.getChild(0)), predicates); - case 4: + yield createStep(axis, verifyTree(QNameContext.class, first), predicates); + } + case 3 -> axis.asStep(parseNodeType(nodeTest.getChild(0)), predicates); + case 4 -> { final String text = verifyToken(nodeTest, 2, xpathParser.Literal).getText(); - return axis.asStep(text.substring(1, text.length() - 1), predicates); - default: - throw illegalShape(nodeTest); - } + yield axis.asStep(text.substring(1, text.length() - 1), predicates); + } + default -> throw illegalShape(nodeTest); + }; } private static YangXPathAxis parseAxis(final AxisSpecifierContext expr) { - switch (expr.getChildCount()) { - case 0: - return YangXPathAxis.CHILD; - case 1: + return switch (expr.getChildCount()) { + case 0 -> YangXPathAxis.CHILD; + case 1 -> { verify(getTerminalType(expr, 0) == xpathParser.AT, "Unhandled axis specifier shape %s", expr); - return YangXPathAxis.ATTRIBUTE; - case 2: + yield YangXPathAxis.ATTRIBUTE; + } + case 2 -> { final String str = verifyTerminal(expr.getChild(0)).getText(); - return verifyNotNull(XPATH_AXES.get(str), "Unhandled axis %s", str); - default: - throw illegalShape(expr); - } + yield verifyNotNull(XPATH_AXES.get(str), "Unhandled axis %s", str); + } + default -> throw illegalShape(expr); + }; } private QName parseQName(final QNameContext expr) { - switch (expr.getChildCount()) { - case 1: - return createQName(getChild(expr, NCNameContext.class, 0).getText()); - case 3: - return createQName(getChild(expr, NCNameContext.class, 0).getText(), + return switch (expr.getChildCount()) { + case 1 -> createQName(getChild(expr, NCNameContext.class, 0).getText()); + case 3 -> createQName(getChild(expr, NCNameContext.class, 0).getText(), getChild(expr, NCNameContext.class, 2).getText()); - default: - throw illegalShape(expr); - } + default -> throw illegalShape(expr); + }; } private static T nextContext(final Iterator it, final Class type) { @@ -677,13 +649,10 @@ abstract class AntlrXPathParser implements YangXPathParser { } private static Optional parseStepShorthand(final ParseTree tree) { - switch (verifyTerminal(tree).getSymbol().getType()) { - case xpathParser.PATHSEP: - return Optional.empty(); - case xpathParser.ABRPATH: - return Optional.of(YangXPathAxis.DESCENDANT_OR_SELF.asStep()); - default: - throw illegalShape(tree); - } + return switch (verifyTerminal(tree).getSymbol().getType()) { + case xpathParser.PATHSEP -> Optional.empty(); + case xpathParser.ABRPATH -> Optional.of(YangXPathAxis.DESCENDANT_OR_SELF.asStep()); + default -> throw illegalShape(tree); + }; } }