blob: 6963a9867f3b530c70b61105a576d8831a24ec8e [file] [log] [blame]
Gaurav Agrawalf5766422016-03-23 19:04:17 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawalf5766422016-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;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053029import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CASE_DATA;
30import static org.onosproject.yangutils.datamodel.utils.YangConstructType.SHORT_CASE_DATA;
Gaurav Agrawalf5766422016-03-23 19:04:17 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CHILD;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053043import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangCaseNode;
Gaurav Agrawalf5766422016-03-23 19:04:17 +053044
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 saraswal63f26fb2016-04-05 15:13:44 +053060 * Represents listener based call back function corresponding to the "short
Gaurav Agrawalf5766422016-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
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053077 * @param ctx context object of the grammar rule
Gaurav Agrawalf5766422016-03-23 19:04:17 +053078 */
79 public static void processShortCaseEntry(TreeWalkListener listener,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053080 GeneratedYangParser.ShortCaseStatementContext ctx) {
Gaurav Agrawalf5766422016-03-23 19:04:17 +053081
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
Bharat saraswale3175d32016-08-31 17:50:11 +053089 caseNode.setLineNumber(ctx.getStart().getLine());
90 caseNode.setCharPosition(ctx.getStart().getCharPositionInLine());
91 caseNode.setFileName(listener.getFileName());
Gaurav Agrawalf5766422016-03-23 19:04:17 +053092 if (ctx.containerStatement() != null) {
93 caseNode.setName(getValidIdentifier(ctx.containerStatement().identifier().getText(), CASE_DATA, ctx));
94 errorConstructContext = ctx.containerStatement();
95 } else if (ctx.listStatement() != null) {
96 caseNode.setName(getValidIdentifier(ctx.listStatement().identifier().getText(), CASE_DATA, ctx));
97 errorConstructContext = ctx.listStatement();
98 } else if (ctx.leafListStatement() != null) {
99 caseNode.setName(getValidIdentifier(ctx.leafListStatement().identifier().getText(), CASE_DATA, ctx));
100 errorConstructContext = ctx.leafListStatement();
101 } else if (ctx.leafStatement() != null) {
102 caseNode.setName(getValidIdentifier(ctx.leafStatement().identifier().getText(), CASE_DATA, ctx));
103 errorConstructContext = ctx.leafStatement();
104 } else {
105 throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, SHORT_CASE_DATA, "", ENTRY));
106 }
107 // TODO implement for augment.
108
109 int line = ((ParserRuleContext) errorConstructContext).getStart().getLine();
110 int charPositionInLine = ((ParserRuleContext) errorConstructContext).getStart().getCharPositionInLine();
111
112 // Check for identifier collision
113 detectCollidingChildUtil(listener, line, charPositionInLine, caseNode.getName(), CASE_DATA);
114
115 if ((listener.getParsedDataStack().peek()) instanceof YangChoice) {
116 try {
117 ((YangChoice) listener.getParsedDataStack().peek()).addChild(caseNode);
118 } catch (DataModelException e) {
119 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
120 SHORT_CASE_DATA, caseNode.getName(), ENTRY, e.getMessage()));
121 }
122 listener.getParsedDataStack().push(caseNode);
123 } else {
124 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, SHORT_CASE_DATA,
125 caseNode.getName(), ENTRY));
126 }
127 }
128
129 /**
130 * It is called when parser exits from grammar rule (short case), it perform
131 * validations and update the data model tree.
132 *
133 * @param listener Listener's object
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530134 * @param ctx context object of the grammar rule
Gaurav Agrawalf5766422016-03-23 19:04:17 +0530135 */
136 public static void processShortCaseExit(TreeWalkListener listener,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530137 GeneratedYangParser.ShortCaseStatementContext ctx) {
Gaurav Agrawalf5766422016-03-23 19:04:17 +0530138
139 // Check for stack to be non empty.
140 checkStackIsNotEmpty(listener, MISSING_HOLDER, SHORT_CASE_DATA, "", EXIT);
141
142 if (listener.getParsedDataStack().peek() instanceof YangCase) {
143 listener.getParsedDataStack().pop();
144 } else {
145 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SHORT_CASE_DATA,
146 "", EXIT));
147 }
148 }
149}