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.YangInclude; |
| 20 | import org.onosproject.yangutils.datamodel.YangModule; |
| 21 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 22 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame^] | 26 | |
| 27 | import static org.onosproject.yangutils.parser.ParsableDataType.INCLUDE_DATA; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 36 | * |
| 37 | * ABNF grammar as per RFC6020 |
| 38 | * linkage-stmts = ;; these stmts can appear in any order |
| 39 | * *(import-stmt stmtsep) |
| 40 | * *(include-stmt stmtsep) |
| 41 | * |
| 42 | * include-stmt = include-keyword sep identifier-arg-str optsep |
| 43 | * (";" / |
| 44 | * "{" stmtsep |
| 45 | * [revision-date-stmt stmtsep] |
| 46 | * "}") |
| 47 | * |
| 48 | * ANTLR grammar rule |
| 49 | * linkage_stmts : (import_stmt |
| 50 | * | include_stmt)*; |
| 51 | * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 52 | * revision_date_stmt? RIGHT_CURLY_BRACE); |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 53 | */ |
| 54 | |
| 55 | /** |
| 56 | * Implements listener based call back function corresponding to the "include" |
| 57 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 58 | */ |
| 59 | public final class IncludeListener { |
| 60 | |
| 61 | /** |
| 62 | * Creates a new include listener. |
| 63 | */ |
| 64 | private IncludeListener() { |
| 65 | } |
| 66 | |
| 67 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame^] | 68 | * It is called when parser receives an input matching the grammar rule |
| 69 | * (include), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 70 | * |
| 71 | * @param listener Listener's object. |
| 72 | * @param ctx context object of the grammar rule. |
| 73 | */ |
| 74 | public static void processIncludeEntry(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 75 | |
| 76 | // Check for stack to be non empty. |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame^] | 77 | checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, String.valueOf(ctx.IDENTIFIER().getText()), |
| 78 | ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 79 | |
| 80 | YangInclude includeNode = new YangInclude(); |
| 81 | includeNode.setSubModuleName(String.valueOf(ctx.IDENTIFIER().getText())); |
| 82 | |
| 83 | listener.getParsedDataStack().push(includeNode); |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * It is called when parser exits from grammar rule (include), it perform |
| 88 | * validations and update the data model tree. |
| 89 | * |
| 90 | * @param listener Listener's object. |
| 91 | * @param ctx context object of the grammar rule. |
| 92 | */ |
| 93 | public static void processIncludeExit(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 94 | |
| 95 | // Check for stack to be non empty. |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame^] | 96 | checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, String.valueOf(ctx.IDENTIFIER().getText()), EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 97 | |
| 98 | Parsable tmpIncludeNode = listener.getParsedDataStack().peek(); |
| 99 | if (tmpIncludeNode instanceof YangInclude) { |
| 100 | listener.getParsedDataStack().pop(); |
| 101 | |
| 102 | // Check for stack to be non empty. |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame^] | 103 | checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, String.valueOf(ctx.IDENTIFIER().getText()), |
| 104 | EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 105 | |
| 106 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 107 | switch (tmpNode.getParsableDataType()) { |
| 108 | case MODULE_DATA: { |
| 109 | YangModule module = (YangModule) tmpNode; |
| 110 | module.addIncludedInfo((YangInclude) tmpIncludeNode); |
| 111 | break; |
| 112 | } |
| 113 | case SUB_MODULE_DATA: { |
| 114 | YangSubModule subModule = (YangSubModule) tmpNode; |
| 115 | subModule.addIncludedInfo((YangInclude) tmpIncludeNode); |
| 116 | break; |
| 117 | } |
| 118 | default: |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame^] | 119 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, INCLUDE_DATA, |
| 120 | String.valueOf(ctx.IDENTIFIER().getText()), |
| 121 | EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 122 | } |
| 123 | } else { |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame^] | 124 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INCLUDE_DATA, |
| 125 | String.valueOf(ctx.IDENTIFIER().getText()), EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 126 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 127 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame^] | 128 | } |