1"""Example using pre-built "re" parsing object
2
3The Pre-built Element Token lets you include elements
4which cannot be readily defined in the SimpleParse EBNF
5including items defined by a callout to a Python
6function.  This example demonstrates the technique.
7
8The example also (obviously) demonstrates the use of an
9re object during the parsing process.
10"""
11from __future__ import print_function
12
13import re
14from simpleparse.stt.TextTools.TextTools import *
15from simpleparse.parser import Parser
16from simpleparse import dispatchprocessor
17try:
18    raw_input
19except NameError:
20    raw_input = input
21
22class REMatch:
23    """An object wrapping a regular expression with __call__ (and Call) semantics"""
24    def __init__( self, expression, flags=0 ):
25        self.matcher = re.compile( expression, flags )
26    def __call__( self, text, position, endPosition ):
27        """Return new text position, if > position, then matched, otherwise fails"""
28        result = self.matcher.match( text, position, endPosition)
29        if result:
30            return result.end()
31        else:
32            # doesn't necessarily mean it went forward, merely
33            # that it was satisfied, which means that an optional
34            # satisfied but un-matched re will just get treated
35            # like an error :(
36            return position
37    def table( self ):
38        """Build the TextTools table for the object"""
39        return ( (None, Call, self ), )
40
41declaration = r"""
42v :=  white?,(word,white?)+
43"""
44
45class WordProcessor( dispatchprocessor.DispatchProcessor ):
46    """Processor sub-class defining processing functions for the productions"""
47    # you'd likely provide a "resetBeforeParse" method
48    # in a real-world application, but we don't store anything
49    # in our parser.
50    def word( self, tup, buffer ):
51        """Deal with a "word" production by printing out value"""
52        print("word: ", repr(dispatchprocessor.getString(tup, buffer)))
53    def white( self, tup, buffer ):
54        """Deal with a "white" production by printing out value"""
55        print("white:", repr(dispatchprocessor.getString(tup, buffer)))
56
57
58parser = Parser( declaration, "v", prebuilts = [
59    ("word", REMatch( "\w+").table()),
60    ("white", REMatch( "\W+").table()),
61])
62
63if __name__ == "__main__":
64    print("""Please enter some number of words seperated by whitespace.
65We will attempt to parse them and return the parse results""")
66    data = raw_input( ">>> " )
67    parser.parse( data , processor = WordProcessor())
68