Path: blob/main/third_party/ply/example/hedit/hedit.py
7087 views
# -----------------------------------------------------------------------------1# hedit.py2#3# Paring of Fortran H Edit descriptions (Contributed by Pearu Peterson)4#5# These tokens can't be easily tokenized because they are of the following6# form:7#8# nHc1...cn9#10# where n is a positive integer and c1 ... cn are characters.11#12# This example shows how to modify the state of the lexer to parse13# such tokens14# -----------------------------------------------------------------------------1516import sys17sys.path.insert(0,"../..")181920tokens = (21'H_EDIT_DESCRIPTOR',22)2324# Tokens25t_ignore = " \t\n"2627def t_H_EDIT_DESCRIPTOR(t):28r"\d+H.*" # This grabs all of the remaining text29i = t.value.index('H')30n = eval(t.value[:i])3132# Adjust the tokenizing position33t.lexer.lexpos -= len(t.value) - (i+1+n)3435t.value = t.value[i+1:i+1+n]36return t3738def t_error(t):39print("Illegal character '%s'" % t.value[0])40t.lexer.skip(1)4142# Build the lexer43import ply.lex as lex44lex.lex()45lex.runmain()4647484950