in tokenizer precompute the lineOrigin

We know we need it on every line now because we use it for the newline token.
So no advantage to doing it on-demand. Better to share among all tokens on the line.
This commit is contained in:
Havoc Pennington 2011-12-06 15:11:17 -05:00
parent 81a4fe63f9
commit 002fd53e8e

View File

@ -111,20 +111,22 @@ final class Tokenizer {
} }
} }
final private ConfigOrigin origin; final private SimpleConfigOrigin origin;
final private Reader input; final private Reader input;
final private LinkedList<Integer> buffer; final private LinkedList<Integer> buffer;
private int lineNumber; private int lineNumber;
private ConfigOrigin lineOrigin;
final private Queue<Token> tokens; final private Queue<Token> tokens;
final private WhitespaceSaver whitespaceSaver; final private WhitespaceSaver whitespaceSaver;
final private boolean allowComments; final private boolean allowComments;
TokenIterator(ConfigOrigin origin, Reader input, boolean allowComments) { TokenIterator(ConfigOrigin origin, Reader input, boolean allowComments) {
this.origin = origin; this.origin = (SimpleConfigOrigin) origin;
this.input = input; this.input = input;
this.allowComments = allowComments; this.allowComments = allowComments;
this.buffer = new LinkedList<Integer>(); this.buffer = new LinkedList<Integer>();
lineNumber = 1; lineNumber = 1;
lineOrigin = this.origin.setLineNumber(lineNumber);
tokens = new LinkedList<Token>(); tokens = new LinkedList<Token>();
tokens.add(Tokens.START); tokens.add(Tokens.START);
whitespaceSaver = new WhitespaceSaver(); whitespaceSaver = new WhitespaceSaver();
@ -233,12 +235,12 @@ final class Tokenizer {
} }
private ProblemException problem(String what, String message, Throwable cause) { private ProblemException problem(String what, String message, Throwable cause) {
return problem(lineOrigin(), what, message, cause); return problem(lineOrigin, what, message, cause);
} }
private ProblemException problem(String what, String message, boolean suggestQuotes, private ProblemException problem(String what, String message, boolean suggestQuotes,
Throwable cause) { Throwable cause) {
return problem(lineOrigin(), what, message, suggestQuotes, cause); return problem(lineOrigin, what, message, suggestQuotes, cause);
} }
private static ProblemException problem(ConfigOrigin origin, String what, private static ProblemException problem(ConfigOrigin origin, String what,
@ -260,10 +262,6 @@ final class Tokenizer {
return problem(origin, "", message, null); return problem(origin, "", message, null);
} }
private ConfigOrigin lineOrigin() {
return lineOrigin(origin, lineNumber);
}
private static ConfigOrigin lineOrigin(ConfigOrigin baseOrigin, private static ConfigOrigin lineOrigin(ConfigOrigin baseOrigin,
int lineNumber) { int lineNumber) {
return ((SimpleConfigOrigin) baseOrigin).setLineNumber(lineNumber); return ((SimpleConfigOrigin) baseOrigin).setLineNumber(lineNumber);
@ -281,7 +279,7 @@ final class Tokenizer {
// that parses as JSON is treated the JSON way and otherwise // that parses as JSON is treated the JSON way and otherwise
// we assume it's a string and let the parser sort it out. // we assume it's a string and let the parser sort it out.
private Token pullUnquotedText() { private Token pullUnquotedText() {
ConfigOrigin origin = lineOrigin(); ConfigOrigin origin = lineOrigin;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int c = nextCharSkippingComments(); int c = nextCharSkippingComments();
while (true) { while (true) {
@ -338,11 +336,10 @@ final class Tokenizer {
try { try {
if (containedDecimalOrE) { if (containedDecimalOrE) {
// force floating point representation // force floating point representation
return Tokens.newDouble(lineOrigin(), return Tokens.newDouble(lineOrigin, Double.parseDouble(s), s);
Double.parseDouble(s), s);
} else { } else {
// this should throw if the integer is too large for Long // this should throw if the integer is too large for Long
return Tokens.newLong(lineOrigin(), Long.parseLong(s), s); return Tokens.newLong(lineOrigin, Long.parseLong(s), s);
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw problem(s, "Invalid number: '" + s + "'", true /* suggestQuotes */, e); throw problem(s, "Invalid number: '" + s + "'", true /* suggestQuotes */, e);
@ -426,12 +423,12 @@ final class Tokenizer {
sb.appendCodePoint(c); sb.appendCodePoint(c);
} }
} while (c != '"'); } while (c != '"');
return Tokens.newString(lineOrigin(), sb.toString()); return Tokens.newString(lineOrigin, sb.toString());
} }
private Token pullSubstitution() throws ProblemException { private Token pullSubstitution() throws ProblemException {
// the initial '$' has already been consumed // the initial '$' has already been consumed
ConfigOrigin origin = lineOrigin(); ConfigOrigin origin = lineOrigin;
int c = nextCharSkippingComments(); int c = nextCharSkippingComments();
if (c != '{') { if (c != '{') {
throw problem(asString(c), "'$' not followed by {, '" + asString(c) throw problem(asString(c), "'$' not followed by {, '" + asString(c)
@ -479,8 +476,9 @@ final class Tokenizer {
return Tokens.END; return Tokens.END;
} else if (c == '\n') { } else if (c == '\n') {
// newline tokens have the just-ended line number // newline tokens have the just-ended line number
Token line = Tokens.newLine(lineOrigin()); // uses lineNumber Token line = Tokens.newLine(lineOrigin);
lineNumber += 1; lineNumber += 1;
lineOrigin = origin.setLineNumber(lineNumber);
return line; return line;
} else { } else {
Token t = null; Token t = null;