[Pyparsing] How can I determine if a fragment is valid?
Brought to you by:
ptmcg
From: Andrew S. <agt...@ya...> - 2008-01-14 06:32:23
|
I'd like to have my application accept incremental input until a statement conforming to my grammar has been entered. My current approach is simplistic but works well enough. I create a small "Interpreter" to buffer input until it's ready. Something like this: class Interpreter(object): def __init__(self, grammar): self.buffer = [] self.grammar = grammar def push(self, line): result = None self.buffer.append(line) try: result = self.grammar.parse(self.buffer) except ParseException, pe: pass if result: self.buffer = [] return result Now I'd like to add the following feature to my Interpreter. If the buffer has enough input to determine that the contents will _never_ be valid I'd like to re-raise the ParseException so the clients of the Interpreter can stop accepting input. Is there a way to determine if a "fragment" has the potential to be parsed by my grammar? I thought about comparing positions of the parse element in the exception with the elements in the grammar but wanted to check with the list to find out if there is an accepted way of doing this. -a. ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs |