mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-15 08:57:11 +08:00
Use proper leafs for fstring start/end
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from parso.python import tree
|
from parso.python import tree
|
||||||
from parso.python.token import (DEDENT, INDENT, ENDMARKER, NEWLINE, NUMBER,
|
from parso.python.token import (DEDENT, INDENT, ENDMARKER, NEWLINE, NUMBER,
|
||||||
STRING, tok_name, NAME, FSTRING_STRING)
|
STRING, tok_name, NAME, FSTRING_STRING,
|
||||||
|
FSTRING_START, FSTRING_END)
|
||||||
from parso.parser import BaseParser
|
from parso.parser import BaseParser
|
||||||
from parso.pgen2.parse import token_to_ilabel
|
from parso.pgen2.parse import token_to_ilabel
|
||||||
|
|
||||||
@@ -50,6 +51,17 @@ class Parser(BaseParser):
|
|||||||
}
|
}
|
||||||
default_node = tree.PythonNode
|
default_node = tree.PythonNode
|
||||||
|
|
||||||
|
# Names/Keywords are handled separately
|
||||||
|
_leaf_map = {
|
||||||
|
STRING: tree.String,
|
||||||
|
NUMBER: tree.Number,
|
||||||
|
NEWLINE: tree.Newline,
|
||||||
|
ENDMARKER: tree.EndMarker,
|
||||||
|
FSTRING_STRING: tree.FStringString,
|
||||||
|
FSTRING_START: tree.FStringStart,
|
||||||
|
FSTRING_END: tree.FStringEnd,
|
||||||
|
}
|
||||||
|
|
||||||
def __init__(self, pgen_grammar, error_recovery=True, start_symbol='file_input'):
|
def __init__(self, pgen_grammar, error_recovery=True, start_symbol='file_input'):
|
||||||
super(Parser, self).__init__(pgen_grammar, start_symbol, error_recovery=error_recovery)
|
super(Parser, self).__init__(pgen_grammar, start_symbol, error_recovery=error_recovery)
|
||||||
|
|
||||||
@@ -121,18 +133,8 @@ class Parser(BaseParser):
|
|||||||
return tree.Keyword(value, start_pos, prefix)
|
return tree.Keyword(value, start_pos, prefix)
|
||||||
else:
|
else:
|
||||||
return tree.Name(value, start_pos, prefix)
|
return tree.Name(value, start_pos, prefix)
|
||||||
elif type == STRING:
|
|
||||||
return tree.String(value, start_pos, prefix)
|
return self._leaf_map.get(type, tree.Operator)(value, start_pos, prefix)
|
||||||
elif type == NUMBER:
|
|
||||||
return tree.Number(value, start_pos, prefix)
|
|
||||||
elif type == NEWLINE:
|
|
||||||
return tree.Newline(value, start_pos, prefix)
|
|
||||||
elif type == ENDMARKER:
|
|
||||||
return tree.EndMarker(value, start_pos, prefix)
|
|
||||||
elif type == FSTRING_STRING:
|
|
||||||
return tree.FStringString(value, start_pos, prefix)
|
|
||||||
else:
|
|
||||||
return tree.Operator(value, start_pos, prefix)
|
|
||||||
|
|
||||||
def error_recovery(self, pgen_grammar, stack, arcs, typ, value, start_pos, prefix,
|
def error_recovery(self, pgen_grammar, stack, arcs, typ, value, start_pos, prefix,
|
||||||
add_token_callback):
|
add_token_callback):
|
||||||
|
|||||||
@@ -271,6 +271,24 @@ class FStringString(Leaf):
|
|||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
|
class FStringStart(Leaf):
|
||||||
|
"""
|
||||||
|
f-strings contain f-string expressions and normal python strings. These are
|
||||||
|
the string parts of f-strings.
|
||||||
|
"""
|
||||||
|
type = 'fstring_start'
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
|
class FStringEnd(Leaf):
|
||||||
|
"""
|
||||||
|
f-strings contain f-string expressions and normal python strings. These are
|
||||||
|
the string parts of f-strings.
|
||||||
|
"""
|
||||||
|
type = 'fstring_end'
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
class _StringComparisonMixin(object):
|
class _StringComparisonMixin(object):
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user