blob: 6e28adad524d1d306b123357c4694eb3a84e5339 [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;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053033import static org.onosproject.yangutils.utils.YangConstructType.INCLUDE_DATA;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * linkage-stmts = ;; these stmts can appear in any order
40 * *(import-stmt stmtsep)
41 * *(include-stmt stmtsep)
42 *
43 * include-stmt = include-keyword sep identifier-arg-str optsep
44 * (";" /
45 * "{" stmtsep
46 * [revision-date-stmt stmtsep]
47 * "}")
48 *
49 * ANTLR grammar rule
50 * linkage_stmts : (import_stmt
51 * | include_stmt)*;
52 * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053053 * revision_date_stmt? RIGHT_CURLY_BRACE);
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053054 */
55
56/**
57 * Implements listener based call back function corresponding to the "include"
58 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
59 */
60public final class IncludeListener {
61
62 /**
63 * Creates a new include listener.
64 */
65 private IncludeListener() {
66 }
67
68 /**
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053069 * It is called when parser receives an input matching the grammar rule
70 * (include), perform validations and update the data model tree.
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053071 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053072 * @param listener Listener's object
73 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053074 */
75 public static void processIncludeEntry(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053076
77 // Check for stack to be non empty.
Gaurav Agrawal0520c7c2016-02-21 02:56:46 +053078 checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.IDENTIFIER().getText(),
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053079 ENTRY);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053080
81 YangInclude includeNode = new YangInclude();
Gaurav Agrawal0520c7c2016-02-21 02:56:46 +053082 includeNode.setSubModuleName(ctx.IDENTIFIER().getText());
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053083
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 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053091 * @param listener Listener's object
92 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053093 */
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.
Gaurav Agrawal0520c7c2016-02-21 02:56:46 +053097 checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.IDENTIFIER().getText(), EXIT);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053098
99 Parsable tmpIncludeNode = listener.getParsedDataStack().peek();
100 if (tmpIncludeNode instanceof YangInclude) {
101 listener.getParsedDataStack().pop();
102
103 // Check for stack to be non empty.
Gaurav Agrawal0520c7c2016-02-21 02:56:46 +0530104 checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.IDENTIFIER().getText(),
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530105 EXIT);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530106
107 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530108 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530109 case MODULE_DATA: {
110 YangModule module = (YangModule) tmpNode;
111 module.addIncludedInfo((YangInclude) tmpIncludeNode);
112 break;
113 }
114 case SUB_MODULE_DATA: {
115 YangSubModule subModule = (YangSubModule) tmpNode;
116 subModule.addIncludedInfo((YangInclude) tmpIncludeNode);
117 break;
118 }
119 default:
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530120 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, INCLUDE_DATA,
Gaurav Agrawal0520c7c2016-02-21 02:56:46 +0530121 ctx.IDENTIFIER().getText(),
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530122 EXIT));
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530123 }
124 } else {
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530125 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INCLUDE_DATA,
Gaurav Agrawal0520c7c2016-02-21 02:56:46 +0530126 ctx.IDENTIFIER().getText(), EXIT));
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530127 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530128 }
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530129}