I am in the middle of re-writing my translator and I am being much more disciplined about tests this time, since this version is likely to live for more than a few weeks.
Because you can run a visitor starting at any node, you can almost write beautiful small tests like this ...
expect(parse("some test code", "startGrammarRule")).toEqual(new ASTForGrammarRule())
and then write one ( or a few of these ) for each visitor function
EXCEPT that the rule you are invoking is a sub rule, and so does not have "EOF" in it, so if my grammar has somewhere in it
numberList: NUMBER ( ',' NUMBER )* ;
... then parse("1,2,3", "numberList")
only parses "1" (because it is only an "EOF" which would make the parser hungry enough to consume all the string).
Editing the rule to add EOF is a non starter. I could, for every rule I write a test for, add a test version of the rule ...
numberList: NUMBER ( ',' NUMBER )* ;
numberList_TEST: numberList EOF ;
... but that is going to make the grammar cluttered and introduce worry that the _TEST
rules have to always be maintained scrupulously ...
I want a flag when I create a parser which constructs that faux TEST rule dynamically and then parses from there, or something like that ...
Is there a better way to write tests for my parser that I haven't figured out yet?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…