Rework keyword parser rule
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / YangStatementStreamSource.java
index 588cac66ad990dec83aa0b3f4c06d9cbc249d69b..a307b4bb0415c97a0f2ac522f4785ce58ce1561d 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,8 @@ 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.KeywordContext;
 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;
@@ -143,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);
     }
@@ -166,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
@@ -174,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());
     }
 }