Debian Codegraph @

third_party/python/ply/example/hedit/hedit.py

raw
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
17 import sys
18 sys.path.insert(0, "../..")
19
20
21 tokens = (
22 'H_EDIT_DESCRIPTOR',
23 )
24
25 # Tokens
26 t_ignore = " \t\n"
27
28
29 def t_H_EDIT_DESCRIPTOR(t):
30 r"\d+H.*" # This grabs all of the remaining text
31 i = t.value.index('H')
32 n = eval(t.value[:i])
33
34 # Adjust the tokenizing position
35 t.lexer.lexpos -= len(t.value) - (i + 1 + n)
36
37 t.value = t.value[i + 1:i + 1 + n]
38 return t
39
40
41 def t_error(t):
42 print("Illegal character '%s'" % t.value[0])
43 t.lexer.skip(1)
44
45 # Build the lexer
46 import ply.lex as lex
47 lex.lex()
48 lex.runmain()