Skip to content

Commit

Permalink
Fix py2
Browse files Browse the repository at this point in the history
  • Loading branch information
evhub committed Dec 23, 2023
1 parent 5efdaba commit bc0bad1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
2 changes: 1 addition & 1 deletion coconut/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ def make_err(self, errtype, message, original, loc=0, ln=None, extra=None, refor
ln,
endpoint=endpt_in_snip,
filename=self.filename,
**kwargs,
**kwargs # no comma
).set_formatting(
point_to_endpoint=True if use_startpoint else None,
max_err_msg_lines=2 if use_startpoint else None,
Expand Down
64 changes: 30 additions & 34 deletions coconut/compiler/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@
unwrapper,
incremental_cache_limit,
incremental_mode_cache_successes,
adaptive_reparse_usage_weight,
use_adaptive_any_of,
disable_incremental_for_len,
coconut_cache_dir,
use_adaptive_if_available,
use_fast_pyparsing_reprs,
save_new_cache_items,
cache_validation_info,
Expand Down Expand Up @@ -454,35 +452,6 @@ def attach(item, action, ignore_no_tokens=None, ignore_one_token=None, ignore_ar
return add_action(item, action, make_copy)


@contextmanager
def adaptive_manager(original, loc, item, reparse=False):
"""Manage the use of MatchFirst.setAdaptiveMode."""
if reparse:
cleared_cache = clear_packrat_cache()
if cleared_cache is not True:
item.include_in_packrat_context = True
MatchFirst.setAdaptiveMode(False, usage_weight=adaptive_reparse_usage_weight)
try:
yield
finally:
MatchFirst.setAdaptiveMode(False, usage_weight=1)
if cleared_cache is not True:
item.include_in_packrat_context = False
else:
MatchFirst.setAdaptiveMode(True)
try:
yield
except Exception as exc:
if DEVELOP:
logger.log("reparsing due to:", exc)
logger.record_stat("adaptive", False)
else:
if DEVELOP:
logger.record_stat("adaptive", True)
finally:
MatchFirst.setAdaptiveMode(False)


def final_evaluate_tokens(tokens):
"""Same as evaluate_tokens but should only be used once a parse is assured."""
result = evaluate_tokens(tokens, is_final=True)
Expand All @@ -493,8 +462,6 @@ def final_evaluate_tokens(tokens):

def final(item):
"""Collapse the computation graph upon parsing the given item."""
if SUPPORTS_ADAPTIVE and use_adaptive_if_available:
item = Wrap(item, adaptive_manager, greedy=True)
# evaluate_tokens expects a computation graph, so we just call add_action directly
return add_action(trace(item), final_evaluate_tokens)

Expand Down Expand Up @@ -2040,7 +2007,7 @@ def sub_all(inputstr, regexes, replacements):


# -----------------------------------------------------------------------------------------------------------------------
# PYTEST:
# EXTRAS:
# -----------------------------------------------------------------------------------------------------------------------


Expand Down Expand Up @@ -2071,3 +2038,32 @@ def pytest_rewrite_asserts(code, module_name=reserved_prefix + "_pytest_module")
rewrite_asserts(tree, module_name)
fixed_tree = ast.fix_missing_locations(FixPytestNames().visit(tree))
return ast.unparse(fixed_tree)


@contextmanager
def adaptive_manager(original, loc, item, reparse=False):
"""Manage the use of MatchFirst.setAdaptiveMode."""
if reparse:
cleared_cache = clear_packrat_cache()
if cleared_cache is not True:
item.include_in_packrat_context = True
MatchFirst.setAdaptiveMode(False, usage_weight=10)
try:
yield
finally:
MatchFirst.setAdaptiveMode(False, usage_weight=1)
if cleared_cache is not True:
item.include_in_packrat_context = False
else:
MatchFirst.setAdaptiveMode(True)
try:
yield
except Exception as exc:
if DEVELOP:
logger.log("reparsing due to:", exc)
logger.record_stat("adaptive", False)
else:
if DEVELOP:
logger.record_stat("adaptive", True)
finally:
MatchFirst.setAdaptiveMode(False)
3 changes: 0 additions & 3 deletions coconut/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ def get_path_env_var(env_var, default):

use_line_by_line_parser = False

use_adaptive_if_available = False # currently broken
adaptive_reparse_usage_weight = 10

# these only apply to use_incremental_if_available, not compiler.util.enable_incremental_parsing()
default_incremental_cache_size = None
repeatedly_clear_incremental_cache = True
Expand Down

0 comments on commit bc0bad1

Please sign in to comment.