Refactor YANG statement parser structure 45/92045/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 11 Aug 2020 08:47:25 +0000 (10:47 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 11 Aug 2020 15:35:18 +0000 (17:35 +0200)
Current definition leads to leading whitespace being considered
a part of the statement. This leads to misleading line/character
being reported as the start of the statement.

Fix this by introducing a top-level 'file', which encapsulates
at least one statement with its leading/trailing whitespace. The
other parts then come together naturally.

JIRA: YANGTOOLS-1129
Change-Id: I9f725b09eae6f8acf4397c97c60f99caaade798b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit b462288cc6e678b9350551c64ecda96196ebe8b4)

yang/yang-parser-rfc7950/src/main/antlr/org/opendaylight/yangtools/antlrv4/code/gen/YangStatementParser.g4
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/YangStatementStreamSource.java

index 9f836e2e3210053ccbc39cda5eaccdcfcde9111d..dadf258875e27209d5d4010fb7fabc7b1b78354c 100644 (file)
@@ -11,7 +11,10 @@ options {
     tokenVocab = YangStatementLexer;
 }
 
-statement : SEP* keyword SEP* (argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (statement)* SEP* RIGHT_BRACE SEP*) SEP*;
+// NOTE: we need to use SEP*/SEP+ because comments end up breaking whitespace
+//       sequences into two.
+file : SEP* statement SEP* EOF;
+statement : keyword (SEP+ argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (statement SEP*)* RIGHT_BRACE);
 keyword : (IDENTIFIER COLON)? IDENTIFIER;
 
 argument : STRING (SEP* PLUS SEP* STRING)* | IDENTIFIER;
index 354e8d93afe0b742d3ba113bf7b7a05fbac05aec..e46db1ff8c7d3b4cdb0e25f5c29e58781273413d 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
@@ -23,6 +24,7 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker;
 import org.antlr.v4.runtime.tree.TerminalNode;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementLexer;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
+import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.FileContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
 import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -166,7 +168,7 @@ public final class YangStatementStreamSource extends AbstractIdentifiable<Source
         lexer.addErrorListener(errorListener);
         parser.addErrorListener(errorListener);
 
-        final StatementContext result = parser.statement();
+        final FileContext result = parser.file();
         errorListener.validate();
 
         // Walk the resulting tree and replace each children with an immutable list, lowering memory requirements
@@ -174,6 +176,6 @@ public final class YangStatementStreamSource extends AbstractIdentifiable<Source
         // org.antlr.v4.runtime.Parser.TrimToSizeListener, but that does not make the tree immutable.
         ParseTreeWalker.DEFAULT.walk(MAKE_IMMUTABLE_LISTENER, result);
 
-        return result;
+        return verifyNotNull(result.statement());
     }
 }