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