Module: MilkTea::Parse::Statements

Included in:
MilkTea::Parser
Defined in:
lib/milk_tea/core/parser/statements.rb

Instance Method Summary collapse

Instance Method Details

#check_inline_stmt_start?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
# File 'lib/milk_tea/core/parser/statements.rb', line 6

def check_inline_stmt_start?
  return false unless check(:inline)

  next_idx = @current + 1
  return false if next_idx >= @tokens.length

  next_token = @tokens[next_idx]
  %i[for while match if].include?(next_token.type)
end

#check_parallel_block_start?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/milk_tea/core/parser/statements.rb', line 25

def check_parallel_block_start?
  return false unless check(:parallel)

  next_idx = @current + 1
  return false if next_idx >= @tokens.length

  @tokens[next_idx].type == :colon
end

#check_parallel_for_start?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
# File 'lib/milk_tea/core/parser/statements.rb', line 16

def check_parallel_for_start?
  return false unless check(:parallel)

  next_idx = @current + 1
  return false if next_idx >= @tokens.length

  @tokens[next_idx].type == :for
end

#check_when_start?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/milk_tea/core/parser/statements.rb', line 34

def check_when_start?
  check(:when)
end

#inline_block_body?Boolean

Returns:

  • (Boolean)


267
268
269
# File 'lib/milk_tea/core/parser/statements.rb', line 267

def inline_block_body?
  check(:colon) && @current + 1 < @tokens.length && @tokens[@current + 1].type != :newline
end

#match_arm_expr_form?Boolean

Returns:

  • (Boolean)


384
385
386
# File 'lib/milk_tea/core/parser/statements.rb', line 384

def match_arm_expr_form?
  check(:colon) && @current + 1 < @tokens.length && @tokens[@current + 1].type != :newline
end

#parse_assignment_or_expression_stmtObject



739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/milk_tea/core/parser/statements.rb', line 739

def parse_assignment_or_expression_stmt
  line = peek.line
  expression = parse_expression
  if match(*Token::ASSIGNMENT_TYPES)
    operator = previous.lexeme
    column = previous.column
    value = parse_expression
    consume_end_of_statement unless block_expression?(value)
    AST::Assignment.new(target: expression, operator:, value:, line:, column:)
  else
    consume_end_of_statement unless block_expression?(expression)
    AST::ExpressionStmt.new(expression:, line:)
  end
end

#parse_break_stmtObject



541
542
543
544
545
546
# File 'lib/milk_tea/core/parser/statements.rb', line 541

def parse_break_stmt
  token = previous
  line = token.line
  consume_end_of_statement
  AST::BreakStmt.new(line:, column: token.column, length: token.lexeme.length)
end

#parse_continue_stmtObject



548
549
550
551
552
553
# File 'lib/milk_tea/core/parser/statements.rb', line 548

def parse_continue_stmt
  token = previous
  line = token.line
  consume_end_of_statement
  AST::ContinueStmt.new(line:, column: token.column, length: token.lexeme.length)
end

#parse_decl_when_armsObject



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/milk_tea/core/parser/statements.rb', line 597

def parse_decl_when_arms
  consume(:colon, "expected ':' before block")
  consume(:newline, "expected newline before block")
  consume(:indent, "expected indented block")

  arms = []
  skip_newlines
  until check(:dedent) || eof?
    pattern = parse_expression
    binding_name = if match(:as)
                      binding_token = consume_name("expected binding name after 'as'")
                      binding_token.lexeme
                    end
    body = parse_declaration_block
    binding_token ||= previous
    arms << AST::WhenBranch.new(
      pattern:,
      binding_name:,
      binding_line: binding_token.line,
      binding_column: binding_token.column,
      body:,
    )
    skip_newlines
  end

  consume(:dedent, "expected end of block")
  arms
end

#parse_defer_stmtObject



563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/milk_tea/core/parser/statements.rb', line 563

