Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +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 | |
| 19 | import org.antlr.v4.runtime.ParserRuleContext; |
| 20 | import org.antlr.v4.runtime.tree.ParseTree; |
| 21 | import org.onosproject.yangutils.datamodel.YangCase; |
| 22 | import org.onosproject.yangutils.datamodel.YangChoice; |
| 23 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 24 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 26 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 27 | |
| 28 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
| 29 | import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangCaseNode; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CHILD; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
| 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 42 | import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA; |
| 43 | import static org.onosproject.yangutils.utils.YangConstructType.SHORT_CASE_DATA; |
| 44 | |
| 45 | /* |
| 46 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 47 | * |
| 48 | * ABNF grammar as per RFC6020 |
| 49 | * short-case-stmt = container-stmt / |
| 50 | * leaf-stmt / |
| 51 | * leaf-list-stmt / |
| 52 | * list-stmt / |
| 53 | * anyxml-stmt |
| 54 | * |
| 55 | * ANTLR grammar rule |
| 56 | * shortCaseStatement : containerStatement | leafStatement | leafListStatement | listStatement; |
| 57 | */ |
| 58 | |
| 59 | /** |
| 60 | * Implements listener based call back function corresponding to the "short |
| 61 | * case" rule defined in ANTLR grammar file for corresponding ABNF rule in RFC |
| 62 | * 6020. |
| 63 | */ |
| 64 | public final class ShortCaseListener { |
| 65 | |
| 66 | /** |
| 67 | * Create a new short case listener. |
| 68 | */ |
| 69 | private ShortCaseListener() { |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * It is called when parser enters grammar rule (short case), it perform |
| 74 | * validations and updates the data model tree. |
| 75 | * |
| 76 | * @param listener listener's object |
| 77 | * @param ctx context object of the grammar rule |
| 78 | */ |
| 79 | public static void processShortCaseEntry(TreeWalkListener listener, |
| 80 | GeneratedYangParser.ShortCaseStatementContext ctx) { |
| 81 | |
| 82 | ParseTree errorConstructContext; |
| 83 | |
| 84 | // Check for stack to be non empty. |
| 85 | checkStackIsNotEmpty(listener, MISSING_HOLDER, SHORT_CASE_DATA, "", ENTRY); |
| 86 | |
| 87 | YangCase caseNode = getYangCaseNode(JAVA_GENERATION); |
| 88 | |
| 89 | if (ctx.containerStatement() != null) { |
| 90 | caseNode.setName(getValidIdentifier(ctx.containerStatement().identifier().getText(), CASE_DATA, ctx)); |
| 91 | errorConstructContext = ctx.containerStatement(); |
| 92 | } else if (ctx.listStatement() != null) { |
| 93 | caseNode.setName(getValidIdentifier(ctx.listStatement().identifier().getText(), CASE_DATA, ctx)); |
| 94 | errorConstructContext = ctx.listStatement(); |
| 95 | } else if (ctx.leafListStatement() != null) { |
| 96 | caseNode.setName(getValidIdentifier(ctx.leafListStatement().identifier().getText(), CASE_DATA, ctx)); |
| 97 | errorConstructContext = ctx.leafListStatement(); |
| 98 | } else if (ctx.leafStatement() != null) { |
| 99 | caseNode.setName(getValidIdentifier(ctx.leafStatement().identifier().getText(), CASE_DATA, ctx)); |
| 100 | errorConstructContext = ctx.leafStatement(); |
| 101 | } else { |
| 102 | throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, SHORT_CASE_DATA, "", ENTRY)); |
| 103 | } |
| 104 | // TODO implement for augment. |
| 105 | |
| 106 | int line = ((ParserRuleContext) errorConstructContext).getStart().getLine(); |
| 107 | int charPositionInLine = ((ParserRuleContext) errorConstructContext).getStart().getCharPositionInLine(); |
| 108 | |
| 109 | // Check for identifier collision |
| 110 | detectCollidingChildUtil(listener, line, charPositionInLine, caseNode.getName(), CASE_DATA); |
| 111 | |
| 112 | if ((listener.getParsedDataStack().peek()) instanceof YangChoice) { |
| 113 | try { |
| 114 | ((YangChoice) listener.getParsedDataStack().peek()).addChild(caseNode); |
| 115 | } catch (DataModelException e) { |
| 116 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 117 | SHORT_CASE_DATA, caseNode.getName(), ENTRY, e.getMessage())); |
| 118 | } |
| 119 | listener.getParsedDataStack().push(caseNode); |
| 120 | } else { |
| 121 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, SHORT_CASE_DATA, |
| 122 | caseNode.getName(), ENTRY)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * It is called when parser exits from grammar rule (short case), it perform |
| 128 | * validations and update the data model tree. |
| 129 | * |
| 130 | * @param listener Listener's object |
| 131 | * @param ctx context object of the grammar rule |
| 132 | */ |
| 133 | public static void processShortCaseExit(TreeWalkListener listener, |
| 134 | GeneratedYangParser.ShortCaseStatementContext ctx) { |
| 135 | |
| 136 | // Check for stack to be non empty. |
| 137 | checkStackIsNotEmpty(listener, MISSING_HOLDER, SHORT_CASE_DATA, "", EXIT); |
| 138 | |
| 139 | if (listener.getParsedDataStack().peek() instanceof YangCase) { |
| 140 | listener.getParsedDataStack().pop(); |
| 141 | } else { |
| 142 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SHORT_CASE_DATA, |
| 143 | "", EXIT)); |
| 144 | } |
| 145 | } |
| 146 | } |