Skip to content

Commit

Permalink
Fix some unicode alts
Browse files Browse the repository at this point in the history
Resolves   #822.
  • Loading branch information
evhub committed Jan 19, 2024
1 parent e357041 commit 5ff7425
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 50 deletions.
6 changes: 3 additions & 3 deletions coconut/compiler/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,9 @@ class Grammar(object):
pipe = Literal("|>") | fixto(Literal("\u21a6"), "|>")
star_pipe = Literal("|*>") | fixto(Literal("*\u21a6"), "|*>")
dubstar_pipe = Literal("|**>") | fixto(Literal("**\u21a6"), "|**>")
back_pipe = Literal("<|") | fixto(Literal("\u21a4"), "<|")
back_star_pipe = Literal("<*|") | ~Literal("\u21a4**") + fixto(Literal("\u21a4*"), "<*|")
back_dubstar_pipe = Literal("<**|") | fixto(Literal("\u21a4**"), "<**|")
back_pipe = Literal("<|") | disambiguate_literal("\u21a4", ["\u21a4*", "\u21a4?"], fixesto="<|")
back_star_pipe = Literal("<*|") | disambiguate_literal("\u21a4*", ["\u21a4**", "\u21a4*?"], fixesto="<*|")
back_dubstar_pipe = Literal("<**|") | disambiguate_literal("\u21a4**", ["\u21a4**?"], fixesto="<**|")
none_pipe = Literal("|?>") | fixto(Literal("?\u21a6"), "|?>")
none_star_pipe = (
Literal("|?*>")
Expand Down
7 changes: 5 additions & 2 deletions coconut/compiler/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,12 +1454,15 @@ def disallow_keywords(kwds, with_suffix=""):
return regex_item(r"(?!" + "|".join(to_disallow) + r")").suppress()


def disambiguate_literal(literal, not_literals):
def disambiguate_literal(literal, not_literals, fixesto=None):
"""Get an item that matchesl literal and not any of not_literals."""
return regex_item(
item = regex_item(
r"(?!" + "|".join(re.escape(s) for s in not_literals) + ")"
+ re.escape(literal)
)
if fixesto is not None:
item = fixto(item, fixesto)
return item


def any_keyword_in(kwds):
Expand Down
71 changes: 32 additions & 39 deletions coconut/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,45 +576,34 @@ def get_path_env_var(env_var, default):
)

python_builtins = (
'__import__', 'abs', 'all', 'any', 'bin', 'bool', 'bytearray',
'breakpoint', 'bytes', 'chr', 'classmethod', 'compile', 'complex',
'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'filter',
'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',
'hash', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',
'iter', 'len', 'list', 'locals', 'map', 'max', 'memoryview',
'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print',
'property', 'range', 'repr', 'reversed', 'round', 'set', 'setattr',
'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple',
'type', 'vars', 'zip',
'Ellipsis', 'NotImplemented',
'ArithmeticError', 'AssertionError', 'AttributeError',
'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning',
'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
'NotImplementedError', 'OSError', 'OverflowError',
'PendingDeprecationWarning', 'ReferenceError', 'ResourceWarning',
'RuntimeError', 'RuntimeWarning', 'StopIteration',
'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
'UnicodeWarning', 'UserWarning', 'ValueError', 'VMSError',
'Warning', 'WindowsError', 'ZeroDivisionError',
"abs", "aiter", "all", "anext", "any", "ascii",
"bin", "bool", "breakpoint", "bytearray", "bytes",
"callable", "chr", "classmethod", "compile", "complex",
"delattr", "dict", "dir", "divmod",
"enumerate", "eval", "exec",
"filter", "float", "format", "frozenset",
"getattr", "globals",
"hasattr", "hash", "help", "hex",
"id", "input", "int", "isinstance", "issubclass", "iter",
"len", "list", "locals",
"map", "max", "memoryview", "min",
"next",
"object", "oct", "open", "ord",
"pow", "print", "property",
"range", "repr", "reversed", "round",
"set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super",
"tuple", "type",
"vars",
"zip",
"__import__",
'__name__',
'__file__',
'__annotations__',
'__debug__',
# we treat these as coconut_exceptions so the highlighter will always know about them:
# 'ExceptionGroup', 'BaseExceptionGroup',
# don't include builtins that aren't always made available by Coconut:
# 'BlockingIOError', 'ChildProcessError', 'ConnectionError',
# 'BrokenPipeError', 'ConnectionAbortedError', 'ConnectionRefusedError',
# 'ConnectionResetError', 'FileExistsError', 'FileNotFoundError',
# 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError',
# 'PermissionError', 'ProcessLookupError', 'TimeoutError',
# 'StopAsyncIteration', 'ModuleNotFoundError', 'RecursionError',
# 'EncodingWarning',
)

python_exceptions = (
"BaseException", "BaseExceptionGroup", "GeneratorExit", "KeyboardInterrupt", "SystemExit", "Exception", "ArithmeticError", "FloatingPointError", "OverflowError", "ZeroDivisionError", "AssertionError", "AttributeError", "BufferError", "EOFError", "ExceptionGroup", "BaseExceptionGroup", "ImportError", "ModuleNotFoundError", "LookupError", "IndexError", "KeyError", "MemoryError", "NameError", "UnboundLocalError", "OSError", "BlockingIOError", "ChildProcessError", "ConnectionError", "BrokenPipeError", "ConnectionAbortedError", "ConnectionRefusedError", "ConnectionResetError", "FileExistsError", "FileNotFoundError", "InterruptedError", "IsADirectoryError", "NotADirectoryError", "PermissionError", "ProcessLookupError", "TimeoutError", "ReferenceError", "RuntimeError", "NotImplementedError", "RecursionError", "StopAsyncIteration", "StopIteration", "SyntaxError", "IndentationError", "TabError", "SystemError", "TypeError", "ValueError", "UnicodeError", "UnicodeDecodeError", "UnicodeEncodeError", "UnicodeTranslateError", "Warning", "BytesWarning", "DeprecationWarning", "EncodingWarning", "FutureWarning", "ImportWarning", "PendingDeprecationWarning", "ResourceWarning", "RuntimeWarning", "SyntaxWarning", "UnicodeWarning", "UserWarning",
)

# -----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -842,12 +831,16 @@ def get_path_env_var(env_var, default):

coconut_exceptions = (
"MatchError",
"ExceptionGroup",
"BaseExceptionGroup",
)

highlight_builtins = coconut_specific_builtins + interp_only_builtins
all_builtins = frozenset(python_builtins + coconut_specific_builtins + coconut_exceptions)
highlight_builtins = coconut_specific_builtins + interp_only_builtins + python_builtins
highlight_exceptions = coconut_exceptions + python_exceptions
all_builtins = frozenset(
python_builtins
+ python_exceptions
+ coconut_specific_builtins
+ coconut_exceptions
)

magic_methods = (
"__fmap__",
Expand Down
4 changes: 2 additions & 2 deletions coconut/highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
shebang_regex,
magic_methods,
template_ext,
coconut_exceptions,
highlight_exceptions,
main_prompt,
style_env_var,
default_style,
Expand Down Expand Up @@ -100,7 +100,7 @@ class CoconutLexer(Python3Lexer):
]
tokens["builtins"] += [
(words(highlight_builtins, suffix=r"\b"), Name.Builtin),
(words(coconut_exceptions, suffix=r"\b"), Name.Exception),
(words(highlight_exceptions, suffix=r"\b"), Name.Exception),
]
tokens["numbers"] = [
(r"0b[01_]+", Number.Integer),
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 = 16
DEVELOP = 17
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
3 changes: 3 additions & 0 deletions coconut/tests/src/cocotest/agnostic/primary_2.coco
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ def primary_test_2() -> bool:
b = "b"
assert "abc".find b == 1
assert_raises(-> "a" 10, TypeError)
assert (,) ↤* (1, 2, 3) == (1, 2, 3)
assert (,) ↤? None is None
assert (,) ↤*? None is None # type: ignore

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
2 changes: 2 additions & 0 deletions coconut/tests/src/cocotest/agnostic/suite.coco
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,8 @@ forward 2""") == 900
assert pickle_round_trip(.method(x=10)) <| (method=x -> x) == 10
assert sq_and_t2p1(10) == (100, 21)
assert first_false_and_last_true([3, 2, 1, 0, "11", "1", ""]) == (0, "1")
assert ret_args_kwargs ↤** dict(a=1) == ((), dict(a=1))
assert ret_args_kwargs ↤**? None is None

with process_map.multiple_sequential_calls(): # type: ignore
assert process_map(tuple <.. (|>)$(to_sort), qsorts) |> list == [to_sort |> sorted |> tuple] * len(qsorts)
Expand Down
6 changes: 3 additions & 3 deletions coconut/tests/src/cocotest/target_3/py3_test.coco
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def py3_test() -> bool:
čeština = "czech"
assert čeština == "czech"
class HasExecMethod:
def exec(self, x) = x()
def \exec(self, x) = x()
has_exec = HasExecMethod()
assert hasattr(has_exec, "exec")
assert has_exec.exec(-> 1) == 1
def exec_rebind_test():
exec = 1
\exec = 1
assert exec + 1 == 2
def exec(x) = x
def \exec(x) = x
assert exec(1) == 1
return True
assert exec_rebind_test() is True
Expand Down

0 comments on commit 5ff7425

Please sign in to comment.