Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangModule; |
| 20 | import org.onosproject.yangutils.datamodel.YangNode; |
| 21 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 22 | import org.onosproject.yangutils.parser.ParsableDataType; |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation; |
| 27 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction; |
| 28 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType; |
| 29 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 30 | |
| 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 | */ |
| 43 | public 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 Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 60 | |
| 61 | // Check if stack is empty. |
| 62 | ListenerValidation.checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER, |
| 63 | ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.ENTRY); |
| 64 | |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 65 | } |
| 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 Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 75 | |
| 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 Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 95 | } |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 96 | } |