Refactor YANG statement parser structure 27/92027/3
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 13:12:28 +0000 (15:12 +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>
yang/yang-parser-antlr/src/main/antlr4/org/opendaylight/yangtools/yang/parser/antlr/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 7d26fa946cfd0879222491ae2efb59775a79bb2d..9f1f6dd08b0b512437b4c6c47249f9a2d9f5c7e1 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;
@@ -31,6 +32,7 @@ import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementLexer;
 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser;
+import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.FileContext;
 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.StatementContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
@@ -171,7 +173,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
@@ -179,6 +181,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());
     }
 }