def parse_defer_stmt
  token = previous
  line = token.line
  if check(:colon)
    body = parse_block
    AST::DeferStmt.new(expression: nil, body:, line:, column: token.column, length: token.lexeme.length)
  else
    expression = parse_expression
    consume_end_of_statement unless block_expression?(expression)
    AST::DeferStmt.new(expression:, body: nil, line:, column: token.column, length: token.lexeme.length)
  end
end

#parse_destructure_patternObject



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/milk_tea/core/parser/statements.rb', line 180

def parse_destructure_pattern
  match(:lparen)
  bindings = []
  loop do
    token = consume_name("expected binding name in destructure pattern")
    bindings << token.lexeme
    break unless match(:comma)
  end
  consume(:rparen, "expected ')' after destructure pattern")
  bindings
end

#parse_else_branch_bodyObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/milk_tea/core/parser/statements.rb', line 247

def parse_else_branch_body
  if inline_block_body?
    consume(:colon, "expected ':' before else body")
    @in_inline_block_body = true
    result = [parse_statement]
    @in_inline_block_body = false
    result
  else
    parse_block
  end
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  raise unless recovered_body

  recovered_body
end

#parse_emit_stmtObject



417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/milk_tea/core/parser/statements.rb', line 417

def parse_emit_stmt
  line = previous.line
  column = previous.column
  decl = parse_declaration
  AST::EmitStmt.new(declaration: decl, line:, column:)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  synchronize_to_statement_boundary
  AST::EmitStmt.new(declaration: AST::ErrorExpr.new(message: e.message), line:, column:)
end

#parse_for_stmtObject



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/milk_tea/core/parser/statements.rb', line 430

def parse_for_stmt
  line = previous.line
  bindings = []
  iterables = nil
  loop do
    name_token = consume_name("expected loop variable name")
    bindings << AST::ForBinding.new(name: name_token.lexeme, line: name_token.line, column: name_token.column)
    break unless match(:comma)
  end
  consume(:in, "expected 'in' in for loop")
  iterables = [parse_expression]
  iterables << parse_expression while match(:comma)
  body = parse_block
  AST::ForStmt.new(bindings:, iterables:, body:, line:, column: bindings.first.column)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return recovery_error_block_stmt(
    e,
    recovered_body,
    header_type: :for,
    header_bindings: bindings.empty? ? nil : bindings,
    header_iterables: iterables&.any? ? iterables : nil,
  ) if recovered_body

  recovery_error_stmt(e)
end

#parse_gather_stmtObject



499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/milk_tea/core/parser/statements.rb', line 499

def parse_gather_stmt
  line = previous.line
  column = previous.column
  handles = []
  handle = parse_expression
  handles << handle
  while match(:comma)
    handle = parse_expression
    handles << handle
  end
  AST::GatherStmt.new(handles:, line:, column:)
end

#parse_if_branch(token) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/milk_tea/core/parser/statements.rb', line 212

def parse_if_branch(token)
  condition = nil
  condition = parse_expression
  body = if inline_block_body?
           consume(:colon, "expected ':' after if condition")
           @in_inline_block_body = true
           result = [parse_statement]
           @in_inline_block_body = false
           result
         else
           parse_block
         end
  AST::IfBranch.new(
    condition:,
    body:,
    line: token.line,
    column: token.column,
    length: token.lexeme.length,
  )
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  raise unless recovered_body

  AST::IfBranch.new(
    condition: condition || recovery_error_expr(e),
    body: recovered_body,
    line: token.line,
    column: token.column,
    length: token.lexeme.length,
  )
end

#parse_if_stmtObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/milk_tea/core/parser/statements.rb', line 192

def parse_if_stmt
  line = previous.line
  branches = [parse_if_branch(previous)]

  while check(:else) && check_next(:if)
    advance
    advance
    branches << parse_if_branch(previous)
  end

  else_line = nil
  else_column = nil
  else_body = if match(:else)
                else_line = previous.line
                else_column = previous.column
                parse_else_branch_body
              end
  AST::IfStmt.new(branches:, else_body:, line:, else_line:, else_column:)
