blob: ebf85a7b33699eb403b285e577468fbed5999523 [file] [log] [blame]
Shankara-Huawei234cd092016-07-14 11:35:34 +05301/*
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 */
16package org.onosproject.yangutils.parser.impl.listeners;
17
18import org.onosproject.yangutils.datamodel.YangBase;
19import org.onosproject.yangutils.datamodel.YangIdentity;
20import org.onosproject.yangutils.datamodel.YangIdentityRef;
21import org.onosproject.yangutils.datamodel.YangNode;
22import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
23import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
25import org.onosproject.yangutils.datamodel.utils.Parsable;
26import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
27import org.onosproject.yangutils.parser.exceptions.ParserException;
28import org.onosproject.yangutils.parser.impl.TreeWalkListener;
29
30import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
38import 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 */
50public 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-Huawei8f164222016-08-31 15:47:30 +053061 * @param ctx context object of the grammar rule
Shankara-Huawei234cd092016-07-14 11:35:34 +053062 */
63 public static void processBaseEntry(TreeWalkListener listener,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053064 GeneratedYangParser.BaseStatementContext ctx) {
Shankara-Huawei234cd092016-07-14 11:35:34 +053065
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 saraswale3175d32016-08-31 17:50:11 +053093 yangBase.setLineNumber(errorLine);
94 yangBase.setCharPosition(errorPosition);
95 yangBase.setFileName(listener.getFileName());
96
Shankara-Huawei234cd092016-07-14 11:35:34 +053097 // 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-Huawei8f164222016-08-31 15:47:30 +0530107 * @param ctx context object of the grammar rule
Shankara-Huawei234cd092016-07-14 11:35:34 +0530108 */
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}