Path: blob/main/third_party/ply/example/yply/ylex.py
7087 views
# lexer for yacc-grammars1#2# Author: David Beazley ([email protected])3# Date : October 2, 200645import sys6sys.path.append("../..")78from ply import *910tokens = (11'LITERAL','SECTION','TOKEN','LEFT','RIGHT','PREC','START','TYPE','NONASSOC','UNION','CODE',12'ID','QLITERAL','NUMBER',13)1415states = (('code','exclusive'),)1617literals = [ ';', ',', '<', '>', '|',':' ]18t_ignore = ' \t'1920t_TOKEN = r'%token'21t_LEFT = r'%left'22t_RIGHT = r'%right'23t_NONASSOC = r'%nonassoc'24t_PREC = r'%prec'25t_START = r'%start'26t_TYPE = r'%type'27t_UNION = r'%union'28t_ID = r'[a-zA-Z_][a-zA-Z_0-9]*'29t_QLITERAL = r'''(?P<quote>['"]).*?(?P=quote)'''30t_NUMBER = r'\d+'3132def t_SECTION(t):33r'%%'34if getattr(t.lexer,"lastsection",0):35t.value = t.lexer.lexdata[t.lexpos+2:]36t.lexer.lexpos = len(t.lexer.lexdata)37else:38t.lexer.lastsection = 039return t4041# Comments42def t_ccomment(t):43r'/\*(.|\n)*?\*/'44t.lexer.lineno += t.value.count('\n')4546t_ignore_cppcomment = r'//.*'4748def t_LITERAL(t):49r'%\{(.|\n)*?%\}'50t.lexer.lineno += t.value.count("\n")51return t5253def t_NEWLINE(t):54r'\n'55t.lexer.lineno += 15657def t_code(t):58r'\{'59t.lexer.codestart = t.lexpos60t.lexer.level = 161t.lexer.begin('code')6263def t_code_ignore_string(t):64r'\"([^\\\n]|(\\.))*?\"'6566def t_code_ignore_char(t):67r'\'([^\\\n]|(\\.))*?\''6869def t_code_ignore_comment(t):70r'/\*(.|\n)*?\*/'7172def t_code_ignore_cppcom(t):73r'//.*'7475def t_code_lbrace(t):76r'\{'77t.lexer.level += 17879def t_code_rbrace(t):80r'\}'81t.lexer.level -= 182if t.lexer.level == 0:83t.type = 'CODE'84t.value = t.lexer.lexdata[t.lexer.codestart:t.lexpos+1]85t.lexer.begin('INITIAL')86t.lexer.lineno += t.value.count('\n')87return t8889t_code_ignore_nonspace = r'[^\s\}\'\"\{]+'90t_code_ignore_whitespace = r'\s+'91t_code_ignore = ""9293def t_code_error(t):94raise RuntimeError9596def t_error(t):97print "%d: Illegal character '%s'" % (t.lexer.lineno, t.value[0])98print t.value99t.lexer.skip(1)100101lex.lex()102103if __name__ == '__main__':104lex.runmain()105106107108109110111112113114