@@ -611,51 +611,14 @@ def sp_code_in_scad_comment(calling_file: PathStr) -> str:
611
611
# ===========
612
612
# = Parsing =
613
613
# ===========
614
+ def parse_scad_callables (filename : str ) -> List [dict ]:
615
+ from .py_scadparser import scad_parser
614
616
615
- def parse_scad_callables (scad_code_str : str ) -> List [dict ]:
616
- callables = []
617
+ _ , _ , modules , functions , _ = scad_parser .parseFile (filename )
617
618
618
- # Note that this isn't comprehensive; tuples or nested data structures in
619
- # a module definition will defeat it.
620
-
621
- # Current implementation would throw an error if you tried to call a(x, y)
622
- # since Python would expect a(x); OpenSCAD itself ignores extra arguments,
623
- # but that's not really preferable behavior
624
-
625
- # TODO: write a pyparsing grammar for OpenSCAD, or, even better, use the yacc parse grammar
626
- # used by the language itself. -ETJ 06 Feb 2011
627
-
628
- # FIXME: OpenSCAD use/import includes top level variables. We should parse
629
- # those out (e.g. x = someValue;) as well -ETJ 21 May 2019
630
- no_comments_re = r'(?mxs)(//.*?\n|/\*.*?\*/)'
631
-
632
- # Also note: this accepts: 'module x(arg) =' and 'function y(arg) {', both
633
- # of which are incorrect syntax
634
- mod_re = r'(?mxs)^\s*(?:module|function)\s+(?P<callable_name>\w+)\s*\((?P<all_args>.*?)\)\s*(?:{|=)'
635
-
636
- # See https://github.com/SolidCode/SolidPython/issues/95; Thanks to https://github.com/Torlos
637
- args_re = r'(?mxs)(?P<arg_name>\w+)(?:\s*=\s*(?P<default_val>([\w.\"\s\?:\-+\\\/*]+|\((?>[^()]|(?2))*\)|\[(?>[^\[\]]|(?2))*\])+))?(?:,|$)'
638
-
639
- # remove all comments from SCAD code
640
- scad_code_str = re .sub (no_comments_re , '' , scad_code_str )
641
- # get all SCAD callables
642
- mod_matches = re .finditer (mod_re , scad_code_str )
643
-
644
- for m in mod_matches :
645
- callable_name = m .group ('callable_name' )
646
- args = []
647
- kwargs = []
648
- all_args = m .group ('all_args' )
649
- if all_args :
650
- arg_matches = re .finditer (args_re , all_args )
651
- for am in arg_matches :
652
- arg_name = am .group ('arg_name' )
653
- # NOTE: OpenSCAD's arguments to all functions are effectively
654
- # optional, in contrast to Python in which all args without
655
- # default values are required.
656
- kwargs .append (arg_name )
657
-
658
- callables .append ({'name' : callable_name , 'args' : args , 'kwargs' : kwargs })
619
+ callables = []
620
+ for c in modules + functions :
621
+ callables .append ({'name' : c .name , 'args' : [], 'kwargs' : c .parameters })
659
622
660
623
return callables
661
624
0 commit comments