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; |
| 20 | import org.onosproject.yangutils.parser.ParsableDataType; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation; |
| 25 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction; |
| 26 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType; |
| 27 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 31 | * |
| 32 | * ABNF grammar as per RFC6020 |
| 33 | * module-stmt = optsep module-keyword sep identifier-arg-str |
| 34 | * optsep |
| 35 | * "{" stmtsep |
| 36 | * module-header-stmts |
| 37 | * linkage-stmts |
| 38 | * meta-stmts |
| 39 | * revision-stmts |
| 40 | * body-stmts |
| 41 | * "}" optsep |
| 42 | * |
| 43 | * ANTLR grammar rule |
| 44 | * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE; |
| 45 | */ |
| 46 | |
| 47 | /** |
| 48 | * Implements listener based call back function corresponding to the "module" |
| 49 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 50 | */ |
| 51 | public final class ModuleListener { |
| 52 | |
| 53 | /** |
| 54 | * Creates a new module listener. |
| 55 | */ |
| 56 | private ModuleListener() { |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * It is called when parser receives an input matching the grammar |
| 61 | * rule (module), perform validations and update the data model |
| 62 | * tree. |
| 63 | * |
| 64 | * @param listener Listener's object. |
| 65 | * @param ctx context object of the grammar rule. |
| 66 | */ |
| 67 | public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 68 | |
| 69 | // Check if stack is empty. |
| 70 | ListenerValidation |
| 71 | .checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER, ParsableDataType.MODULE_DATA, |
| 72 | String.valueOf(ctx.IDENTIFIER().getText()), ListenerErrorLocation.ENTRY); |
| 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. |
| 90 | ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, |
| 91 | ParsableDataType.MODULE_DATA, |
| 92 | String.valueOf(ctx.IDENTIFIER().getText()), |
| 93 | ListenerErrorLocation.EXIT); |
| 94 | |
| 95 | if (!(listener.getParsedDataStack().peek() instanceof YangModule)) { |
| 96 | throw new ParserException( |
| 97 | ListenerErrorMessageConstruction |
| 98 | .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER, |
| 99 | ParsableDataType.MODULE_DATA, String |
| 100 | .valueOf(ctx.IDENTIFIER() |
| 101 | .getText()), |
| 102 | ListenerErrorLocation.EXIT)); |
| 103 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 104 | } |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 105 | } |