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 java.util.Objects.requireNonNull;
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;
39 * This class represents implementation of StatementStreamSource in order to emit YANG statements using supplied
42 * @author Robert Varga
45 public final class YangStatementStreamSource extends AbstractIdentifiable<SourceIdentifier>
46 implements StatementStreamSource {
47 private final IRStatement rootStatement;
48 private final String sourceName;
50 private YangStatementStreamSource(final SourceIdentifier identifier, final IRStatement rootStatement,
51 final String sourceName) {
53 this.rootStatement = requireNonNull(rootStatement);
54 this.sourceName = sourceName;
58 * Create a {@link YangStatementStreamSource} for a {@link YangTextSchemaSource}.
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
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);
72 return new YangStatementStreamSource(source.getIdentifier(), rootStatement,
73 source.getSymbolicName().orElse(null));
77 * Create a {@link YangStatementStreamSource} for a {@link ASTSchemaSource}.
79 * @param source YangTextSchemaSource, must not be null
80 * @return A new {@link YangStatementStreamSource}
82 public static YangStatementStreamSource create(final ASTSchemaSource 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 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();
141 final YangErrorListener errorListener = new YangErrorListener(source);
142 lexer.addErrorListener(errorListener);
143 parser.addErrorListener(errorListener);
145 final FileContext result = parser.file();
146 errorListener.validate();
147 return IRStatement.forContext(result.statement());