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