2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
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.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.model.api.meta.StatementDefinition;
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.antlr.YangStatementParser.StatementContext;
29 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
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;
43 * This class represents implementation of StatementStreamSource in order to emit YANG statements using supplied
46 * @author Robert Varga
49 public final class YangStatementStreamSource extends AbstractSimpleIdentifiable<SourceIdentifier>
50 implements StatementStreamSource {
51 private final IRStatement rootStatement;
52 private final String sourceName;
54 private YangStatementStreamSource(final SourceIdentifier identifier, final IRStatement rootStatement,
55 final String sourceName) {
57 this.rootStatement = requireNonNull(rootStatement);
58 this.sourceName = sourceName;
62 * Create a {@link YangStatementStreamSource} for a {@link YangTextSchemaSource}.
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
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));
76 * Create a {@link YangStatementStreamSource} for a {@link IRSchemaSource}.
78 * @param source YangTextSchemaSource, must not be null
79 * @return A new {@link YangStatementStreamSource}
80 * @throws NullPointerException if {@code source} is null
82 public static YangStatementStreamSource create(final IRSchemaSource source) {
83 return create(source.getIdentifier(), source.getRootStatement(), source.getSymbolicName().orElse(null));
86 public static YangStatementStreamSource create(final SourceIdentifier identifier, final IRStatement rootStatement,
87 final String symbolicName) {
88 return new YangStatementStreamSource(identifier, rootStatement, symbolicName);
92 public void writePreLinkage(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
93 new StatementContextVisitor(sourceName, writer, stmtDef, null, YangVersion.VERSION_1).visit(rootStatement);
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) {
101 StatementDefinition resolveStatement(final QNameModule module, final String localName) {
102 return stmtDef.getByNamespaceAndLocalName(module.getNamespace(), localName);
104 }.visit(rootStatement);
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);
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) {
118 QName getValidStatementDefinition(final IRKeyword keyword, final StatementSourceReference ref) {
119 final QName ret = super.getValidStatementDefinition(keyword, ref);
121 throw new SourceException(ref, "%s is not a YANG statement or use of extension.",
122 keyword.asStringDeclaration());
126 }.visit(rootStatement);
129 IRStatement rootStatement() {
130 return rootStatement;
133 static StatementContext parseYangSource(final YangTextSchemaSource source)
134 throws IOException, YangSyntaxErrorException {
135 try (InputStream stream = source.openStream()) {
136 return parseYangSource(source.getIdentifier(), stream);
140 private static StatementContext parseYangSource(final SourceIdentifier source, final InputStream stream)
141 throws IOException, YangSyntaxErrorException {
142 final YangStatementLexer lexer = new CompactYangStatementLexer(CharStreams.fromStream(stream));
143 final YangStatementParser parser = new YangStatementParser(new CommonTokenStream(lexer));
144 // disconnect from console error output
145 lexer.removeErrorListeners();
146 parser.removeErrorListeners();
148 final YangErrorListener errorListener = new YangErrorListener(source);
149 lexer.addErrorListener(errorListener);
150 parser.addErrorListener(errorListener);
152 final FileContext result = parser.file();
153 errorListener.validate();
154 return verifyNotNull(result.statement());