Rework keyword parser rule 46/92046/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 11 Aug 2020 09:38:52 +0000 (11:38 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 11 Aug 2020 15:35:50 +0000 (17:35 +0200)
A keyword is either a plain identifier, or two identifiers concatenated
through a colon. Make this a bit clearer, aiding debugging of issues.

Also push KeywordContext down to
StatementContextVisitor.getValidStatementDefinition(), so that we can
improve efficiency once IDENTIFIER token has been fixed up.

Change-Id: Ic64285255564ae33f8ad208b9a3598ba20cd26fb
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 220f54de00be7e3906349059fdd4bb139859ee23)

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/StatementContextVisitor.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/YangStatementStreamSource.java

index dadf258875e27209d5d4010fb7fabc7b1b78354c..1d7554bf781b553beeff994d68a8d941788d8f84 100644 (file)
@@ -15,6 +15,6 @@ options {
 //       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;
+keyword : IDENTIFIER (COLON IDENTIFIER)?;
 
 argument : STRING (SEP* PLUS SEP* STRING)* | IDENTIFIER;
index 70bff4d2ad1c2b994c5b7077bfc00858d3b31e80..e5a9c679e683e5a3b85988b06a2d6c8e19bd145a 100644 (file)
@@ -56,11 +56,12 @@ class StatementContextVisitor {
      *
      * @param prefixes collection of all relevant prefix mappings supplied for actual parsing phase
      * @param stmtDef collection of all relevant statement definition mappings provided for actual parsing phase
-     * @param keywordText statement keyword text to parse from source
+     * @param keyword statement keyword text to parse from source
      * @param ref Source reference
      * @return valid QName for declared statement to be written, or null
      */
-    QName getValidStatementDefinition(final String keywordText, final StatementSourceReference ref) {
+    QName getValidStatementDefinition(final KeywordContext keyword, final StatementSourceReference ref) {
+        final String keywordText = keyword.getText();
         final int firstColon = keywordText.indexOf(':');
         if (firstColon == -1) {
             final StatementDefinition def = stmtDef.get(QName.create(YangConstants.RFC6020_YIN_MODULE, keywordText));
@@ -103,11 +104,10 @@ class StatementContextVisitor {
 
     // Slow-path allocation of a new statement
     private boolean processNewStatement(final int myOffset, final StatementContext ctx) {
-        final String keywordTxt = verifyNotNull(ctx.getChild(KeywordContext.class, 0)).getText();
         final Token start = ctx.getStart();
         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, start.getLine(),
             start.getCharPositionInLine());
-        final QName def = getValidStatementDefinition(keywordTxt, ref);
+        final QName def = getValidStatementDefinition(verifyNotNull(ctx.getChild(KeywordContext.class, 0)), ref);
         if (def == null) {
             return false;
         }
index e46db1ff8c7d3b4cdb0e25f5c29e58781273413d..e6ad2cdf3308905eeee4a7bb55c52d4eb7aafc24 100644 (file)
@@ -25,6 +25,7 @@ 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.KeywordContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
 import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -145,9 +146,13 @@ public final class YangStatementStreamSource extends AbstractIdentifiable<Source
             final PrefixToModule prefixes, final YangVersion yangVersion) {
         new StatementContextVisitor(sourceName, writer, stmtDef, prefixes, yangVersion) {
             @Override
-            QName getValidStatementDefinition(final String keywordText, final StatementSourceReference ref) {
-                return SourceException.throwIfNull(super.getValidStatementDefinition(keywordText, ref), ref,
-                    "%s is not a YANG statement or use of extension.", keywordText);
+            QName getValidStatementDefinition(final KeywordContext keyword, final StatementSourceReference ref) {
+                final QName ret = super.getValidStatementDefinition(keyword, ref);
+                if (ret == null) {
+                    throw new SourceException(ref, "%s is not a YANG statement or use of extension.",
+                        keyword.getText());
+                }
+                return ret;
             }
         }.visit(context);
     }