Add an explicit intermediate YANG representation
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / YangStatementStreamSource.java
1 /*
2  * Copyright (c) 2017 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 java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import org.antlr.v4.runtime.CharStreams;
16 import org.antlr.v4.runtime.CommonTokenStream;
17 import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.YangVersion;
21 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
22 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementLexer;
26 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser;
27 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.FileContext;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.CompactYangStatementLexer;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRKeyword;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
32 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
37
38 /**
39  * This class represents implementation of StatementStreamSource in order to emit YANG statements using supplied
40  * StatementWriter.
41  *
42  * @author Robert Varga
43  */
44 @Beta
45 public final class YangStatementStreamSource extends AbstractIdentifiable<SourceIdentifier>
46         implements StatementStreamSource {
47     private final IRStatement rootStatement;
48     private final String sourceName;
49
50     private YangStatementStreamSource(final SourceIdentifier identifier,  final IRStatement rootStatement,
51             final String sourceName) {
52         super(identifier);
53         this.rootStatement = requireNonNull(rootStatement);
54         this.sourceName = sourceName;
55     }
56
57     /**
58      * Create a {@link YangStatementStreamSource} for a {@link YangTextSchemaSource}.
59      *
60      * @param source YangTextSchemaSource, must not be null
61      * @return A new {@link YangStatementStreamSource}
62      * @throws IOException When we fail to read the source
63      * @throws YangSyntaxErrorException If the source fails basic parsing
64      */
65     public static YangStatementStreamSource create(final YangTextSchemaSource source) throws IOException,
66             YangSyntaxErrorException {
67         final IRStatement rootStatement;
68         try (InputStream stream = source.openStream()) {
69             rootStatement = parseYangSource(source.getIdentifier(), stream);
70         }
71
72         return new YangStatementStreamSource(source.getIdentifier(), rootStatement,
73             source.getSymbolicName().orElse(null));
74     }
75
76     /**
77      * Create a {@link YangStatementStreamSource} for a {@link ASTSchemaSource}.
78      *
79      * @param source YangTextSchemaSource, must not be null
80      * @return A new {@link YangStatementStreamSource}
81      */
82     public static YangStatementStreamSource create(final ASTSchemaSource source) {
83         return create(source.getIdentifier(), source.getRootStatement(), source.getSymbolicName().orElse(null));
84     }
85
86     public static YangStatementStreamSource create(final SourceIdentifier identifier, final IRStatement rootStatement,
87             final String symbolicName) {
88         return new YangStatementStreamSource(identifier, rootStatement, symbolicName);
89     }
90
91     @Override
92     public void writePreLinkage(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
93         new StatementContextVisitor(sourceName, writer, stmtDef, null, YangVersion.VERSION_1).visit(rootStatement);
94     }
95
96     @Override
97     public void writeLinkage(final StatementWriter writer, final QNameToStatementDefinition stmtDef,
98             final PrefixToModule preLinkagePrefixes, final YangVersion yangVersion) {
99         new StatementContextVisitor(sourceName, writer, stmtDef, preLinkagePrefixes, yangVersion) {
100             @Override
101             StatementDefinition resolveStatement(final QNameModule module, final String localName) {
102                 return stmtDef.getByNamespaceAndLocalName(module.getNamespace(), localName);
103             }
104         }.visit(rootStatement);
105     }
106
107     @Override
108     public void writeLinkageAndStatementDefinitions(final StatementWriter writer,
109             final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes, final YangVersion yangVersion) {
110         new StatementContextVisitor(sourceName, writer, stmtDef, prefixes, yangVersion).visit(rootStatement);
111     }
112
113     @Override
114     public void writeFull(final StatementWriter writer, final QNameToStatementDefinition stmtDef,
115             final PrefixToModule prefixes, final YangVersion yangVersion) {
116         new StatementContextVisitor(sourceName, writer, stmtDef, prefixes, yangVersion) {
117             @Override
118             QName getValidStatementDefinition(final IRKeyword keyword, final StatementSourceReference ref) {
119                 final QName ret = super.getValidStatementDefinition(keyword, ref);
120                 if (ret == null) {
121                     throw new SourceException(ref, "%s is not a YANG statement or use of extension.",
122                         keyword.asStringDeclaration());
123                 }
124                 return ret;
125             }
126         }.visit(rootStatement);
127     }
128
129     IRStatement rootStatement() {
130         return rootStatement;
131     }
132
133     private static IRStatement parseYangSource(final SourceIdentifier source, final InputStream stream)
134             throws IOException, YangSyntaxErrorException {
135         final YangStatementLexer lexer = new CompactYangStatementLexer(CharStreams.fromStream(stream));
136         final YangStatementParser parser = new YangStatementParser(new CommonTokenStream(lexer));
137         // disconnect from console error output
138         lexer.removeErrorListeners();
139         parser.removeErrorListeners();
140
141         final YangErrorListener errorListener = new YangErrorListener(source);
142         lexer.addErrorListener(errorListener);
143         parser.addErrorListener(errorListener);
144
145         final FileContext result = parser.file();
146         errorListener.validate();
147         return IRStatement.forContext(result.statement());
148     }
149 }