blob: f500cca90ad0f67ec8660e9668349e863181036b [file] [log] [blame]
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +05301/*
2 * Copyright 2014-2016 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
17package org.onosproject.yangutils.parser.impl.listeners;
18
19/*
20 * Reference: RFC6020 and YANG ANTLR Grammar
21 *
22 * typedef-stmt = typedef-keyword sep identifier-arg-str optsep
23 * "{" stmtsep
24 * ;; these stmts can appear in any order
25 * type-stmt stmtsep
26 * [units-stmt stmtsep]
27 * [default-stmt stmtsep]
28 * [status-stmt stmtsep]
29 * [description-stmt stmtsep]
30 * [reference-stmt stmtsep]
31 * "}"
32 * default-stmt = default-keyword sep string stmtend
33
34 *
35 * ANTLR grammar rule
36 * typedefStatement : TYPEDEF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE
37 * (typeStatement | unitsStatement | defaultStatement | statusStatement
38 * | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
39 * defaultStatement : DEFAULT_KEYWORD string STMTEND;
40 */
41
42import org.onosproject.yangutils.datamodel.YangTypeDef;
43import org.onosproject.yangutils.parser.Parsable;
44import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
45import org.onosproject.yangutils.parser.exceptions.ParserException;
46import org.onosproject.yangutils.parser.impl.TreeWalkListener;
47
48import static org.onosproject.yangutils.parser.ParsableDataType.DEFAULT_DATA;
49import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
50import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
51import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
52import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
53import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
54
55public final class DefaultListener {
56
57 /**
58 * Creates a new default listener.
59 */
60 private DefaultListener() {
61 }
62
63 /**
64 * It is called when parser enters grammar rule (default), it perform
65 * validations and updates the data model tree.
66 *
67 * @param listener listener's object.
68 * @param ctx context object of the grammar rule.
69 */
70 public static void processDefaultEntry(TreeWalkListener listener,
71 GeneratedYangParser.DefaultStatementContext ctx) {
72
73 // Check for stack to be non empty.
74 checkStackIsNotEmpty(listener, MISSING_HOLDER, DEFAULT_DATA, ctx.string().getText(), ENTRY);
75
76 Parsable tmpNode = listener.getParsedDataStack().peek();
77 switch (tmpNode.getParsableDataType()) {
78 case TYPEDEF_DATA: {
79 YangTypeDef typeDef = (YangTypeDef) tmpNode;
80 typeDef.setDefaultValueInString(ctx.string().getText());
81 break;
82 }
83 default:
84 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
85 DEFAULT_DATA, ctx.string().getText(), ENTRY));
86 }
87 }
88}