blob: f53a767372a92ee2cc0216386733b1e2b7f11713 [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;
20import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.datamodel.YangSubModule;
22import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053026import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053030
31/*
32 * Reference: RFC6020 and YANG ANTLR Grammar
33 *
34 * ANTLR grammar rule
35 * yangfile : module_stmt
36 * | submodule_stmt;
37 */
38
39/**
40 * Implements call back function corresponding to the "base rule" defined in
41 * ANTLR grammar file.
42 */
43public final class BaseFileListener {
44
45 /**
46 * Creates a new base listener.
47 */
48 private BaseFileListener() {
49 }
50
51 /**
52 * It is called when parser receives an input matching the grammar
53 * rule (yangfile), perform validations and update the data model
54 * tree.
55 *
56 * @param listener Listener's object.
57 * @param ctx context object of the grammar rule.
58 */
59 public static void processYangFileEntry(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053060
61 // Check if stack is empty.
62 ListenerValidation.checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER,
63 ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.ENTRY);
64
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053065 }
66
67 /**
68 * It is called when parser exits from grammar rule (yangfile), it perform
69 * validations and update the data model tree.
70 *
71 * @param listener Listener's object.
72 * @param ctx context object of the grammar rule.
73 */
74 public static void processYangFileExit(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053075
76 // Check for stack to be non empty.
77 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
78 ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT);
79
80 // Data Model tree root node is set.
81 if (listener.getParsedDataStack().peek() instanceof YangModule
82 | listener.getParsedDataStack().peek() instanceof YangSubModule) {
83 listener.setRootNode((YangNode) listener.getParsedDataStack().pop());
84 } else {
85 throw new ParserException(
86 ListenerErrorMessageConstruction
87 .constructListenerErrorMessage(ListenerErrorType.INVALID_CHILD,
88 ParsableDataType.YANGBASE_DATA, "",
89 ListenerErrorLocation.EXIT));
90 }
91
92 // Check if stack is empty.
93 ListenerValidation.checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER,
94 ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053095 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +053096}