Skip to content

Commit

Permalink
Merge pull request #2065 from Zac-HD/lark-fix
Browse files Browse the repository at this point in the history
Fix for newest Lark parser
  • Loading branch information
Zac-HD committed Aug 5, 2019
2 parents 5b598b0 + c957ca2 commit 744c049
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ RELEASE_TYPE: patch
This patch tidies up the repr of several ``settings``-related objects,
at runtime and in the documentation, and deprecates the undocumented
edge case that ``phases=None`` was treated like ``phases=tuple(Phase)``.

It *also* fixes :func:`~hypothesis.extra.lark.from_lark` with
:pypi:`lark 0.7.2 <lark-parser>` and later.
2 changes: 2 additions & 0 deletions hypothesis-python/scripts/basic-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pip uninstall -y dpcontracts

pip install ".[lark]"
$PYTEST tests/lark/
pip install lark-parser==0.7.1
$PYTEST tests/lark/
pip uninstall -y lark-parser

if [ "$(python -c 'import sys; print(sys.version_info[:2] in ((2, 7), (3, 6)))')" = "False" ] ; then
Expand Down
14 changes: 11 additions & 3 deletions hypothesis-python/src/hypothesis/extra/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import hypothesis._strategies as st
from hypothesis.errors import InvalidArgument
from hypothesis.internal.compat import getfullargspec
from hypothesis.internal.conjecture.utils import calc_label_from_name
from hypothesis.internal.validation import check_type
from hypothesis.searchstrategy import SearchStrategy
Expand Down Expand Up @@ -78,9 +79,15 @@ def __init__(self, grammar, start=None):
check_type(lark.lark.Lark, grammar, "grammar")
if start is None:
start = grammar.options.start
if not isinstance(start, list):
start = [start]
self.grammar = grammar

terminals, rules, ignore_names = grammar.grammar.compile()
if "start" in getfullargspec(grammar.grammar.compile).args:
terminals, rules, ignore_names = grammar.grammar.compile(start)
else: # pragma: no cover
# This branch is to support lark <= 0.7.1, without the start argument.
terminals, rules, ignore_names = grammar.grammar.compile()

self.names_to_symbols = {}

Expand All @@ -91,7 +98,7 @@ def __init__(self, grammar, start=None):
for t in terminals:
self.names_to_symbols[t.name] = Terminal(t.name)

self.start = self.names_to_symbols[start]
self.start = st.sampled_from([self.names_to_symbols[s] for s in start])

self.ignored_symbols = (
st.sampled_from([self.names_to_symbols[n] for n in ignore_names])
Expand Down Expand Up @@ -120,7 +127,8 @@ def __init__(self, grammar, start=None):

def do_draw(self, data):
state = DrawState()
self.draw_symbol(data, self.start, state)
start = data.draw(self.start)
self.draw_symbol(data, start, state)
return u"".join(state.result)

def rule_label(self, name):
Expand Down
2 changes: 1 addition & 1 deletion requirements/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# pip-compile --output-file=requirements/coverage.txt requirements/coverage.in
#
coverage==4.5.4
lark-parser==0.7.1
lark-parser==0.7.2
numpy==1.17.0
pandas==0.25.0
python-dateutil==2.8.0 # via pandas
Expand Down

0 comments on commit 744c049

Please sign in to comment.