mirror of
https://github.com/davidhalter/parso.git
synced 2026-05-24 17:28:53 +08:00
Work with token types whenever possible
This commit is contained in:
@@ -41,15 +41,6 @@ class Grammar(object):
|
|||||||
accesses the instance variables directly. The class here does not
|
accesses the instance variables directly. The class here does not
|
||||||
provide initialization of the tables; several subclasses exist to
|
provide initialization of the tables; several subclasses exist to
|
||||||
do this (see the conv and pgen modules).
|
do this (see the conv and pgen modules).
|
||||||
|
|
||||||
The instance variables are as follows:
|
|
||||||
|
|
||||||
start -- the number of the grammar's start nonterminal.
|
|
||||||
|
|
||||||
keywords -- a dict mapping keyword strings to arc labels.
|
|
||||||
|
|
||||||
tokens -- a dict mapping token numbers to arc labels.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, bnf_grammar, start_nonterminal, rule_to_dfas, token_namespace):
|
def __init__(self, bnf_grammar, start_nonterminal, rule_to_dfas, token_namespace):
|
||||||
@@ -57,7 +48,6 @@ class Grammar(object):
|
|||||||
self._nonterminal_to_dfas = rule_to_dfas
|
self._nonterminal_to_dfas = rule_to_dfas
|
||||||
|
|
||||||
self.reserved_syntax_strings = {}
|
self.reserved_syntax_strings = {}
|
||||||
self.tokens = {}
|
|
||||||
self.start_nonterminal = start_nonterminal
|
self.start_nonterminal = start_nonterminal
|
||||||
|
|
||||||
self._make_grammar()
|
self._make_grammar()
|
||||||
@@ -94,7 +84,6 @@ class Grammar(object):
|
|||||||
|
|
||||||
# A named token (e.g. NAME, NUMBER, STRING)
|
# A named token (e.g. NAME, NUMBER, STRING)
|
||||||
token_type = getattr(self._token_namespace, label, None)
|
token_type = getattr(self._token_namespace, label, None)
|
||||||
self.tokens[token_type] = token_type
|
|
||||||
return token_type
|
return token_type
|
||||||
else:
|
else:
|
||||||
# Either a keyword or an operator
|
# Either a keyword or an operator
|
||||||
|
|||||||
@@ -74,10 +74,7 @@ def token_to_ilabel(grammar, type_, value):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
return type_
|
||||||
return grammar.tokens[type_]
|
|
||||||
except KeyError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class PgenParser(object):
|
class PgenParser(object):
|
||||||
|
|||||||
Reference in New Issue
Block a user