Skip to content

Commit

Permalink
Fix for #1060: add error marker for control and format characters
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 16, 2020
1 parent 69f3f86 commit c2425bd
Show file tree
Hide file tree
Showing 17 changed files with 7,962 additions and 7,803 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class UnicodeEscapingReader extends Reader {

private static class DummyLexer extends CharScanner {
private final Token t = new Token();
@Override
public Token nextToken() throws TokenStreamException {
return t;
}
Expand All @@ -56,7 +57,7 @@ public int getLine() {
return 0;
}
}

/**
* Constructor.
* @param reader The reader that this reader will filter over.
Expand Down Expand Up @@ -84,6 +85,7 @@ public void setLexer(CharScanner lexer) {
* Reads characters from the underlying reader.
* @see java.io.Reader#read(char[],int,int)
*/
@Override
public int read(char cbuf[], int off, int len) throws IOException {
int c = 0;
int count = 0;
Expand All @@ -99,9 +101,13 @@ public int read(char cbuf[], int off, int len) throws IOException {
* translating escapes as required.
* @see java.io.Reader#close()
*/
@Override
public int read() throws IOException {
if (hasNextChar) {
hasNextChar = false;
// GRECLIPSE add
checkCodePoint(nextChar);
// GRECLIPSE add
write(nextChar);
return nextChar;
}
Expand All @@ -111,9 +117,12 @@ public int read() throws IOException {
numUnicodeEscapesFoundOnCurrentLine = 0;
previousLine = lexer.getLine();
}

int c = reader.read();
if (c != '\\') {
// GRECLIPSE add
checkCodePoint(c);
// GRECLIPSE add
write(c);
return c;
}
Expand Down Expand Up @@ -147,15 +156,25 @@ public int read() throws IOException {
}
int rv = Integer.parseInt(charNum.toString(), 16);
write(rv);

numUnicodeEscapesFound += 4 + numberOfUChars;
numUnicodeEscapesFoundOnCurrentLine += 4 + numberOfUChars;

return rv;
}

private void write(int c) {
if (sourceBuffer != null) {sourceBuffer.write(c);}
}

// GRECLIPSE add
private void checkCodePoint(int c) {
if (Character.isIdentifierIgnorable(c) || Character.getType(c) == Character.CONTROL && c != '\t' && c != '\r' && c != '\n') {
lexer.reportError(String.format("Unexpected character 0x%02X (%s) at column %d", c, Character.getName(c), lexer.getColumn()));
}
}
// GRECLIPSE end

/**
* Checks that the given character is indeed a hex digit.
*/
Expand Down Expand Up @@ -189,6 +208,7 @@ public int getUnescapedUnicodeOffsetCount() {
*
* @see java.io.Reader#close()
*/
@Override
public void close() throws IOException {
reader.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3981,10 +3981,15 @@ options {
return "["+ttn+",\""+t.getText()+"\"]";
}

protected GroovyRecognizer parser; // little-used link; TODO: get rid of
protected GroovyRecognizer parser;
// GRECLIPSE add
public void reportError(String message) {
parser.reportError(message, getLine(), getColumn());
}
// GRECLIPSE end
private void require(boolean z, String problem, String solution) throws SemanticException {
// TODO: Direct to a common error handler, rather than through the parser.
if (!z && parser!=null) parser.requireFailed(problem, solution);
if (!z && parser!=null) parser.requireFailed(problem, solution);
if (!z) {
int lineNum = inputState.getLine(), colNum = inputState.getColumn();
throw new SemanticException(problem + ";\n solution: " + solution,
Expand All @@ -3993,8 +3998,6 @@ options {
}
}

// TODO: Borneo-style ops.

// OPERATORS
QUESTION options {paraphrase="'?'";} : '?' ;
LPAREN options {paraphrase="'('";} : '(' {++parenLevel;};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,15 @@ private static String tokenStringOf(Token t) {
return "["+ttn+",\""+t.getText()+"\"]";
}

protected GroovyRecognizer parser; // little-used link; TODO: get rid of
protected GroovyRecognizer parser;
// GRECLIPSE add
public void reportError(String message) {
parser.reportError(message, getLine(), getColumn());
}
// GRECLIPSE end
private void require(boolean z, String problem, String solution) throws SemanticException {
// TODO: Direct to a common error handler, rather than through the parser.
if (!z && parser!=null) parser.requireFailed(problem, solution);
if (!z && parser!=null) parser.requireFailed(problem, solution);
if (!z) {
int lineNum = inputState.getLine(), colNum = inputState.getColumn();
throw new SemanticException(problem + ";\n solution: " + solution,
Expand Down
Loading

0 comments on commit c2425bd

Please sign in to comment.