[egenix-users] Lookahead parameter in MxTextTools ?

Pekka Niiranen krissepu at vip.fi
Thu Jun 27 20:28:00 CEST 2002


I managed to make my parser for nested structures workin by
inspecting your example code altRTF.py of the mxTextTool -package.
However, I have 3 questions of the code below:

1) Can I use LookAhead -parameter to replace this two liner in the code:
        (None,Is+CallTag,'!','nesting', +1),
        (None,Skip,-1,0,MatchOk),

    I could not find any examples of it.

2) It seems that last line
     (pr4, AllNotIn+CallTag, '?!',MatchFail,'start')

    never fails. Instead it seems the program allways jumps to 'start'.
    What am I missing ?

3) I would like to merge starting ?-sign and ending ! -sign to the match
    but creating additional group like

    ( 'match', Table+AppendMatch,
      ((pr2,Is+CallTag,'?','letters', +1),
       ('group',SubTable+AppendMatch,ThisTable),
       (pr3,Is+CallTag,'!'))),

    fails because jump address 'letters' must be found from the group itself.
    Any ideas ?

---code starts ---

import sys,string

from mx.TextTools import *

text = "aa??CC!DD!aa?FF!?GG!xxxx?XX!aa"

def pr1(taglist,txt,l,r,subtag):
    print 'pr1 ',txt[l:r]

def pr2(taglist,txt,l,r,subtag):
    print 'pr2 ',txt[l:r]

def pr3(taglist,txt,l,r,subtag):
    print 'pr3 ',txt[l:r]

def pr4(taglist,txt,l,r,subtag):
    print 'pr4 ',txt[l:r]

rtf = ('start',
       (None,Is+CallTag,'!','nesting', +1),
       (None,Skip,-1,0,MatchOk),

       'nesting',
       (pr2,Is+CallTag,'?','letters', +1),
       ('group',SubTable+AppendMatch,ThisTable),
       (pr3,Is+CallTag,'!'),
       (None,Jump,To,'start'),

       'letters',
       (pr4, AllNotIn+CallTag, '?!',MatchFail,'start'))


if __name__ == '__main__':

    result, taglist, nextindex = tag(text,rtf)
    print taglist

---code ends ---


-pekka-






More information about the egenix-users mailing list