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.parser.api.YangSyntaxErrorException;
16 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
17 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
18 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
21 * Abstract Syntax Tree representation of a schema source. This representation
22 * is internal to the YANG parser implementation, as it relies on ANTLR types.
24 * Instances of this representation are used for caching purposes, as they
25 * are a natural intermediate step in YANG text processing pipeline: the text
26 * has been successfully parsed, so we know it is syntactically correct. It also
27 * passes basic semantic validation and we were able to extract dependency
31 public final class ASTSchemaSource implements SchemaSourceRepresentation {
32 public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = new Function<ASTSchemaSource, SourceIdentifier>() {
34 public SourceIdentifier apply(@Nonnull final ASTSchemaSource input) {
35 Preconditions.checkNotNull(input);
36 return input.getIdentifier();
39 public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
41 public YangModelDependencyInfo apply(@Nonnull final ASTSchemaSource input) {
42 Preconditions.checkNotNull(input);
43 return input.getDependencyInformation();
46 public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
48 public ParserRuleContext apply(@Nonnull final ASTSchemaSource input) {
49 Preconditions.checkNotNull(input);
50 return input.getAST();
54 private final YangModelDependencyInfo depInfo;
55 private final ParserRuleContext tree;
56 private final SourceIdentifier id;
57 private final String text;
59 private ASTSchemaSource(@Nonnull final SourceIdentifier id, @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo, final String text) {
60 this.depInfo = Preconditions.checkNotNull(depInfo);
61 this.tree = Preconditions.checkNotNull(tree);
62 this.id = Preconditions.checkNotNull(id);
67 * Create a new instance of AST representation for a abstract syntax tree,
68 * performing minimal semantic analysis to acquire dependency information.
70 * @param name YANG source name. Used only for error reporting.
71 * @param tree ANTLR abstract syntax tree
72 * @return A new representation instance.
73 * @throws YangSyntaxErrorException if we fail to extract dependency information.
75 public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
76 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
77 final SourceIdentifier id = getSourceId(depInfo);
78 return new ASTSchemaSource(id, tree, depInfo, null);
81 private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
82 final String name = depInfo.getName();
83 return depInfo.getFormattedRevision() == null
84 ? new SourceIdentifier(name)
85 : new SourceIdentifier(name, depInfo.getFormattedRevision());
89 * Create a new instance of AST representation for a abstract syntax tree,
90 * performing minimal semantic analysis to acquire dependency information.
92 * @param name YANG source name. Used only for error reporting.
93 * @param tree ANTLR abstract syntax tree
94 * @param text YANG text source
95 * @return A new representation instance.
96 * @throws YangSyntaxErrorException if we fail to extract dependency information.
98 * @deprecated Migration only, will be removed as soon as the migration is completed.
101 public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
102 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
103 final SourceIdentifier id = getSourceId(depInfo);
104 return new ASTSchemaSource(id, tree, depInfo, text);
109 public SourceIdentifier getIdentifier() {
114 public Class<? extends SchemaSourceRepresentation> getType() {
115 return ASTSchemaSource.class;
119 * Return the underlying abstract syntax tree.
121 * @return Underlying AST.
123 @Nonnull public ParserRuleContext getAST() {
128 * Return the dependency information as extracted from the AST.
130 * FIXME: this method should be extracted into a public interface in the
131 * model.api.repo class, relying solely on model.api types.
133 * @return Dependency information.
135 @Nonnull public YangModelDependencyInfo getDependencyInformation() {
140 * Return the semantically-equivalent text YANG text source.
142 * @return YANG text source
143 * @deprecated Used for migration purposes. Users are advised to use the
144 * schema repository to acquire the representation of their
145 * choice. Will be removed as soon as the migration is completed.
148 @Nonnull public String getYangText() {