Speeded up ANTLR tree walk for statement parser.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangStatementParserListenerImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import com.google.common.base.Verify;
13 import java.util.ArrayList;
14 import java.util.List;
15 import javax.annotation.concurrent.Immutable;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.ArgumentContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.KeywordContext;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
24 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
25 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
26 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
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.stmt.rfc6020.TypeUtils;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 @Immutable
36 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
37
38     private StatementWriter writer;
39     private final String sourceName;
40     private QNameToStatementDefinition stmtDef;
41     private PrefixToModule prefixes;
42     private final List<String> toBeSkipped = new ArrayList<>();
43     private boolean isType = false;
44     private static final Logger LOG = LoggerFactory.getLogger(YangStatementParserListenerImpl.class);
45
46     public YangStatementParserListenerImpl(final String sourceName) {
47         this.sourceName = sourceName;
48     }
49
50     public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
51         this.writer = writer;
52         this.stmtDef = stmtDef;
53     }
54
55     public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes) {
56         this.writer = writer;
57         this.stmtDef = stmtDef;
58         this.prefixes = prefixes;
59     }
60
61     @Override
62     public void enterStatement(final YangStatementParser.StatementContext ctx) {
63         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
64                 .getStart().getLine(), ctx.getStart().getCharPositionInLine());
65         final KeywordContext keywordCtx = Verify.verifyNotNull(ctx.getChild(KeywordContext.class, 0));
66         final ArgumentContext argumentCtx = ctx.getChild(ArgumentContext.class, 0);
67
68         String keywordTxt = keywordCtx.getText();
69         try {
70             // FIXME: This is broken for extensions - not all statements are in RFC6020 ns.
71             final QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, keywordTxt);
72             if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier)
73                     && toBeSkipped.isEmpty()) {
74                 final String argument = argumentCtx != null ? Utils.stringFromStringContext(argumentCtx) : null;
75                 // See,s to be fishy specialcase
76                 if (identifier.equals(Rfc6020Mapping.TYPE.getStatementName())) {
77                     Preconditions.checkArgument(argument != null);
78                     if (TypeUtils.isYangTypeBodyStmtString(argument)) {
79                         writer.startStatement(QName.create(YangConstants.RFC6020_YIN_MODULE, argument), ref);
80                     } else {
81                         writer.startStatement(QName.create(YangConstants.RFC6020_YIN_MODULE, Rfc6020Mapping
82                                 .TYPE.getStatementName().getLocalName()), ref);
83                     }
84                     writer.argumentValue(argument, ref);
85                 } else {
86                     writer.startStatement(identifier, ref);
87                     if(argument != null) {
88                         writer.argumentValue(argument, ref);
89                     }
90                 }
91             } else if (writer.getPhase().equals(ModelProcessingPhase.FULL_DECLARATION)) {
92                     throw new IllegalArgumentException(identifier.getLocalName() + " is not a YANG statement "
93                             + "or use of extension. Source: " + ref);
94             } else {
95                 toBeSkipped.add(keywordTxt);
96             }
97         } catch (SourceException e) {
98             throw Throwables.propagate(e);
99         }
100     }
101
102     @Override
103     public void exitStatement(final YangStatementParser.StatementContext ctx) {
104         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(), ctx
105                 .getStart().getCharPositionInLine());
106         try {
107             KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
108             String statementName = keyword.getText();
109             QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
110             if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier)
111                     && toBeSkipped.isEmpty()) {
112                 writer.endStatement(ref);
113             }
114             if (toBeSkipped.contains(statementName)) {
115                 toBeSkipped.remove(statementName);
116             }
117         } catch (SourceException e) {
118             LOG.warn(e.getMessage(), e);
119         }
120     }
121 }