Refactor ArgumentContextUtils
[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.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;
20
21 /**
22  * Utilities for dealing with YANG statement argument strings, encapsulated in ANTLR grammar's ArgumentContext.
23  */
24 enum ArgumentContextUtils {
25     /**
26      * YANG 1.0 version of strings, which were not completely clarified in RFC6020.
27      */
28     RFC6020 {
29         @Override
30         void checkDoubleQuotedString(final String str, final StatementSourceReference ref) {
31             // No-op
32         }
33
34         @Override
35         void checkUnquotedString(final String str, final StatementSourceReference ref) {
36             // No-op
37         }
38     },
39     /**
40      * YANG 1.1 version of strings, which were clarified in RFC7950.
41      */
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.
44     RFC7950 {
45         @Override
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)) {
50                         case 'n':
51                         case 't':
52                         case '\\':
53                         case '\"':
54                             i++;
55                             break;
56                         default:
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));
60                     }
61                 }
62             }
63         }
64
65         @Override
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);
69         }
70     };
71
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);
78
79     static @NonNull ArgumentContextUtils forVersion(final YangVersion version) {
80         switch (version) {
81             case VERSION_1:
82                 return RFC6020;
83             case VERSION_1_1:
84                 return RFC7950;
85             default:
86                 throw new IllegalStateException("Unhandled version " + version);
87         }
88     }
89
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);
96             }
97         } else {
98             appendString(sb, context.IDENTIFIER(), ref);
99         }
100
101         return sb.toString();
102     }
103
104     private void appendString(final StringBuilder sb, final TerminalNode stringNode,
105             final StatementSourceReference ref) {
106
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
113         //       want to do that.
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);
117             /*
118              * Unescape escaped double quotes, tabs, new line and backslash
119              * in the inner string and trim the result.
120              */
121             checkDoubleQuotedString(innerStr, ref);
122             sb.append(unescape(trimWhitespace(innerStr, stringNode.getSymbol().getCharPositionInLine())));
123         } else if (firstChar == '\'' && lastChar == '\'') {
124             /*
125              * According to RFC6020 a single quote character cannot occur in
126              * a single-quoted string, even when preceded by a backslash.
127              */
128             sb.append(str, 1, str.length() - 1);
129         } else {
130             checkUnquotedString(str, ref);
131             sb.append(str);
132         }
133     }
134
135     abstract void checkDoubleQuotedString(String str, StatementSourceReference ref);
136
137     abstract void checkUnquotedString(String str, StatementSourceReference ref);
138
139     private static String unescape(final String str) {
140         final int backslash = str.indexOf('\\');
141         if (backslash == -1) {
142             return str;
143         }
144
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(
148                     ESCAPED_LF.matcher(
149                         ESCAPED_BACKSLASH.matcher(
150                             ESCAPED_DQUOT.matcher(str).replaceAll("\\\""))
151                         .replaceAll("\\\\"))
152                     .replaceAll("\\\n"))
153                .replaceAll("\\\t");
154     }
155
156     @VisibleForTesting
157     static String trimWhitespace(final String str, final int dquot) {
158         int brk = str.indexOf('\n');
159         if (brk == -1) {
160             // No need to trim whitespace
161             return str;
162         }
163
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);
167
168         // Append first segment, which needs only tail-trimming
169         sb.append(str, 0, trimTrailing(str, 0, brk)).append('\n');
170
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.
174         int start = brk + 1;
175         brk = str.indexOf('\n', start);
176
177         // Loop over inner strings
178         while (brk != -1) {
179             trimLeadingAndAppend(sb, dquot, str, start, trimTrailing(str, start, brk)).append('\n');
180             start = brk + 1;
181             brk = str.indexOf('\n', start);
182         }
183
184         return trimLeadingAndAppend(sb, dquot, str, start, length).toString();
185     }
186
187     private static StringBuilder trimLeadingAndAppend(final StringBuilder sb, final int dquot, final String str,
188             final int start, final int end) {
189         int offset = start;
190         int pos = 0;
191
192         while (pos <= dquot) {
193             if (offset == end) {
194                 // We ran out of data, nothing to append
195                 return sb;
196             }
197
198             final char ch = str.charAt(offset);
199             if (ch == '\t') {
200                 // tabs are to be treated as 8 spaces
201                 pos += 8;
202             } else if (WHITESPACE_MATCHER.matches(ch)) {
203                 pos++;
204             } else {
205                 break;
206             }
207
208             offset++;
209         }
210
211         // We have expanded beyond double quotes, push equivalent spaces
212         while (pos - 1 > dquot) {
213             sb.append(' ');
214             pos--;
215         }
216
217         return sb.append(str, offset, end);
218     }
219
220     private static int trimTrailing(final String str, final int start, final int end) {
221         int ret = end;
222         while (ret > start) {
223             final int prev = ret - 1;
224             if (!WHITESPACE_MATCHER.matches(str.charAt(prev))) {
225                 break;
226             }
227             ret = prev;
228         }
229         return ret;
230     }
231 }