This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
OpenSCAD Parser is a PEG parser for the OpenSCAD language written in Python. It parses OpenSCAD source code and generates an Abstract Syntax Tree (AST) for programmatic analysis and manipulation. Built on the Arpeggio parsing library.
# Install with development dependencies
pip install -e ".[dev]"
# Or with uv:
uv pip install -e ".[dev]"
# Run all tests
pytest tests/
# Run specific test file
pytest tests/test_lexical.py
# Run specific test
pytest tests/test_lexical.py::TestComments::test_single_line_comment
# Build package
uv buildParser Layer (src/openscad_parser/):
__init__.py- ExportsgetOpenSCADParser()which creates Arpeggio parser instancesgrammar.py- Complete PEG grammar for OpenSCAD (~420 lines of parsing rules)
AST Layer (src/openscad_parser/ast/):
nodes.py- Dataclass definitions for all AST node types (literals, operators, expressions, modules, declarations, comments)builder.py-ASTBuilderVisitorthat converts Arpeggio parse trees to AST using visitor patternsource_map.py-SourceMapclass for tracking source positions across multiple origins (essential for include statements)serialization.py- Support for serializing ASTs to JSON or YAML.__init__.py- Public API:getASTfromString(),getASTfromFile(),getASTfromLibraryFile(),parse_ast()
- Source code →
getOpenSCADParser()creates Arpeggio parser - Parser applies PEG grammar from
grammar.py→ parse tree ASTBuilderVisitortraverses parse tree → AST nodesSourceMapmaintains position tracking throughout
- Caching: AST results are cached in-memory with modification time validation. Use
clear_ast_cache()to clear. - Include processing:
process_includes=True(default) expands include statements before parsing. Set toFalseto preserveIncludeStatementnodes. - Library resolution:
findLibraryFile()andgetASTfromLibraryFile()use platform-aware search paths (OPENSCADPATH env var, then platform defaults).
Tests in tests/ are organized by feature:
test_lexical.py- Comments, strings, numbers, identifierstest_modules.py,test_functions.py- Module/function definitions and callstest_expressions.py- Expressions and operator precedencetest_control_structures.py- If/else, for loops, let, assert, echotest_ast_generation.py,test_builder.py,test_nodes.py- AST internalstest_source_map.py- Source position tracking
Fixtures in conftest.py: parser (full tree), parser_reduced (reduced tree).