Path: blob/main/third_party/ply/test/lex_hedit.py
6162 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# -----------------------------------------------------------------------------15import sys16if ".." not in sys.path: sys.path.insert(0,"..")1718import ply.lex as lex1920tokens = (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)34t.value = t.value[i+1:i+1+n]35return t3637def t_error(t):38print("Illegal character '%s'" % t.value[0])39t.lexer.skip(1)4041# Build the lexer42lex.lex()43lex.runmain(data="3Habc 10Habcdefghij 2Hxy")444546474849