end

#parse_inline_for_stmt(_inline_token) ⇒ Object



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/milk_tea/core/parser/statements.rb', line 662

def parse_inline_for_stmt(_inline_token)
  line = previous.line
  bindings = []
  iterables = nil
  loop do
    name_token = consume_name("expected loop variable name")
    bindings << AST::ForBinding.new(name: name_token.lexeme, line: name_token.line, column: name_token.column)
    break unless match(:comma)
  end
  consume(:in, "expected 'in' in for loop")
  iterables = [parse_expression]
  iterables << parse_expression while match(:comma)
  body = parse_block
  AST::ForStmt.new(bindings:, iterables:, body:, inline: true, line:, column: bindings.first.column)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return recovery_error_block_stmt(e, recovered_body, header_type: :for) if recovered_body

  recovery_error_stmt(e)
end

#parse_inline_if_stmt(inline_token) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/milk_tea/core/parser/statements.rb', line 724

def parse_inline_if_stmt(inline_token)
  token = previous
  line = token.line
  branches = [parse_if_branch(token)]

  while check(:else) && check_next(:if)
    advance
    advance
    branches << parse_if_branch(previous)
  end

  else_body = match(:else) ? parse_else_branch_body : nil
  AST::IfStmt.new(branches:, else_body:, inline: true, line:)
end

#parse_inline_match_stmt(inline_token) ⇒ Object



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/milk_tea/core/parser/statements.rb', line 707

def parse_inline_match_stmt(inline_token)
  token = previous
  line = token.line
  arms = []
  expression = parse_expression
  arms = parse_match_arms(arms)
  AST::MatchStmt.new(expression:, arms:, inline: true, line:, column: token.column, length: token.lexeme.length)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_arms = synchronize_to_match_arm_boundary
  return AST::MatchStmt.new(expression: expression || recovery_error_expr(e), arms: arms + recovered_arms, inline: true, line:, column: token.column, length: token.lexeme.length) if recovered_arms

  recovery_error_stmt(e)
end

#parse_inline_stmtObject



647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/milk_tea/core/parser/statements.rb', line 647

def parse_inline_stmt
  token = previous
  if match(:for)
    parse_inline_for_stmt(token)
  elsif match(:while)
    parse_inline_while_stmt(token)
  elsif match(:match)
    parse_inline_match_stmt(token)
  elsif match(:if)
    parse_inline_if_stmt(token)
  else
    raise error(peek, "expected for, while, match, or if after inline")
  end
end

#parse_inline_while_stmt(inline_token) ⇒ Object



686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/milk_tea/core/parser/statements.rb', line 686

def parse_inline_while_stmt(inline_token)
  line = inline_token.line
  condition = parse_expression
  body = parse_block
  AST::WhileStmt.new(condition:, body:, inline: true, line:, column: inline_token.column, length: inline_token.lexeme.length)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return AST::WhileStmt.new(
    condition: condition || recovery_error_expr(e),
    body: recovered_body,
    line:,
    column: inline_token.column,
    length: inline_token.lexeme.length,
  ) if recovered_body

  recovery_error_stmt(e)
end

#parse_local_decl(kind) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/milk_tea/core/parser/statements.rb', line 86

