blob: 8e808a2bdfd0b5dcca13c495cc72d545700c05f8 [file] [log] [blame]
Gaurav Agrawal2cbb9502016-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 Agrawala04483c2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangModule;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053020import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053021import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053023
24import static org.onosproject.yangutils.parser.ParsableDataType.MODULE_DATA;
25import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
26import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053031
32/*
33 * Reference: RFC6020 and YANG ANTLR Grammar
34 *
35 * ABNF grammar as per RFC6020
36 * module-stmt = optsep module-keyword sep identifier-arg-str
37 * optsep
38 * "{" stmtsep
39 * module-header-stmts
40 * linkage-stmts
41 * meta-stmts
42 * revision-stmts
43 * body-stmts
44 * "}" optsep
45 *
46 * ANTLR grammar rule
47 * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
48 */
49
50/**
51 * Implements listener based call back function corresponding to the "module"
52 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
53 */
54public final class ModuleListener {
55
56 /**
57 * Creates a new module listener.
58 */
59 private ModuleListener() {
60 }
61
62 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053063 * It is called when parser receives an input matching the grammar rule
64 * (module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053065 *
66 * @param listener Listener's object.
67 * @param ctx context object of the grammar rule.
68 */
69 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053070
71 // Check if stack is empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053072 checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053073
74 YangModule yangModule = new YangModule();
75 yangModule.setName(ctx.IDENTIFIER().getText());
76
77 listener.getParsedDataStack().push(yangModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053078 }
79
80 /**
81 * It is called when parser exits from grammar rule (module), it perform
82 * validations and update the data model tree.
83 *
84 * @param listener Listener's object.
85 * @param ctx context object of the grammar rule.
86 */
87 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053088
89 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053090 checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053091
92 if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053093 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053094 ctx.IDENTIFIER().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +053095 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053096 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053097}