Squash self-steps when parsing expression 02/86002/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 27 Nov 2019 00:17:33 +0000 (01:17 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 27 Nov 2019 01:04:34 +0000 (02:04 +0100)
There is no point to repeat self-reference steps, make sure we
skip them when creating a location path. This results in slightly
smaller expressions as well as more obvious comparisons.

JIRA: YANGTOOLS-1045
Change-Id: If681dce2a6bc7155965fed91c47f19f2c541805a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 63b3f4804b408d86a27a6dbe80c21ea7cca921b0)

yang/yang-xpath-impl/src/main/java/org/opendaylight/yangtools/yang/xpath/impl/AntlrXPathParser.java

index cbec6455aa5a805fa982e40278d0fe73ea8a2f5d..0c8a55413591fa14a36f69b46f0efc24dec6fbe3 100644 (file)
@@ -434,21 +434,24 @@ abstract class AntlrXPathParser implements YangXPathParser {
     private Deque<Step> parseLocationPathSteps(final RelativeLocationPathContext expr) {
         final Deque<Step> steps = new ArrayDeque<>(expr.getChildCount());
         final Iterator<ParseTree> it = expr.children.iterator();
-        steps.add(parseStep(nextContext(it, StepContext.class)));
+        addNotSelfStep(steps, parseStep(nextContext(it, StepContext.class)));
 
         while (it.hasNext()) {
             parseStepShorthand(it.next()).ifPresent(steps::add);
 
             // Parse step and add it if it's not SELF_STEP
-            final Step step = parseStep(nextContext(it, StepContext.class));
-            if (!SELF_STEP.equals(step)) {
-                steps.add(step);
-            }
+            addNotSelfStep(steps, parseStep(nextContext(it, StepContext.class)));
         }
 
         return steps;
     }
 
+    private static void addNotSelfStep(final Deque<Step> steps, final Step step) {
+        if (!SELF_STEP.equals(step)) {
+            steps.add(step);
+        }
+    }
+
     private YangExpr parseTerminal(final TerminalNode term) {
         final String text = term.getText();
         switch (term.getSymbol().getType()) {