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.Optional;
12 import com.google.common.base.Preconditions;
14 import javax.annotation.Nonnull;
16 import org.antlr.v4.runtime.ParserRuleContext;
17 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
18 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
19 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
20 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
23 * Abstract Syntax Tree representation of a schema source. This representation
24 * is internal to the YANG parser implementation, as it relies on ANTLR types.
26 * Instances of this representation are used for caching purposes, as they
27 * are a natural intermediate step in YANG text processing pipeline: the text
28 * has been successfully parsed, so we know it is syntactically correct. It also
29 * passes basic semantic validation and we were able to extract dependency
33 public final class ASTSchemaSource implements SchemaSourceRepresentation {
34 public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = new Function<ASTSchemaSource, SourceIdentifier>() {
36 public SourceIdentifier apply(final ASTSchemaSource input) {
37 return input.getIdentifier();
40 public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
42 public YangModelDependencyInfo apply(final ASTSchemaSource input) {
43 return input.getDependencyInformation();
46 public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
48 public ParserRuleContext apply(final ASTSchemaSource 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(final @Nonnull SourceIdentifier id, @Nonnull final ParserRuleContext tree, final @Nonnull YangModelDependencyInfo depInfo, final @Nonnull String text) {
59 this.depInfo = Preconditions.checkNotNull(depInfo);
60 this.tree = Preconditions.checkNotNull(tree);
61 this.id = Preconditions.checkNotNull(id);
62 this.text = Preconditions.checkNotNull(text);
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 final ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree) throws YangSyntaxErrorException {
75 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
76 final SourceIdentifier id = new SourceIdentifier(depInfo.getName(), Optional.of(depInfo.getFormattedRevision()));
77 return new ASTSchemaSource(id, tree, depInfo, null);
81 * Create a new instance of AST representation for a abstract syntax tree,
82 * performing minimal semantic analysis to acquire dependency information.
84 * @param name YANG source name. Used only for error reporting.
85 * @param tree ANTLR abstract syntax tree
86 * @return A new representation instance.
87 * @throws YangSyntaxErrorException if we fail to extract dependency information.
89 * @deprecated Migration only, will be removed as soon as the migration is completed.
92 public static final ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
93 final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
94 final SourceIdentifier id = new SourceIdentifier(depInfo.getName(), Optional.of(depInfo.getFormattedRevision()));
95 return new ASTSchemaSource(id, tree, depInfo, text);
100 public SourceIdentifier getIdentifier() {
105 public Class<? extends SchemaSourceRepresentation> getType() {
106 return ASTSchemaSource.class;
110 * Return the underlying abstract syntax tree.
112 * @return Underlying AST.
114 public @Nonnull ParserRuleContext getAST() {
119 * Return the dependency information as extracted from the AST.
121 * FIXME: this method should be extracted into a public interface in the
122 * model.api.repo class, relying solely on model.api types.
124 * @return Dependency information.
126 public @Nonnull YangModelDependencyInfo getDependencyInformation() {
131 * Return the semantically-equivalent text YANG text source.
133 * @return YANG text source
134 * @deprecated Used for migration purposes. Users are advised to use the
135 * schema repository to acquire the representation of their
136 * choice. Will be removed as soon as the migration is completed.
139 public @Nonnull String getYangText() {