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.rfc7950.repo;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.CharMatcher;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.regex.Pattern;
15 import org.antlr.v4.runtime.tree.TerminalNode;
16 import org.opendaylight.yangtools.yang.common.YangVersion;
17 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.ArgumentContext;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
21 final class ArgumentContextUtils {
22 private static final CharMatcher WHITESPACE_MATCHER = CharMatcher.whitespace();
23 private static final CharMatcher ANYQUOTE_MATCHER = CharMatcher.anyOf("'\"");
24 private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
25 private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
26 private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
27 private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
29 private ArgumentContextUtils() {
33 static String stringFromStringContext(final ArgumentContext context, final YangVersion yangVersion,
34 final StatementSourceReference ref) {
35 final StringBuilder sb = new StringBuilder();
36 List<TerminalNode> strings = context.STRING();
37 if (strings.isEmpty()) {
38 strings = Collections.singletonList(context.IDENTIFIER());
40 for (final TerminalNode stringNode : strings) {
41 final String str = stringNode.getText();
42 final char firstChar = str.charAt(0);
43 final char lastChar = str.charAt(str.length() - 1);
44 if (firstChar == '"' && lastChar == '"') {
45 final String innerStr = str.substring(1, str.length() - 1);
47 * Unescape escaped double quotes, tabs, new line and backslash
48 * in the inner string and trim the result.
50 checkDoubleQuotedString(innerStr, yangVersion, ref);
51 sb.append(unescape(trimWhitespace(innerStr, stringNode.getSymbol().getCharPositionInLine())));
52 } else if (firstChar == '\'' && lastChar == '\'') {
54 * According to RFC6020 a single quote character cannot occur in
55 * a single-quoted string, even when preceded by a backslash.
57 sb.append(str, 1, str.length() - 1);
59 checkUnquotedString(str, yangVersion, ref);
66 private static String unescape(final String str) {
67 final int backslash = str.indexOf('\\');
68 if (backslash == -1) {
72 // FIXME: given we the leading backslash, it would be more efficient to walk the string and unescape in one go
73 return ESCAPED_TAB.matcher(
75 ESCAPED_BACKSLASH.matcher(
76 ESCAPED_DQUOT.matcher(str).replaceAll("\\\""))
82 private static void checkUnquotedString(final String str, final YangVersion yangVersion,
83 final StatementSourceReference ref) {
84 if (yangVersion == YangVersion.VERSION_1_1) {
85 SourceException.throwIf(ANYQUOTE_MATCHER.matchesAnyOf(str), ref,
86 "YANG 1.1: unquoted string (%s) contains illegal characters", str);
90 private static void checkDoubleQuotedString(final String str, final YangVersion yangVersion,
91 final StatementSourceReference ref) {
92 if (yangVersion == YangVersion.VERSION_1_1) {
93 for (int i = 0; i < str.length() - 1; i++) {
94 if (str.charAt(i) == '\\') {
95 switch (str.charAt(i + 1)) {
103 throw new SourceException(ref, "YANG 1.1: illegal double quoted string (%s). In double "
104 + "quoted string the backslash must be followed by one of the following character "
105 + "[n,t,\",\\], but was '%s'.", str, str.charAt(i + 1));
113 static String trimWhitespace(final String str, final int dquot) {
114 int brk = str.indexOf('\n');
116 // No need to trim whitespace
120 // Okay, we may need to do some trimming, set up a builder and append the first segment
121 final int length = str.length();
122 final StringBuilder sb = new StringBuilder(length);
124 // Append first segment, which needs only tail-trimming
125 sb.append(str, 0, trimTrailing(str, 0, brk)).append('\n');
127 // With that out of the way, setup our iteration state. The string segment we are looking at is
128 // str.substring(start, end), which is guaranteed not to include any line breaks, i.e. end <= brk unless we are
129 // at the last segment.
131 brk = str.indexOf('\n', start);
133 // Loop over inner strings
135 trimLeadingAndAppend(sb, dquot, str, start, trimTrailing(str, start, brk)).append('\n');
137 brk = str.indexOf('\n', start);
140 return trimLeadingAndAppend(sb, dquot, str, start, length).toString();
143 private static StringBuilder trimLeadingAndAppend(final StringBuilder sb, final int dquot, final String str,
144 final int start, final int end) {
148 while (pos <= dquot) {
150 // We ran out of data, nothing to append
154 final char ch = str.charAt(offset);
156 // tabs are to be treated as 8 spaces
158 } else if (WHITESPACE_MATCHER.matches(ch)) {
167 // We have expanded beyond double quotes, push equivalent spaces
168 while (pos - 1 > dquot) {
173 return sb.append(str, offset, end);
176 private static int trimTrailing(final String str, final int start, final int end) {
178 while (ret > start) {
179 final int prev = ret - 1;
180 if (!WHITESPACE_MATCHER.matches(str.charAt(prev))) {