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 com.google.common.annotations.Beta;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import javax.annotation.Nonnull;
14 import org.antlr.v4.runtime.ParserRuleContext;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
17 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
18 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
19 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
24 * Abstract Syntax Tree representation of a schema source. This representation
25 * is internal to the YANG parser implementation, as it relies on ANTLR types.
27 * Instances of this representation are used for caching purposes, as they
28 * are a natural intermediate step in YANG text processing pipeline: the text
29 * has been successfully parsed, so we know it is syntactically correct. It also
30 * passes basic semantic validation and we were able to extract dependency
34 public final class ASTSchemaSource implements SchemaSourceRepresentation {
36 public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = ASTSchemaSource::getIdentifier;
38 public static final Function<ASTSchemaSource, SourceIdentifier> GET_SEMVER_IDENTIFIER = ASTSchemaSource::getSemVerIdentifier;
40 public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = ASTSchemaSource::getDependencyInformation;
42 public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = ASTSchemaSource::getAST;
44 private final YangModelDependencyInfo depInfo;
45 private final ParserRuleContext tree;
46 private final SourceIdentifier id;
47 private final SemVerSourceIdentifier semVerId;
49 private ASTSchemaSource(@Nonnull final SourceIdentifier id, @Nonnull final SemVerSourceIdentifier semVerId, @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo) {
50 this.depInfo = Preconditions.checkNotNull(depInfo);
51 this.tree = Preconditions.checkNotNull(tree);
52 this.id = Preconditions.checkNotNull(id);
53 this.semVerId = Preconditions.checkNotNull(semVerId);
57 * Create a new instance of AST representation for a abstract syntax tree,
58 * performing minimal semantic analysis to acquire dependency information.
60 * @param name YANG source name. Used only for error reporting.
61 * @param tree ANTLR abstract syntax tree
62 * @return A new representation instance.
63 * @throws YangSyntaxErrorException if we fail to extract dependency information.
65 public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
66 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
67 final SourceIdentifier id = getSourceId(depInfo);
68 final SemVerSourceIdentifier semVerId = getSemVerSourceId(depInfo);
69 return new ASTSchemaSource(id, semVerId, tree, depInfo);
72 private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
73 final String name = depInfo.getName();
74 return depInfo.getFormattedRevision() == null
75 ? RevisionSourceIdentifier.create(name)
76 : RevisionSourceIdentifier.create(name, depInfo.getFormattedRevision());
79 private static SemVerSourceIdentifier getSemVerSourceId(final YangModelDependencyInfo depInfo) {
80 return depInfo.getFormattedRevision() == null ? SemVerSourceIdentifier.create(depInfo.getName(), depInfo
81 .getSemanticVersion().or(Module.DEFAULT_SEMANTIC_VERSION)) : SemVerSourceIdentifier.create(
82 depInfo.getName(), depInfo.getFormattedRevision(),
83 depInfo.getSemanticVersion().or(Module.DEFAULT_SEMANTIC_VERSION));
87 * Create a new instance of AST representation for a abstract syntax tree,
88 * performing minimal semantic analysis to acquire dependency information.
91 * SourceIdentifier of yang schema source.
93 * ANTLR abstract syntax tree
96 * @return A new representation instance.
97 * @throws YangSyntaxErrorException
98 * if we fail to extract dependency information.
100 public static ASTSchemaSource create(@Nonnull final SourceIdentifier identifier,
101 @Nonnull final ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
102 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(identifier.getName(), tree);
103 final SourceIdentifier id = getSourceId(depInfo);
105 final SemVerSourceIdentifier semVerId;
106 if (identifier instanceof SemVerSourceIdentifier && !depInfo.getSemanticVersion().isPresent()) {
107 semVerId = (SemVerSourceIdentifier) identifier;
109 semVerId = getSemVerSourceId(depInfo);
112 return new ASTSchemaSource(id, semVerId, tree, depInfo);
116 public SourceIdentifier getIdentifier() {
120 public SemVerSourceIdentifier getSemVerIdentifier() {
125 public Class<? extends SchemaSourceRepresentation> getType() {
126 return ASTSchemaSource.class;
130 * Return the underlying abstract syntax tree.
132 * @return Underlying AST.
134 @Nonnull public ParserRuleContext getAST() {
139 * Return the dependency information as extracted from the AST.
141 * FIXME: this method should be extracted into a public interface in the
142 * model.api.repo class, relying solely on model.api types.
144 * @return Dependency information.
146 @Nonnull public YangModelDependencyInfo getDependencyInformation() {