def parse_local_decl(kind)
  line = previous.line
  column = previous.column
  name_token = nil
  name = nil
  var_type = nil

  destructure_bindings = nil
  destructure_type_name = nil
  if check(:lparen)
    destructure_bindings = parse_destructure_pattern
  elsif check_name && check_next(:lparen)
    destructure_type_name = advance.lexeme
    destructure_bindings = parse_destructure_pattern
  elsif check_name && check_next(:dot)
    parts = [advance.lexeme]
    while check(:dot)
      advance
      parts << consume_name("expected type name after '.' in destructure pattern").lexeme
    end
    if match(:lparen)
      destructure_type_name = parts.length == 1 ? parts[0].dup : parts
      destructure_bindings = parse_destructure_pattern
    else
      raise error(@tokens[@current], "expected '(' after type name in destructure pattern")
    end
  else
    name_token = consume_name("expected local variable name")
    name = name_token.lexeme
  end

  var_type = match(:colon) ? parse_type_ref : nil
  value = nil
  else_binding = nil
  else_body = nil
  else_started = false

  if destructure_bindings
    consume(:equal, "expected '=' after destructure pattern")
    value = parse_expression
    consume_end_of_statement
  elsif match(:equal)
    value = parse_expression
    if match(:else)
      else_started = true

      if match(:as)
        binding_token = consume_name("expected error binding name after 'as'")
        else_binding = AST::Identifier.new(name: binding_token.lexeme, line: binding_token.line, column: binding_token.column)
      end

      else_body = parse_block
    else
      consume_end_of_statement unless block_expression?(value)
    end
  else
    raise error(name_token, "local declaration without initializer requires a type") unless var_type

    consume_end_of_statement
  end

  AST::LocalDecl.new(kind:, name:, type: var_type, value:, else_binding:, else_body:, line:, column: name_token&.column || column, destructure_bindings:, destructure_type_name:)
rescue ParseError => e
  raise unless @recovery_errors && name

  @recovery_errors << e
  synchronize_to_statement_boundary

  if else_started
    return AST::LocalDecl.new(
      kind:,
      name:,
      type: var_type,
      value:,
      else_binding:,
      else_body: nil,
      line:,
      column: name_token.column,
      recovered_else: true,
    )
  end

  AST::LocalDecl.new(
    kind:,
    name:,
    type: var_type,
    value: recovery_error_expr(e),
    else_binding: nil,
    else_body: nil,
    line:,
    column: name_token.column,
  )
end

#parse_match_armObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/milk_tea/core/parser/statements.rb', line 325

def parse_match_arm
  patterns = []
  binding_token = nil
  binding_name = nil

  if match(:else)
    patterns << AST::Identifier.new(name: "_", line: previous.line, column: previous.column)
  else
    patterns << parse_match_arm_pattern
    while match(:pipe)
      patterns << parse_match_arm_pattern
    end
  end
  binding_name = if match(:as)
                   binding_token = consume_name("expected binding name after 'as'")
                   binding_token.lexeme
                 end

  if match_arm_expr_form?
    consume(:colon, "expected ':' after match expression arm pattern")
    value = parse_expression
    consume_end_of_statement unless block_expression?(value)
    patterns.map do |pattern|
      AST::MatchExprArm.new(
        pattern:,
        binding_name:,
        binding_line: binding_token&.line,
        binding_column: binding_token&.column,
        value:,
      )
    end
  else
    body = parse_block
    patterns.map do |pattern|
      AST::MatchArm.new(
        pattern:,
        binding_name:,
        binding_line: binding_token&.line,
        binding_column: binding_token&.column,
        body:,
      )
    end
  end
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return [AST::MatchArm.new(
    pattern: patterns.first || recovery_error_expr(e),
    binding_name:,
    binding_line: binding_token&.line,
    binding_column: binding_token&.column,
    body: recovered_body,
  )] if recovered_body

  raise
end

#parse_match_arm_body(arms = []) ⇒ Object



315
316
317
318
319
320
321
322
323
# File 'lib/milk_tea/core/parser/statements.rb', line 315

def parse_match_arm_body(arms = [])
  skip_newlines
  until check(:dedent) || eof?
    arms.concat(parse_match_arm)
    skip_newlines
  end

  arms
end

#parse_match_arms(arms = []) ⇒ Object



304
305
306
307
308
309
310
311
312
313
# File 'lib/milk_tea/core/parser/statements.rb', line 304

