2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
10 import com.google.common.base.CharMatcher;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.regex.Pattern;
14 import org.antlr.v4.runtime.tree.TerminalNode;
15 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
16 import org.opendaylight.yangtools.yang.common.YangVersion;
17 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
18 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
20 public final class Utils {
21 private static final CharMatcher ANYQUOTE_MATCHER = CharMatcher.anyOf("'\"");
22 private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
23 private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
24 private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
25 private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
28 throw new UnsupportedOperationException();
31 public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
32 final StatementSourceReference ref) {
33 return stringFromStringContext(context, YangVersion.VERSION_1, ref);
36 public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
37 final YangVersion yangVersion, final StatementSourceReference ref) {
38 final StringBuilder sb = new StringBuilder();
39 List<TerminalNode> strings = context.STRING();
40 if (strings.isEmpty()) {
41 strings = Collections.singletonList(context.IDENTIFIER());
43 for (final TerminalNode stringNode : strings) {
44 final String str = stringNode.getText();
45 final char firstChar = str.charAt(0);
46 final char lastChar = str.charAt(str.length() - 1);
47 if (firstChar == '"' && lastChar == '"') {
48 final String innerStr = str.substring(1, str.length() - 1);
50 * Unescape escaped double quotes, tabs, new line and backslash
51 * in the inner string and trim the result.
53 checkDoubleQuotedString(innerStr, yangVersion, ref);
54 sb.append(ESCAPED_TAB.matcher(
56 ESCAPED_BACKSLASH.matcher(
57 ESCAPED_DQUOT.matcher(innerStr).replaceAll("\\\""))
61 } else if (firstChar == '\'' && lastChar == '\'') {
63 * According to RFC6020 a single quote character cannot occur in
64 * a single-quoted string, even when preceded by a backslash.
66 sb.append(str.substring(1, str.length() - 1));
68 checkUnquotedString(str, yangVersion, ref);
75 private static void checkUnquotedString(final String str, final YangVersion yangVersion,
76 final StatementSourceReference ref) {
77 if (yangVersion == YangVersion.VERSION_1_1) {
78 SourceException.throwIf(ANYQUOTE_MATCHER.matchesAnyOf(str), ref,
79 "YANG 1.1: unquoted string (%s) contains illegal characters", str);
83 private static void checkDoubleQuotedString(final String str, final YangVersion yangVersion,
84 final StatementSourceReference ref) {
85 if (yangVersion == YangVersion.VERSION_1_1) {
86 for (int i = 0; i < str.length() - 1; i++) {
87 if (str.charAt(i) == '\\') {
88 switch (str.charAt(i + 1)) {
96 throw new SourceException(ref, "YANG 1.1: illegal double quoted string (%s). In double "
97 + "quoted string the backslash must be followed by one of the following character "
98 + "[n,t,\",\\], but was '%s'.", str, str.charAt(i + 1));