c9bd2c61a499ad76d73b6fb23967ea197785be46
[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.Verify;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.annotation.concurrent.Immutable;
14 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.ArgumentContext;
15 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.KeywordContext;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.YangConstants;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
21 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
22 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
23 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
28
29 @Immutable
30 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
31     private final List<String> toBeSkipped = new ArrayList<>();
32     private final String sourceName;
33     private QNameToStatementDefinition stmtDef;
34     private PrefixToModule prefixes;
35     private StatementWriter writer;
36
37     public YangStatementParserListenerImpl(final String sourceName) {
38         this.sourceName = sourceName;
39     }
40
41     public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
42         this.writer = writer;
43         this.stmtDef = stmtDef;
44     }
45
46     public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes) {
47         this.writer = writer;
48         this.stmtDef = stmtDef;
49         this.prefixes = prefixes;
50     }
51
52     @Override
53     public void enterStatement(final StatementContext ctx) {
54         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
55             .getStart().getLine(), ctx.getStart().getCharPositionInLine());
56         final String keywordTxt = Verify.verifyNotNull(ctx.getChild(KeywordContext.class, 0)).getText();
57         final QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, keywordTxt);
58         final QName validStatementDefinition = Utils.getValidStatementDefinition(prefixes, stmtDef, identifier);
59
60         if (stmtDef == null || validStatementDefinition == null || !toBeSkipped.isEmpty()) {
61             SourceException.throwIf(writer.getPhase() == ModelProcessingPhase.FULL_DECLARATION, ref,
62                     "%s is not a YANG statement or use of extension.", keywordTxt);
63             toBeSkipped.add(keywordTxt);
64             return;
65         }
66
67         final ArgumentContext argumentCtx = ctx.getChild(ArgumentContext.class, 0);
68         final String argument = argumentCtx != null ? Utils.stringFromStringContext(argumentCtx) : null;
69         writer.startStatement(validStatementDefinition, argument, ref);
70     }
71
72     @Override
73     public void exitStatement(final StatementContext ctx) {
74         final StatementSourceReference ref = DeclarationInTextSource.atPosition(
75             sourceName, ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
76
77         KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
78         String statementName = keyword.getText();
79         QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
80         if (stmtDef != null && Utils.getValidStatementDefinition(prefixes, stmtDef, identifier) != null
81                 && toBeSkipped.isEmpty()) {
82             writer.endStatement(ref);
83         }
84
85         // No-op if the statement is not on the list
86         toBeSkipped.remove(statementName);
87     }
88 }