t-ruby 0.0.42 → 0.0.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -2
  3. data/bin/t-ruby +6 -0
  4. data/lib/t_ruby/ast_type_inferrer.rb +2 -0
  5. data/lib/t_ruby/benchmark.rb +1 -0
  6. data/lib/t_ruby/bundler_integration.rb +1 -0
  7. data/lib/t_ruby/cache.rb +40 -10
  8. data/lib/t_ruby/cli.rb +30 -8
  9. data/lib/t_ruby/compiler.rb +168 -0
  10. data/lib/t_ruby/diagnostic.rb +115 -0
  11. data/lib/t_ruby/diagnostic_formatter.rb +162 -0
  12. data/lib/t_ruby/doc_generator.rb +1 -0
  13. data/lib/t_ruby/docs_badge_generator.rb +1 -0
  14. data/lib/t_ruby/error_handler.rb +201 -35
  15. data/lib/t_ruby/error_reporter.rb +57 -0
  16. data/lib/t_ruby/ir.rb +53 -69
  17. data/lib/t_ruby/lsp_server.rb +40 -97
  18. data/lib/t_ruby/package_manager.rb +1 -0
  19. data/lib/t_ruby/parser.rb +18 -4
  20. data/lib/t_ruby/parser_combinator/combinators/alternative.rb +20 -0
  21. data/lib/t_ruby/parser_combinator/combinators/chain_left.rb +34 -0
  22. data/lib/t_ruby/parser_combinator/combinators/choice.rb +29 -0
  23. data/lib/t_ruby/parser_combinator/combinators/flat_map.rb +21 -0
  24. data/lib/t_ruby/parser_combinator/combinators/label.rb +22 -0
  25. data/lib/t_ruby/parser_combinator/combinators/lookahead.rb +21 -0
  26. data/lib/t_ruby/parser_combinator/combinators/many.rb +29 -0
  27. data/lib/t_ruby/parser_combinator/combinators/many1.rb +32 -0
  28. data/lib/t_ruby/parser_combinator/combinators/map.rb +17 -0
  29. data/lib/t_ruby/parser_combinator/combinators/not_followed_by.rb +21 -0
  30. data/lib/t_ruby/parser_combinator/combinators/optional.rb +21 -0
  31. data/lib/t_ruby/parser_combinator/combinators/sep_by.rb +34 -0
  32. data/lib/t_ruby/parser_combinator/combinators/sep_by1.rb +34 -0
  33. data/lib/t_ruby/parser_combinator/combinators/sequence.rb +23 -0
  34. data/lib/t_ruby/parser_combinator/combinators/skip_right.rb +23 -0
  35. data/lib/t_ruby/parser_combinator/declaration_parser.rb +147 -0
  36. data/lib/t_ruby/parser_combinator/dsl.rb +115 -0
  37. data/lib/t_ruby/parser_combinator/parse_error.rb +48 -0
  38. data/lib/t_ruby/parser_combinator/parse_result.rb +46 -0
  39. data/lib/t_ruby/parser_combinator/parser.rb +84 -0
  40. data/lib/t_ruby/parser_combinator/primitives/end_of_input.rb +16 -0
  41. data/lib/t_ruby/parser_combinator/primitives/fail.rb +16 -0
  42. data/lib/t_ruby/parser_combinator/primitives/lazy.rb +18 -0
  43. data/lib/t_ruby/parser_combinator/primitives/literal.rb +21 -0
  44. data/lib/t_ruby/parser_combinator/primitives/pure.rb +16 -0
  45. data/lib/t_ruby/parser_combinator/primitives/regex.rb +25 -0
  46. data/lib/t_ruby/parser_combinator/primitives/satisfy.rb +21 -0
  47. data/lib/t_ruby/parser_combinator/token/expression_parser.rb +541 -0
  48. data/lib/t_ruby/parser_combinator/token/statement_parser.rb +644 -0
  49. data/lib/t_ruby/parser_combinator/token/token_alternative.rb +20 -0
  50. data/lib/t_ruby/parser_combinator/token/token_body_parser.rb +54 -0
  51. data/lib/t_ruby/parser_combinator/token/token_declaration_parser.rb +962 -0
  52. data/lib/t_ruby/parser_combinator/token/token_dsl.rb +16 -0
  53. data/lib/t_ruby/parser_combinator/token/token_label.rb +22 -0
  54. data/lib/t_ruby/parser_combinator/token/token_many.rb +29 -0
  55. data/lib/t_ruby/parser_combinator/token/token_many1.rb +32 -0
  56. data/lib/t_ruby/parser_combinator/token/token_map.rb +17 -0
  57. data/lib/t_ruby/parser_combinator/token/token_matcher.rb +29 -0
  58. data/lib/t_ruby/parser_combinator/token/token_optional.rb +21 -0
  59. data/lib/t_ruby/parser_combinator/token/token_parse_result.rb +40 -0
  60. data/lib/t_ruby/parser_combinator/token/token_parser.rb +62 -0
  61. data/lib/t_ruby/parser_combinator/token/token_sep_by.rb +34 -0
  62. data/lib/t_ruby/parser_combinator/token/token_sep_by1.rb +34 -0
  63. data/lib/t_ruby/parser_combinator/token/token_sequence.rb +23 -0
  64. data/lib/t_ruby/parser_combinator/token/token_skip_right.rb +23 -0
  65. data/lib/t_ruby/parser_combinator/type_parser.rb +118 -0
  66. data/lib/t_ruby/parser_combinator.rb +64 -936
  67. data/lib/t_ruby/runner.rb +132 -0
  68. data/lib/t_ruby/scanner.rb +883 -0
  69. data/lib/t_ruby/version.rb +1 -1
  70. data/lib/t_ruby/watcher.rb +67 -75
  71. data/lib/t_ruby.rb +16 -1
  72. metadata +73 -4
  73. data/lib/t_ruby/body_parser.rb +0 -561
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+
5
+ module TRuby
6
+ # Thor-based CLI for t-ruby command
7
+ # Runs .trb files directly without generating intermediate files
8
+ class RunnerCLI < Thor
9
+ def self.exit_on_failure?
10
+ true
11
+ end
12
+
13
+ # Override Thor's default behavior to treat unknown arguments as the file to run
14
+ def self.start(given_args = ARGV, _config = {})
15
+ # Handle version flag
16
+ if given_args.include?("--version") || given_args.include?("-v")
17
+ new.version
18
+ return
19
+ end
20
+
21
+ # Handle help flag or no arguments
22
+ if given_args.empty? || given_args.include?("--help") || given_args.include?("-h")
23
+ new.help
24
+ return
25
+ end
26
+
27
+ # Treat first argument as file, rest as script arguments
28
+ file = given_args.first
29
+ args = given_args[1..] || []
30
+
31
+ runner = Runner.new
32
+ runner.run_file(file, args)
33
+ end
34
+
35
+ desc "FILE [ARGS...]", "Run a .trb file directly without generating files"
36
+ def run_file(file, *args)
37
+ runner = Runner.new
38
+ runner.run_file(file, args)
39
+ end
40
+
41
+ map %w[--version -v] => :version
42
+ desc "--version, -v", "Show version"
43
+ def version
44
+ puts "t-ruby #{VERSION}"
45
+ end
46
+
47
+ desc "--help, -h", "Show help"
48
+ def help
49
+ puts <<~HELP
50
+ t-ruby v#{VERSION} - Run T-Ruby files directly
51
+
52
+ Usage:
53
+ t-ruby <file.trb> Run a .trb file directly
54
+ t-ruby <file.trb> [args...] Run with arguments passed to the script
55
+ t-ruby --version, -v Show version
56
+ t-ruby --help, -h Show this help
57
+
58
+ Examples:
59
+ t-ruby hello.trb Run hello.trb
60
+ t-ruby server.trb 8080 Run with argument 8080
61
+ t-ruby script.trb foo bar Run with multiple arguments
62
+
63
+ Notes:
64
+ - No .rb or .rbs files are generated
65
+ - Type annotations are stripped at runtime
66
+ - Arguments after the file are passed to ARGV
67
+ HELP
68
+ end
69
+ end
70
+
71
+ # Runner class - executes T-Ruby code directly
72
+ # Can be used as a library or through RunnerCLI
73
+ class Runner
74
+ def initialize(config = nil)
75
+ @config = config || Config.new
76
+ @compiler = Compiler.new(@config)
77
+ end
78
+
79
+ # Run a .trb file directly
80
+ # @param input_path [String] Path to the .trb file
81
+ # @param argv [Array<String>] Arguments to pass to the script via ARGV
82
+ def run_file(input_path, argv = [])
83
+ unless File.exist?(input_path)
84
+ warn "Error: File not found: #{input_path}"
85
+ exit 1
86
+ end
87
+
88
+ source = File.read(input_path)
89
+ result = @compiler.compile_string(source)
90
+
91
+ if result[:errors].any?
92
+ result[:errors].each { |e| warn e }
93
+ exit 1
94
+ end
95
+
96
+ execute_ruby(result[:ruby], input_path, argv)
97
+ end
98
+
99
+ # Run T-Ruby source code from a string
100
+ # @param source [String] T-Ruby source code
101
+ # @param filename [String] Filename for error reporting
102
+ # @param argv [Array<String>] Arguments to pass via ARGV
103
+ # @return [Boolean] true if execution succeeded
104
+ def run_string(source, filename: "(t-ruby)", argv: [])
105
+ result = @compiler.compile_string(source)
106
+
107
+ if result[:errors].any?
108
+ result[:errors].each { |e| warn e }
109
+ return false
110
+ end
111
+
112
+ execute_ruby(result[:ruby], filename, argv)
113
+ true
114
+ end
115
+
116
+ private
117
+
118
+ # Execute Ruby code with proper script environment
119
+ # @param ruby_code [String] Ruby code to execute
120
+ # @param filename [String] Script filename (for $0 and stack traces)
121
+ # @param argv [Array<String>] Script arguments
122
+ def execute_ruby(ruby_code, filename, argv)
123
+ # Set up script environment
124
+ ARGV.replace(argv)
125
+ $0 = filename
126
+
127
+ # Execute using eval with filename and line number preserved
128
+ # This ensures stack traces point to the original .trb file
129
+ TOPLEVEL_BINDING.eval(ruby_code, filename, 1)
130
+ end
131
+ end
132
+ end