Gaurav Agrawal | 22db16d | 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 | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangModule; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 23 | |
| 24 | import static org.onosproject.yangutils.parser.ParsableDataType.MODULE_DATA; |
| 25 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 26 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 31 | |
| 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 | */ |
| 54 | public final class ModuleListener { |
| 55 | |
| 56 | /** |
| 57 | * Creates a new module listener. |
| 58 | */ |
| 59 | private ModuleListener() { |
| 60 | } |
| 61 | |
| 62 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 63 | * It is called when parser receives an input matching the grammar rule |
| 64 | * (module), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 65 | * |
| 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 Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 70 | |
| 71 | // Check if stack is empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 72 | checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 73 | |
| 74 | YangModule yangModule = new YangModule(); |
| 75 | yangModule.setName(ctx.IDENTIFIER().getText()); |
| 76 | |
| 77 | listener.getParsedDataStack().push(yangModule); |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 78 | } |
| 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 Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 88 | |
| 89 | // Check for stack to be non empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 90 | checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 91 | |
| 92 | if (!(listener.getParsedDataStack().peek() instanceof YangModule)) { |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 93 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA, |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 94 | ctx.IDENTIFIER().getText(), EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 95 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 96 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 97 | } |