Commit 60872f99 authored by Boris Zbarsky's avatar Boris Zbarsky
Browse files

Bug 742151. Fix the error reporting in the WebIDL parser to list correct line numbers. r=khuey

There are two changes here.  One is to pass tracking=True to our parser.  This makes it properly track positions
of all productions, not just of lexer tokens.  The second is to properly count up our newlines in the lex data,
since the lexer seems to report the line number of the start of the lex data, which is always 1 in our case.
parent bcf9d165
Loading
Loading
Loading
Loading
+5 −2
Original line number Original line Diff line number Diff line
@@ -88,6 +88,9 @@ class Location(object):
            self._line = self._lexdata[startofline:]
            self._line = self._lexdata[startofline:]
        self._colno = self._lexpos - startofline
        self._colno = self._lexpos - startofline


        # Our line number seems to point to the start of self._lexdata
        self._lineno += self._lexdata.count('\n', 0, startofline)

    def get(self):
    def get(self):
        self.resolve()
        self.resolve()
        return "%s line %s:%s" % (self._file, self._lineno, self._colno)
        return "%s line %s:%s" % (self._file, self._lineno, self._colno)
@@ -2923,7 +2926,7 @@ class Parser(Tokenizer):
        self.lexer.input(Parser._builtins)
        self.lexer.input(Parser._builtins)
        self._filename = None
        self._filename = None


        self.parser.parse(lexer=self.lexer)
        self.parser.parse(lexer=self.lexer,tracking=True)


    def _installBuiltins(self, scope):
    def _installBuiltins(self, scope):
        assert isinstance(scope, IDLScope)
        assert isinstance(scope, IDLScope)
@@ -2943,7 +2946,7 @@ class Parser(Tokenizer):
        #    print tok
        #    print tok


        self._filename = filename
        self._filename = filename
        self._productions.extend(self.parser.parse(lexer=self.lexer))
        self._productions.extend(self.parser.parse(lexer=self.lexer,tracking=True))
        self._filename = None
        self._filename = None


    def finish(self):
    def finish(self):
+29 −0
Original line number Original line Diff line number Diff line
import WebIDL

def WebIDLTest(parser, harness):
    # Check that error messages put the '^' in the right place.

    threw = False
    input = """\
// This is a comment.
interface Foo {
};

/* This is also a comment. */
interface ?"""
    try:
        parser.parse(input)
        results = parser.finish()
    except WebIDL.WebIDLError as e:
        threw = True
        lines = str(e).split('\n')
        print lines

        harness.check(len(lines), 3, 'Expected number of lines in error message')
        harness.ok(lines[0].endswith('line 6:10'), 'First line of error should end with "line 6:10", but was "%s".' % lines[0])
        harness.check(lines[1], 'interface ?', 'Second line of error message is the line which caused the error.')
        harness.check(lines[2], ' ' * (len('interface ?') - 1) + '^',
                      'Correct column pointer in error message.')

    harness.ok(threw, "Should have thrown.")