Skip to content

Commit 49988f2

Browse files
committed
use py_scadparser
1 parent e64dff0 commit 49988f2

File tree

2 files changed

+7
-53
lines changed

2 files changed

+7
-53
lines changed

solid/objects.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -853,16 +853,7 @@ def use(scad_file_path: PathStr, use_not_include: bool = True, dest_namespace_di
853853

854854
scad_file_path = _find_library(scad_file_path)
855855

856-
contents = None
857-
try:
858-
contents = scad_file_path.read_text()
859-
except Exception as e:
860-
raise Exception(f"Failed to import SCAD module '{scad_file_path}' with error: {e} ")
861-
862-
# Once we have a list of all callables and arguments, dynamically
863-
# add OpenSCADObject subclasses for all callables to the calling module's
864-
# namespace.
865-
symbols_dicts = parse_scad_callables(contents)
856+
symbols_dicts = parse_scad_callables(scad_file_path)
866857

867858
for sd in symbols_dicts:
868859
class_str = new_openscad_class_str(sd['name'], sd['args'], sd['kwargs'],

solid/solidpython.py

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -611,51 +611,14 @@ def sp_code_in_scad_comment(calling_file: PathStr) -> str:
611611
# ===========
612612
# = Parsing =
613613
# ===========
614+
def parse_scad_callables(filename: str) -> List[dict]:
615+
from .py_scadparser import scad_parser
614616

615-
def parse_scad_callables(scad_code_str: str) -> List[dict]:
616-
callables = []
617+
_, _, modules, functions, _ = scad_parser.parseFile(filename)
617618

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})
659622

660623
return callables
661624

0 commit comments

Comments
 (0)