-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from CodeVisionaries/add/multilines
Add draft version of multi-line grammar
- Loading branch information
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pytest | ||
from typing import Optional, Union | ||
|
||
from lark import Lark | ||
|
||
from larktools.ebnf_grammar import grammar | ||
from larktools.evaluation import eval_multi_line_block | ||
|
||
|
||
class SyntaxParser: | ||
def __init__(self): | ||
self.parser = Lark(grammar, parser="lalr", start="multi_line_block") | ||
self.parse = self.parser.parse | ||
|
||
def parse_and_eval(self, expression: str, env: Optional[Union[None, dict]] = None) -> Union[int, float]: | ||
tree = self.parse(expression) | ||
res = eval_multi_line_block(tree, {} if env is None else env) | ||
return res | ||
|
||
|
||
def _parse_and_assert(expression: str, expected: Union[int, float]) -> None: | ||
parser = SyntaxParser() | ||
res = parser.parse_and_eval(expression) | ||
assert expected == res | ||
|
||
def test_multi_line(): | ||
_parse_and_assert("5\n8", 8) | ||
_parse_and_assert("\n\n\n8", 8) | ||
_parse_and_assert("8\n\n\n", 8) | ||
_parse_and_assert("5+5\n3+4\n1+2", 3) | ||
_parse_and_assert("\n\n5\n\n3\n8", 8) | ||
|