[ONOS-3897] Yang Listener for Enumeration Data Type
Change-Id: If257c73da8fe2dcc2f4111f103967cfcdd7fa273
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumListener.java
new file mode 100644
index 0000000..e255441
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumListener.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2014-2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ * enum-stmt = enum-keyword sep string optsep
+ * (";" /
+ * "{" stmtsep
+ * ;; these stmts can appear in any order
+ * [value-stmt stmtsep]
+ * [status-stmt stmtsep]
+ * [description-stmt stmtsep]
+ * [reference-stmt stmtsep]
+ * "}")
+ *
+ * ANTLR grammar rule
+ * enumStatement : ENUM_KEYWORD string (STMTEND | LEFT_CURLY_BRACE enumStatementBody RIGHT_CURLY_BRACE);
+ *
+ * enumStatementBody : valueStatement? statusStatement? descriptionStatement? referenceStatement?
+ * | valueStatement? statusStatement? referenceStatement? descriptionStatement?
+ * | valueStatement? descriptionStatement? statusStatement? referenceStatement?
+ * | valueStatement? descriptionStatement? referenceStatement? statusStatement?
+ * | valueStatement? referenceStatement? statusStatement? descriptionStatement?
+ * | valueStatement? referenceStatement? descriptionStatement? statusStatement?
+ * | statusStatement? valueStatement? descriptionStatement? referenceStatement?
+ * | statusStatement? valueStatement? referenceStatement? descriptionStatement?
+ * | statusStatement? descriptionStatement? descriptionStatement? valueStatement?
+ * | statusStatement? descriptionStatement? valueStatement? descriptionStatement?
+ * | statusStatement? referenceStatement? valueStatement? descriptionStatement?
+ * | statusStatement? referenceStatement? descriptionStatement? valueStatement?
+ * | descriptionStatement? valueStatement? statusStatement? referenceStatement?
+ * | descriptionStatement? valueStatement? referenceStatement? statusStatement?
+ * | descriptionStatement? statusStatement? valueStatement? referenceStatement?
+ * | descriptionStatement? statusStatement? referenceStatement? valueStatement?
+ * | descriptionStatement? referenceStatement? valueStatement? statusStatement?
+ * | descriptionStatement? referenceStatement? statusStatement? valueStatement?
+ * | referenceStatement? valueStatement? descriptionStatement? statusStatement?
+ * | referenceStatement? valueStatement? statusStatement? descriptionStatement?
+ * | referenceStatement? statusStatement? descriptionStatement? valueStatement?
+ * | referenceStatement? statusStatement? valueStatement? descriptionStatement?
+ * | referenceStatement? descriptionStatement? valueStatement? statusStatement?
+ * | referenceStatement? descriptionStatement? statusStatement? valueStatement?
+ * ;
+ */
+
+import org.onosproject.yangutils.datamodel.YangEnum;
+import org.onosproject.yangutils.datamodel.YangEnumeration;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import static org.onosproject.yangutils.parser.ParsableDataType.ENUM_DATA;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.DUPLICATE_ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/**
+ * Implements listener based call back function corresponding to the "enum" rule
+ * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class EnumListener {
+
+ /**
+ * Creates a new enum listener.
+ */
+ private EnumListener() {
+ }
+
+ /**
+ * It is called when parser enters grammar rule (enum), it perform
+ * validations and updates the data model tree.
+ *
+ * @param listener listener's object.
+ * @param ctx context object of the grammar rule.
+ */
+ public static void processEnumEntry(TreeWalkListener listener, GeneratedYangParser.EnumStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), ENTRY);
+
+ YangEnum enumNode = new YangEnum();
+ enumNode.setNamedValue(ctx.string().getText());
+ listener.getParsedDataStack().push(enumNode);
+ }
+
+ /**
+ * It is called when parser exits from grammar rule (enum), it perform
+ * validations and update the data model tree.
+ *
+ * @param listener Listener's object.
+ * @param ctx context object of the grammar rule.
+ */
+ public static void processEnumExit(TreeWalkListener listener, GeneratedYangParser.EnumStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT);
+
+ Parsable tmpEnumNode = listener.getParsedDataStack().peek();
+ if (tmpEnumNode instanceof YangEnum) {
+ listener.getParsedDataStack().pop();
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT);
+
+ Parsable tmpNode = listener.getParsedDataStack().peek();
+ switch (tmpNode.getParsableDataType()) {
+ case ENUMERATION_DATA: {
+ YangEnumeration yangEnumeration = (YangEnumeration) tmpNode;
+ if ((ctx.enumStatementBody() == null) || (ctx.enumStatementBody().valueStatement() == null)) {
+ int maxValue = 0;
+ boolean isValuePresent = false;
+
+ for (YangEnum curEnum : yangEnumeration.getEnumSet()) {
+ if (maxValue <= curEnum.getValue()) {
+ maxValue = curEnum.getValue();
+ isValuePresent = true;
+ }
+ }
+ if (isValuePresent) {
+ maxValue++;
+ }
+ ((YangEnum) tmpEnumNode).setValue(maxValue);
+ }
+ try {
+ yangEnumeration.addEnumInfo((YangEnum) tmpEnumNode);
+ } catch (DataModelException e) {
+ ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
+ DUPLICATE_ENTRY, ENUM_DATA, ctx.string().getText(), EXIT, e.getMessage()));
+ parserException.setLine(ctx.string().STRING(0).getSymbol().getLine());
+ parserException.setCharPosition(ctx.string().STRING(0).getSymbol().getCharPositionInLine());
+ throw parserException;
+ }
+ break;
+ }
+ default:
+ throw new ParserException(
+ constructListenerErrorMessage(INVALID_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT));
+ }
+ } else {
+ throw new ParserException(
+ constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT));
+ }
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumerationListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumerationListener.java
new file mode 100644
index 0000000..f83c49c
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumerationListener.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2014-2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ * type-body-stmts = numerical-restrictions /
+ * decimal64-specification /
+ * string-restrictions /
+ * enum-specification /
+ * leafref-specification /
+ * identityref-specification /
+ * instance-identifier-specification /
+ * bits-specification /
+ * union-specification
+ *
+ * enum-specification = 1*(enum-stmt stmtsep)
+ *
+ * ANTLR grammar rule
+ *
+ * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification
+ * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification
+ * | bitsSpecification | unionSpecification;
+ *
+ * enumSpecification : enumStatement+;
+ */
+
+import org.onosproject.yangutils.datamodel.YangEnumeration;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.parser.Parsable;
+import static org.onosproject.yangutils.parser.ParsableDataType.ENUMERATION_DATA;
+import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/**
+ * Implements listener based call back function corresponding to the
+ * "enumeration" rule defined in ANTLR grammar file for corresponding ABNF rule
+ * in RFC 6020.
+ */
+public final class EnumerationListener {
+
+ /**
+ * Creates a new enumeration listener.
+ */
+ private EnumerationListener() {
+ }
+
+ /**
+ * It is called when parser enters grammar rule (enumeration), it perform
+ * validations and updates the data model tree.
+ *
+ * @param listener listener's object.
+ * @param ctx context object of the grammar rule.
+ */
+ public static void processEnumerationEntry(TreeWalkListener listener,
+ GeneratedYangParser.EnumSpecificationContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
+
+ if (listener.getParsedDataStack().peek() instanceof YangType) {
+ YangEnumeration enumerationNode = new YangEnumeration();
+ Parsable typeData = listener.getParsedDataStack().pop();
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
+
+ Parsable tmpData = listener.getParsedDataStack().peek();
+
+ switch (tmpData.getParsableDataType()) {
+ case LEAF_DATA:
+ enumerationNode.setEnumerationName(((YangLeaf) tmpData).getLeafName());
+ break;
+ case LEAF_LIST_DATA:
+ enumerationNode.setEnumerationName(((YangLeafList) tmpData).getLeafName());
+ break;
+ // TODO typedef, union, deviate.
+ default:
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
+ ((YangType) typeData).getDataTypeName(), ENTRY));
+ }
+ listener.getParsedDataStack().push(typeData);
+ listener.getParsedDataStack().push(enumerationNode);
+ } else {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", ENTRY));
+ }
+ }
+
+ /**
+ * It is called when parser exits from grammar rule (enumeration), it
+ * perform validations and update the data model tree.
+ *
+ * @param listener Listener's object.
+ * @param ctx context object of the grammar rule.
+ */
+ public static void processEnumerationExit(TreeWalkListener listener,
+ GeneratedYangParser.EnumSpecificationContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT);
+
+ Parsable tmpEnumerationNode = listener.getParsedDataStack().peek();
+ if (tmpEnumerationNode instanceof YangEnumeration) {
+ listener.getParsedDataStack().pop();
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT);
+
+ Parsable tmpNode = listener.getParsedDataStack().peek();
+ switch (tmpNode.getParsableDataType()) {
+ case TYPE_DATA: {
+ YangType typeNode = (YangType) tmpNode;
+ typeNode.setDataTypeExtendedInfo((YangEnumeration) tmpEnumerationNode);
+ break;
+ }
+ default:
+ throw new ParserException(
+ constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", EXIT));
+ }
+ } else {
+ throw new ParserException(
+ constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUMERATION_DATA, "", EXIT));
+ }
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
index e5ebf36..de595f5 100644
--- a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/TypeListener.java
@@ -21,18 +21,18 @@
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.parser.Parsable;
+import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
-
-import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
-
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
@@ -48,8 +48,8 @@
*/
/**
- * Implements listener based call back function corresponding to the "type"
- * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ * Implements listener based call back function corresponding to the "type" rule
+ * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
*/
public final class TypeListener {
@@ -60,15 +60,14 @@
}
/**
- * It is called when parser receives an input matching the grammar
- * rule (type), performs validation and updates the data model
- * tree.
+ * It is called when parser receives an input matching the grammar rule
+ * (type), performs validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processTypeEntry(TreeWalkListener listener,
- GeneratedYangParser.TypeStatementContext ctx) {
+ GeneratedYangParser.TypeStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY);
@@ -78,21 +77,46 @@
type.setDataTypeName(ctx.string().getText());
type.setDataType(yangDataTypes);
+ listener.getParsedDataStack().push(type);
+ }
+
+ /**
+ * It is called when parser exits from grammar rule (type), it perform
+ * validations and update the data model tree.
+ *
+ * @param listener Listener's object.
+ * @param ctx context object of the grammar rule.
+ */
+ public static void processTypeExit(TreeWalkListener listener,
+ GeneratedYangParser.TypeStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
+
+ Parsable type = listener.getParsedDataStack().pop();
+ if (!(type instanceof YangType)) {
+ throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
+ ctx.string().getText(), EXIT));
+ }
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
+
Parsable tmpData = listener.getParsedDataStack().peek();
switch (tmpData.getParsableDataType()) {
case LEAF_DATA:
YangLeaf leaf = (YangLeaf) tmpData;
- leaf.setDataType(type);
+ leaf.setDataType((YangType) type);
break;
case LEAF_LIST_DATA:
YangLeafList leafList = (YangLeafList) tmpData;
- leafList.setDataType(type);
+ leafList.setDataType((YangType) type);
break;
case TYPEDEF_DATA: //TODO
break;
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
- ctx.string().getText(), ENTRY));
+ ctx.string().getText(), EXIT));
}
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ValueListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ValueListener.java
new file mode 100644
index 0000000..07ada45
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ValueListener.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2014-2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+/*
+ * Reference: RFC6020 and YANG ANTLR Grammar
+ *
+ * ABNF grammar as per RFC6020
+ * value-stmt = value-keyword sep integer-value stmtend
+ *
+ * ANTLR grammar rule
+ * valueStatement : VALUE_KEYWORD ((MINUS INTEGER) | INTEGER) STMTEND;
+ */
+
+import org.onosproject.yangutils.datamodel.YangEnum;
+import org.onosproject.yangutils.datamodel.YangEnumeration;
+import org.onosproject.yangutils.parser.Parsable;
+import static org.onosproject.yangutils.parser.ParsableDataType.VALUE_DATA;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/**
+ * Implements listener based call back function corresponding to the "value"
+ * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
+ */
+public final class ValueListener {
+
+ /**
+ * Creates a new value listener.
+ */
+ private ValueListener() {
+ }
+
+ /**
+ * It is called when parser receives an input matching the grammar rule
+ * (value), perform validations and update the data model tree.
+ *
+ * @param listener Listener's object.
+ * @param ctx context object of the grammar rule.
+ */
+ public static void processValueEntry(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
+
+ // Check for stack to be non empty.
+ checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY);
+
+ // Obtain the node of the stack.
+ Parsable tmpNode = listener.getParsedDataStack().peek();
+ switch (tmpNode.getParsableDataType()) {
+ case ENUM_DATA: {
+ YangEnum enumNode = (YangEnum) tmpNode;
+ if (!isEnumValueValid(listener, ctx)) {
+ ParserException parserException = new ParserException("Input version not supported");
+ parserException.setLine(ctx.INTEGER().getSymbol().getLine());
+ parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
+ throw parserException;
+ }
+ enumNode.setValue(Integer.valueOf(ctx.INTEGER().getText()));
+ break;
+ }
+ default:
+ throw new ParserException(
+ constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY));
+ }
+ }
+
+ /**
+ * Validates ENUM value uniqueness.
+ *
+ * @param listener Listener's object.
+ * @param ctx context object of the grammar rule.
+ * @return validation result
+ */
+ private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
+ Parsable enumNode = listener.getParsedDataStack().pop();
+ Parsable tmpNode = listener.getParsedDataStack().peek();
+ switch (tmpNode.getParsableDataType()) {
+ case ENUMERATION_DATA: {
+ YangEnumeration yangEnumeration = (YangEnumeration) tmpNode;
+ for (YangEnum curEnum : yangEnumeration.getEnumSet()) {
+ if (Integer.valueOf(ctx.INTEGER().getText()) == curEnum.getValue()) {
+ listener.getParsedDataStack().push(enumNode);
+ return false;
+ }
+ }
+ listener.getParsedDataStack().push(enumNode);
+ return true;
+ }
+ default:
+ listener.getParsedDataStack().push(enumNode);
+ throw new ParserException(
+ constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), EXIT));
+ }
+ }
+}