Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +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 | package org.onosproject.yangutils.parser.impl.listeners; |
| 17 | |
| 18 | import org.onosproject.yangutils.datamodel.YangBase; |
| 19 | import org.onosproject.yangutils.datamodel.YangIdentity; |
| 20 | import org.onosproject.yangutils.datamodel.YangIdentityRef; |
| 21 | import org.onosproject.yangutils.datamodel.YangNode; |
| 22 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
| 23 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 24 | import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl; |
| 25 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 26 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 27 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 28 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 29 | |
| 30 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 38 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.BASE_DATA; |
| 39 | |
| 40 | /** |
| 41 | * base-stmt = base-keyword sep identifier-ref-arg-str |
| 42 | * optsep stmtend* |
| 43 | * identifier-ref-arg = [prefix ":"] identifier |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * Represents listener based call back function corresponding to the "base" |
| 48 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 49 | */ |
| 50 | public final class BaseListener { |
| 51 | |
| 52 | //Creates a new base listener. |
| 53 | private BaseListener() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Performs validation and updates the data model tree when parser receives an |
| 58 | * input matching the grammar rule (base). |
| 59 | * |
| 60 | * @param listener listener's object |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 61 | * @param ctx context object of the grammar rule |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 62 | */ |
| 63 | public static void processBaseEntry(TreeWalkListener listener, |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 64 | GeneratedYangParser.BaseStatementContext ctx) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 65 | |
| 66 | // Check for stack to be non empty. |
| 67 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BASE_DATA, ctx.string().getText(), ENTRY); |
| 68 | |
| 69 | YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), BASE_DATA, ctx); |
| 70 | |
| 71 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 72 | |
| 73 | /** |
| 74 | * For identityref base node identifier is copied in identity listener itself, so no need to process |
| 75 | * base statement for indentityref |
| 76 | */ |
| 77 | if (tmpData instanceof YangIdentityRef) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (!(tmpData instanceof YangIdentity)) { |
| 82 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BASE_DATA, |
| 83 | ctx.string().getText(), ENTRY)); |
| 84 | } |
| 85 | |
| 86 | YangBase yangBase = new YangBase(); |
| 87 | yangBase.setBaseIdentifier(nodeIdentifier); |
| 88 | ((YangIdentity) tmpData).setBaseNode(yangBase); |
| 89 | |
| 90 | int errorLine = ctx.getStart().getLine(); |
| 91 | int errorPosition = ctx.getStart().getCharPositionInLine(); |
| 92 | |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 93 | yangBase.setLineNumber(errorLine); |
| 94 | yangBase.setCharPosition(errorPosition); |
| 95 | yangBase.setFileName(listener.getFileName()); |
| 96 | |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 97 | // Add resolution information to the list |
| 98 | YangResolutionInfoImpl resolutionInfo = |
| 99 | new YangResolutionInfoImpl<YangBase>(yangBase, (YangNode) tmpData, errorLine, errorPosition); |
| 100 | addToResolutionList(resolutionInfo, ctx); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Add to resolution list. |
| 105 | * |
| 106 | * @param resolutionInfo resolution information |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 107 | * @param ctx context object of the grammar rule |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 108 | */ |
| 109 | private static void addToResolutionList(YangResolutionInfoImpl<YangBase> resolutionInfo, |
| 110 | GeneratedYangParser.BaseStatementContext ctx) { |
| 111 | |
| 112 | try { |
| 113 | addResolutionInfo(resolutionInfo); |
| 114 | } catch (DataModelException e) { |
| 115 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 116 | BASE_DATA, ctx.string().getText(), EXIT, e.getMessage())); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } |