2 * Copyright (c) 2014 Cisco Systems, Inc. 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.util;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import java.util.Optional;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.antlr.v4.runtime.ParserRuleContext;
17 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
18 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
20 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
25 * Abstract Syntax Tree representation of a schema source. This representation is internal to the YANG parser
26 * implementation, as it relies on ANTLR types.
29 * Instances of this representation are used for caching purposes, as they are a natural intermediate step in YANG text
30 * processing pipeline: the text has been successfully parsed, so we know it is syntactically correct. It also passes
31 * basic semantic validation and we were able to extract dependency information.
34 public final class ASTSchemaSource implements SchemaSourceRepresentation {
35 private final YangModelDependencyInfo depInfo;
36 private final SemVerSourceIdentifier semVerId;
37 private final ParserRuleContext tree;
38 private final SourceIdentifier id;
39 private final String symbolicName;
41 private ASTSchemaSource(@Nonnull final SourceIdentifier id, @Nonnull final SemVerSourceIdentifier semVerId,
42 @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo,
43 @Nullable final String symbolicName) {
44 this.depInfo = requireNonNull(depInfo);
45 this.tree = requireNonNull(tree);
46 this.id = requireNonNull(id);
47 this.semVerId = requireNonNull(semVerId);
48 this.symbolicName = symbolicName;
52 * Create a new instance of AST representation for a abstract syntax tree,
53 * performing minimal semantic analysis to acquire dependency information.
55 * @param name YANG source name. Used only for error reporting.
56 * @param tree ANTLR abstract syntax tree
57 * @return A new representation instance.
58 * @throws YangSyntaxErrorException if we fail to extract dependency information.
60 public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree)
61 throws YangSyntaxErrorException {
62 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
63 final SourceIdentifier id = getSourceId(depInfo);
64 final SemVerSourceIdentifier semVerId = getSemVerSourceId(depInfo);
65 return new ASTSchemaSource(id, semVerId, tree, depInfo, null);
69 * Create a new instance of AST representation for a abstract syntax tree,
70 * performing minimal semantic analysis to acquire dependency information.
73 * SourceIdentifier of yang schema source.
75 * ANTLR abstract syntax tree
78 * @return A new representation instance.
79 * @throws YangSyntaxErrorException
80 * if we fail to extract dependency information.
82 * @deprecated Use {@link #create(SourceIdentifier, ParserRuleContext)} instead.
85 public static ASTSchemaSource create(@Nonnull final SourceIdentifier identifier,
86 @Nonnull final ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
87 return create(identifier, tree);
91 * Create a new instance of AST representation for a abstract syntax tree, performing minimal semantic analysis
92 * to acquire dependency information.
95 * SourceIdentifier of yang schema source.
97 * ANTLR abstract syntax tree
98 * @return A new representation instance.
99 * @throws YangSyntaxErrorException
100 * if we fail to extract dependency information.
102 public static ASTSchemaSource create(@Nonnull final SourceIdentifier identifier,
103 @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
104 return create(identifier, null, tree);
108 * Create a new instance of AST representation for a abstract syntax tree, performing minimal semantic analysis
109 * to acquire dependency information.
111 * @param symbolicName
114 * SourceIdentifier of yang schema source.
116 * ANTLR abstract syntax tree
117 * @return A new representation instance.
118 * @throws YangSyntaxErrorException
119 * if we fail to extract dependency information.
121 public static ASTSchemaSource create(@Nonnull final String symbolicName, @Nonnull final SourceIdentifier identifier,
122 @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
123 return create(identifier, symbolicName, tree);
126 private static ASTSchemaSource create(@Nonnull final SourceIdentifier identifier,
127 @Nullable final String symbolicName, @Nonnull final ParserRuleContext tree)
128 throws YangSyntaxErrorException {
129 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(identifier.getName(), tree);
130 final SourceIdentifier id = getSourceId(depInfo);
132 final SemVerSourceIdentifier semVerId;
133 if (identifier instanceof SemVerSourceIdentifier && !depInfo.getSemanticVersion().isPresent()) {
134 semVerId = (SemVerSourceIdentifier) identifier;
136 semVerId = getSemVerSourceId(depInfo);
139 return new ASTSchemaSource(id, semVerId, tree, depInfo, symbolicName);
144 public SourceIdentifier getIdentifier() {
149 public Optional<String> getSymbolicName() {
150 return Optional.ofNullable(symbolicName);
153 public SemVerSourceIdentifier getSemVerIdentifier() {
159 public Class<? extends SchemaSourceRepresentation> getType() {
160 return ASTSchemaSource.class;
164 * Return the underlying abstract syntax tree.
166 * @return Underlying AST.
168 @Nonnull public ParserRuleContext getAST() {
173 * Return the dependency information as extracted from the AST.
175 * @return Dependency information.
177 // FIXME: this method should be extracted into a public interface in the model.api.repo class, relying solely
178 // on model.api types.
179 @Nonnull public YangModelDependencyInfo getDependencyInformation() {
183 private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
184 final String name = depInfo.getName();
185 return depInfo.getFormattedRevision() == null ? RevisionSourceIdentifier.create(name)
186 : RevisionSourceIdentifier.create(name, depInfo.getRevision());
189 private static SemVerSourceIdentifier getSemVerSourceId(final YangModelDependencyInfo depInfo) {
190 return depInfo.getFormattedRevision() == null
191 ? SemVerSourceIdentifier.create(depInfo.getName(), depInfo.getSemanticVersion().orElse(null))
192 : SemVerSourceIdentifier.create(depInfo.getName(), depInfo.getRevision(),
193 depInfo.getSemanticVersion().orElse(null));