Skip to content

Parser architecture improvement with TypeSlot#48

Merged
yhk1038 merged 5 commits intomainfrom
feature/parser-architecture
Jan 22, 2026
Merged

Parser architecture improvement with TypeSlot#48
yhk1038 merged 5 commits intomainfrom
feature/parser-architecture

Conversation

@yhk1038
Copy link
Contributor

@yhk1038 yhk1038 commented Jan 22, 2026

Summary

Parser architecture improvement with TypeSlot infrastructure. TypeSlot explicitly tracks "positions where types should appear" to enable early validation, better error messages, and enhanced gradual typing support.

Changes

Phase 1: TypeSlot Infrastructure

  • Add IR::TypeSlot class - tracks type positions for parameters, return values, variables
  • Add type_slot attribute to IR::Parameter
  • Add return_type_slot attribute to IR::MethodDef

Phase 2: TokenDeclarationParser Integration

  • Auto-generate TypeSlots during parsing in TokenDeclarationParser
  • Add create_param_type_slot helper method

Phase 3: Error Message System

  • Implement TRuby::Errors::TypeSlotError class
  • Context-aware error messages (includes method name, parameter name)
  • LSP diagnostic format support for IDE integration

Phase 4: Type Resolution

  • Implement TRuby::TypeResolution::SlotResolver class
  • Identify empty TypeSlots and support untyped fallback
  • Slot summary statistics for gradual typing

Phase 5: Parser Facade

  • Add use_token_parser: true option to select new parser
  • Support TRUBY_NEW_PARSER=1 environment variable
  • Full backward compatibility with existing API

Test Plan

  • All 2002 existing tests pass
  • TypeSlot generation tests
  • TypeSlotError formatting tests
  • SlotResolver behavior tests
  • Parser facade tests (compare results from both parsers)

Closes #47

Introduce TypeSlot class that represents positions where type
annotations are expected. This enables explicit tracking of
explicit, inferred, and resolved types for each position.

- Add IR::TypeSlot class with kind, location, and context
- Add type_slot attribute to IR::Parameter
- Add return_type_slot attribute to IR::MethodDef
- Maintain backward compatibility with existing code

Refs #47
- Add TypeSlot creation during parameter parsing in parse_parameter
- Add return_type_slot creation in parse_method_def
- Pass method_name context to parameter parsers for TypeSlot context
- Support all parameter types (regular, rest, keyrest, block, keyword)
- Add comprehensive tests for TypeSlot integration
- Maintain backward compatibility with existing type_annotation/return_type

Refs #47
- Add TRuby::Errors::TypeSlotError class for type-related errors
- Support location info (line, column) from TypeSlot
- Include context description (parameter/return/variable info)
- Support suggestion field for helpful error hints
- Provide to_lsp_diagnostic for LSP integration
- Add comprehensive tests

Refs #47
- Add TRuby::TypeResolution::SlotResolver class
- Collect unresolved TypeSlots from IR program
- Support resolve_to_untyped for gradual typing fallback
- Provide slot_summary for type coverage statistics
- Handle method parameters and return types
- Traverse ClassDef and ModuleDef for nested methods

Refs #47
- Add use_token_parser option to Parser.new for opt-in new parser
- Support TRUBY_NEW_PARSER=1 environment variable
- Implement parse_with_token_parser using TokenDeclarationParser
- Add backward-compatible legacy format conversion
- Mark legacy regex-based parser as @deprecated
- Add comprehensive facade tests

This enables gradual migration from regex-based parsing to
the new TokenDeclarationParser with TypeSlot support.

Refs #47
@yhk1038 yhk1038 merged commit 5059621 into main Jan 22, 2026
10 checks passed
@yhk1038 yhk1038 deleted the feature/parser-architecture branch January 22, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parser architecture improvement

1 participant