Promote SchemaSourceRepresentation
[yangtools.git] / parser / 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.Reader;
16 import org.antlr.v4.runtime.CharStreams;
17 import org.antlr.v4.runtime.CommonTokenStream;
18 import org.opendaylight.yangtools.concepts.AbstractSimpleIdentifiable;
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.ir.IRKeyword;
23 import org.opendaylight.yangtools.yang.ir.IRStatement;
24 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
27 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
29 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementLexer;
30 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser;
31 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.FileContext;
32 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.StatementContext;
33 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.CompactYangStatementLexer;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.IRSupport;
36 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixResolver;
37 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
38 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
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 AbstractSimpleIdentifiable<SourceIdentifier>
50         implements StatementStreamSource {
51     private final IRStatement rootStatement;
52     private final String sourceName;
53
54     private YangStatementStreamSource(final SourceIdentifier sourceId, final IRStatement rootStatement,
55             final String sourceName) {
56         super(sourceId);
57         this.rootStatement = requireNonNull(rootStatement);
58         this.sourceName = sourceName;
59     }
60
61     /**
62      * Create a {@link YangStatementStreamSource} for a {@link YangTextSource}.
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 YangTextSource source)
70             throws IOException, YangSyntaxErrorException {
71         return new YangStatementStreamSource(source.sourceId(),
72             IRSupport.createStatement(parseYangSource(source)), source.symbolicName());
73     }
74
75     /**
76      * Create a {@link YangStatementStreamSource} for a {@link YangIRSchemaSource}.
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 YangIRSchemaSource source) {
83         return create(source.sourceId(), source.getRootStatement(), source.symbolicName());
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 PrefixResolver 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 PrefixResolver 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 PrefixResolver 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     static StatementContext parseYangSource(final YangTextSource source)
134             throws IOException, YangSyntaxErrorException {
135         try (var reader = source.openStream()) {
136             return parseYangSource(source.sourceId(), reader);
137         }
138     }
139
140     private static StatementContext parseYangSource(final SourceIdentifier sourceId, final Reader stream)
141             throws IOException, YangSyntaxErrorException {
142         final YangStatementLexer lexer = new CompactYangStatementLexer(CharStreams.fromReader(stream));
143         final YangStatementParser parser = new YangStatementParser(new CommonTokenStream(lexer));
144         // disconnect from console error output
145         lexer.removeErrorListeners();
146         parser.removeErrorListeners();
147
148         final YangErrorListener errorListener = new YangErrorListener(sourceId);
149         lexer.addErrorListener(errorListener);
150         parser.addErrorListener(errorListener);
151
152         final FileContext result = parser.file();
153         errorListener.validate();
154         return verifyNotNull(result.statement());
155     }
156 }