Track newlines within triple quotes, fixes #61

This commit is contained in:
Havoc Pennington 2013-03-04 09:50:17 -05:00
parent 41d3d0bb1c
commit b5b0f17ac1
3 changed files with 24 additions and 1 deletions

View File

@ -200,12 +200,19 @@ final class Parser {
while (Tokens.isNewline(t.token)) {
// line number tokens have the line that was _ended_ by the
// newline, so we have to add one.
// newline, so we have to add one. We have to update lineNumber
// here and also below, because not all tokens store a line
// number, but newline tokens always do.
lineNumber = t.token.lineNumber() + 1;
t = nextToken();
}
// update line number again, iff we have one
int newNumber = t.token.lineNumber();
if (newNumber >= 0)
lineNumber = newNumber;
return t;
}

View File

@ -435,6 +435,11 @@ final class Tokenizer {
consecutiveQuotes = 0;
if (c == -1)
throw problem("End of input but triple-quoted string was still open");
else if (c == '\n') {
// keep the line number accurate
lineNumber += 1;
lineOrigin = origin.setLineNumber(lineNumber);
}
}
sb.appendCodePoint(c);

View File

@ -343,6 +343,17 @@ class ConfParserTest extends TestUtils {
lineNumberTest(1, "1e\n")
lineNumberTest(2, "\n1e\n")
lineNumberTest(3, "\n\n1e\n")
// newlines in triple-quoted string should not hose up the numbering
lineNumberTest(1, "a : \"\"\"foo\"\"\"}")
lineNumberTest(2, "a : \"\"\"foo\n\"\"\"}")
lineNumberTest(3, "a : \"\"\"foo\nbar\nbaz\"\"\"}")
// newlines after the triple quoted string
lineNumberTest(5, "a : \"\"\"foo\nbar\nbaz\"\"\"\n\n}")
// triple quoted string ends in a newline
lineNumberTest(6, "a : \"\"\"foo\nbar\nbaz\n\"\"\"\n\n}")
// end in the middle of triple-quoted string
lineNumberTest(5, "a : \"\"\"foo\n\n\nbar\n")
}
@Test