xref: /aosp_15_r20/external/ply/ply/example/yply/ylex.py (revision 09948d41d6b3b06d4f51351d12c4c470db0bfb8f)
1# lexer for yacc-grammars
2#
3# Author: David Beazley ([email protected])
4# Date  : October 2, 2006
5
6import sys
7sys.path.append("../..")
8
9from ply import *
10
11tokens = (
12    'LITERAL', 'SECTION', 'TOKEN', 'LEFT', 'RIGHT', 'PREC', 'START', 'TYPE', 'NONASSOC', 'UNION', 'CODE',
13    'ID', 'QLITERAL', 'NUMBER',
14)
15
16states = (('code', 'exclusive'),)
17
18literals = [';', ',', '<', '>', '|', ':']
19t_ignore = ' \t'
20
21t_TOKEN = r'%token'
22t_LEFT = r'%left'
23t_RIGHT = r'%right'
24t_NONASSOC = r'%nonassoc'
25t_PREC = r'%prec'
26t_START = r'%start'
27t_TYPE = r'%type'
28t_UNION = r'%union'
29t_ID = r'[a-zA-Z_][a-zA-Z_0-9]*'
30t_QLITERAL  = r'''(?P<quote>['"]).*?(?P=quote)'''
31t_NUMBER = r'\d+'
32
33
34def t_SECTION(t):
35    r'%%'
36    if getattr(t.lexer, "lastsection", 0):
37        t.value = t.lexer.lexdata[t.lexpos + 2:]
38        t.lexer.lexpos = len(t.lexer.lexdata)
39    else:
40        t.lexer.lastsection = 0
41    return t
42
43# Comments
44
45
46def t_ccomment(t):
47    r'/\*(.|\n)*?\*/'
48    t.lexer.lineno += t.value.count('\n')
49
50t_ignore_cppcomment = r'//.*'
51
52
53def t_LITERAL(t):
54    r'%\{(.|\n)*?%\}'
55    t.lexer.lineno += t.value.count("\n")
56    return t
57
58
59def t_NEWLINE(t):
60    r'\n'
61    t.lexer.lineno += 1
62
63
64def t_code(t):
65    r'\{'
66    t.lexer.codestart = t.lexpos
67    t.lexer.level = 1
68    t.lexer.begin('code')
69
70
71def t_code_ignore_string(t):
72    r'\"([^\\\n]|(\\.))*?\"'
73
74
75def t_code_ignore_char(t):
76    r'\'([^\\\n]|(\\.))*?\''
77
78
79def t_code_ignore_comment(t):
80    r'/\*(.|\n)*?\*/'
81
82
83def t_code_ignore_cppcom(t):
84    r'//.*'
85
86
87def t_code_lbrace(t):
88    r'\{'
89    t.lexer.level += 1
90
91
92def t_code_rbrace(t):
93    r'\}'
94    t.lexer.level -= 1
95    if t.lexer.level == 0:
96        t.type = 'CODE'
97        t.value = t.lexer.lexdata[t.lexer.codestart:t.lexpos + 1]
98        t.lexer.begin('INITIAL')
99        t.lexer.lineno += t.value.count('\n')
100        return t
101
102t_code_ignore_nonspace = r'[^\s\}\'\"\{]+'
103t_code_ignore_whitespace = r'\s+'
104t_code_ignore = ""
105
106
107def t_code_error(t):
108    raise RuntimeError
109
110
111def t_error(t):
112    print("%d: Illegal character '%s'" % (t.lexer.lineno, t.value[0]))
113    print(t.value)
114    t.lexer.skip(1)
115
116lex.lex()
117
118if __name__ == '__main__':
119    lex.runmain()
120