Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-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 | /* |
| 20 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 21 | * |
| 22 | * ABNF grammar as per RFC6020 |
| 23 | * bit-stmt = bit-keyword sep identifier-arg-str optsep |
| 24 | * (";" / |
| 25 | * "{" stmtsep |
| 26 | * ;; these stmts can appear in any order |
| 27 | * [position-stmt stmtsep] |
| 28 | * [status-stmt stmtsep] |
| 29 | * [description-stmt stmtsep] |
| 30 | * [reference-stmt stmtsep] |
| 31 | * "}" |
| 32 | * "}") |
| 33 | * |
| 34 | * ANTLR grammar rule |
| 35 | * bitStatement : BIT_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE bitBodyStatement RIGHT_CURLY_BRACE); |
| 36 | * |
| 37 | * bitBodyStatement : positionStatement? statusStatement? descriptionStatement? referenceStatement? |
| 38 | * | positionStatement? statusStatement? referenceStatement? descriptionStatement? |
| 39 | * | positionStatement? descriptionStatement? statusStatement? referenceStatement? |
| 40 | * | positionStatement? descriptionStatement? referenceStatement? statusStatement? |
| 41 | * | positionStatement? referenceStatement? statusStatement? descriptionStatement? |
| 42 | * | positionStatement? referenceStatement? descriptionStatement? statusStatement? |
| 43 | * | statusStatement? positionStatement? descriptionStatement? referenceStatement? |
| 44 | * | statusStatement? positionStatement? referenceStatement? descriptionStatement? |
| 45 | * | statusStatement? descriptionStatement? descriptionStatement? positionStatement? |
| 46 | * | statusStatement? descriptionStatement? positionStatement? descriptionStatement? |
| 47 | * | statusStatement? referenceStatement? positionStatement? descriptionStatement? |
| 48 | * | statusStatement? referenceStatement? descriptionStatement? positionStatement? |
| 49 | * | descriptionStatement? positionStatement? statusStatement? referenceStatement? |
| 50 | * | descriptionStatement? positionStatement? referenceStatement? statusStatement? |
| 51 | * | descriptionStatement? statusStatement? positionStatement? referenceStatement? |
| 52 | * | descriptionStatement? statusStatement? referenceStatement? positionStatement? |
| 53 | * | descriptionStatement? referenceStatement? positionStatement? statusStatement? |
| 54 | * | descriptionStatement? referenceStatement? statusStatement? positionStatement? |
| 55 | * | referenceStatement? positionStatement? descriptionStatement? statusStatement? |
| 56 | * | referenceStatement? positionStatement? statusStatement? descriptionStatement? |
| 57 | * | referenceStatement? statusStatement? descriptionStatement? positionStatement? |
| 58 | * | referenceStatement? statusStatement? positionStatement? descriptionStatement? |
| 59 | * | referenceStatement? descriptionStatement? positionStatement? statusStatement? |
| 60 | * | referenceStatement? descriptionStatement? statusStatement? positionStatement? |
| 61 | * ; |
| 62 | */ |
| 63 | |
| 64 | import org.onosproject.yangutils.datamodel.YangBit; |
| 65 | import org.onosproject.yangutils.datamodel.YangBits; |
| 66 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 67 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.utils.YangConstructType.BIT_DATA; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 69 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 70 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 71 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 72 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 73 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 74 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 75 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 76 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT; |
| 77 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 78 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 79 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 80 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 81 | |
| 82 | /** |
| 83 | * Implements listener based call back function corresponding to the "bit" |
| 84 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 85 | */ |
| 86 | public final class BitListener { |
| 87 | |
| 88 | /** |
| 89 | * Creates a new bit listener. |
| 90 | */ |
| 91 | private BitListener() { |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * It is called when parser enters grammar rule (bit), it perform |
| 96 | * validations and updates the data model tree. |
| 97 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 98 | * @param listener listener's object |
| 99 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 100 | */ |
| 101 | public static void processBitEntry(TreeWalkListener listener, |
| 102 | GeneratedYangParser.BitStatementContext ctx) { |
| 103 | |
| 104 | // Check for stack to be non empty. |
| 105 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), ENTRY); |
| 106 | |
| 107 | YangBit bitNode = new YangBit(); |
| 108 | bitNode.setBitName(ctx.IDENTIFIER().getText()); |
| 109 | listener.getParsedDataStack().push(bitNode); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * It is called when parser exits from grammar rule (bit), it perform |
| 114 | * validations and update the data model tree. |
| 115 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 116 | * @param listener Listener's object |
| 117 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 118 | */ |
| 119 | public static void processBitExit(TreeWalkListener listener, |
| 120 | GeneratedYangParser.BitStatementContext ctx) { |
| 121 | |
| 122 | // Check for stack to be non empty. |
| 123 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT); |
| 124 | |
| 125 | Parsable tmpBitNode = listener.getParsedDataStack().peek(); |
| 126 | if (tmpBitNode instanceof YangBit) { |
| 127 | listener.getParsedDataStack().pop(); |
| 128 | |
| 129 | // Check for stack to be non empty. |
| 130 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT); |
| 131 | |
| 132 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 133 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 134 | case BITS_DATA: { |
| 135 | YangBits yangBits = (YangBits) tmpNode; |
| 136 | if ((ctx.bitBodyStatement() == null) || (ctx.bitBodyStatement().positionStatement() == null)) { |
| 137 | int maxPosition = 0; |
| 138 | boolean isPositionPresent = false; |
| 139 | |
| 140 | for (YangBit curBit : yangBits.getBitSet()) { |
| 141 | if (maxPosition <= curBit.getPosition()) { |
| 142 | maxPosition = curBit.getPosition(); |
| 143 | isPositionPresent = true; |
| 144 | } |
| 145 | } |
| 146 | if (isPositionPresent) { |
| 147 | maxPosition++; |
| 148 | } |
| 149 | ((YangBit) tmpBitNode).setPosition(maxPosition); |
| 150 | } |
| 151 | try { |
| 152 | yangBits.addBitInfo((YangBit) tmpBitNode); |
| 153 | } catch (DataModelException e) { |
| 154 | ParserException parserException = new ParserException(constructExtendedListenerErrorMessage( |
| 155 | INVALID_CONTENT, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT, e.getMessage())); |
| 156 | parserException.setLine(ctx.IDENTIFIER().getSymbol().getLine()); |
| 157 | parserException.setCharPosition(ctx.IDENTIFIER().getSymbol().getCharPositionInLine()); |
| 158 | throw parserException; |
| 159 | } |
| 160 | break; |
| 161 | } |
| 162 | default: |
| 163 | throw new ParserException( |
| 164 | constructListenerErrorMessage(INVALID_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT)); |
| 165 | } |
| 166 | } else { |
| 167 | throw new ParserException( |
| 168 | constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT)); |
| 169 | } |
| 170 | } |
| 171 | } |