YANGTOOLS-706: Split out yang-parser-rfc7950
[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.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.ArgumentContext;
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;
19
20 final class ArgumentContextUtils {
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);
26
27     private ArgumentContextUtils() {
28         throw new UnsupportedOperationException();
29     }
30
31     static String stringFromStringContext(final ArgumentContext context, final StatementSourceReference ref) {
32         return stringFromStringContext(context, YangVersion.VERSION_1, ref);
33     }
34
35     static String stringFromStringContext(final ArgumentContext context, final YangVersion yangVersion,
36             final StatementSourceReference ref) {
37         final StringBuilder sb = new StringBuilder();
38         List<TerminalNode> strings = context.STRING();
39         if (strings.isEmpty()) {
40             strings = Collections.singletonList(context.IDENTIFIER());
41         }
42         for (final TerminalNode stringNode : strings) {
43             final String str = stringNode.getText();
44             final char firstChar = str.charAt(0);
45             final char lastChar = str.charAt(str.length() - 1);
46             if (firstChar == '"' && lastChar == '"') {
47                 final String innerStr = str.substring(1, str.length() - 1);
48                 /*
49                  * Unescape escaped double quotes, tabs, new line and backslash
50                  * in the inner string and trim the result.
51                  */
52                 checkDoubleQuotedString(innerStr, yangVersion, ref);
53                 sb.append(ESCAPED_TAB.matcher(
54                     ESCAPED_LF.matcher(
55                         ESCAPED_BACKSLASH.matcher(
56                             ESCAPED_DQUOT.matcher(innerStr).replaceAll("\\\""))
57                         .replaceAll("\\\\"))
58                     .replaceAll("\\\n"))
59                     .replaceAll("\\\t"));
60             } else if (firstChar == '\'' && lastChar == '\'') {
61                 /*
62                  * According to RFC6020 a single quote character cannot occur in
63                  * a single-quoted string, even when preceded by a backslash.
64                  */
65                 sb.append(str.substring(1, str.length() - 1));
66             } else {
67                 checkUnquotedString(str, yangVersion, ref);
68                 sb.append(str);
69             }
70         }
71         return sb.toString();
72     }
73
74     private static void checkUnquotedString(final String str, final YangVersion yangVersion,
75             final StatementSourceReference ref) {
76         if (yangVersion == YangVersion.VERSION_1_1) {
77             SourceException.throwIf(ANYQUOTE_MATCHER.matchesAnyOf(str), ref,
78                 "YANG 1.1: unquoted string (%s) contains illegal characters", str);
79         }
80     }
81
82     private static void checkDoubleQuotedString(final String str, final YangVersion yangVersion,
83             final StatementSourceReference ref) {
84         if (yangVersion == YangVersion.VERSION_1_1) {
85             for (int i = 0; i < str.length() - 1; i++) {
86                 if (str.charAt(i) == '\\') {
87                     switch (str.charAt(i + 1)) {
88                         case 'n':
89                         case 't':
90                         case '\\':
91                         case '\"':
92                             i++;
93                             break;
94                         default:
95                             throw new SourceException(ref, "YANG 1.1: illegal double quoted string (%s). In double "
96                                     + "quoted string the backslash must be followed by one of the following character "
97                                     + "[n,t,\",\\], but was '%s'.", str, str.charAt(i + 1));
98                     }
99                 }
100             }
101         }
102     }
103 }