refactor: incremental structural cleanup across expression, loader, and generator layers - #53
Merged
Conversation
- extract reusable parser helper for left-associative operators - clean up lexer/validator small inconsistencies - keep expression behavior unchanged
- extract list/dict/comprehension/match Lua compilation helpers - document sparse table behavior in Lua template - add regression coverage for filter comprehension generation
- extract shared helpers for file checks and validation output - build load config through a dedicated helper - register repetitive generate subcommands declaratively
- reuse sequence and dict construction helpers - centralize comprehension iteration and evaluation - keep evaluator semantics unchanged
- extract attribute, subscript, binop and match helpers - shrink the main evaluate dispatcher - preserve existing expression runtime behavior
- extract common helpers for AST node lists and key/value pairs - reduce duplicate recursion in name collection and validation - keep validator behavior unchanged
- extract dedicated helpers for comprehension and match validation - separate parse-to-errors orchestration from validate_expr - preserve existing validator behavior
- extract common aliases for repeated AST node collections - reuse shared tuple/list/pair types across literal nodes - keep expression AST API unchanged
- reuse aliases for comprehension kind and operator fields - reduce repeated string typing in AST dataclasses - keep expression AST behavior unchanged
- extract helper functions for loader construction decisions - centralize isolated-loader setup for load_protocol - preserve loader public API behavior
- choose global vs isolated loader through a shared helper - make load_protocol orchestration smaller and easier to follow - preserve public loader behavior
- Add _process_field(raw) that encapsulates type lookup, str special-case handling, and GeneratorError raises for missing/unsupported types - generate() loop now delegates to _process_field and skips empty results
- Add _render_output(context, output_dir, proto_id) that owns Jinja2 env creation, template loading, directory creation, and file write - generate() builds context dict and delegates I/O to _render_output
Contributor
There was a problem hiding this comment.
Pull request overview
This PR performs a broad, behavior-preserving refactor across the protocollab expression subsystem (lexer/parser/evaluator/validator/AST), loader selection logic, and the Lua/Python generators, primarily by extracting helper functions and tightening internal structure.
Changes:
- Refactors expression parsing/evaluation/validation by extracting reusable traversal and evaluation helpers, plus adds AST type aliases.
- Refactors generators by splitting large Lua expression compilation dispatch and extracting Python field processing + Jinja2 rendering helpers.
- Refactors loader and CLI entrypoints by extracting selection/build helpers and consolidating CLI command registration/printing logic.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/protocollab/type_system/primitives.py | Adds clarifying comment for u3/uint24 struct handling. |
| src/protocollab/tests/generators/test_python_lua_generators.py | Adds regression test for Lua filter(...) comprehension compilation. |
| src/protocollab/main.py | Extracts CLI helper functions for file checks, config building, validation printing, and generate subcommand registration. |
| src/protocollab/loader/init.py | Extracts loader construction/selection helpers; load_protocol() delegates to a single call. |
| src/protocollab/generators/templates/lua/dissector.lua.j2 | Adds explanatory comment about sparse numeric-table membership semantics. |
| src/protocollab/generators/python_generator.py | Extracts _process_field() and _render_output() to simplify generator flow. |
| src/protocollab/generators/lua_generator.py | Splits _compile_lua_expr() into focused helpers; adjusts instance ordering logic. |
| src/protocollab/expression/validator.py | Extracts traversal and validation helpers; centralizes builtins set. |
| src/protocollab/expression/parser.py | Extracts left-associative parsing helper; adjusts grammar to parse in at correct precedence; strengthens match parsing diagnostic. |
| src/protocollab/expression/lexer.py | Exposes keyword set as KEYWORDS; modernizes typing. |
| src/protocollab/expression/evaluator.py | Extracts evaluation helpers for sequences/dicts/comprehensions/attribute/subscript/binop/match. |
| src/protocollab/expression/ast_nodes.py | Adds and applies AST type aliases for containers and scalar concepts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- main.py: move strict-warning handling into the is_valid+warnings branch so it is reachable; exit non-zero when --strict and warnings exist - evaluator.py: annotate _iter_comprehension_contexts return type as Iterator[tuple[Any, dict[str, Any]]] instead of Any
Replace named captures that were immediately discarded (F841) with bare class-pattern matches e.g. Comprehension() instead of Comprehension(kind=kind, expr=expr, ...) in evaluator and validator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Incremental, behaviour-preserving refactoring of the largest production
modules in
src/protocollab. Each step was followed by a targeted testrun; no public API or observable behaviour was changed.
Changes by scope
expression/(parser · evaluator · validator · ast_nodes)8248e0eparser – extract_parse_left_associative(), replacing sixcopy-pasted precedence methods (
_bitwise_or…_mult).9ce0804evaluator – extract collection helpers (_evaluate_sequence,_evaluate_dict_pairs,_iter_comprehension_contexts,_evaluate_comprehension).b5abba6evaluator – extract runtime helpers (_evaluate_attribute,_evaluate_subscript,_evaluate_binop,_evaluate_match).4b3807bvalidator – extract traversal helpers (_collect_name_nodes,_collect_name_pairs,_validate_nodes,_validate_pairs).11cf2a7validator – extract rule helpers (_collect_names_from_*,_validate_*_node,_parse_expr_for_validation).5626ea8ast_nodes – add container-type aliases (ASTNodeSequence,ASTNodeList,ASTNodePair,ASTNodePairs); apply toList,Dict,ListLiteral,DictLiteral.13b0826ast_nodes – add scalar aliases (LiteralValue,OptionalASTNode,ComprehensionKind,UnaryOperator,BinaryOperator);apply across all relevant nodes.
generators/6f46e69lua_generator – split monolithic_compile_lua_expr()dispatcher into
_compile_lua_list,_compile_lua_dict_pairs,_compile_lua_comprehension,_compile_lua_match.4ee6a25python_generator – extract_process_field(raw)for typelookup,
strspecial-casing, andGeneratorErrorraises.e1bc261python_generator – extract_render_output(context, output_dir, proto_id)to isolate Jinja2 env creation and file I/O.loader/2920f15– extract factory helpers (_build_loader,_should_use_isolated_loader,_build_isolated_loader,_load_with_loader).30235cb– add_select_loader(config, use_cache);load_protocol()reduced to a single delegating call.main.py(CLI)3de00e9– extract_ensure_input_file_exists,_build_load_config,_print_validation_result,_register_generate_command; generatesub-commands registered declaratively.
Test coverage
All targeted suites passed after every commit:
test_expression_*,test_python_lua_generators,test_loader,test_cli,test_type_system.