> During my parsing (with PetitParser) I'd like to retain some context as > I go. A simple example would be the current line number, which gets > incremented at line ends. A more complex example would be a current > list of variables.
Tokens could easily provide line and column number. They currently don't, but I have done that for a separate project. I think I will merge that with the main code. > I can avoid the whole problem by only retaining state after the parse > tree is constructed, i.e., when I walk the tree. Is it just a bad idea > to do anything else? It depends what you want to use it for. > The methods I've thought of, and their drawbacks are > * use a global variable (ugly and requires only one active parse at a > time) Yeah, I wouldn't do that. > * keep the state in the stream I'm parsing with a special stream class > that holds a regular stream and the state (not sure all parsers would > have access to the stream; requires more hacks for parse: to work, as > opposed to parseOn:) That's what I would suggest. The stream is the parser context and you can subclass it to keep other state. The PetitParser GUI does that for example to profile and trace parsers while reading input. If you subclass PPStream you should not need to change #parseOn:. > * make special parser subclasses, each of which refers to the shared > state (lots of work and lots of extra classes; possibly I'd have one > special parser that held the state and an arbitrary real parser) That's another possibility. I wouldn't subclass though (that would lead to an explosion of parsers), but use wrappers instead (like method wrappers but for parsers). For example, you can wrap aParser like this: aParser >=> [ :stream :continuation | before. result := continuation value. after. result ] We've documented some of these (and other) tricks in the DYLA 2010 paper: http://bergel.eu/download/Dyla2010/dyla10_submission_4.pdf Lukas -- Lukas Renggli www.lukas-renggli.ch _______________________________________________ Pharo-project mailing list [email protected] http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
