Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/openscad_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def getOpenSCADParser(reduce_tree=False, debug=False, include_comments=False):
Args:
reduce_tree: If True, reduce the parse tree (default: False)
debug: If True, enable debug output (default: False)
include_comments: If True, include comments in the AST instead of skipping them (default: False)
include_comments: If True, a grammar that parses comments at the points
it lists (default: False). The getAST* functions no longer use it:
they parse with comments as whitespace and attach them afterwards
(openscad_parser.ast.comments), which accepts a comment anywhere.

Returns:
ParserPython instance configured for OpenSCAD parsing
Expand Down
13 changes: 10 additions & 3 deletions src/openscad_parser/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from arpeggio import NoMatch
from openscad_parser import getOpenSCADParser, strict_commas
from openscad_parser.grammar import _STRICT_COMMAS
from .comments import _attach_all_comments
from .source_map import SourceMap, process_includes as process_includes_func

# Import all AST nodes from nodes
Expand Down Expand Up @@ -279,8 +280,11 @@ def getASTfromString(code: str, include_comments: bool = False, origin: str = "<
source_map = SourceMap()
source_map.add_origin(origin, code)

parser = getOpenSCADParser(reduce_tree=False, include_comments=include_comments)
# Comments are parsed as whitespace and attached afterwards (see comments.py).
parser = getOpenSCADParser(reduce_tree=False)
ast = parse_ast(parser, code, source_map=source_map)
if ast is not None and include_comments:
ast = _attach_all_comments(ast, code, origin)
return ast


Expand Down Expand Up @@ -322,7 +326,8 @@ def _ast_format_tag() -> str:
here = os.path.dirname(os.path.abspath(__file__))
h = hashlib.sha256()
for path in (os.path.join(here, "..", "grammar.py"), os.path.join(here, "nodes.py"),
os.path.join(here, "builder.py"), os.path.join(here, "__init__.py")):
os.path.join(here, "builder.py"), os.path.join(here, "__init__.py"),
os.path.join(here, "comments.py")):
with open(path, "rb") as f:
h.update(f.read())
return h.hexdigest()[:16]
Expand Down Expand Up @@ -493,8 +498,10 @@ def _parse_single_file(file_path: str, include_comments: bool = False) -> list[A
source_map = SourceMap()
source_map.add_origin(file_path, code)

parser = getOpenSCADParser(reduce_tree=False, include_comments=include_comments)
parser = getOpenSCADParser(reduce_tree=False)
ast = parse_ast(parser, code, source_map=source_map)
if ast is not None and include_comments:
ast = _attach_all_comments(ast, code, file_path)

# Cache in memory and on disk
_ast_cache[cache_key] = (ast, current_mtime)
Expand Down
Loading
Loading