Skip to content

Parser: implement comparison operators (<, >, <=, >=) #34

@unclesp1d3r

Description

@unclesp1d3r

Summary

Implement comparison operators <, >, <=, >= in both the parser and evaluator. These are currently explicitly rejected by the parser (grammar.rs) and are critical for compatibility with real magic files.

Context

Many magic rules use comparison operators for version checks, size validation, and range matching. Without these, the vast majority of system magic files cannot be fully interpreted.

Current state: Only =, !=, &, and &mask operators are implemented.

Acceptance Criteria

  • Parser recognizes <, >, <=, >= operators in magic rule syntax
  • Operator enum extended with LessThan, GreaterThan, LessEqual, GreaterEqual variants
  • Evaluator applies comparison logic for all numeric types (byte, short, long)
  • String comparison works lexicographically where applicable
  • Unit tests for each operator with each type
  • Integration tests with real magic rules using these operators
  • All existing tests continue to pass

Impact

HIGH - Would unlock ~40% more magic file compatibility. Used extensively in format version detection, file size checks, and range-based matching.

Files to Modify

  • src/parser/ast.rs - Extend Operator enum
  • src/parser/grammar.rs - Parse new operators
  • src/evaluator/operators.rs - Implement comparison logic
  • Tests in all above modules

References

Metadata

Metadata

Assignees

Labels

compatibilitylibmagic compatibility and migrationenhancementNew feature or requestevaluatorRule evaluation engine and logicparserMagic file parsing components and grammarpriority:criticalMust have, blocks release
No fields configured for Feature.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions