mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 05:14:29 +08:00
Use enum instead of our own logic
This commit is contained in:
@@ -86,7 +86,7 @@ class StackNode(object):
|
|||||||
|
|
||||||
def _token_to_transition(grammar, type_, value):
|
def _token_to_transition(grammar, type_, value):
|
||||||
# Map from token to label
|
# Map from token to label
|
||||||
if type_.contains_syntax:
|
if type_.value.contains_syntax:
|
||||||
# Check for reserved words (keywords)
|
# Check for reserved words (keywords)
|
||||||
try:
|
try:
|
||||||
return grammar.reserved_syntax_strings[value]
|
return grammar.reserved_syntax_strings[value]
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class TokenType(object):
|
class TokenType(object):
|
||||||
def __init__(self, name, contains_syntax=False):
|
def __init__(self, name, contains_syntax=False):
|
||||||
@@ -10,18 +12,17 @@ class TokenType(object):
|
|||||||
return '%s(%s)' % (self.__class__.__name__, self.name)
|
return '%s(%s)' % (self.__class__.__name__, self.name)
|
||||||
|
|
||||||
|
|
||||||
class TokenTypes(object):
|
class PythonTokenTypes(Enum):
|
||||||
"""
|
STRING = TokenType('STRING')
|
||||||
Basically an enum, but Python 2 doesn't have enums in the standard library.
|
NUMBER = TokenType('NUMBER')
|
||||||
"""
|
NAME = TokenType('NAME', contains_syntax=True)
|
||||||
def __init__(self, names, contains_syntax):
|
ERRORTOKEN = TokenType('ERRORTOKEN')
|
||||||
for name in names:
|
NEWLINE = TokenType('NEWLINE')
|
||||||
setattr(self, name, TokenType(name, contains_syntax=name in contains_syntax))
|
INDENT = TokenType('INDENT')
|
||||||
|
DEDENT = TokenType('DEDENT')
|
||||||
|
ERROR_DEDENT = TokenType('ERROR_DEDENT')
|
||||||
PythonTokenTypes = TokenTypes((
|
FSTRING_STRING = TokenType('FSTRING_STRING')
|
||||||
'STRING', 'NUMBER', 'NAME', 'ERRORTOKEN', 'NEWLINE', 'INDENT', 'DEDENT',
|
FSTRING_START = TokenType('FSTRING_START')
|
||||||
'ERROR_DEDENT', 'FSTRING_STRING', 'FSTRING_START', 'FSTRING_END', 'OP',
|
FSTRING_END = TokenType('FSTRING_END')
|
||||||
'ENDMARKER'),
|
OP = TokenType('OP', contains_syntax=True)
|
||||||
contains_syntax=('NAME', 'OP'),
|
ENDMARKER = TokenType('ENDMARKER')
|
||||||
)
|
|
||||||
|
|||||||
@@ -351,7 +351,7 @@ def test_left_recursion():
|
|||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'grammar, error_match', [
|
'grammar, error_match', [
|
||||||
['foo: bar | baz\nbar: NAME\nbaz: NAME\n',
|
['foo: bar | baz\nbar: NAME\nbaz: NAME\n',
|
||||||
r"foo is ambiguous.*given a TokenType\(NAME\).*bar or baz"],
|
r"foo is ambiguous.*given a PythonTokenTypes\.NAME.*bar or baz"],
|
||||||
['''foo: bar | baz\nbar: 'x'\nbaz: "x"\n''',
|
['''foo: bar | baz\nbar: 'x'\nbaz: "x"\n''',
|
||||||
r"foo is ambiguous.*given a ReservedString\(x\).*bar or baz"],
|
r"foo is ambiguous.*given a ReservedString\(x\).*bar or baz"],
|
||||||
['''foo: bar | 'x'\nbar: 'x'\n''',
|
['''foo: bar | 'x'\nbar: 'x'\n''',
|
||||||
|
|||||||
Reference in New Issue
Block a user