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)


390
391
392
# File 'lib/milk_tea/core/parser/statements.rb', line 390

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

#parse_assignment_or_expression_stmtObject



747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/milk_tea/core/parser/statements.rb', line 747

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



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

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



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

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



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/milk_tea/core/parser/statements.rb', line 603

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:,
      line: pattern.line,
      column: pattern.column,
    )
    skip_newlines
  end

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

#parse_defer_stmtObject



569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/milk_tea/core/parser/statements.rb', line 569

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



423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/milk_tea/core/parser/statements.rb', line 423

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



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/milk_tea/core/parser/statements.rb', line 436

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



505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/milk_tea/core/parser/statements.rb', line 505

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



670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/milk_tea/core/parser/statements.rb', line 670

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



732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/milk_tea/core/parser/statements.rb', line 732

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



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/milk_tea/core/parser/statements.rb', line 715

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



655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/milk_tea/core/parser/statements.rb', line 655

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



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/milk_tea/core/parser/statements.rb', line 694

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
383
384
385
386
387
388
# 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:,
        line: pattern.line,
        column: pattern.column,
      )
    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:,
        line: pattern.line,
        column: pattern.column,
      )
    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,
    line: patterns.first&.line,
    column: patterns.first&.column,
  )] 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



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/milk_tea/core/parser/statements.rb', line 485

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



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/milk_tea/core/parser/statements.rb', line 466

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



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

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



561
562
563
564
565
566
567
# File 'lib/milk_tea/core/parser/statements.rb', line 561

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



412
413
414
415
416
417
418
419
420
421
# File 'lib/milk_tea/core/parser/statements.rb', line 412

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



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/milk_tea/core/parser/statements.rb', line 394

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



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/milk_tea/core/parser/statements.rb', line 582

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



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/milk_tea/core/parser/statements.rb', line 634

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



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/milk_tea/core/parser/statements.rb', line 518

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