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