Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +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 | import org.onosproject.yangutils.datamodel.YangCompilerAnnotation; |
| 20 | import org.onosproject.yangutils.datamodel.YangModule; |
| 21 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 22 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 26 | |
| 27 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.COMPILER_ANNOTATION_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.INVALID_HOLDER; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidPrefix; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 37 | |
| 38 | /* |
| 39 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 40 | * |
| 41 | * ABNF grammar as per RFC6020 |
| 42 | * compiler-annotation-stmt = prefix:compiler-annotation-keyword string |
| 43 | * "{" |
| 44 | * [app-data-structure-stmt stmtsep] |
| 45 | * [app-extended-stmt stmtsep] |
| 46 | * "}" |
| 47 | * |
| 48 | * ANTLR grammar rule |
| 49 | * compilerAnnotationStatement : COMPILER_ANNOTATION string LEFT_CURLY_BRACE |
| 50 | * compilerAnnotationBodyStatement RIGHT_CURLY_BRACE; |
| 51 | * |
| 52 | * compilerAnnotationBodyStatement : appDataStructureStatement? appExtendedStatement? ; |
| 53 | */ |
| 54 | |
| 55 | /** |
| 56 | * Represents listener based call back function corresponding to the "compiler-annotation" |
| 57 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 58 | */ |
| 59 | public final class CompilerAnnotationListener { |
| 60 | |
| 61 | /** |
| 62 | * Creates a new compiler-annotation listener. |
| 63 | */ |
| 64 | private CompilerAnnotationListener() { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Performs validation and updates the data model tree. It is called when parser receives an |
| 69 | * input matching the grammar rule(compiler-annotation). |
| 70 | * |
| 71 | * @param listener listener's object |
| 72 | * @param ctx context object of the grammar rule |
| 73 | */ |
| 74 | public static void processCompilerAnnotationEntry(TreeWalkListener listener, |
| 75 | GeneratedYangParser.CompilerAnnotationStatementContext ctx) { |
| 76 | // Check for stack to be non empty. |
| 77 | checkStackIsNotEmpty(listener, MISSING_HOLDER, COMPILER_ANNOTATION_DATA, ctx.string().getText(), ENTRY); |
| 78 | String prefix = getValidPrefix(ctx.COMPILER_ANNOTATION().getText(), COMPILER_ANNOTATION_DATA, ctx); |
| 79 | |
| 80 | YangCompilerAnnotation compilerAnnotation = new YangCompilerAnnotation(); |
| 81 | compilerAnnotation.setPrefix(prefix); |
| 82 | compilerAnnotation.setPath(removeQuotesAndHandleConcat(ctx.string().getText())); |
| 83 | |
| 84 | Parsable curData = listener.getParsedDataStack().peek(); |
| 85 | switch (curData.getYangConstructType()) { |
| 86 | case MODULE_DATA: |
| 87 | YangModule module = ((YangModule) curData); |
| 88 | module.addCompilerAnnotation(compilerAnnotation); |
| 89 | break; |
| 90 | case SUB_MODULE_DATA: |
| 91 | YangSubModule subModule = ((YangSubModule) curData); |
| 92 | subModule.addCompilerAnnotation(compilerAnnotation); |
| 93 | break; |
| 94 | default: |
| 95 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, COMPILER_ANNOTATION_DATA, |
| 96 | ctx.string().getText(), ENTRY)); |
| 97 | } |
| 98 | listener.getParsedDataStack().push(compilerAnnotation); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Performs validation and updates the data model tree. It is called when parser |
| 103 | * exits from grammar rule (compiler-annotation). |
| 104 | * |
| 105 | * @param listener listener's object |
| 106 | * @param ctx context object of the grammar rule |
| 107 | */ |
| 108 | public static void processCompilerAnnotationExit(TreeWalkListener listener, |
| 109 | GeneratedYangParser.CompilerAnnotationStatementContext ctx) { |
| 110 | |
| 111 | checkStackIsNotEmpty(listener, MISSING_HOLDER, COMPILER_ANNOTATION_DATA, ctx.string().getText(), EXIT); |
| 112 | if (!(listener.getParsedDataStack().peek() instanceof YangCompilerAnnotation)) { |
| 113 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, COMPILER_ANNOTATION_DATA, |
| 114 | ctx.string().getText(), EXIT)); |
| 115 | } |
| 116 | listener.getParsedDataStack().pop(); |
| 117 | } |
| 118 | } |