Changes to token specification. More metamagic
This commit is contained in:
@@ -98,9 +98,94 @@ def test_error_return():
|
||||
assert vals == [123, ':+-', '+', '-']
|
||||
assert lexer.errors == [ ':+-' ]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class ModernCalcLexer(Lexer):
|
||||
# Set of token names. This is always required
|
||||
tokens = { ID, NUMBER, PLUS, MINUS, TIMES, DIVIDE, ASSIGN, LT, LE, IF, ELSE }
|
||||
literals = { '(', ')' }
|
||||
|
||||
# String containing ignored characters between tokens
|
||||
ignore = ' \t'
|
||||
|
||||
# Regular expression rules for tokens
|
||||
ID = r'[a-zA-Z_][a-zA-Z0-9_]*'
|
||||
ID['if'] = IF
|
||||
ID['else'] = ELSE
|
||||
|
||||
NUMBER = r'\d+'
|
||||
PLUS = r'\+'
|
||||
MINUS = r'-'
|
||||
TIMES = r'\*'
|
||||
DIVIDE = r'/'
|
||||
ASSIGN = r'='
|
||||
LE = r'<='
|
||||
LT = r'<'
|
||||
|
||||
def NUMBER(self, t):
|
||||
t.value = int(t.value)
|
||||
return t
|
||||
|
||||
# Ignored text
|
||||
ignore_comment = r'\#.*'
|
||||
|
||||
@_(r'\n+')
|
||||
def ignore_newline(self, t):
|
||||
self.lineno += t.value.count('\n')
|
||||
|
||||
# Attached rule
|
||||
def ID(self, t):
|
||||
t.value = t.value.upper()
|
||||
return t
|
||||
|
||||
def error(self, t):
|
||||
self.errors.append(t.value)
|
||||
self.index += 1
|
||||
if hasattr(self, 'return_error'):
|
||||
return t
|
||||
|
||||
def __init__(self):
|
||||
self.errors = []
|
||||
|
||||
|
||||
# Test basic recognition of various tokens and literals
|
||||
def test_modern_tokens():
|
||||
lexer = ModernCalcLexer()
|
||||
toks = list(lexer.tokenize('abc if else 123 + - * / = < <= ( )'))
|
||||
types = [t.type for t in toks]
|
||||
vals = [t.value for t in toks]
|
||||
assert types == ['ID','IF','ELSE', 'NUMBER','PLUS','MINUS','TIMES','DIVIDE','ASSIGN','LT','LE','(',')']
|
||||
assert vals == ['ABC','if','else', 123, '+', '-', '*', '/', '=', '<', '<=', '(', ')']
|
||||
|
||||
# Test ignored comments and newlines
|
||||
def test_modern_ignored():
|
||||
lexer = ModernCalcLexer()
|
||||
toks = list(lexer.tokenize('\n\n# A comment\n123\nabc\n'))
|
||||
types = [t.type for t in toks]
|
||||
vals = [t.value for t in toks]
|
||||
linenos = [t.lineno for t in toks]
|
||||
assert types == ['NUMBER', 'ID']
|
||||
assert vals == [123, 'ABC']
|
||||
assert linenos == [4,5]
|
||||
assert lexer.lineno == 6
|
||||
|
||||
# Test error handling
|
||||
def test_modern_error():
|
||||
lexer = ModernCalcLexer()
|
||||
toks = list(lexer.tokenize('123 :+-'))
|
||||
types = [t.type for t in toks]
|
||||
vals = [t.value for t in toks]
|
||||
assert types == ['NUMBER', 'PLUS', 'MINUS']
|
||||
assert vals == [123, '+', '-']
|
||||
assert lexer.errors == [ ':+-' ]
|
||||
|
||||
# Test error token return handling
|
||||
def test_modern_error_return():
|
||||
lexer = ModernCalcLexer()
|
||||
lexer.return_error = True
|
||||
toks = list(lexer.tokenize('123 :+-'))
|
||||
types = [t.type for t in toks]
|
||||
vals = [t.value for t in toks]
|
||||
assert types == ['NUMBER', 'ERROR', 'PLUS', 'MINUS']
|
||||
assert vals == [123, ':+-', '+', '-']
|
||||
assert lexer.errors == [ ':+-' ]
|
||||
|
||||
|
@@ -3,16 +3,7 @@ from sly import Lexer, Parser
|
||||
|
||||
class CalcLexer(Lexer):
|
||||
# Set of token names. This is always required
|
||||
tokens = {
|
||||
'ID',
|
||||
'NUMBER',
|
||||
'PLUS',
|
||||
'MINUS',
|
||||
'TIMES',
|
||||
'DIVIDE',
|
||||
'ASSIGN',
|
||||
}
|
||||
|
||||
tokens = { ID, NUMBER, PLUS, MINUS, TIMES, DIVIDE, ASSIGN }
|
||||
literals = { '(', ')' }
|
||||
|
||||
# String containing ignored characters between tokens
|
||||
@@ -38,8 +29,8 @@ class CalcLexer(Lexer):
|
||||
def newline(self, t):
|
||||
self.lineno += t.value.count('\n')
|
||||
|
||||
def error(self, value):
|
||||
self.errors.append(value)
|
||||
def error(self, t):
|
||||
self.errors.append(t.value[0])
|
||||
self.index += 1
|
||||
|
||||
def __init__(self):
|
||||
@@ -49,9 +40,9 @@ class CalcParser(Parser):
|
||||
tokens = CalcLexer.tokens
|
||||
|
||||
precedence = (
|
||||
('left', 'PLUS', 'MINUS'),
|
||||
('left', 'TIMES', 'DIVIDE'),
|
||||
('right', 'UMINUS'),
|
||||
('left', PLUS, MINUS),
|
||||
('left', TIMES, DIVIDE),
|
||||
('right', UMINUS),
|
||||
)
|
||||
|
||||
def __init__(self):
|
||||
|
Reference in New Issue
Block a user