lex_hedit.py revision 2632:1bb2f91485ea
1# ----------------------------------------------------------------------------- 2# hedit.py 3# 4# Paring of Fortran H Edit descriptions (Contributed by Pearu Peterson) 5# 6# These tokens can't be easily tokenized because they are of the following 7# form: 8# 9# nHc1...cn 10# 11# where n is a positive integer and c1 ... cn are characters. 12# 13# This example shows how to modify the state of the lexer to parse 14# such tokens 15# ----------------------------------------------------------------------------- 16 17tokens = ( 18 'H_EDIT_DESCRIPTOR', 19 ) 20 21# Tokens 22t_ignore = " \t\n" 23 24def t_H_EDIT_DESCRIPTOR(t): 25 r"\d+H.*" # This grabs all of the remaining text 26 i = t.value.index('H') 27 n = eval(t.value[:i]) 28 29 # Adjust the tokenizing position 30 t.lexer.lexpos -= len(t.value) - (i+1+n) 31 t.value = t.value[i+1:i+1+n] 32 return t 33 34def t_error(t): 35 print "Illegal character '%s'" % t.value[0] 36 t.skip(1) 37 38# Build the lexer 39import lex 40lex.lex() 41lex.runmain(data="3Habc 10Habcdefghij 2Hxy") 42 43 44 45