Make parser REs into module-level "constants".

This commit is contained in:
Simon Forman 2019-12-02 14:26:07 -08:00
parent 39a0e73fac
commit fdf0339e16
1 changed files with 17 additions and 8 deletions

View File

@ -36,11 +36,20 @@ brackets. Terms must be separated by blanks, which can be omitted
around square brackets. around square brackets.
''' '''
#TODO: explain the details of float lits and strings.
from re import Scanner from re import Scanner
from .utils.stack import list_to_stack from .utils.stack import list_to_stack
#TODO: explain the details of float lits and strings.
FLOAT = r'-?\d+\.\d*'
INT = r'-?\d+'
SYMBOL = r'[•\w!@$%^&*()_+<>?|\/;:`~,.=-]+'
BRACKETS = r'\[|\]'
STRING_DOUBLE_QUOTED = r'"(?:[^"\\]|\\.)*"'
STRING_SINGLE_QUOTED = r"'(?:[^'\\]|\\.)*'"
BLANKS = r'\s+'
class Symbol(str): class Symbol(str):
'''A string class that represents Joy function names.''' '''A string class that represents Joy function names.'''
__repr__ = str.__str__ __repr__ = str.__str__
@ -105,11 +114,11 @@ def _parse(tokens):
_scanner = Scanner([ _scanner = Scanner([
(r'-?\d+\.\d*', lambda _, token: float(token)), (FLOAT, lambda _, token: float(token)),
(r'-?\d+', lambda _, token: int(token)), (INT, lambda _, token: int(token)),
(r'[•\w!@$%^&*()_+<>?|\/;:`~,.=-]+', lambda _, token: Symbol(token)), (SYMBOL, lambda _, token: Symbol(token)),
(r'\[|\]', lambda _, token: token), (BRACKETS, lambda _, token: token),
(r'"(?:[^"\\]|\\.)*"', lambda _, token: token[1:-1].replace('\\"', '"')), (STRING_DOUBLE_QUOTED, lambda _, token: token[1:-1].replace('\\"', '"')),
(r"'(?:[^'\\]|\\.)*'", lambda _, token: token[1:-1].replace("\\'", "'")), (STRING_SINGLE_QUOTED, lambda _, token: token[1:-1].replace("\\'", "'")),
(r'\s+', None), (BLANKS, None),
]) ])