Fix broken tests in build_oracle

This commit is contained in:
Emi Simpson 2023-03-06 13:03:59 -05:00
parent 0605b759f0
commit 9d6da82dd4
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

View file

@ -3,6 +3,9 @@ Tools for building an oracle table
See `grammar` and `build_oracle.sh` for scripts which actually produce python code. This
module only produces an oracle table in python, without outputting it.
NOTE! Doctests in this module use `GRAMMAR` from `grammar.py` and `EGRAMMAR` as a version
of that grammar with the actions erased, with `_erase_actions()`.
"""
from emis_funky_funktions import *
@ -48,13 +51,13 @@ def _first(
The output contains two values. The first is a set of possible terminals, and the
second is a boolean indicating whether this term can derive epsilon.
>>> _first(flip(cur2(isinstance))(Tok), GRAMMAR, [Variable.Clause])
>>> _first(flip(cur2(isinstance))(Tok), EGRAMMAR, [Variable.Clause])
({Negate, Identifier}, False)
>>> _first(flip(cur2(isinstance))(Tok), GRAMMAR, [Variable.CSTerms])
>>> _first(flip(cur2(isinstance))(Tok), EGRAMMAR, [Variable.CSTerms])
({Comma}, True)
>>> _first(flip(cur2(isinstance))(Tok), GRAMMAR, [Variable.CSTerms, Tok.CloseP])
>>> _first(flip(cur2(isinstance))(Tok), EGRAMMAR, [Variable.CSTerms, Tok.CloseP])
({CloseP, Comma}, False)
"""
def inner(vs: Sequence[A | B]) -> Tuple[Set[B], bool]:
@ -87,7 +90,7 @@ def _follow(
"""
Produce a table indicating exactly which terminals can follow each variable
>>> _follow(flip(cur2(isinstance))(Tok), GRAMMAR) #doctest: +NORMALIZE_WHITESPACE
>>> _follow(flip(cur2(isinstance))(Tok), EGRAMMAR) #doctest: +NORMALIZE_WHITESPACE
{<Start>: set(),
<Idents>: {Newline},
<Clauses>: {Eof},
@ -137,8 +140,8 @@ def _predict(
Given a production, identify the terminals which this production would be valid under
>>> is_tok = flip(cur2(isinstance))(Tok)
>>> follow = _follow(is_tok, GRAMMAR)
>>> _predict(is_tok, GRAMMAR, follow, Variable.Clause, [Variable.Term, Variable.Clause_])
>>> follow = _follow(is_tok, EGRAMMAR)
>>> _predict(is_tok, EGRAMMAR, follow, Variable.Clause, [Variable.Term, Variable.Clause_])
{Negate, Identifier}
"""
first_rhs, epsilon_rhs = _first(is_term, grammar, rhs)
@ -159,7 +162,7 @@ def oracle_table(
>>> is_tok = p_instance(Tok)
>>> is_var = p_instance(Variable)
>>> my_oracle_table = oracle_table(is_tok, is_var, GRAMMAR)
>>> my_oracle_table = oracle_table(is_tok, is_var, EGRAMMAR)
One valid expansion:
>>> my_oracle_table[Variable.Clauses_][Tok.Negate]
@ -196,4 +199,5 @@ def oracle_table(
if __name__ == '__main__':
import doctest
from grammar import GRAMMAR, Tok, Variable
EGRAMMAR = _erase_actions(GRAMMAR, lambda x: not hasattr(x, '__call__')) #type: ignore
doctest.testmod()