Skip to content

Commit

Permalink
fixing parsing tree bug and using lark standalone
Browse files Browse the repository at this point in the history
Signed-off-by: Aryan Roy <[email protected]>
  • Loading branch information
aryan26roy committed Sep 14, 2024
1 parent 37702f2 commit 2f34a9b
Show file tree
Hide file tree
Showing 10 changed files with 6,597 additions and 178 deletions.
10 changes: 7 additions & 3 deletions src/formulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

from __future__ import annotations

from . import ttreeformula, numexpr # noqa # noqa
from . import ttreeformula_parser, numexpr_parser # noqa # noqa

from . import convert_ptree

from . import AST

Expand All @@ -20,11 +22,13 @@


def from_root(exp : str, **kwargs) -> AST :
parser = ttreeformula.Lark_StandAlone()
parser = ttreeformula_parser.Lark_StandAlone()
ptree = parser.parse(exp)
convert_ptree.convert_ptree(ptree)
return toast.toast(ptree, nxp=False)

def from_numexpr(exp : str, **kwargs) -> AST :
parser = numexpr.Lark_StandAlone()
parser = numexpr_parser.Lark_StandAlone()
ptree = parser.parse(exp)
convert_ptree.convert_ptree(ptree)
return toast.toast(ptree, nxp=True)
21 changes: 21 additions & 0 deletions src/formulate/convert_ptree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Copyright (c) 2023 Aryan Roy. All rights reserved.
formulate: Easy conversions between different styles of expressions
"""

from . import matching_tree
from . import numexpr_parser
from . import ttreeformula_parser

def convert_ptree(raw_ptree):

if isinstance(raw_ptree, numexpr_parser.Token) or isinstance(raw_ptree, ttreeformula_parser.Token):
return

raw_ptree.__class__ = matching_tree.ptnode

for x in raw_ptree.children:
convert_ptree(x)

return
4 changes: 3 additions & 1 deletion src/formulate/matching_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


import lark
from . import numexpr_parser
from . import ttreeformula_parser


class ptnode(lark.tree.Tree):
class ptnode(numexpr_parser.Tree, ttreeformula_parser.Tree):
__match_args__ = ("data", "children")
51 changes: 0 additions & 51 deletions src/formulate/numexpr.py

This file was deleted.

Loading

0 comments on commit 2f34a9b

Please sign in to comment.