properly lex a string with escaped characters

This commit is contained in:
Josh Holtrop 2011-08-30 13:59:15 -04:00
parent 13fbaea4d1
commit f534f3e753

View File

@ -11,6 +11,10 @@ tokens = [
'ID',
] + list(reserved.values())
states = (
('string', 'exclusive'),
)
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_SEMICOLON = r';'
@ -23,8 +27,31 @@ def t_ID(t):
return t
def t_STRING(t):
r'"([^"])*"'
t.value = t.value[1:-1]
r'\"'
t.lexer.str_buildup = ''
t.lexer.begin('string')
def t_string_1(t):
r'[^\\\n\"]'
t.lexer.str_buildup += t.value
def t_string_2(t):
r'\\[^\n]'
c = {
't': '\t',
'r': '\r',
'n': '\n',
'f': '\f',
'b': '\b',
'v': '\v',
}.get(t.value[1], t.value[1])
t.lexer.str_buildup += c
def t_string_3(t):
r'\"'
t.type = 'STRING'
t.value = t.lexer.str_buildup
t.lexer.begin('INITIAL')
return t
def t_newline(t):