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.List;
13 import java.util.regex.Pattern;
14 import org.antlr.v4.runtime.tree.TerminalNode;
15 import org.eclipse.jdt.annotation.NonNull;
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;
22 * Utilities for dealing with YANG statement argument strings, encapsulated in ANTLR grammar's ArgumentContext.
24 enum ArgumentContextUtils {
26 * YANG 1.0 version of strings, which were not completely clarified in RFC6020.
30 void checkDoubleQuotedString(final String str, final StatementSourceReference ref) {
35 void checkUnquotedString(final String str, final StatementSourceReference ref) {
40 * YANG 1.1 version of strings, which were clarified in RFC7950.
42 // NOTE: the differences clarified lead to a proper ability to delegate this to ANTLR lexer, but that does not
43 // understand versions and needs to work with both.
46 void checkDoubleQuotedString(final String str, final StatementSourceReference ref) {
47 for (int i = 0; i < str.length() - 1; i++) {
48 if (str.charAt(i) == '\\') {
49 switch (str.charAt(i + 1)) {
57 throw new SourceException(ref, "YANG 1.1: illegal double quoted string (%s). In double "
58 + "quoted string the backslash must be followed by one of the following character "
59 + "[n,t,\",\\], but was '%s'.", str, str.charAt(i + 1));
66 void checkUnquotedString(final String str, final StatementSourceReference ref) {
67 SourceException.throwIf(ANYQUOTE_MATCHER.matchesAnyOf(str), ref,
68 "YANG 1.1: unquoted string (%s) contains illegal characters", str);
72 private static final CharMatcher WHITESPACE_MATCHER = CharMatcher.whitespace();
73 private static final CharMatcher ANYQUOTE_MATCHER = CharMatcher.anyOf("'\"");
74 private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
75 private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
76 private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
77 private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
79 static @NonNull ArgumentContextUtils forVersion(final YangVersion version) {
86 throw new IllegalStateException("Unhandled version " + version);
90 final @NonNull String stringFromStringContext(final ArgumentContext context, final StatementSourceReference ref) {
91 final StringBuilder sb = new StringBuilder();
92 final List<TerminalNode> strings = context.STRING();
93 if (!strings.isEmpty()) {
94 for (final TerminalNode stringNode : strings) {
95 appendString(sb, stringNode, ref);
98 appendString(sb, context.IDENTIFIER(), ref);
101 return sb.toString();
104 private void appendString(final StringBuilder sb, final TerminalNode stringNode,
105 final StatementSourceReference ref) {
107 final String str = stringNode.getText();
108 final char firstChar = str.charAt(0);
109 final char lastChar = str.charAt(str.length() - 1);
110 // NOTE: Enforcement and transformation logic here should certainly be pushed down to the lexer, as ANTLR can
111 // account the for it with lexer modes. One problem is that lexing here depends on version being lexed,
112 // hence we really would have to re-parse the YANG file after determining its version. We certainly do not
114 // FIXME: YANGTOOLS-1079: but since we are performing quoting checks, perhaps at least that part could be lexed?
115 if (firstChar == '"' && lastChar == '"') {
116 final String innerStr = str.substring(1, str.length() - 1);
118 * Unescape escaped double quotes, tabs, new line and backslash
119 * in the inner string and trim the result.
121 checkDoubleQuotedString(innerStr, ref);
122 sb.append(unescape(trimWhitespace(innerStr, stringNode.getSymbol().getCharPositionInLine())));
123 } else if (firstChar == '\'' && lastChar == '\'') {
125 * According to RFC6020 a single quote character cannot occur in
126 * a single-quoted string, even when preceded by a backslash.
128 sb.append(str, 1, str.length() - 1);
130 checkUnquotedString(str, ref);
135 abstract void checkDoubleQuotedString(String str, StatementSourceReference ref);
137 abstract void checkUnquotedString(String str, StatementSourceReference ref);
139 private static String unescape(final String str) {
140 final int backslash = str.indexOf('\\');
141 if (backslash == -1) {
145 // FIXME: YANGTOOLS-1079: given we the leading backslash, it would be more efficient to walk the string and
146 // unescape in one go
147 return ESCAPED_TAB.matcher(
149 ESCAPED_BACKSLASH.matcher(
150 ESCAPED_DQUOT.matcher(str).replaceAll("\\\""))
157 static String trimWhitespace(final String str, final int dquot) {
158 int brk = str.indexOf('\n');
160 // No need to trim whitespace
164 // Okay, we may need to do some trimming, set up a builder and append the first segment
165 final int length = str.length();
166 final StringBuilder sb = new StringBuilder(length);
168 // Append first segment, which needs only tail-trimming
169 sb.append(str, 0, trimTrailing(str, 0, brk)).append('\n');
171 // With that out of the way, setup our iteration state. The string segment we are looking at is
172 // str.substring(start, end), which is guaranteed not to include any line breaks, i.e. end <= brk unless we are
173 // at the last segment.
175 brk = str.indexOf('\n', start);
177 // Loop over inner strings
179 trimLeadingAndAppend(sb, dquot, str, start, trimTrailing(str, start, brk)).append('\n');
181 brk = str.indexOf('\n', start);
184 return trimLeadingAndAppend(sb, dquot, str, start, length).toString();
187 private static StringBuilder trimLeadingAndAppend(final StringBuilder sb, final int dquot, final String str,
188 final int start, final int end) {
192 while (pos <= dquot) {
194 // We ran out of data, nothing to append
198 final char ch = str.charAt(offset);
200 // tabs are to be treated as 8 spaces
202 } else if (WHITESPACE_MATCHER.matches(ch)) {
211 // We have expanded beyond double quotes, push equivalent spaces
212 while (pos - 1 > dquot) {
217 return sb.append(str, offset, end);
220 private static int trimTrailing(final String str, final int start, final int end) {
222 while (ret > start) {
223 final int prev = ret - 1;
224 if (!WHITESPACE_MATCHER.matches(str.charAt(prev))) {