This repository has been archived on 2024-11-05. You can view files and clone it, but cannot push or open issues/pull-requests.
lamb/lamb_engine/__main__.py

62 lines
1.4 KiB
Python
Raw Normal View History

2022-11-07 19:58:11 -08:00
if __name__ != "__main__":
2022-11-11 17:20:59 -08:00
raise ImportError("lamb_engine.__main__ should never be imported. Run it directly.")
2022-11-07 19:58:11 -08:00
from prompt_toolkit import PromptSession
2022-10-21 17:05:25 -07:00
from prompt_toolkit import print_formatted_text as printf
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.formatted_text import to_plain_text
from pyparsing import exceptions as ppx
2022-11-11 17:20:59 -08:00
import lamb_engine
2022-10-20 11:02:49 -07:00
2022-11-11 17:20:59 -08:00
lamb_engine.utils.show_greeting()
2022-11-11 17:20:59 -08:00
r = lamb_engine.Runner(
prompt_session = PromptSession(
2022-11-11 17:20:59 -08:00
style = lamb_engine.utils.style,
lexer = lamb_engine.utils.LambdaLexer(),
key_bindings = lamb_engine.utils.bindings
),
prompt_message = FormattedText([
2022-11-01 21:29:39 -07:00
("class:prompt", "==> ")
2022-10-28 14:19:29 -07:00
])
2022-10-23 08:53:26 -07:00
)
2022-10-28 14:19:29 -07:00
while True:
try:
i = r.prompt()
# Catch Ctrl-C and Ctrl-D
except KeyboardInterrupt:
printf("\n\nGoodbye.\n")
break
except EOFError:
printf("\n\nGoodbye.\n")
break
# Skip empty lines
if i.strip() == "":
continue
# Try to run an input line.
# Catch parse errors and point them out.
try:
x = r.run(i)
except ppx.ParseException as e:
l = len(to_plain_text(r.prompt_session.message))
printf(FormattedText([
("class:err", " "*(e.loc + l) + "^\n"),
("class:err", f"Syntax error at char {e.loc}."),
("class:text", "\n")
2022-11-11 17:20:59 -08:00
]), style = lamb_engine.utils.style)
2022-10-28 14:19:29 -07:00
continue
2022-11-11 17:20:59 -08:00
except lamb_engine.nodes.ReductionError as e:
2022-10-28 14:19:29 -07:00
printf(FormattedText([
("class:err", f"{e.msg}\n")
2022-11-11 17:20:59 -08:00
]), style = lamb_engine.utils.style)
2022-10-28 14:19:29 -07:00
continue
printf("")