blob: 7744866cd1199266e19a6687bb66d41923d285fc [file] [log] [blame]
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05303 *
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
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import org.antlr.v4.runtime.ParserRuleContext;
20import org.antlr.v4.runtime.tree.ParseTree;
21import org.onosproject.yangutils.datamodel.YangCase;
22import org.onosproject.yangutils.datamodel.YangChoice;
23import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25import org.onosproject.yangutils.parser.exceptions.ParserException;
26import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27
28import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
29import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangCaseNode;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
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.INVALID_CHILD;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
42import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
43import static org.onosproject.yangutils.utils.YangConstructType.SHORT_CASE_DATA;
44
45/*
46 * Reference: RFC6020 and YANG ANTLR Grammar
47 *
48 * ABNF grammar as per RFC6020
49 * short-case-stmt = container-stmt /
50 * leaf-stmt /
51 * leaf-list-stmt /
52 * list-stmt /
53 * anyxml-stmt
54 *
55 * ANTLR grammar rule
56 * shortCaseStatement : containerStatement | leafStatement | leafListStatement | listStatement;
57 */
58
59/**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Represents listener based call back function corresponding to the "short
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053061 * case" rule defined in ANTLR grammar file for corresponding ABNF rule in RFC
62 * 6020.
63 */
64public final class ShortCaseListener {
65
66 /**
67 * Create a new short case listener.
68 */
69 private ShortCaseListener() {
70 }
71
72 /**
73 * It is called when parser enters grammar rule (short case), it perform
74 * validations and updates the data model tree.
75 *
76 * @param listener listener's object
77 * @param ctx context object of the grammar rule
78 */
79 public static void processShortCaseEntry(TreeWalkListener listener,
80 GeneratedYangParser.ShortCaseStatementContext ctx) {
81
82 ParseTree errorConstructContext;
83
84 // Check for stack to be non empty.
85 checkStackIsNotEmpty(listener, MISSING_HOLDER, SHORT_CASE_DATA, "", ENTRY);
86
87 YangCase caseNode = getYangCaseNode(JAVA_GENERATION);
88
89 if (ctx.containerStatement() != null) {
90 caseNode.setName(getValidIdentifier(ctx.containerStatement().identifier().getText(), CASE_DATA, ctx));
91 errorConstructContext = ctx.containerStatement();
92 } else if (ctx.listStatement() != null) {
93 caseNode.setName(getValidIdentifier(ctx.listStatement().identifier().getText(), CASE_DATA, ctx));
94 errorConstructContext = ctx.listStatement();
95 } else if (ctx.leafListStatement() != null) {
96 caseNode.setName(getValidIdentifier(ctx.leafListStatement().identifier().getText(), CASE_DATA, ctx));
97 errorConstructContext = ctx.leafListStatement();
98 } else if (ctx.leafStatement() != null) {
99 caseNode.setName(getValidIdentifier(ctx.leafStatement().identifier().getText(), CASE_DATA, ctx));
100 errorConstructContext = ctx.leafStatement();
101 } else {
102 throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, SHORT_CASE_DATA, "", ENTRY));
103 }
104 // TODO implement for augment.
105
106 int line = ((ParserRuleContext) errorConstructContext).getStart().getLine();
107 int charPositionInLine = ((ParserRuleContext) errorConstructContext).getStart().getCharPositionInLine();
108
109 // Check for identifier collision
110 detectCollidingChildUtil(listener, line, charPositionInLine, caseNode.getName(), CASE_DATA);
111
112 if ((listener.getParsedDataStack().peek()) instanceof YangChoice) {
113 try {
114 ((YangChoice) listener.getParsedDataStack().peek()).addChild(caseNode);
115 } catch (DataModelException e) {
116 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
117 SHORT_CASE_DATA, caseNode.getName(), ENTRY, e.getMessage()));
118 }
119 listener.getParsedDataStack().push(caseNode);
120 } else {
121 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, SHORT_CASE_DATA,
122 caseNode.getName(), ENTRY));
123 }
124 }
125
126 /**
127 * It is called when parser exits from grammar rule (short case), it perform
128 * validations and update the data model tree.
129 *
130 * @param listener Listener's object
131 * @param ctx context object of the grammar rule
132 */
133 public static void processShortCaseExit(TreeWalkListener listener,
134 GeneratedYangParser.ShortCaseStatementContext ctx) {
135
136 // Check for stack to be non empty.
137 checkStackIsNotEmpty(listener, MISSING_HOLDER, SHORT_CASE_DATA, "", EXIT);
138
139 if (listener.getParsedDataStack().peek() instanceof YangCase) {
140 listener.getParsedDataStack().pop();
141 } else {
142 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SHORT_CASE_DATA,
143 "", EXIT));
144 }
145 }
146}