blob: c4678b89f50f43b23495707344f035ef43d1d1bf [file] [log] [blame]
Gaurav Agrawal22db16d2016-02-12 16:50:55 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.parser.impl.listeners;
18
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangInclude;
20import org.onosproject.yangutils.datamodel.YangModule;
21import org.onosproject.yangutils.datamodel.YangSubModule;
22import org.onosproject.yangutils.parser.Parsable;
23import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053027import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
30import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053031
32/*
33 * Reference: RFC6020 and YANG ANTLR Grammar
34 *
35 * ABNF grammar as per RFC6020
36 * linkage-stmts = ;; these stmts can appear in any order
37 * *(import-stmt stmtsep)
38 * *(include-stmt stmtsep)
39 *
40 * include-stmt = include-keyword sep identifier-arg-str optsep
41 * (";" /
42 * "{" stmtsep
43 * [revision-date-stmt stmtsep]
44 * "}")
45 *
46 * ANTLR grammar rule
47 * linkage_stmts : (import_stmt
48 * | include_stmt)*;
49 * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053050 * revision_date_stmt? RIGHT_CURLY_BRACE);
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053051 */
52
53/**
54 * Implements listener based call back function corresponding to the "include"
55 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
56 */
57public final class IncludeListener {
58
59 /**
60 * Creates a new include listener.
61 */
62 private IncludeListener() {
63 }
64
65 /**
66 * It is called when parser receives an input matching the grammar
67 * rule (include), perform validations and update the data model
68 * tree.
69 *
70 * @param listener Listener's object.
71 * @param ctx context object of the grammar rule.
72 */
73 public static void processIncludeEntry(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053074
75 // Check for stack to be non empty.
76 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
77 ParsableDataType.INCLUDE_DATA,
78 String.valueOf(ctx.IDENTIFIER().getText()),
79 ListenerErrorLocation.ENTRY);
80
81 YangInclude includeNode = new YangInclude();
82 includeNode.setSubModuleName(String.valueOf(ctx.IDENTIFIER().getText()));
83
84 listener.getParsedDataStack().push(includeNode);
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053085 }
86
87 /**
88 * It is called when parser exits from grammar rule (include), it perform
89 * validations and update the data model tree.
90 *
91 * @param listener Listener's object.
92 * @param ctx context object of the grammar rule.
93 */
94 public static void processIncludeExit(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053095
96 // Check for stack to be non empty.
97 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
98 ParsableDataType.INCLUDE_DATA,
99 String.valueOf(ctx.IDENTIFIER().getText()),
100 ListenerErrorLocation.EXIT);
101
102 Parsable tmpIncludeNode = listener.getParsedDataStack().peek();
103 if (tmpIncludeNode instanceof YangInclude) {
104 listener.getParsedDataStack().pop();
105
106 // Check for stack to be non empty.
107 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
108 ParsableDataType.INCLUDE_DATA,
109 String.valueOf(ctx.IDENTIFIER().getText()),
110 ListenerErrorLocation.EXIT);
111
112 Parsable tmpNode = listener.getParsedDataStack().peek();
113 switch (tmpNode.getParsableDataType()) {
114 case MODULE_DATA: {
115 YangModule module = (YangModule) tmpNode;
116 module.addIncludedInfo((YangInclude) tmpIncludeNode);
117 break;
118 }
119 case SUB_MODULE_DATA: {
120 YangSubModule subModule = (YangSubModule) tmpNode;
121 subModule.addIncludedInfo((YangInclude) tmpIncludeNode);
122 break;
123 }
124 default:
125 throw new ParserException(
126 ListenerErrorMessageConstruction
127 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
128 ParsableDataType.INCLUDE_DATA,
129 String.valueOf(ctx.IDENTIFIER()
130 .getText()),
131 ListenerErrorLocation.EXIT));
132 }
133 } else {
134 throw new ParserException(
135 ListenerErrorMessageConstruction
136 .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
137 ParsableDataType.INCLUDE_DATA, String
138 .valueOf(ctx.IDENTIFIER()
139 .getText()),
140 ListenerErrorLocation.EXIT));
141 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530142 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530143}