Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | * decimal64-specification = fraction-digits-stmt [range-stmt stmtsep] |
| 34 | * |
| 35 | * fraction-digits-stmt = fraction-digits-keyword sep |
| 36 | * fraction-digits-arg-str stmtend |
| 37 | * |
| 38 | * fraction-digits-arg-str = < a string that matches the rule |
| 39 | * fraction-digits-arg > |
| 40 | * |
| 41 | * fraction-digits-arg = ("1" ["0" / "1" / "2" / "3" / "4" / |
| 42 | * "5" / "6" / "7" / "8"]) |
| 43 | * / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" |
| 44 | * |
| 45 | * range-stmt = range-keyword sep range-arg-str optsep |
| 46 | * (";" / |
| 47 | * "{" stmtsep |
| 48 | * ;; these stmts can appear in any order |
| 49 | * [error-message-stmt stmtsep] |
| 50 | * [error-app-tag-stmt stmtsep] |
| 51 | * [description-stmt stmtsep] |
| 52 | * [reference-stmt stmtsep] |
| 53 | * "}") |
| 54 | * ANTLR grammar rule |
| 55 | * |
| 56 | * typeBodyStatements : numericalRestrictions | decimal64Specification | stringRestrictions | enumSpecification |
| 57 | * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification |
| 58 | * | bitsSpecification | unionSpecification; |
| 59 | * |
| 60 | * decimal64Specification : fractionDigitStatement rangeStatement?; |
| 61 | * |
| 62 | * fractionDigitStatement : FRACTION_DIGITS_KEYWORD fraction STMTEND; |
| 63 | * |
| 64 | * fraction : string; |
| 65 | */ |
| 66 | |
| 67 | import org.onosproject.yangutils.datamodel.YangDecimal64; |
| 68 | import org.onosproject.yangutils.datamodel.YangRangeRestriction; |
| 69 | import org.onosproject.yangutils.datamodel.YangType; |
| 70 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 71 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 72 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 73 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 74 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 75 | |
| 76 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DECIMAL64_DATA; |
| 77 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 78 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 79 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 80 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 81 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 82 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 83 | |
| 84 | /** |
| 85 | * Represents listener based call back function corresponding to the "decimal64" rule |
| 86 | * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 87 | */ |
| 88 | public final class Decimal64Listener { |
| 89 | |
| 90 | /** |
| 91 | * Creates a new Decimal64 listener. |
| 92 | */ |
| 93 | private Decimal64Listener() { |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * It is called when parser enters grammar rule (decimal64), it perform |
| 98 | * validations and updates the data model tree. |
| 99 | * |
| 100 | * @param listener listener's object |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 101 | * @param ctx context object of the grammar rule |
Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 102 | */ |
| 103 | public static void processDecimal64Entry(TreeWalkListener listener, |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 104 | GeneratedYangParser.Decimal64SpecificationContext ctx) { |
Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 105 | |
| 106 | // Check for stack to be non empty. |
| 107 | checkStackIsNotEmpty(listener, MISSING_HOLDER, DECIMAL64_DATA, "", ENTRY); |
| 108 | |
| 109 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 110 | if (tmpNode instanceof YangType) { |
| 111 | YangType<YangDecimal64<YangRangeRestriction>> typeNode = |
| 112 | (YangType<YangDecimal64<YangRangeRestriction>>) tmpNode; |
| 113 | YangDecimal64 decimal64Node = new YangDecimal64(); |
| 114 | typeNode.setDataTypeExtendedInfo(decimal64Node); |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame^] | 115 | |
| 116 | decimal64Node.setLineNumber(ctx.getStart().getLine()); |
| 117 | decimal64Node.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 118 | decimal64Node.setFileName(listener.getFileName()); |
Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 119 | } else { |
| 120 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, DECIMAL64_DATA, "", ENTRY)); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Performs validation and updates the data model tree. |
| 126 | * It is called when parser exits from grammar rule (decimal64). |
| 127 | * |
| 128 | * @param listener listener's object |
| 129 | * @param ctx context object of the grammar rule |
| 130 | */ |
| 131 | public static void processDecimal64Exit(TreeWalkListener listener, |
| 132 | GeneratedYangParser.Decimal64SpecificationContext ctx) { |
| 133 | |
| 134 | // Check for stack to be non empty. |
| 135 | checkStackIsNotEmpty(listener, MISSING_HOLDER, DECIMAL64_DATA, "", EXIT); |
| 136 | |
| 137 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 138 | if (tmpNode instanceof YangType) { |
| 139 | YangType<YangDecimal64<YangRangeRestriction>> typeNode = |
| 140 | (YangType<YangDecimal64<YangRangeRestriction>>) tmpNode; |
| 141 | YangDecimal64<YangRangeRestriction> decimal64Node = typeNode.getDataTypeExtendedInfo(); |
| 142 | try { |
| 143 | decimal64Node.validateRange(); |
| 144 | } catch (DataModelException e) { |
| 145 | throw new ParserException(e); |
| 146 | } |
| 147 | } else { |
| 148 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, DECIMAL64_DATA, "", EXIT)); |
| 149 | } |
| 150 | } |
| 151 | } |