Skip to content

Commit

Permalink
Fix parsing inconsistencies
Browse files Browse the repository at this point in the history
Resolves   #819.
  • Loading branch information
evhub committed Jan 17, 2024
1 parent b88803b commit 788f857
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
12 changes: 2 additions & 10 deletions coconut/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,16 +1630,8 @@ def str_proc(self, inputstring, **kwargs):

# start the string hold if we're at the start of a string
if hold is not None:
is_f = False
j = i - len(hold["start"])
while j >= 0:
prev_c = inputstring[j]
if prev_c == "f":
is_f = True
break
elif prev_c != "r":
break
j -= 1
is_f_check_str = inputstring[clip(i - len(hold["start"]) + 1 - self.start_f_str_regex_len, min=0): i - len(hold["start"]) + 1]
is_f = self.start_f_str_regex.search(is_f_check_str)
if is_f:
hold.update({
"type": "f string",
Expand Down
5 changes: 4 additions & 1 deletion coconut/compiler/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ class Grammar(object):
octint = combine(Word("01234567") + ZeroOrMore(underscore.suppress() + Word("01234567")))
hexint = combine(Word(hexnums) + ZeroOrMore(underscore.suppress() + Word(hexnums)))

imag_j = caseless_literal("j") | fixto(caseless_literal("i", suppress=True), "j")
imag_j = caseless_literal("j") | fixto(caseless_literal("i", suppress=True, disambiguate=True), "j")
basenum = combine(
Optional(integer) + dot + integer
| integer + Optional(dot + Optional(integer))
Expand Down Expand Up @@ -2660,6 +2660,9 @@ class Grammar(object):
| fixto(end_of_line, "misplaced newline (maybe missing ':')")
)

start_f_str_regex = compile_regex(r"\br?fr?$")
start_f_str_regex_len = 4

end_f_str_expr = combine(start_marker + (rbrace | colon | bang))

string_start = start_marker + python_quoted_string
Expand Down
11 changes: 8 additions & 3 deletions coconut/compiler/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
cache_validation_info,
require_cache_clear_frac,
reverse_any_of,
all_keywords,
)
from coconut.exceptions import (
CoconutException,
Expand Down Expand Up @@ -1537,12 +1538,16 @@ def any_len_perm_at_least_one(*elems, **kwargs):
return any_len_perm_with_one_of_each_group(*groups_and_elems)


def caseless_literal(literalstr, suppress=False):
def caseless_literal(literalstr, suppress=False, disambiguate=False):
"""Version of CaselessLiteral that always parses to the given literalstr."""
out = CaselessLiteral(literalstr)
if suppress:
return CaselessLiteral(literalstr).suppress()
out = out.suppress()
else:
return fixto(CaselessLiteral(literalstr), literalstr)
out = fixto(out, literalstr)
if disambiguate:
out = disallow_keywords(k for k in all_keywords if k.startswith((literalstr[0].lower(), literalstr[0].upper()))) + out
return out


# -----------------------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion coconut/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def fixpath(path):
return os.path.normpath(os.path.realpath(os.path.expanduser(path)))


def get_bool_env_var(env_var, default=False):
def get_bool_env_var(env_var, default=None):
"""Get a boolean from an environment variable."""
boolstr = os.getenv(env_var, "").lower()
if boolstr in ("true", "yes", "on", "1", "t"):
Expand Down
2 changes: 1 addition & 1 deletion coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "3.0.4"
VERSION_NAME = None
# False for release, int >= 1 for develop
DEVELOP = 12
DEVELOP = 13
ALPHA = False # for pre releases rather than post releases

assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"
Expand Down
2 changes: 1 addition & 1 deletion coconut/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def __init__(self, other=None):
@classmethod
def enable_colors(cls, file=None):
"""Attempt to enable CLI colors."""
use_color = get_bool_env_var(use_color_env_var)
use_color = get_bool_env_var(use_color_env_var, default=None)
if (
use_color is False
or use_color is None and file is not None and not isatty(file)
Expand Down
4 changes: 4 additions & 0 deletions coconut/tests/src/cocotest/agnostic/primary_2.coco
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ def primary_test_2() -> bool:
assert all_equal([], to=10)
assert all_equal([10; 10; 10; 10], to=10)
assert not all_equal([1, 1], to=10)
assert not 0in[1,2,3]
if"0":assert True
if"0":
assert True

with process_map.multiple_sequential_calls(): # type: ignore
assert map((+), range(3), range(4)$[:-1], strict=True) |> list == [0, 2, 4] == process_map((+), range(3), range(4)$[:-1], strict=True) |> list # type: ignore
Expand Down

0 comments on commit 788f857

Please sign in to comment.