20657353b76ffea911e83c3a65cdeb9e36a43e20
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / ArgumentContextUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
9
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;
20
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);
28
29     private ArgumentContextUtils() {
30         // Hidden on purpose
31     }
32
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());
39         }
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);
46                 /*
47                  * Unescape escaped double quotes, tabs, new line and backslash
48                  * in the inner string and trim the result.
49                  */
50                 checkDoubleQuotedString(innerStr, yangVersion, ref);
51                 sb.append(unescape(trimWhitespace(innerStr, stringNode.getSymbol().getCharPositionInLine())));
52             } else if (firstChar == '\'' && lastChar == '\'') {
53                 /*
54                  * According to RFC6020 a single quote character cannot occur in
55                  * a single-quoted string, even when preceded by a backslash.
56                  */
57                 sb.append(str, 1, str.length() - 1);
58             } else {
59                 checkUnquotedString(str, yangVersion, ref);
60                 sb.append(str);
61             }
62         }
63         return sb.toString();
64     }
65
66     private static String unescape(final String str) {
67         final int backslash = str.indexOf('\\');
68         if (backslash == -1) {
69             return str;
70         }
71
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(
74                     ESCAPED_LF.matcher(
75                         ESCAPED_BACKSLASH.matcher(
76                             ESCAPED_DQUOT.matcher(str).replaceAll("\\\""))
77                         .replaceAll("\\\\"))
78                     .replaceAll("\\\n"))
79                .replaceAll("\\\t");
80     }
81
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);
87         }
88     }
89
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)) {
96                         case 'n':
97                         case 't':
98                         case '\\':
99                         case '\"':
100                             i++;
101                             break;
102                         default:
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));
106                     }
107                 }
108             }
109         }
110     }
111
112     @VisibleForTesting
113     static String trimWhitespace(final String str, final int dquot) {
114         int brk = str.indexOf('\n');
115         if (brk == -1) {
116             // No need to trim whitespace
117             return str;
118         }
119
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);
123
124         // Append first segment, which needs only tail-trimming
125         sb.append(str, 0, trimTrailing(str, 0, brk)).append('\n');
126
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.
130         int start = brk + 1;
131         brk = str.indexOf('\n', start);
132
133         // Loop over inner strings
134         while (brk != -1) {
135             trimLeadingAndAppend(sb, dquot, str, start, trimTrailing(str, start, brk)).append('\n');
136             start = brk + 1;
137             brk = str.indexOf('\n', start);
138         }
139
140         return trimLeadingAndAppend(sb, dquot, str, start, length).toString();
141     }
142
143     private static StringBuilder trimLeadingAndAppend(final StringBuilder sb, final int dquot, final String str,
144             final int start, final int end) {
145         int offset = start;
146         int pos = 0;
147
148         while (pos <= dquot) {
149             if (offset == end) {
150                 // We ran out of data, nothing to append
151                 return sb;
152             }
153
154             final char ch = str.charAt(offset);
155             if (ch == '\t') {
156                 // tabs are to be treated as 8 spaces
157                 pos += 8;
158             } else if (WHITESPACE_MATCHER.matches(ch)) {
159                 pos++;
160             } else {
161                 break;
162             }
163
164             offset++;
165         }
166
167         // We have expanded beyond double quotes, push equivalent spaces
168         while (pos - 1 > dquot) {
169             sb.append(' ');
170             pos--;
171         }
172
173         return sb.append(str, offset, end);
174     }
175
176     private static int trimTrailing(final String str, final int start, final int end) {
177         int ret = end;
178         while (ret > start) {
179             final int prev = ret - 1;
180             if (!WHITESPACE_MATCHER.matches(str.charAt(prev))) {
181                 break;
182             }
183             ret = prev;
184         }
185         return ret;
186     }
187 }