def parse_match_arms(arms = [])
  consume(:colon, "expected ':' before block")
  consume(:newline, "expected newline before block")
  consume(:indent, "expected indented block")

  parse_match_arm_body(arms)

  consume(:dedent, "expected end of block")
  arms
end

#parse_match_stmtObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/milk_tea/core/parser/statements.rb', line 271

def parse_match_stmt
  token = previous
  line = token.line
  expression = nil
  arms = []
  expression = parse_expression
  arms = parse_match_arms(arms)
  if arms.first&.is_a?(AST::MatchExprArm)
    expr = AST::MatchExpr.new(expression:, arms:, line:, column: token.column, length: token.lexeme.length)
    AST::ExpressionStmt.new(expression: expr, line:)
  else
    AST::MatchStmt.new(expression:, arms:, line:, column: token.column, length: token.lexeme.length)
  end
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_arms = synchronize_to_match_arm_boundary
  target_line = line
  if recovered_arms
    stmt =
      if recovered_arms.first&.is_a?(AST::MatchExprArm)
        expr = AST::MatchExpr.new(expression: expression || recovery_error_expr(e), arms: arms + recovered_arms, line: target_line, column: token.column, length: token.lexeme.length)
        AST::ExpressionStmt.new(expression: expr, line: target_line)
      else
        AST::MatchStmt.new(expression: expression || recovery_error_expr(e), arms: arms + recovered_arms, line: target_line, column: token.column, length: token.lexeme.length)
      end
    return stmt
  end

  recovery_error_stmt(e)
end

#parse_parallel_block_stmtObject



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/milk_tea/core/parser/statements.rb', line 479

def parse_parallel_block_stmt
  line = previous.line
  column = previous.column
  consume(:colon, "expected ':' after 'parallel'")
  consume(:newline, "expected newline after 'parallel:'")
  consume(:indent, "expected indented block after 'parallel:'")
  statements = parse_statement_block_body
  consume(:dedent, "expected end of parallel block")
  bodies = statements.map { |stmt| [stmt] }
  AST::ParallelBlockStmt.new(bodies:, line:, column:)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return recovery_error_block_stmt(e, recovered_body, header_type: :parallel) if recovered_body

  recovery_error_stmt(e)
end

#parse_parallel_for_stmtObject



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/milk_tea/core/parser/statements.rb', line 460

def parse_parallel_for_stmt
  consume(:for, "expected 'for' after 'parallel'")
  line = previous.line
  name_token = consume_name("expected loop variable name")
  bindings = [AST::ForBinding.new(name: name_token.lexeme, line: name_token.line, column: name_token.column)]
  consume(:in, "expected 'in' in parallel for loop")
  iterables = [parse_expression]
  body = parse_block
  AST::ForStmt.new(bindings:, iterables:, body:, threaded: true, line:, column: bindings.first.column)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return recovery_error_block_stmt(e, recovered_body, header_type: :for) if recovered_body

  recovery_error_stmt(e)
end

#parse_pass_stmtObject



534
535
536
537
538
539
# File 'lib/milk_tea/core/parser/statements.rb', line 534

def parse_pass_stmt
  token = previous
  line = token.line
  consume_end_of_statement
  AST::PassStmt.new(line:, column: token.column, length: token.lexeme.length)
end

#parse_return_stmtObject



555
556
557
558
559
560
561
# File 'lib/milk_tea/core/parser/statements.rb', line 555

def parse_return_stmt
  token = previous
  line = token.line
  value = check(:newline) ? nil : parse_expression
  consume_end_of_statement unless block_expression?(value)
  AST::ReturnStmt.new(value:, line:, column: token.column, length: token.lexeme.length)
end

#parse_statementObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/milk_tea/core/parser/statements.rb', line 38

