2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.parser.util;
9 import com.google.common.annotations.Beta;
10 import com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13 import org.antlr.v4.runtime.ParserRuleContext;
14 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
15 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
16 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
17 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
20 * Abstract Syntax Tree representation of a schema source. This representation
21 * is internal to the YANG parser implementation, as it relies on ANTLR types.
23 * Instances of this representation are used for caching purposes, as they
24 * are a natural intermediate step in YANG text processing pipeline: the text
25 * has been successfully parsed, so we know it is syntactically correct. It also
26 * passes basic semantic validation and we were able to extract dependency
30 public final class ASTSchemaSource implements SchemaSourceRepresentation {
31 public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = new Function<ASTSchemaSource, SourceIdentifier>() {
33 public SourceIdentifier apply(@Nonnull final ASTSchemaSource input) {
34 Preconditions.checkNotNull(input);
35 return input.getIdentifier();
38 public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
40 public YangModelDependencyInfo apply(@Nonnull final ASTSchemaSource input) {
41 Preconditions.checkNotNull(input);
42 return input.getDependencyInformation();
45 public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
47 public ParserRuleContext apply(@Nonnull final ASTSchemaSource input) {
48 Preconditions.checkNotNull(input);
49 return input.getAST();
53 private final YangModelDependencyInfo depInfo;
54 private final ParserRuleContext tree;
55 private final SourceIdentifier id;
56 private final String text;
58 private ASTSchemaSource(@Nonnull final SourceIdentifier id, @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo, final String text) {
59 this.depInfo = Preconditions.checkNotNull(depInfo);
60 this.tree = Preconditions.checkNotNull(tree);
61 this.id = Preconditions.checkNotNull(id);
66 * Create a new instance of AST representation for a abstract syntax tree,
67 * performing minimal semantic analysis to acquire dependency information.
69 * @param name YANG source name. Used only for error reporting.
70 * @param tree ANTLR abstract syntax tree
71 * @return A new representation instance.
72 * @throws YangSyntaxErrorException if we fail to extract dependency information.
74 public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
75 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
76 final SourceIdentifier id = getSourceId(depInfo);
77 return new ASTSchemaSource(id, tree, depInfo, null);
80 private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
81 final String name = depInfo.getName();
82 return depInfo.getFormattedRevision() == null
83 ? new SourceIdentifier(name)
84 : new SourceIdentifier(name, depInfo.getFormattedRevision());
88 * Create a new instance of AST representation for a abstract syntax tree,
89 * performing minimal semantic analysis to acquire dependency information.
91 * @param name YANG source name. Used only for error reporting.
92 * @param tree ANTLR abstract syntax tree
93 * @return A new representation instance.
94 * @throws YangSyntaxErrorException if we fail to extract dependency information.
96 * @deprecated Migration only, will be removed as soon as the migration is completed.
99 public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
100 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
101 final SourceIdentifier id = getSourceId(depInfo);
102 return new ASTSchemaSource(id, tree, depInfo, text);
107 public SourceIdentifier getIdentifier() {
112 public Class<? extends SchemaSourceRepresentation> getType() {
113 return ASTSchemaSource.class;
117 * Return the underlying abstract syntax tree.
119 * @return Underlying AST.
121 @Nonnull public ParserRuleContext getAST() {
126 * Return the dependency information as extracted from the AST.
128 * FIXME: this method should be extracted into a public interface in the
129 * model.api.repo class, relying solely on model.api types.
131 * @return Dependency information.
133 @Nonnull public YangModelDependencyInfo getDependencyInformation() {
138 * Return the semantically-equivalent text YANG text source.
140 * @return YANG text source
141 * @deprecated Used for migration purposes. Users are advised to use the
142 * schema repository to acquire the representation of their
143 * choice. Will be removed as soon as the migration is completed.
146 @Nonnull public String getYangText() {