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