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(final ASTSchemaSource input) {
34 return input.getIdentifier();
37 public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
39 public YangModelDependencyInfo apply(final ASTSchemaSource input) {
40 return input.getDependencyInformation();
43 public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
45 public ParserRuleContext apply(final ASTSchemaSource input) {
46 return input.getAST();
50 private final YangModelDependencyInfo depInfo;
51 private final ParserRuleContext tree;
52 private final SourceIdentifier id;
53 private final String text;
55 private ASTSchemaSource(final @Nonnull SourceIdentifier id, @Nonnull final ParserRuleContext tree, final @Nonnull YangModelDependencyInfo depInfo, final @Nonnull String text) {
56 this.depInfo = Preconditions.checkNotNull(depInfo);
57 this.tree = Preconditions.checkNotNull(tree);
58 this.id = Preconditions.checkNotNull(id);
59 this.text = Preconditions.checkNotNull(text);
63 * Create a new instance of AST representation for a abstract syntax tree,
64 * performing minimal semantic analysis to acquire dependency information.
66 * @param name YANG source name. Used only for error reporting.
67 * @param tree ANTLR abstract syntax tree
68 * @return A new representation instance.
69 * @throws YangSyntaxErrorException if we fail to extract dependency information.
71 public static ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree) throws YangSyntaxErrorException {
72 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
73 final SourceIdentifier id = getSourceId(depInfo);
74 return new ASTSchemaSource(id, tree, depInfo, null);
77 private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
78 final String name = depInfo.getName();
79 return depInfo.getFormattedRevision() == null
80 ? new SourceIdentifier(name)
81 : new SourceIdentifier(name, depInfo.getFormattedRevision());
85 * Create a new instance of AST representation for a abstract syntax tree,
86 * performing minimal semantic analysis to acquire dependency information.
88 * @param name YANG source name. Used only for error reporting.
89 * @param tree ANTLR abstract syntax tree
90 * @return A new representation instance.
91 * @throws YangSyntaxErrorException if we fail to extract dependency information.
93 * @deprecated Migration only, will be removed as soon as the migration is completed.
96 public static ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
97 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
98 final SourceIdentifier id = getSourceId(depInfo);
99 return new ASTSchemaSource(id, tree, depInfo, text);
104 public SourceIdentifier getIdentifier() {
109 public Class<? extends SchemaSourceRepresentation> getType() {
110 return ASTSchemaSource.class;
114 * Return the underlying abstract syntax tree.
116 * @return Underlying AST.
118 public @Nonnull ParserRuleContext getAST() {
123 * Return the dependency information as extracted from the AST.
125 * FIXME: this method should be extracted into a public interface in the
126 * model.api.repo class, relying solely on model.api types.
128 * @return Dependency information.
130 public @Nonnull YangModelDependencyInfo getDependencyInformation() {
135 * Return the semantically-equivalent text YANG text source.
137 * @return YANG text source
138 * @deprecated Used for migration purposes. Users are advised to use the
139 * schema repository to acquire the representation of their
140 * choice. Will be removed as soon as the migration is completed.
143 public @Nonnull String getYangText() {