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.stmt.rfc6020;
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 javax.annotation.Nullable;
15 import org.antlr.v4.runtime.tree.TerminalNode;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
17 import org.opendaylight.yangtools.yang.common.YangVersion;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.SchemaNodeIdentifierBuildNamespace;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
25 public final class Utils {
26 private static final CharMatcher ANYQUOTE_MATCHER = CharMatcher.anyOf("'\"");
27 private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
28 private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
29 private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
30 private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
33 throw new UnsupportedOperationException();
36 public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
37 final StatementSourceReference ref) {
38 return stringFromStringContext(context, YangVersion.VERSION_1, ref);
41 public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
42 final YangVersion yangVersion, final StatementSourceReference ref) {
43 final StringBuilder sb = new StringBuilder();
44 List<TerminalNode> strings = context.STRING();
45 if (strings.isEmpty()) {
46 strings = Collections.singletonList(context.IDENTIFIER());
48 for (final TerminalNode stringNode : strings) {
49 final String str = stringNode.getText();
50 final char firstChar = str.charAt(0);
51 final char lastChar = str.charAt(str.length() - 1);
52 if (firstChar == '"' && lastChar == '"') {
53 final String innerStr = str.substring(1, str.length() - 1);
55 * Unescape escaped double quotes, tabs, new line and backslash
56 * in the inner string and trim the result.
58 checkDoubleQuotedString(innerStr, yangVersion, ref);
59 sb.append(ESCAPED_TAB.matcher(
61 ESCAPED_BACKSLASH.matcher(
62 ESCAPED_DQUOT.matcher(innerStr).replaceAll("\\\""))
66 } else if (firstChar == '\'' && lastChar == '\'') {
68 * According to RFC6020 a single quote character cannot occur in
69 * a single-quoted string, even when preceded by a backslash.
71 sb.append(str.substring(1, str.length() - 1));
73 checkUnquotedString(str, yangVersion, ref);
80 private static void checkUnquotedString(final String str, final YangVersion yangVersion,
81 final StatementSourceReference ref) {
82 if (yangVersion == YangVersion.VERSION_1_1) {
83 SourceException.throwIf(ANYQUOTE_MATCHER.matchesAnyOf(str), ref,
84 "YANG 1.1: unquoted string (%s) contains illegal characters", str);
88 private static void checkDoubleQuotedString(final String str, final YangVersion yangVersion,
89 final StatementSourceReference ref) {
90 if (yangVersion == YangVersion.VERSION_1_1) {
91 for (int i = 0; i < str.length() - 1; i++) {
92 if (str.charAt(i) == '\\') {
93 switch (str.charAt(i + 1)) {
101 throw new SourceException(ref, "YANG 1.1: illegal double quoted string (%s). In double "
102 + "quoted string the backslash must be followed by one of the following character "
103 + "[n,t,\",\\], but was '%s'.", str, str.charAt(i + 1));
111 public static StatementContextBase<?, ?, ?> findNode(final StmtContext<?, ?, ?> rootStmtCtx,
112 final SchemaNodeIdentifier node) {
113 return (StatementContextBase<?, ?, ?>) rootStmtCtx.getFromNamespace(SchemaNodeIdentifierBuildNamespace.class,