Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 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 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangBelongsTo; |
| 20 | import org.onosproject.yangutils.datamodel.YangImport; |
| 21 | import org.onosproject.yangutils.datamodel.YangModule; |
| 22 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 26 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.YangConstructType.PREFIX_DATA; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 37 | * |
| 38 | * ABNF grammar as per RFC6020 |
| 39 | * module-header-stmts = ;; these stmts can appear in any order |
| 40 | * [yang-version-stmt stmtsep] |
| 41 | * namespace-stmt stmtsep |
| 42 | * prefix-stmt stmtsep |
| 43 | * |
| 44 | * prefix-stmt = prefix-keyword sep prefix-arg-str |
| 45 | * optsep stmtend |
| 46 | * |
| 47 | * ANTLR grammar rule |
| 48 | * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt |
| 49 | * | yang_version_stmt? prefix_stmt namespace_stmt |
| 50 | * | namespace_stmt yang_version_stmt? prefix_stmt |
| 51 | * | namespace_stmt prefix_stmt yang_version_stmt? |
| 52 | * | prefix_stmt namespace_stmt yang_version_stmt? |
| 53 | * | prefix_stmt yang_version_stmt? namespace_stmt |
| 54 | * ; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 55 | * prefix_stmt : PREFIX_KEYWORD identifier STMTEND; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 56 | */ |
| 57 | |
| 58 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 59 | * Represents listener based call back function corresponding to the "prefix" |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 60 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 61 | */ |
| 62 | public final class PrefixListener { |
| 63 | |
| 64 | /** |
| 65 | * Creates a new prefix listener. |
| 66 | */ |
| 67 | private PrefixListener() { |
| 68 | } |
| 69 | |
| 70 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 71 | * It is called when parser receives an input matching the grammar rule |
| 72 | * (prefix),perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 73 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 74 | * @param listener Listener's object |
| 75 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 76 | */ |
| 77 | public static void processPrefixEntry(TreeWalkListener listener, GeneratedYangParser.PrefixStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 78 | |
| 79 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 80 | checkStackIsNotEmpty(listener, MISSING_HOLDER, PREFIX_DATA, ctx.identifier().getText(), ENTRY); |
| 81 | |
| 82 | String identifier = getValidIdentifier(ctx.identifier().getText(), PREFIX_DATA, ctx); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 83 | |
| 84 | // Obtain the node of the stack. |
| 85 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 86 | switch (tmpNode.getYangConstructType()) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 87 | case MODULE_DATA: { |
| 88 | YangModule module = (YangModule) tmpNode; |
| 89 | module.setPrefix(identifier); |
| 90 | break; |
| 91 | } |
| 92 | case IMPORT_DATA: { |
| 93 | YangImport importNode = (YangImport) tmpNode; |
| 94 | importNode.setPrefixId(identifier); |
| 95 | break; |
| 96 | } |
| 97 | case BELONGS_TO_DATA: { |
| 98 | YangBelongsTo belongstoNode = (YangBelongsTo) tmpNode; |
| 99 | belongstoNode.setPrefix(identifier); |
| 100 | break; |
| 101 | } |
| 102 | default: |
| 103 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PREFIX_DATA, |
| 104 | ctx.identifier().getText(), ENTRY)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 105 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 106 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 107 | } |