Gaurav Agrawal | 0cfdeed | 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 | * type-body-stmts = numerical-restrictions / |
| 24 | * decimal64-specification / |
| 25 | * string-restrictions / |
| 26 | * enum-specification / |
| 27 | * leafref-specification / |
| 28 | * identityref-specification / |
| 29 | * instance-identifier-specification / |
| 30 | * bits-specification / |
| 31 | * union-specification |
| 32 | * |
| 33 | * bits-specification = 1*(bit-stmt stmtsep) |
| 34 | * |
| 35 | * ANTLR grammar rule |
| 36 | * |
| 37 | * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification |
| 38 | * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification |
| 39 | * | bitsSpecification | unionSpecification; |
| 40 | * |
| 41 | * bitsSpecification : bitStatement+; |
| 42 | */ |
| 43 | |
| 44 | import org.onosproject.yangutils.datamodel.YangBits; |
| 45 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 46 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 47 | import org.onosproject.yangutils.datamodel.YangType; |
| 48 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.YangConstructType.BITS_DATA; |
| 50 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 51 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 52 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 53 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 54 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 55 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 56 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 57 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 58 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 59 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 60 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 61 | |
| 62 | /** |
| 63 | * Implements listener based call back function corresponding to the "bits" |
| 64 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 65 | */ |
| 66 | public final class BitsListener { |
| 67 | |
| 68 | /** |
| 69 | * Creates a new bits listener. |
| 70 | */ |
| 71 | private BitsListener() { |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * It is called when parser enters grammar rule (bits), it perform |
| 76 | * validations and updates the data model tree. |
| 77 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 78 | * @param listener listener's object |
| 79 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 80 | */ |
| 81 | public static void processBitsEntry(TreeWalkListener listener, |
| 82 | GeneratedYangParser.BitsSpecificationContext ctx) { |
| 83 | |
| 84 | // Check for stack to be non empty. |
| 85 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY); |
| 86 | |
| 87 | if (listener.getParsedDataStack().peek() instanceof YangType) { |
| 88 | YangBits bitsNode = new YangBits(); |
| 89 | Parsable typeData = listener.getParsedDataStack().pop(); |
| 90 | |
| 91 | // Check for stack to be non empty. |
| 92 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY); |
| 93 | |
| 94 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 95 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 96 | switch (tmpData.getYangConstructType()) { |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 97 | case LEAF_DATA: |
| 98 | bitsNode.setBitsName(((YangLeaf) tmpData).getLeafName()); |
| 99 | break; |
| 100 | case LEAF_LIST_DATA: |
| 101 | bitsNode.setBitsName(((YangLeafList) tmpData).getLeafName()); |
| 102 | break; |
| 103 | // TODO typedef, union, deviate. |
| 104 | default: |
| 105 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 106 | ((YangType) typeData).getDataTypeName(), ENTRY)); |
| 107 | } |
| 108 | listener.getParsedDataStack().push(typeData); |
| 109 | listener.getParsedDataStack().push(bitsNode); |
| 110 | } else { |
| 111 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", ENTRY)); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * It is called when parser exits from grammar rule (bits), it perform |
| 117 | * validations and update the data model tree. |
| 118 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 119 | * @param listener Listener's object |
| 120 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 121 | */ |
| 122 | public static void processBitsExit(TreeWalkListener listener, |
| 123 | GeneratedYangParser.BitsSpecificationContext ctx) { |
| 124 | |
| 125 | // Check for stack to be non empty. |
| 126 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT); |
| 127 | |
| 128 | Parsable tmpBitsNode = listener.getParsedDataStack().peek(); |
| 129 | if (tmpBitsNode instanceof YangBits) { |
| 130 | listener.getParsedDataStack().pop(); |
| 131 | |
| 132 | // Check for stack to be non empty. |
| 133 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT); |
| 134 | |
| 135 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 136 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 137 | case TYPE_DATA: { |
| 138 | YangType typeNode = (YangType) tmpNode; |
| 139 | typeNode.setDataTypeExtendedInfo((YangBits) tmpBitsNode); |
| 140 | break; |
| 141 | } |
| 142 | default: |
| 143 | throw new ParserException( |
| 144 | constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", EXIT)); |
| 145 | } |
| 146 | } else { |
| 147 | throw new ParserException( |
| 148 | constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BITS_DATA, "", EXIT)); |
| 149 | } |
| 150 | } |
| 151 | } |