Merge "BUG-869: added proper handling of nullable parameter"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / ParserListenerUtils.java
index b5f66925e4a0ba9cd83997273efdce4b7b936d16..83b56314cb93de7f7fbfd603e66d05b3966b5037 100644 (file)
@@ -141,7 +141,8 @@ public final class ParserListenerUtils {
     private static final Splitter KEYDEF_SPLITTER = Splitter.on(' ').omitEmptyStrings();
     private static final Splitter PIPE_SPLITTER = Splitter.on('|').trimResults();
     private static final Splitter DOT_DOT_SPLITTER = Splitter.on("..").trimResults();
-    private static final CharMatcher QUOTE_MATCHER = CharMatcher.is('"');
+    private static final CharMatcher DOUBLE_QUOTE_MATCHER = CharMatcher.is('"');
+    private static final CharMatcher SINGLE_QUOTE_MATCHER = CharMatcher.is('\'');
 
     private ParserListenerUtils() {
     }
@@ -168,33 +169,26 @@ public final class ParserListenerUtils {
         StringBuilder sb = new StringBuilder();
         for (TerminalNode stringNode : context.STRING()) {
             final String str = stringNode.getText();
-            final int i = str.indexOf('"');
-
-            if (i == -1) {
+            char firstChar = str.charAt(0);
+            final CharMatcher quoteMatcher;
+            if(SINGLE_QUOTE_MATCHER.matches(firstChar)) {
+                quoteMatcher = SINGLE_QUOTE_MATCHER;
+            } else if (DOUBLE_QUOTE_MATCHER.matches(firstChar)) {
+                quoteMatcher = DOUBLE_QUOTE_MATCHER;
+            } else {
                 sb.append(str);
                 continue;
             }
-
             /*
-             * The string contains quotes, so we have to tread carefully.
              *
-             * FIXME: I think this code is broken, but proving that requires
-             *        making sense of the parser/lexer and how they tie into
-             *        this method. Especially what format 'str' can have and
-             *        how we need to handle it. The original check was:
+             * It is safe not to check last argument to be same
+             * grammars enforces that.
              *
-             *        if (!(result.startsWith("\"")) && result.endsWith("\""))
+             * FIXME: Introduce proper escaping and translation of escaped
+             * characters here.
              *
-             *        Looking at the parentheses it is hard to justify the
-             *        pair right after negation -- the intent may have been
-             *        to negate the entire expression.
              */
-            if (i != 0 && str.endsWith("\"")) {
-                LOG.error("Syntax error in module {} at line {}: missing '\"'.", getParentModule(context),
-                        context.getStart().getLine());
-            } else {
-                sb.append(QUOTE_MATCHER.removeFrom(str));
-            }
+            sb.append(quoteMatcher.removeFrom(str.substring(1, str.length()-1)));
         }
         return sb.toString();
     }
@@ -381,7 +375,15 @@ public final class ParserListenerUtils {
             ParseTree child = ctx.getChild(i);
             if (child instanceof Value_stmtContext) {
                 String valueStr = stringFromNode(child);
-                value = Integer.valueOf(valueStr);
+                try {
+                    // yang enum value has same restrictions as JAVA Integer
+                    value = Integer.valueOf(valueStr);
+                } catch (NumberFormatException e) {
+                    String err = String
+                            .format("Error on enum '%s': the enum value MUST be in the range from -2147483648 to 2147483647, but was: %s",
+                                    name, valueStr);
+                    throw new YangParseException(moduleName, ctx.getStart().getLine(), err, e);
+                }
             } else if (child instanceof Description_stmtContext) {
                 description = stringFromNode(child);
             } else if (child instanceof Reference_stmtContext) {
@@ -394,10 +396,6 @@ public final class ParserListenerUtils {
         if (value == null) {
             value = highestValue + 1;
         }
-        if (value < -2147483648 || value > 2147483647) {
-            throw new YangParseException(moduleName, ctx.getStart().getLine(), "Error on enum '" + name
-                    + "': the enum value MUST be in the range from -2147483648 to 2147483647, but was: " + value);
-        }
 
         EnumPairImpl result = new EnumPairImpl();
         result.qname = path.getPathTowardsRoot().iterator().next();
@@ -1094,12 +1092,8 @@ public final class ParserListenerUtils {
      *            type body context
      * @param actualPath
      *            current path in schema
-     * @param namespace
-     *            current namespace
-     * @param revision
-     *            current revision
-     * @param prefix
-     *            current prefix
+     * @param moduleQName
+     *            current module qname
      * @param parent
      *            parent builder
      * @return TypeDefinition object based on parsed values.