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.rfc7950.repo;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import java.util.Optional;
14 import org.antlr.v4.runtime.ParserRuleContext;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
18 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
19 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
21 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
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 extends AbstractIdentifiable<SourceIdentifier>
35 implements SchemaSourceRepresentation {
36 private final @NonNull YangModelDependencyInfo depInfo;
37 private final @NonNull SemVerSourceIdentifier semVerId;
38 private final @NonNull ParserRuleContext tree;
39 private final @Nullable String symbolicName;
41 private ASTSchemaSource(final @NonNull SourceIdentifier identifier, final @NonNull SemVerSourceIdentifier semVerId,
42 final @NonNull ParserRuleContext tree, final @NonNull YangModelDependencyInfo depInfo,
43 @Nullable final String symbolicName) {
45 this.depInfo = requireNonNull(depInfo);
46 this.tree = requireNonNull(tree);
47 this.semVerId = requireNonNull(semVerId);
48 this.symbolicName = symbolicName;
52 * Create a new instance of AST representation for a abstract syntax tree, performing minimal semantic analysis
53 * to acquire dependency information.
58 * SourceIdentifier of YANG schema source.
60 * ANTLR abstract syntax tree
61 * @return A new representation instance.
62 * @throws YangSyntaxErrorException
63 * if we fail to extract dependency information.
65 static @NonNull ASTSchemaSource create(final @NonNull SourceIdentifier identifier,
66 final @Nullable String symbolicName, final @NonNull ParserRuleContext tree)
67 throws YangSyntaxErrorException {
68 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(identifier, tree);
69 final SourceIdentifier id = getSourceId(depInfo);
71 final SemVerSourceIdentifier semVerId;
72 if (identifier instanceof SemVerSourceIdentifier && !depInfo.getSemanticVersion().isPresent()) {
73 semVerId = (SemVerSourceIdentifier) identifier;
75 semVerId = getSemVerSourceId(depInfo);
78 return new ASTSchemaSource(id, semVerId, tree, depInfo, symbolicName);
82 public Optional<String> getSymbolicName() {
83 return Optional.ofNullable(symbolicName);
86 public @NonNull SemVerSourceIdentifier getSemVerIdentifier() {
91 public Class<? extends SchemaSourceRepresentation> getType() {
92 return ASTSchemaSource.class;
96 * Return the underlying abstract syntax tree.
98 * @return Underlying AST.
100 public @NonNull ParserRuleContext getAST() {
105 * Return the dependency information as extracted from the AST.
107 * @return Dependency information.
109 // FIXME: this method should be extracted into a public interface in the model.api.repo class, relying solely
110 // on model.api types.
111 public @NonNull YangModelDependencyInfo getDependencyInformation() {
115 private static @NonNull SourceIdentifier getSourceId(final @NonNull YangModelDependencyInfo depInfo) {
116 final String name = depInfo.getName();
117 return depInfo.getFormattedRevision() == null ? RevisionSourceIdentifier.create(name)
118 : RevisionSourceIdentifier.create(name, depInfo.getRevision());
121 private static @NonNull SemVerSourceIdentifier getSemVerSourceId(final @NonNull YangModelDependencyInfo depInfo) {
122 return depInfo.getFormattedRevision() == null
123 ? SemVerSourceIdentifier.create(depInfo.getName(), depInfo.getSemanticVersion().orElse(null))
124 : SemVerSourceIdentifier.create(depInfo.getName(), depInfo.getRevision(),
125 depInfo.getSemanticVersion().orElse(null));