diff options
author | Shevek <[email protected]> | 2008-06-13 21:53:52 +0000 |
---|---|---|
committer | Shevek <[email protected]> | 2008-06-13 21:53:52 +0000 |
commit | 282706bd6ddf2d2b6279683ab65c5c5b4df92046 (patch) | |
tree | 34144b05e5d78b85d5de69a0b99fd57c9a4215ae /src/java/org | |
parent | 93808fc91f990dbc17a2bc2b350552d9dde89692 (diff) |
handle errors better, using features
Diffstat (limited to 'src/java/org')
-rw-r--r-- | src/java/org/anarres/cpp/Feature.java | 5 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/JoinReader.java | 2 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/LexerSource.java | 23 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/Preprocessor.java | 15 |
4 files changed, 33 insertions, 12 deletions
diff --git a/src/java/org/anarres/cpp/Feature.java b/src/java/org/anarres/cpp/Feature.java index 2fc7a61..2f80245 100644 --- a/src/java/org/anarres/cpp/Feature.java +++ b/src/java/org/anarres/cpp/Feature.java @@ -21,9 +21,14 @@ package org.anarres.cpp; * Features of the Preprocessor, which may be enabled or disabled. */ public enum Feature { + /** Supports ANSI digraphs. */ DIGRAPHS, + /** Supports ANSI trigraphs. */ TRIGRAPHS, + /** Outputs linemarker tokens. */ LINEMARKERS, + /** Reports tokens of type INVALID as errors. */ CSYNTAX, + /** Preserves comments in the lexed output. */ KEEPCOMMENTS, } diff --git a/src/java/org/anarres/cpp/JoinReader.java b/src/java/org/anarres/cpp/JoinReader.java index 4e1a376..91908a7 100644 --- a/src/java/org/anarres/cpp/JoinReader.java +++ b/src/java/org/anarres/cpp/JoinReader.java @@ -68,6 +68,8 @@ import java.io.IOException; private void _unread(int c) { if (c != -1) unget[uptr++] = c; + assert uptr <= unget.length : + "JoinReader ungets too many characters"; } protected void warning(String msg) diff --git a/src/java/org/anarres/cpp/LexerSource.java b/src/java/org/anarres/cpp/LexerSource.java index 44c6224..0eea1c3 100644 --- a/src/java/org/anarres/cpp/LexerSource.java +++ b/src/java/org/anarres/cpp/LexerSource.java @@ -235,6 +235,19 @@ public class LexerSource extends Source { } } + /* Consumes the rest of the current line into an invalid. */ + private Token invalid(StringBuilder text, String reason) + throws IOException, + LexerException { + int d = read(); + while (!isLineSeparator(d)) { + text.append((char)d); + d = read(); + } + unread(d); + return new Token(INVALID, text.toString(), reason); + } + private Token ccomment() throws IOException, LexerException { @@ -325,19 +338,17 @@ public class LexerSource extends Source { } else if (isLineSeparator(d)) { unread(d); - // error("Unterminated character literal"); return new Token(INVALID, text.toString(), "Unterminated character literal"); } else if (d == '\'') { text.append('\''); - // error("Empty character literal"); return new Token(INVALID, text.toString(), "Empty character literal"); } else if (!Character.isDefined(d)) { text.append('?'); - error("Illegal unicode character literal"); + return invalid(text, "Illegal unicode character literal"); } else { text.append((char)d); @@ -348,17 +359,17 @@ public class LexerSource extends Source { // error("Illegal character constant"); /* We consume up to the next ' or the rest of the line. */ for (;;) { - if (e == '\'') - break; if (isLineSeparator(e)) { unread(e); break; } text.append((char)e); + if (e == '\'') + break; e = read(); } return new Token(INVALID, text.toString(), - "Illegal character constant"); + "Illegal character constant " + text); } text.append('\''); /* XXX It this a bad cast? */ diff --git a/src/java/org/anarres/cpp/Preprocessor.java b/src/java/org/anarres/cpp/Preprocessor.java index a1a72d9..82e636b 100644 --- a/src/java/org/anarres/cpp/Preprocessor.java +++ b/src/java/org/anarres/cpp/Preprocessor.java @@ -497,7 +497,7 @@ public class Preprocessor { return new Token(EOF); Source t = inputs.remove(0); push_source(t, true); - if (features.contains(Feature.LINEMARKERS)) + if (getFeature(Feature.LINEMARKERS)) return line_token(t.getLine(), t.getName(), " 1"); continue; } @@ -506,7 +506,7 @@ public class Preprocessor { // System.out.println("Autopop " + s); pop_source(); Source t = getSource(); - if (features.contains(Feature.LINEMARKERS) + if (getFeature(Feature.LINEMARKERS) && s.isNumbered() && t != null) { /* XXX Don't we mean t.isNumbered() as well? */ @@ -1082,7 +1082,7 @@ public class Preprocessor { /* 'tok' is the 'nl' after the include. We use it after the * #line directive. */ - if (features.contains(Feature.LINEMARKERS)) + if (getFeature(Feature.LINEMARKERS)) return line_token(1, name, " 1"); return tok; } @@ -1498,13 +1498,16 @@ public class Preprocessor { /* The preprocessor has to take action here. */ break; case WHITESPACE: + return tok; case CCOMMENT: case CPPCOMMENT: // Patch up to preserve whitespace. - /* XXX We might want to return tok here in C */ + if (getFeature(Feature.KEEPCOMMENTS)) + return tok; return toWhitespace(tok); default: // Return NL to preserve whitespace. + /* XXX This might lose a comment. */ return source_skipline(false); } } @@ -1578,12 +1581,12 @@ public class Preprocessor { return tok; case P_LINE: - if (features.contains(Feature.LINEMARKERS)) + if (getFeature(Feature.LINEMARKERS)) return tok; break; case INVALID: - if (features.contains(Feature.CSYNTAX)) + if (getFeature(Feature.CSYNTAX)) error(tok, String.valueOf(tok.getValue())); return tok; |