Take advantage of keyword tokenization
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / StatementContextVisitor.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.VerifyException;
14 import java.util.Optional;
15 import org.antlr.v4.runtime.Token;
16 import org.antlr.v4.runtime.tree.ParseTree;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.common.YangConstants;
20 import org.opendaylight.yangtools.yang.common.YangVersion;
21 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
22 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.ArgumentContext;
23 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.KeywordContext;
24 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.StatementContext;
25 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
26 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
27 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter.ResumedStatement;
31
32 class StatementContextVisitor {
33     private final QNameToStatementDefinition stmtDef;
34     private final ArgumentContextUtils utils;
35     private final StatementWriter writer;
36     private final PrefixToModule prefixes;
37     private final String sourceName;
38
39     StatementContextVisitor(final String sourceName, final StatementWriter writer,
40             final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes, final YangVersion yangVersion) {
41         this.writer = requireNonNull(writer);
42         this.stmtDef = requireNonNull(stmtDef);
43         this.utils = ArgumentContextUtils.forVersion(yangVersion);
44         this.sourceName = sourceName;
45         this.prefixes = prefixes;
46     }
47
48     void visit(final StatementContext context) {
49         processStatement(0, context);
50     }
51
52     /**
53      * Based on identifier read from source and collections of relevant prefixes and statement definitions mappings
54      * provided for actual phase, method resolves and returns valid QName for declared statement to be written.
55      * This applies to any declared statement, including unknown statements.
56      *
57      * @param prefixes collection of all relevant prefix mappings supplied for actual parsing phase
58      * @param stmtDef collection of all relevant statement definition mappings provided for actual parsing phase
59      * @param keyword statement keyword text to parse from source
60      * @param ref Source reference
61      * @return valid QName for declared statement to be written, or null
62      */
63     QName getValidStatementDefinition(final KeywordContext keyword, final StatementSourceReference ref) {
64         switch (keyword.getChildCount()) {
65             case 1:
66                 final StatementDefinition def = stmtDef.get(QName.create(YangConstants.RFC6020_YIN_MODULE,
67                     keyword.getChild(0).getText()));
68                 return def != null ? def.getStatementName() : null;
69             case 3:
70                 if (prefixes == null) {
71                     // No prefixes to look up from
72                     return null;
73                 }
74
75                 final String prefix = keyword.getChild(0).getText();
76                 final QNameModule qNameModule = prefixes.get(prefix);
77                 if (qNameModule == null) {
78                     // Failed to look the namespace
79                     return null;
80                 }
81
82                 final String localName = keyword.getChild(2).getText();
83                 final StatementDefinition foundStmtDef = resolveStatement(qNameModule, localName);
84                 return foundStmtDef != null ? foundStmtDef.getStatementName() : null;
85             default:
86                 throw new VerifyException("Unexpected shape of " + keyword);
87         }
88     }
89
90     StatementDefinition resolveStatement(final QNameModule module, final String localName) {
91         return stmtDef.get(QName.create(module, localName));
92     }
93
94     // Normal entry point, checks for potential resume
95     private boolean processStatement(final int myOffset, final StatementContext ctx) {
96         final Optional<? extends ResumedStatement> optResumed = writer.resumeStatement(myOffset);
97         if (optResumed.isPresent()) {
98             final ResumedStatement resumed = optResumed.get();
99             return resumed.isFullyDefined() || doProcessStatement(ctx, resumed.getSourceReference());
100         }
101         return processNewStatement(myOffset, ctx);
102     }
103
104     // Slow-path allocation of a new statement
105     private boolean processNewStatement(final int myOffset, final StatementContext ctx) {
106         final Token start = ctx.getStart();
107         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, start.getLine(),
108             start.getCharPositionInLine());
109         final QName def = getValidStatementDefinition(verifyNotNull(ctx.getChild(KeywordContext.class, 0)), ref);
110         if (def == null) {
111             return false;
112         }
113
114         final ArgumentContext argumentCtx = ctx.getChild(ArgumentContext.class, 0);
115         final String argument = argumentCtx == null ? null : utils.stringFromStringContext(argumentCtx, ref);
116         writer.startStatement(myOffset, def, argument, ref);
117         return doProcessStatement(ctx, ref);
118     }
119
120     // Actual processing
121     private boolean doProcessStatement(final StatementContext ctx, final StatementSourceReference ref) {
122         int childOffset = 0;
123         boolean fullyDefined = true;
124         if (ctx.children != null) {
125             for (ParseTree s : ctx.children) {
126                 if (s instanceof StatementContext && !processStatement(childOffset++, (StatementContext) s)) {
127                     fullyDefined = false;
128                 }
129             }
130         }
131
132         writer.storeStatement(childOffset, fullyDefined);
133         writer.endStatement(ref);
134         return fullyDefined;
135     }
136 }