Skip to content

Commit

Permalink
Type annotations and code cleanup in baseparser/fastparser/grammar.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vthorsteinsson committed Nov 19, 2020
1 parent 6cbbf71 commit c88a623
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 77 deletions.
20 changes: 10 additions & 10 deletions src/reynir/baseparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"""

from typing import Union, Dict, List, Optional
from typing import Union, Dict, List, Iterator, Optional

from .grammar import Grammar, Terminal, Nonterminal
from .grammar import Grammar, GrammarItem, Terminal, Nonterminal, Production


class _PackedProduction:
Expand All @@ -43,7 +43,7 @@ class _PackedProduction:
where the component terminals and nonterminals have been packed
into a list of integer indices """

def __init__(self, priority, production):
def __init__(self, priority: int, production: Production) -> None:
# Store the relative priority of this production within its nonterminal
self._priority = priority
# Keep a reference to the original production
Expand All @@ -54,20 +54,20 @@ def __init__(self, priority, production):
self._len = len(self._ix_list)

@property
def production(self):
def production(self) -> Production:
return self._production

@property
def priority(self):
def priority(self) -> int:
return self._priority

def __getitem__(self, index):
def __getitem__(self, index: int) -> int:
return self._ix_list[index] if 0 <= index < self._len else 0

def __len__(self):
def __len__(self) -> int:
return self._len

def __iter__(self):
def __iter__(self) -> Iterator[int]:
return iter(self._ix_list)


Expand All @@ -79,7 +79,7 @@ class Base_Parser:
"""

def __init__(self) -> None:
self._root = None
self._root: Optional[int] = None
self._nt_dict: Dict[int, Optional[List[_PackedProduction]]] = {}
self._nonterminals: Dict[int, Nonterminal] = {}
self._terminals: Dict[int, Terminal] = {}
Expand Down Expand Up @@ -112,7 +112,7 @@ def for_grammar(cls, g: Grammar) -> "Base_Parser":
p.init_from_grammar(g)
return p

def _lookup(self, ix: int) -> Union[Terminal, Nonterminal]:
def _lookup(self, ix: int) -> GrammarItem:
""" Convert a production item from an index to an object reference """
# Terminals have positive indices
# Nonterminals have negative indices
Expand Down
10 changes: 5 additions & 5 deletions src/reynir/fastparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ def __init__(self, start: int, end: int) -> None:
# Priority of highest-priority child family
self._highest_prio = 0
# The nonterminal corresponding to this node, if not a leaf node
self._nonterminal = None
self._nonterminal: Optional[Nonterminal] = None
# The terminal corresponding to this node, if it is a leaf node
self._terminal = None
self._terminal: Optional[Terminal] = None
# The token matching this terminal, if this is a leaf node
self._token = None
self._token: Optional[Token] = None
# If completed is True, this node represents a completed nonterminal.
# Otherwise, it is an internal node representing a position within
# a production of a nonterminal.
Expand Down Expand Up @@ -282,13 +282,13 @@ def from_c_node(
if lb.iNt >= 0:
# Token node: find the corresponding terminal
tix = parent.pList[index]
node._terminal = job.grammar.lookup(tix)
node._terminal = job.grammar.lookup_terminal(tix)
node._token = job.tokens[lb.iNt]
return node

# Nonterminal node
nt = lb.iNt
node._nonterminal = job.grammar.lookup(nt)
node._nonterminal = job.grammar.lookup_nonterminal(nt)
node._completed = lb.pProd == ffi.NULL
# Cache nonterminal nodes
job.c_dict[c_node] = node
Expand Down
Loading

0 comments on commit c88a623

Please sign in to comment.