def parse_statement
  if match(:let)
    parse_local_decl(:let)
  elsif match(:var)
    parse_local_decl(:var)
  elsif match(:if)
    parse_if_stmt
  elsif match(:match)
    parse_match_stmt
  elsif match(:unsafe)
    parse_unsafe_stmt
  elsif match(:static_assert)
    parse_static_assert
  elsif match(:emit)
    parse_emit_stmt
  elsif match(:for)
    parse_for_stmt
  elsif check_parallel_for_start?
    advance
    parse_parallel_for_stmt
  elsif check_parallel_block_start?
    advance
    parse_parallel_block_stmt
  elsif match(:gather)
    parse_gather_stmt
  elsif match(:while)
    parse_while_stmt
  elsif match(:pass)
    parse_pass_stmt
  elsif match(:break)
    parse_break_stmt
  elsif match(:continue)
    parse_continue_stmt
  elsif match(:return)
    parse_return_stmt
  elsif match(:defer)
    parse_defer_stmt
  elsif check_inline_stmt_start?
    advance
    parse_inline_stmt
  elsif check_when_start?
    advance
    parse_when_stmt
  else
    parse_assignment_or_expression_stmt
  end
end

#parse_static_assertObject



406
407
408
409
410
411
412
413
414
415
# File 'lib/milk_tea/core/parser/statements.rb', line 406

def parse_static_assert
  line = previous.line
  consume(:lparen, "expected '(' after static_assert")
  condition = parse_expression
  consume(:comma, "expected ',' after static_assert condition")
  message = parse_expression
  consume(:rparen, "expected ')' after static_assert message")
  consume_end_of_statement
  AST::StaticAssert.new(condition:, message:, line:)
end

#parse_unsafe_stmtObject



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/milk_tea/core/parser/statements.rb', line 388

def parse_unsafe_stmt
  token = previous
  consume(:colon, "expected ':' after unsafe")
  body = if match(:newline)
           consume(:indent, "expected indented block")

           statements = parse_statement_block_body
           consume(:dedent, "expected end of block")
           statements
         else
           statement = parse_statement
           raise ParseError.new("inline unsafe local declarations must use expression form", token:, path: @path) if statement.is_a?(AST::LocalDecl)

           [statement]
         end
  AST::UnsafeStmt.new(body:, line: token.line, column: token.column, length: token.lexeme.length)
end

#parse_when_declObject



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/milk_tea/core/parser/statements.rb', line 576

def parse_when_decl
  token = previous
  line = token.line
  discriminant = parse_expression
  branches = parse_decl_when_arms
  else_body = if check(:else)
    if check_next(:newline) || check_next(:indent)
      parse_declaration_block
    end
  end
  AST::WhenStmt.new(discriminant:, branches:, else_body:, line:, column: token.column, length: token.lexeme.length)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return recovery_error_block_stmt(e, recovered_body, header_type: :when) if recovered_body

  recovery_error_stmt(e)
end

#parse_when_stmtObject



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/milk_tea/core/parser/statements.rb', line 626

def parse_when_stmt
  token = previous
  line = token.line
  discriminant = parse_expression
  branches = parse_match_arms([])
  else_body = if check(:else)
    if check_next(:newline) || check_next(:indent)
      parse_else_branch_body
    end
  end
  AST::WhenStmt.new(discriminant:, branches:, else_body:, line:, column: token.column, length: token.lexeme.length)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return recovery_error_block_stmt(e, recovered_body, header_type: :when) if recovered_body

  recovery_error_stmt(e)
end

#parse_while_stmtObject



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/milk_tea/core/parser/statements.rb', line 512

def parse_while_stmt
  token = previous
  line = token.line
  condition = parse_expression
  body = parse_block
  AST::WhileStmt.new(condition:, body:, line:, column: token.column, length: token.lexeme.length)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  recovered_body = synchronize_to_statement_boundary
  return AST::WhileStmt.new(
    condition: condition || recovery_error_expr(e),
    body: recovered_body,
    line:,
    column: token.column,
    length: token.lexeme.length,
  ) if recovered_body

  recovery_error_stmt(e)
end