New directory structure:
[gem5.git] / ext / ply / example / hedit / hedit.py
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 tokens = (
18 'H_EDIT_DESCRIPTOR',
19 )
20
21 # Tokens
22 t_ignore = " \t\n"
23
24 def 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
32 t.value = t.value[i+1:i+1+n]
33 return t
34
35 def t_error(t):
36 print "Illegal character '%s'" % t.value[0]
37 t.skip(1)
38
39 # Build the lexer
40 import lex
41 lex.lex()
42 lex.runmain()
43
44