properly lex a string with escaped characters
This commit is contained in:
parent
13fbaea4d1
commit
f534f3e753
@ -11,6 +11,10 @@ tokens = [
|
|||||||
'ID',
|
'ID',
|
||||||
] + list(reserved.values())
|
] + list(reserved.values())
|
||||||
|
|
||||||
|
states = (
|
||||||
|
('string', 'exclusive'),
|
||||||
|
)
|
||||||
|
|
||||||
t_LPAREN = r'\('
|
t_LPAREN = r'\('
|
||||||
t_RPAREN = r'\)'
|
t_RPAREN = r'\)'
|
||||||
t_SEMICOLON = r';'
|
t_SEMICOLON = r';'
|
||||||
@ -23,8 +27,31 @@ def t_ID(t):
|
|||||||
return t
|
return t
|
||||||
|
|
||||||
def t_STRING(t):
|
def t_STRING(t):
|
||||||
r'"([^"])*"'
|
r'\"'
|
||||||
t.value = t.value[1:-1]
|
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
|
return t
|
||||||
|
|
||||||
def t_newline(t):
|
def t_newline(t):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user