mirror of
https://github.com/davidhalter/parso.git
synced 2026-05-19 23:10:16 +08:00
Remove labels
This commit is contained in:
+16
-17
@@ -16,6 +16,8 @@ fallback token code OP, but the parser needs the actual token code.
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from ast import literal_eval
|
||||||
|
|
||||||
|
|
||||||
class DFAPlan(object):
|
class DFAPlan(object):
|
||||||
def __init__(self, next_dfa, dfa_pushes=[]):
|
def __init__(self, next_dfa, dfa_pushes=[]):
|
||||||
@@ -26,6 +28,11 @@ class DFAPlan(object):
|
|||||||
return '%s(%s, %s)' % (self.__class__.__name__, self.next_dfa, self.dfa_pushes)
|
return '%s(%s, %s)' % (self.__class__.__name__, self.next_dfa, self.dfa_pushes)
|
||||||
|
|
||||||
|
|
||||||
|
class ReservedString(object):
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
|
||||||
class Grammar(object):
|
class Grammar(object):
|
||||||
"""Pgen parsing tables conversion class.
|
"""Pgen parsing tables conversion class.
|
||||||
|
|
||||||
@@ -37,13 +44,6 @@ class Grammar(object):
|
|||||||
|
|
||||||
The instance variables are as follows:
|
The instance variables are as follows:
|
||||||
|
|
||||||
labels -- a list of (x, y) pairs where x is either a token
|
|
||||||
number or a nonterminal number, and y is either None
|
|
||||||
or a string; the strings are keywords. The label
|
|
||||||
number is the index in this list; label numbers
|
|
||||||
are used to mark state transitions (arcs) in the
|
|
||||||
DFAs.
|
|
||||||
|
|
||||||
start -- the number of the grammar's start nonterminal.
|
start -- the number of the grammar's start nonterminal.
|
||||||
|
|
||||||
keywords -- a dict mapping keyword strings to arc labels.
|
keywords -- a dict mapping keyword strings to arc labels.
|
||||||
@@ -56,7 +56,6 @@ class Grammar(object):
|
|||||||
self._token_namespace = token_namespace
|
self._token_namespace = token_namespace
|
||||||
self._nonterminal_to_dfas = rule_to_dfas
|
self._nonterminal_to_dfas = rule_to_dfas
|
||||||
|
|
||||||
self.labels = [(0, "EMPTY")]
|
|
||||||
self.reserved_syntax_strings = {}
|
self.reserved_syntax_strings = {}
|
||||||
self.tokens = {}
|
self.tokens = {}
|
||||||
self.start_nonterminal = start_nonterminal
|
self.start_nonterminal = start_nonterminal
|
||||||
@@ -102,24 +101,24 @@ class Grammar(object):
|
|||||||
|
|
||||||
@_cache_labels
|
@_cache_labels
|
||||||
def _make_label(self, label):
|
def _make_label(self, label):
|
||||||
ilabel = len(self.labels)
|
|
||||||
if label[0].isalpha():
|
if label[0].isalpha():
|
||||||
# Either a nonterminal name or a named token
|
# Either a nonterminal name or a named token
|
||||||
assert label not in self._nonterminal_to_dfas
|
assert label not in self._nonterminal_to_dfas
|
||||||
|
|
||||||
# A named token (e.g. NAME, NUMBER, STRING)
|
# A named token (e.g. NAME, NUMBER, STRING)
|
||||||
itoken = getattr(self._token_namespace, label, None)
|
token_type = getattr(self._token_namespace, label, None)
|
||||||
self.labels.append((itoken, None))
|
self.tokens[token_type] = token_type
|
||||||
self.tokens[itoken] = ilabel
|
return token_type
|
||||||
return ilabel
|
|
||||||
else:
|
else:
|
||||||
# Either a keyword or an operator
|
# Either a keyword or an operator
|
||||||
assert label[0] in ('"', "'"), label
|
assert label[0] in ('"', "'"), label
|
||||||
# TODO use literal_eval instead of a simple eval.
|
# TODO use literal_eval instead of a simple eval.
|
||||||
value = eval(label)
|
value = literal_eval(label)
|
||||||
self.labels.append(('XXX', value))
|
try:
|
||||||
self.reserved_syntax_strings[value] = ilabel
|
return self.reserved_syntax_strings[value]
|
||||||
return self.reserved_syntax_strings[value]
|
except KeyError:
|
||||||
|
r = self.reserved_syntax_strings[value] = ReservedString(value)
|
||||||
|
return r
|
||||||
|
|
||||||
def _calculate_first_terminals(self, nonterminal):
|
def _calculate_first_terminals(self, nonterminal):
|
||||||
dfas = self._nonterminal_to_dfas[nonterminal]
|
dfas = self._nonterminal_to_dfas[nonterminal]
|
||||||
|
|||||||
Reference in New Issue
Block a user