diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/src/main/java/com/yahoo/javacc |
Publish
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/javacc')
3 files changed, 317 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/javacc/FastCharStream.java b/vespajlib/src/main/java/com/yahoo/javacc/FastCharStream.java new file mode 100644 index 00000000000..892240ce253 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/javacc/FastCharStream.java @@ -0,0 +1,131 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.javacc; + +import java.io.IOException; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class FastCharStream { + + private static final String JAVACC_EXCEPTION_FORMAT = "line -1, column "; + private static final IOException EOF = new IOException(); + private final String inputStr; + private final char[] inputArr; + private int tokenPos = 0; + private int readPos = 0; + + public FastCharStream(String input) { + this.inputStr = input; + this.inputArr = input.toCharArray(); + } + + public char readChar() throws IOException { + if (readPos >= inputArr.length) { + throw EOF; + } + return inputArr[readPos++]; + } + + @SuppressWarnings("deprecation") + public int getColumn() { + return getEndColumn(); + } + + @SuppressWarnings("deprecation") + public int getLine() { + return getEndLine(); + } + + public int getEndColumn() { + return readPos + 1; + } + + public int getEndLine() { + return -1; // indicate unset + } + + public int getBeginColumn() { + return tokenPos + 1; + } + + public int getBeginLine() { + return -1; // indicate unset + } + + public void backup(int amount) { + readPos -= amount; + } + + public char BeginToken() throws IOException { + tokenPos = readPos; + return readChar(); + } + + public String GetImage() { + return inputStr.substring(tokenPos, readPos); + } + + @SuppressWarnings("UnusedParameters") + public char[] GetSuffix(int len) { + throw new UnsupportedOperationException(); + } + + public void Done() { + + } + + public String formatException(String parseException) { + int errPos = findErrPos(parseException); + if (errPos < 0 || errPos > inputArr.length + 1) { + return parseException; + } + int errLine = 0; + int errColumn = 0; + for (int i = 0; i < errPos - 1; ++i) { + if (inputStr.charAt(i) == '\n') { + ++errLine; + errColumn = 0; + } else { + ++errColumn; + } + } + StringBuilder out = new StringBuilder(); + out.append(parseException.replace(JAVACC_EXCEPTION_FORMAT + errPos, + "line " + (errLine + 1) + ", column " + (errColumn + 1))); + out.append("\nAt position:\n"); + appendErrorPosition(errLine, out); + for (int i = 0; i < errColumn; ++i) { + out.append(" "); + } + out.append("^"); + return out.toString(); + } + + private void appendErrorPosition(int errLine, StringBuilder out) { + String[] inputStrLines = inputStr.split("\n"); + if (inputStrLines.length<errLine+1) { + out.append("EOF\n"); + } else { + out.append(inputStrLines[errLine]).append("\n"); + } + } + + private static int findErrPos(String str) { + int from = str.indexOf(JAVACC_EXCEPTION_FORMAT); + if (from < 0) { + return -1; + } + from = from + JAVACC_EXCEPTION_FORMAT.length(); + + int to = from; + while (to < str.length() && Character.isDigit(str.charAt(to))) { + ++to; + } + if (to == from) { + return -1; + } + + return Integer.valueOf(str.substring(from, to)); + } +}
\ No newline at end of file diff --git a/vespajlib/src/main/java/com/yahoo/javacc/UnicodeUtilities.java b/vespajlib/src/main/java/com/yahoo/javacc/UnicodeUtilities.java new file mode 100644 index 00000000000..45099a6855e --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/javacc/UnicodeUtilities.java @@ -0,0 +1,181 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.javacc; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class UnicodeUtilities { + + /** + * Adds a leading and trailing double quotation mark to the given string. This will escape whatever content is + * within the string literal. + * + * @param str The string to quote. + * @param quote The quote character. + * @return The quoted string. + */ + public static String quote(String str, char quote) { + StringBuilder ret = new StringBuilder(); + ret.append(quote); + for (int i = 0; i < str.length(); ++i) { + char c = str.charAt(i); + if (c == quote) { + ret.append("\\").append(c); + } else { + ret.append(escape(c)); + } + } + ret.append(quote); + return ret.toString(); + } + + /** + * Removes leading and trailing quotation mark from the given string. This method will properly unescape whatever + * content is withing the string literal as well. + * + * @param str The string to unquote. + * @return The unquoted string. + */ + public static String unquote(String str) { + if (str.length() == 0) { + return str; + } + char quote = str.charAt(0); + if (quote != '"' && quote != '\'') { + return str; + } + if (str.charAt(str.length() - 1) != quote) { + return str; + } + StringBuilder ret = new StringBuilder(); + for (int i = 1; i < str.length() - 1; ++i) { + char c = str.charAt(i); + if (c == '\\') { + if (++i == str.length() - 1) { + break; // done + } + c = str.charAt(i); + if (c == 'f') { + ret.append("\f"); + } else if (c == 'n') { + ret.append("\n"); + } else if (c == 'r') { + ret.append("\r"); + } else if (c == 't') { + ret.append("\t"); + } else if (c == 'u') { + if (++i > str.length() - 4) { + break; // done + } + try { + ret.append((char)Integer.parseInt(str.substring(i, i + 4), 16)); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(e); + } + i += 3; + } else { + ret.append(c); + } + } else if (c == quote) { + throw new IllegalArgumentException(); + } else { + ret.append(c); + } + } + return ret.toString(); + } + + private static String escape(char c) { + switch (c) { + case '\b': + return "\\b"; + case '\t': + return "\\t"; + case '\n': + return "\\n"; + case '\f': + return "\\f"; + case '\r': + return "\\r"; + case '\\': + return "\\\\"; + } + if (c < 0x20 || c > 0x7e) { + String unicode = Integer.toString(c, 16); + return "\\u" + "0000".substring(0, 4 - unicode.length()) + unicode + ""; + } + return "" + c; + } + + public static String generateToken(Predicate predicate) { + TokenBuilder builder = new TokenBuilder(); + for (int c = 0; c <= 0xffff; ++c) { + if (!predicate.accepts((char)c)) { + continue; + } + builder.add(c); + } + return builder.build(); + } + + public static interface Predicate { + + public boolean accepts(char c); + } + + private static class TokenBuilder { + + final StringBuilder token = new StringBuilder(); + int prevC = -1; + int fromC = 0; + int charCnt = 0; + + void add(int c) { + if (prevC + 1 == c) { + // in range + } else { + flushRange(); + fromC = c; + } + prevC = c; + } + + void flushRange() { + if (fromC > prevC) { + return; // handle initial condition + } + append(fromC); + if (fromC < prevC) { + token.append('-'); + append(prevC); + ++charCnt; + } + token.append(','); + if (++charCnt > 16) { + token.append('\n'); + charCnt = 0; + } + } + + void append(int c) { + token.append("\""); + if (c == '\n') { + token.append("\\n"); + } else if (c == '\r') { + token.append("\\r"); + } else if (c == '"') { + token.append("\\\""); + } else if (c == '\\') { + token.append("\\\\"); + } else { + token.append("\\u").append(String.format("%04x", c & 0xffff)); + } + token.append("\""); + } + + String build() { + flushRange(); + return token.toString(); + } + } +} diff --git a/vespajlib/src/main/java/com/yahoo/javacc/package-info.java b/vespajlib/src/main/java/com/yahoo/javacc/package-info.java new file mode 100644 index 00000000000..c80e05df51f --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/javacc/package-info.java @@ -0,0 +1,5 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@ExportPackage +package com.yahoo.javacc; + +import com.yahoo.osgi.annotation.ExportPackage; |