Refactor large parser modules into smaller components under 400 lines#259
Refactor large parser modules into smaller components under 400 lines#259
Conversation
- Extract rule-body term classification into a dedicated module `rule/classification.rs`. - Move S-expression formatting for Expr into a separate `expr/sexpr.rs` module. - Split Pratt parser postfix parsing, delay-postfix, and diff-marker handling into separate files under `expression/pratt/`. - Remove inline implementations and validations, organizing parser logic into coherent units. - Revamp expression parser tests to use fixture modules, improving maintainability. This refactor improves code organization, readability, and test structure without changing external behavior. Co-authored-by: devboxerhub[bot] <devboxerhub[bot]@users.noreply.github.com>
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Gates Failed
Enforce advisory code health rules
(2 files with Code Duplication)
Gates Passed
5 Quality Gates Passed
See analysis details in CodeScene
Reason for failure
| Enforce advisory code health rules | Violations | Code Health Impact | |
|---|---|---|---|
| pratt.rs | 1 advisory rule | 10.00 → 9.39 | Suppress |
| errors.rs | 1 advisory rule | 9.39 | Suppress |
Quality Gate Profile: Pay Down Tech Debt
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.
| use chumsky::error::Simple; | ||
|
|
||
| use crate::parser::ast::{Expr, StringLiteral}; | ||
| use crate::parser::span_utils::parse_u32_decimal; |
There was a problem hiding this comment.
❌ New issue: Code Duplication
The module contains 2 functions with similar structure: Pratt.with_struct_literal_activation,Pratt.with_struct_literals_suspended
| fn collection_error_cases() -> Vec<CountedErrorCase> { | ||
| vec![ | ||
| CountedErrorCase { | ||
| src: "[1, 2", | ||
| min_errs: 1, | ||
| }, | ||
| CountedErrorCase { | ||
| src: "{a: 1", | ||
| min_errs: 1, | ||
| }, | ||
| CountedErrorCase { | ||
| src: "{a, b}", | ||
| min_errs: 1, | ||
| }, | ||
| ] | ||
| } |
There was a problem hiding this comment.
❌ New issue: Code Duplication
The module contains 4 functions with similar structure: collection_error_cases,control_flow_error_cases,operator_error_cases,postfix_counted_error_cases
Reviewer's GuideRefactors parser internals by extracting S-expression formatting, rule-body classification, and Pratt postfix parsing into dedicated modules, and reworks expression tests around a reusable fixtures system without changing observable parsing behavior. Sequence diagram for the new rule body term parsing pipelinesequenceDiagram
participant RuleBodyExpression
participant RuleClassificationModule as classification
participant PatternParser as parse_pattern
participant ExpressionParser as parse_expression
participant AggregationLogic as aggregation_tracking
participant Errors as errors_vec
RuleBodyExpression->>RuleClassificationModule: parse_rule_body_term(raw, literal_span, first_aggregation_span, errors)
alt raw contains_assignment
RuleClassificationModule->>PatternParser: parse_pattern(parts.pattern)
PatternParser-->>RuleClassificationModule: Result_Pattern_or_errors
alt pattern_ok
RuleClassificationModule->>ExpressionParser: parse_expression(parts.value)
ExpressionParser-->>RuleClassificationModule: Result_Expr_or_errors
alt value_ok
RuleClassificationModule-->>RuleBodyExpression: Some_RuleBodyTerm_Assignment
else value_errors
RuleClassificationModule->>Errors: append(shifted_errors)
RuleClassificationModule-->>RuleBodyExpression: None
end
else pattern_errors
RuleClassificationModule->>Errors: append(shifted_errors)
RuleClassificationModule-->>RuleBodyExpression: None
end
else raw_is_expression
RuleClassificationModule->>ExpressionParser: parse_expression(trimmed_raw)
ExpressionParser-->>RuleClassificationModule: Result_Expr_or_errors
alt expr_ok
RuleClassificationModule->>AggregationLogic: classify_expression(expr, ctx)
AggregationLogic-->>RuleClassificationModule: Option_RuleBodyTerm
RuleClassificationModule-->>RuleBodyExpression: Option_RuleBodyTerm
else expr_errors
RuleClassificationModule->>Errors: append(errors)
RuleClassificationModule-->>RuleBodyExpression: None
end
end
Class diagram for rule body classification and Expr S-expression formattingclassDiagram
class Expr {
+to_sexpr() String
}
class Literal {
}
class Pattern {
+to_source() String
}
class MatchArm {
pattern Pattern
body Expr
}
class RuleBodyTerm {
}
class RuleAssignment {
pattern Pattern
value Expr
}
class RuleAggregation {
project Expr
key Expr
source AggregationSource
}
class RuleForLoop {
pattern Pattern
iterable Expr
guard Expr
body_terms Vec_RuleBodyTerm_
}
class AggregationSource {
<<enum>>
GroupBy
LegacyAggregate
}
class ClassificationContext {
-literal_span Span
-first_aggregation_span Option_Span_
-errors Vec_SimpleSyntaxKind__
}
class ForLoopComponents {
pattern Pattern
iterable Expr
guard Option_Expr_
body Expr
}
class RuleClassifier {
+parse_rule_body_term(raw String, literal_span Span, first_aggregation_span Option_Span_, errors Vec_SimpleSyntaxKind__) Option_RuleBodyTerm_
-parse_assignment(parts AssignmentParts, literal_span Span) Result_Option_RuleBodyTerm__Vec_SimpleSyntaxKind___
-classify_expression(expr Expr, ctx ClassificationContext) Option_RuleBodyTerm_
-classify_aggregation_with_tracking(args Vec_Expr_, source AggregationSource, ctx ClassificationContext) Option_RuleBodyTerm_
-classify_for_loop(components ForLoopComponents, ctx ClassificationContext) RuleBodyTerm
-classify_for_body_with_aggregation_tracking(body Expr, ctx ClassificationContext) Vec_RuleBodyTerm_
-aggregation_source_for(name String) Option_AggregationSource_
-invocation_aggregation_source(callee Expr) Option_AggregationSource_
-validate_aggregation(term RuleBodyTerm, literal_span Span, first_aggregation_span Option_Span_, errors Vec_SimpleSyntaxKind__) bool
-aggregation_arity_error(literal_span Span, source AggregationSource) Vec_SimpleSyntaxKind__
-multiple_aggregations_error(first_span Span, second_span Span) SimpleSyntaxKind
}
Expr --> Literal
Expr --> Pattern
Expr --> MatchArm
RuleBodyTerm <.. RuleAssignment
RuleBodyTerm <.. RuleAggregation
RuleBodyTerm <.. RuleForLoop
RuleForLoop "*" o-- "body_terms" RuleBodyTerm
RuleAggregation --> AggregationSource
RuleClassifier ..> ClassificationContext
RuleClassifier ..> ForLoopComponents
RuleClassifier ..> RuleBodyTerm
RuleClassifier ..> RuleAggregation
RuleClassifier ..> RuleAssignment
ClassificationContext --> Span
ClassificationContext --> AggregationSource
ForLoopComponents --> Pattern
ForLoopComponents --> Expr
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Summary
Changes
Core Refactor
Tests and Fixtures
Miscellaneous
How to test
Notes
◳ Generated by DevBoxer ◰
ℹ️ Tag @devboxerhub to ask questions and address PR feedback
📎 Task: https://www.devboxer.com/task/4e7f979f-30bc-45b5-844c-4e890802856c
📝 Closes #223
Summary by Sourcery
Refactor parser internals and tests into smaller, focused modules while preserving existing parsing behavior.
Enhancements:
Tests: