Module: MilkTea::Parse::Declarations

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

Constant Summary collapse

DECL_KIND =
{
  var:        { method: :parse_var_decl,        reject_attrs: "var" },
  type:       { method: :parse_type_alias_decl, reject_attrs: "type" },
  union:      { method: :parse_union_decl },
  enum:       { method: :parse_enum_decl,       enum_class: AST::EnumDecl },
  flags:      { method: :parse_enum_decl,       enum_class: AST::FlagsDecl },
  opaque:     { method: :parse_opaque_decl,     reject_attrs: "opaque" },
  interface:  { method: :parse_interface_decl,  reject_attrs: "interface" },
  event:      { method: :parse_event_decl },
  attribute:  { method: :parse_attribute_decl,  reject_attrs: "attribute" },
}.freeze

Instance Method Summary collapse

Instance Method Details

#dispatch_decl_kind(kind, visibility:, attributes:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/milk_tea/core/parser/declarations.rb', line 33

def dispatch_decl_kind(kind, visibility:, attributes:)
  info = DECL_KIND.fetch(kind)
  reject_attributes!(attributes, info[:reject_attrs]) if info[:reject_attrs]

  if info[:enum_class]
    send(info[:method], info[:enum_class], visibility:, attributes:)
  elsif info[:reject_attrs]
    send(info[:method], visibility:)
  else
    send(info[:method], visibility:, attributes:)
  end
end

#extending_target_type_param_names(type_name) ⇒ Object



577
578
579
580
581
# File 'lib/milk_tea/core/parser/declarations.rb', line 577

def extending_target_type_param_names(type_name)
  type_name.arguments.flat_map do |argument|
    extending_target_type_param_names_from_argument(argument.value)
  end.uniq
end

#extending_target_type_param_names_from_argument(value) ⇒ Object



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

def extending_target_type_param_names_from_argument(value)
  case value
  when AST::TypeRef
    nested_names = value.arguments.flat_map do |argument|
      extending_target_type_param_names_from_argument(argument.value)
    end
    if value.name.parts.length == 1 && value.arguments.empty? && !value.nullable
      name = value.name.parts.first
      nested_names << name unless known_type_like_name?(name)
    end
    nested_names
  when AST::FunctionType, AST::ProcType
    value.params.flat_map { |param| extending_target_type_param_names_from_argument(param.type) } +
      extending_target_type_param_names_from_argument(value.return_type)
  else
    []
  end
end

#parse_attribute_decl(visibility: :private) ⇒ Object



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
# File 'lib/milk_tea/core/parser/declarations.rb', line 346

def parse_attribute_decl(visibility: :private)
  line = previous.line
  consume(:lbracket, "expected '[' after attribute")
  targets = parse_comma_separated_until(:rbracket) do
    target_token = consume_path_component("expected attribute target")
    case target_token.lexeme
    when "struct"        then :struct
    when "field"         then :field
    when "callable"      then :callable
    when "const"         then :const
    when "event"         then :event
    when "enum"          then :enum
    when "flags"         then :flags
    when "union"         then :union
    when "variant"       then :variant
    else
      raise error(target_token, "unknown attribute target #{target_token.lexeme}")
    end
  end
  consume(:rbracket, "expected ']' after attribute targets")
  name_token = consume_name_allowing_keywords("expected attribute name")
  params = check(:lparen) ? parse_signature_params : []
  consume_end_of_statement
  AST::AttributeDecl.new(name: name_token.lexeme, targets:, params:, visibility:, line:, column: name_token.column)
end

#parse_block_body_safeObject



691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/milk_tea/core/parser/declarations.rb', line 691

def parse_block_body_safe
  parse_block
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  match(:newline)
  if match(:indent)
    begin
      parse_and_dedent_block_body || []
    rescue ParseError => e2
      @recovery_errors << e2
      []
    end
  end
end

#parse_callable_signature(allow_type_params: true, generic_error_token: nil, generic_error_message: nil, allow_body: true) ⇒ Object



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

def parse_callable_signature(allow_type_params: true, generic_error_token: nil, generic_error_message: nil, allow_body: true)
  type_params = parse_declaration_type_params
  if !allow_type_params && type_params.any?
    raise error(generic_error_token || previous, generic_error_message || "generic callable declarations are not allowed here")
  end

  params = nil
  return_type = nil
  body = nil

  with_type_param_names(type_params.map(&:name)) do
    params = parse_params
    return_type = match(:arrow) ? parse_type_ref : nil
    if allow_body
      body = parse_block_body_safe
    else
      consume_end_of_statement
    end
  end

  [type_params, params, return_type, body]
end

#parse_compiler_flag_directiveObject



198
199
200
201
202
# File 'lib/milk_tea/core/parser/declarations.rb', line 198

def parse_compiler_flag_directive
  value = consume(:string, "expected string literal after compiler_flag").literal
  consume_end_of_statement
  AST::CompilerFlagDirective.new(value:)
end

#parse_const_decl(visibility: :private, attributes: []) ⇒ Object



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
# File 'lib/milk_tea/core/parser/declarations.rb', line 271

def parse_const_decl(visibility: :private, attributes: [])
  line = previous.line
  name = nil
  type = nil
  name_token = consume_name("expected constant name")
  name = name_token.lexeme
  if match(:arrow)
    type = parse_type_ref
    body = parse_block
    return AST::ConstDecl.new(name:, type:, value: nil, block_body: body, visibility:, attributes:, line:, column: name_token.column)
  end

  consume(:colon, "expected ':' after constant name")
  type = parse_type_ref
  consume(:equal, "expected '=' after constant type")
  value = parse_expression
  consume_end_of_statement
  AST::ConstDecl.new(name:, type:, value:, visibility:, attributes:, line:, column: name_token.column)
rescue ParseError => e
  raise unless @recovery_errors && name

  @recovery_errors << e
  synchronize_to_statement_boundary
  AST::ConstDecl.new(name:, type: type || recovery_error_expr(e), value: recovery_error_expr(e), visibility:, attributes:, line:, column: name_token.column)
end

#parse_const_or_function_decl(visibility:, attributes:) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/milk_tea/core/parser/declarations.rb', line 46

def parse_const_or_function_decl(visibility:, attributes:)
  if match(:function)
    parse_function_def(visibility:, const: true, attributes:)
  else
    parse_const_decl(visibility:, attributes:)
  end
end

#parse_declarationObject



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
85
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
# File 'lib/milk_tea/core/parser/declarations.rb', line 54

def parse_declaration
  attributes = parse_attribute_applications
  visibility, visibility_token = parse_visibility

  if builtin_attribute_identifier?(peek)
    raise error(peek, "layout modifiers must use attributes like @[packed] or @[align(...)]")
  elsif match(:attribute)
    dispatch_decl_kind(:attribute, visibility:, attributes:)
  elsif match(:const)
    parse_const_or_function_decl(visibility:, attributes:)
  elsif match(:var)
    dispatch_decl_kind(:var, visibility:, attributes:)
  elsif match(:event)
    dispatch_decl_kind(:event, visibility:, attributes:)
  elsif match(:type)
    dispatch_decl_kind(:type, visibility:, attributes:)
  elsif match(:struct)
    parse_struct_decl(visibility:, attributes:)
  elsif match(:union)
    dispatch_decl_kind(:union, visibility:, attributes:)
  elsif match(:enum)
    dispatch_decl_kind(:enum, visibility:, attributes:)
  elsif match(:flags)
    dispatch_decl_kind(:flags, visibility:, attributes:)
  elsif match(:variant)
    parse_variant_decl(visibility:, attributes:)
  elsif match(:interface)
    dispatch_decl_kind(:interface, visibility:, attributes:)
  elsif match(:opaque)
    dispatch_decl_kind(:opaque, visibility:, attributes:)
  elsif match(:extending)
    reject_attributes!(attributes, "extending")
    raise error(visibility_token, "public is not allowed on extending blocks") if visibility == :public

    parse_extending_block
  elsif check(:editable) || check(:static)
    reject_attributes!(attributes, "standalone method")
    raise error(peek, "#{peek.lexeme} function is only allowed inside extending blocks")
  elsif match(:foreign)
    parse_foreign_decl(visibility:, attributes:)
  elsif match(:async)
    consume(:function, "expected function after async")
    parse_function_def(visibility:, async: true, attributes:)
  elsif match(:function)
    parse_function_def(visibility:, attributes:)
  elsif match(:external)
    raise error(visibility_token, "public is not allowed on external declarations") if visibility == :public

    parse_extern_decl(attributes:)
  elsif match(:static_assert)
    reject_attributes!(attributes, "static_assert")
    raise error(visibility_token, "public is not allowed on static_assert") if visibility == :public

    parse_static_assert
  elsif check_when_start?
    reject_attributes!(attributes, "when")
    raise error(visibility_token, "public is not allowed on when") if visibility == :public

    advance
    parse_when_decl
  else
    message = visibility == :public ? "expected exportable declaration after public" : "expected declaration"
    raise error(peek, message)
  end
end

#parse_enum_decl(node_class, visibility: :private, attributes: []) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/milk_tea/core/parser/declarations.rb', line 482

def parse_enum_decl(node_class, visibility: :private, attributes: [])
  line = previous.line
  name_token = consume_name("expected declaration name")
  name = name_token.lexeme
  consume(:colon, "expected ':' after declaration name")
  backing_type = if check(:newline) || check(:indent)
                   AST::TypeRef.new(name: AST::QualifiedName.new(parts: ["int"], type_arguments: []), arguments: [], nullable: false, lifetime: nil, line: line, column: name.length + 1)
                 else
                   parse_type_ref
                 end
  skip_newlines
  consume(:indent, "expected indented declaration body")

  members = []
  skip_newlines
  until check(:dedent) || eof?
    member_token = consume_name_allowing_keywords("expected member name")
    member_name = member_token.lexeme
    if match(:equal)
      value = parse_expression
    else
      value = nil
    end
    consume_end_of_statement
    members << AST::EnumMember.new(name: member_name, value:, line: member_token.line, column: member_token.column)
    skip_newlines
  end

  consume(:dedent, "expected end of declaration body")
  node_class.new(name:, backing_type:, members:, visibility:, attributes:, line:, column: name_token.column)
end

#parse_event_decl(visibility: :private, attributes: []) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/milk_tea/core/parser/declarations.rb', line 321

def parse_event_decl(visibility: :private, attributes: [])
  line = previous.line
  name_token = consume_name("expected event name")
  consume(:lbracket, "expected '[' after event name")
  capacity = consume(:integer, "expected positive integer capacity").literal
  consume(:rbracket, "expected ']' after event capacity")
  payload_type = nil
  if match(:lparen)
    payload_type = parse_type_ref
    consume(:rparen, "expected ')' after event payload type")
  end
  consume_end_of_statement
  AST::EventDecl.new(name: name_token.lexeme, capacity:, payload_type:, visibility:, attributes:, line:, column: name_token.column)
end

#parse_extending_blockObject



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

def parse_extending_block
  line = previous.line
  type_name = parse_type_ref
  receiver_type_param_names = extending_target_type_param_names(type_name)
  methods = with_type_param_names(receiver_type_param_names) do
    parse_named_block do
      method_attributes = parse_attribute_applications
      parse_method_def(attributes: method_attributes)
    end
  end
  AST::ExtendingBlock.new(type_name:, methods:, line:, column: type_name.column)
end

#parse_extern_decl(attributes: []) ⇒ Object



714
715
716
717
# File 'lib/milk_tea/core/parser/declarations.rb', line 714

def parse_extern_decl(attributes: [])
  consume(:function, "expected function after external")
  parse_extern_function_decl(attributes:)
end

#parse_extern_function_decl(attributes: []) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/milk_tea/core/parser/declarations.rb', line 724

def parse_extern_function_decl(attributes: [])
  line = previous.line
  name = consume_name("expected function name").lexeme
  type_params = parse_declaration_type_params
  params = nil
  variadic = false
  return_type = nil
  mapping = nil
  with_type_param_names(type_params.map(&:name)) do
    params, variadic = parse_foreign_params(allow_variadic: true)
    consume(:arrow, "expected '->' before external function return type")
    return_type = parse_type_ref
    if match(:equal)
      mapping = parse_expression
    end
  end
  consume_end_of_statement
  AST::ExternFunctionDecl.new(name:, type_params:, params:, return_type:, variadic:, attributes:, line:, mapping:)
end

#parse_foreign_decl(visibility: :private, attributes: []) ⇒ Object



719
720
721
722
# File 'lib/milk_tea/core/parser/declarations.rb', line 719

def parse_foreign_decl(visibility: :private, attributes: [])
  consume(:function, "expected function after foreign")
  parse_foreign_function_decl(visibility:, attributes:)
end

#parse_foreign_function_decl(visibility: :private, attributes: []) ⇒ Object



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

def parse_foreign_function_decl(visibility: :private, attributes: [])
  line = previous.line
  name = consume_name("expected function name").lexeme
  type_params = parse_declaration_type_params
  params = nil
  variadic = false
  return_type = nil
  mapping = nil
  with_type_param_names(type_params.map(&:name)) do
    params, variadic = parse_foreign_params(allow_variadic: true)
    consume(:arrow, "expected '->' before foreign function return type")
    return_type = parse_type_ref
    consume(:equal, "expected '=' before foreign function mapping")
    mapping = parse_expression
  end
  consume_end_of_statement
  AST::ForeignFunctionDecl.new(name:, type_params:, params:, return_type:, variadic:, mapping:, visibility:, attributes:, line:)
end

#parse_function_def(visibility: :private, async: false, const: false, attributes: []) ⇒ Object



602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/milk_tea/core/parser/declarations.rb', line 602

def parse_function_def(visibility: :private, async: false, const: false, attributes: [])
  line = previous.line
  name_token = consume_name("expected function name")
  name = name_token.lexeme
  type_params, params, return_type, body = parse_callable_signature
  AST::FunctionDef.new(name:, type_params:, params:, return_type:, body:, visibility:, async:, const:, attributes:, line:, column: name_token.column)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  AST::FunctionDef.new(name:, type_params: [], params: [], return_type: nil, body: nil, visibility:, async:, const:, attributes:, line:, column: name_token.column)
end

#parse_importObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/milk_tea/core/parser/declarations.rb', line 6

def parse_import
  line = previous.line
  path = parse_qualified_name
  local_name = path.parts.last
  local_column = previous.column
  alias_name = if match(:as)
                 alias_token = consume_name("expected import alias")
                 local_name = alias_token.lexeme
                 local_column = alias_token.column
                 alias_token.lexeme
               end
  consume_end_of_statement
  AST::Import.new(path:, alias_name:, line:, column: local_column, length: local_name.length)
end

#parse_include_directiveObject



192
193
194
195
196
# File 'lib/milk_tea/core/parser/declarations.rb', line 192

def parse_include_directive
  value = consume(:string, "expected string literal after include").literal
  consume_end_of_statement
  AST::IncludeDirective.new(value:)
end

#parse_interface_decl(visibility: :private) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
# File 'lib/milk_tea/core/parser/declarations.rb', line 552

def parse_interface_decl(visibility: :private)
  line = previous.line
  name_token = consume_name("expected interface name")
  name = name_token.lexeme
  type_params = parse_declaration_type_params
  methods = parse_named_block do
    method_attributes = parse_attribute_applications
    parse_interface_method_decl(attributes: method_attributes)
  end
  AST::InterfaceDecl.new(name:, type_params:, methods:, visibility:, line:, column: name_token.column)
end

#parse_interface_method_decl(attributes: []) ⇒ Object



630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/milk_tea/core/parser/declarations.rb', line 630

def parse_interface_method_decl(attributes: [])
  visibility, visibility_token, async, kind, line, name_token = parse_method_like_decl_head
  raise error(visibility_token, "public is not allowed on interface methods") if visibility == :public

  name = name_token.lexeme
  _type_params, params, return_type, _body = parse_callable_signature(
    allow_type_params: false,
    generic_error_token: name_token,
    generic_error_message: "interface method #{name} cannot be generic",
    allow_body: false
  )
  AST::InterfaceMethodDecl.new(name:, params:, return_type:, kind:, async:, attributes:, line:, column: name_token.column)
end


186
187
188
189
190
# File 'lib/milk_tea/core/parser/declarations.rb', line 186

def parse_link_directive
  value = consume(:string, "expected string literal after link").literal
  consume_end_of_statement
  AST::LinkDirective.new(value:)
end

#parse_method_def(attributes: []) ⇒ Object



615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/milk_tea/core/parser/declarations.rb', line 615

def parse_method_def(attributes: [])
  visibility, _visibility_token, async, kind, line, name_token = parse_method_like_decl_head
  name = name_token.lexeme
  type_params, params, return_type, body = parse_callable_signature
  AST::MethodDef.new(name:, type_params:, params:, return_type:, body:, kind:, visibility:, async:, attributes:, line:, column: name_token.column)
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  name = name_token&.lexeme || "unknown"
  col = name_token&.column || 1
  advance until eof? || check(:function) || check(:dedent)
  AST::MethodDef.new(name:, type_params: [], params: [], return_type: nil, body: nil, kind: kind || :plain, visibility: visibility || :private, async: async || false, attributes:, line:, column: col)
end

#parse_method_kindObject



661
662
663
664
665
666
# File 'lib/milk_tea/core/parser/declarations.rb', line 661

def parse_method_kind
  return :editable if match(:editable)
  return :static if match(:static)

  :plain
end

#parse_method_like_decl_headObject



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

def parse_method_like_decl_head
  visibility, visibility_token = parse_visibility
  async = match(:async)
  kind = parse_method_kind
  if check(:function)
    consume(:function, "expected function declaration")
  elsif check(:identifier)
    bad_token = advance
    @recovery_errors << ParseError.new("unknown keyword '#{bad_token.lexeme}'; expected 'function' (did you mean 'editable', 'static', or 'async'?)", token: bad_token, path: @path) if @recovery_errors
  else
    raise error(peek, "expected function declaration")
  end
  line = previous.line
  name_token = consume_name("expected function name")
  [visibility, visibility_token, async, kind, line, name_token]
end

#parse_opaque_decl(visibility: :private) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/milk_tea/core/parser/declarations.rb', line 539

def parse_opaque_decl(visibility: :private)
  line = previous.line
  name_token = consume_name("expected opaque type name")
  name = name_token.lexeme
  implements = parse_implements_clause
  c_name = nil
  if match(:equal)
    c_name = consume(:cstring, "expected C string literal after '='").literal
  end
  consume_end_of_statement
  AST::OpaqueDecl.new(name:, implements:, c_name:, visibility:, line:, column: name_token.column)
end

#parse_optional_explicit_c_nameObject



476
477
478
479
480
# File 'lib/milk_tea/core/parser/declarations.rb', line 476

def parse_optional_explicit_c_name
  return nil unless match(:equal)

  consume(:cstring, "expected C string literal after '='").literal
end

#parse_raw_module_body(errors: nil) ⇒ Object



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
# File 'lib/milk_tea/core/parser/declarations.rb', line 120

def parse_raw_module_body(errors: nil)
  imports = []
  directives = []
  declarations = []

  while match(:import)
    if errors
      begin
        imports << parse_import
      rescue ParseError => e
        errors << e
        synchronize_to_top_level_boundary
      end
    else
      imports << parse_import
    end
    skip_newlines
  end

  while raw_module_directive_start?
    if errors
      begin
        directives << parse_raw_module_directive
      rescue ParseError => e
        errors << e
        synchronize_to_top_level_boundary
      end
    else
      directives << parse_raw_module_directive
    end
    skip_newlines
  end

  until eof?
    if errors
      begin
        declarations << parse_raw_module_declaration
      rescue ParseError => e
        errors << e
        synchronize_to_top_level_boundary
      end
    else
      declarations << parse_raw_module_declaration
    end
    skip_newlines
  end

  [imports, directives, declarations]
end

#parse_raw_module_declarationObject



204
205
206
207
208
209
210
211
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
246
247
248
249
250
251
252
# File 'lib/milk_tea/core/parser/declarations.rb', line 204

def parse_raw_module_declaration
  attributes = parse_attribute_applications

  if match(:public)
    reject_attributes!(attributes)
    raise error(previous, "public is not allowed in external files")
  elsif builtin_attribute_identifier?(peek)
    raise error(peek, "layout modifiers must use attributes like @[packed] or @[align(...)]")
  elsif match(:attribute)
    reject_attributes!(attributes)
    raise error(previous, "attribute is not allowed in external files")
  elsif match(:const)
    reject_attributes!(attributes)
    if match(:function)
      parse_function_def(visibility:, const: true, attributes:)
    else
      parse_const_decl(visibility: nil)
    end
  elsif match(:event)
    reject_attributes!(attributes)
    raise error(previous, "event is not allowed in external files")
  elsif match(:type)
    reject_attributes!(attributes)
    parse_type_alias_decl(visibility: nil)
  elsif match(:struct)
    parse_struct_decl(visibility: nil, attributes:)
  elsif match(:union)
    reject_attributes!(attributes)
    parse_union_decl(visibility: nil)
  elsif match(:enum)
    reject_attributes!(attributes)
    parse_enum_decl(AST::EnumDecl, visibility: nil)
  elsif match(:flags)
    reject_attributes!(attributes)
    parse_enum_decl(AST::FlagsDecl, visibility: nil)
  elsif match(:opaque)
    reject_attributes!(attributes)
    parse_opaque_decl(visibility: nil)
  elsif match(:external)
    reject_attributes!(attributes)
    parse_extern_decl(attributes:)
  elsif check_when_start?
    reject_attributes!(attributes)
    advance
    parse_when_stmt
  else
    raise error(peek, raw_module_declaration_error_message(peek))
  end
end

#parse_raw_module_directiveObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/milk_tea/core/parser/declarations.rb', line 174

def parse_raw_module_directive
  if match(:link)
    parse_link_directive
  elsif match(:include)
    parse_include_directive
  elsif match(:compiler_flag)
    parse_compiler_flag_directive
  else
    raise error(peek, "expected external directive")
  end
end

#parse_struct_decl(packed: false, alignment: nil, visibility: :private, attributes: []) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/milk_tea/core/parser/declarations.rb', line 372

def parse_struct_decl(packed: false, alignment: nil, visibility: :private, attributes: [])
  line = previous.line
  name_token = consume_name("expected struct name")
  name = name_token.lexeme
  lifetime_params, type_params = parse_struct_decl_params
  implements = parse_implements_clause
  c_name = parse_optional_explicit_c_name
  packed, alignment = parse_struct_layout_attributes(attributes) if attributes.any?
  members = parse_named_block do
    parse_struct_member
  end
  fields = members.filter_map { |kind, member| member if kind == :field }
  events = members.filter_map { |kind, member| member if kind == :event }
  nested_types = members.filter_map { |kind, member| member if kind == :nested_type }
  AST::StructDecl.new(name:, type_params:, implements:, c_name:, fields:, events:, nested_types:, attributes:, packed:, alignment:, visibility:, lifetime_params:, line:, column: name_token.column)
end

#parse_struct_decl_paramsObject



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/milk_tea/core/parser/declarations.rb', line 389

def parse_struct_decl_params
  return [[], []] unless match(:lbracket)

  lifetime_params = []
  type_params = []

  loop do
    break if check(:rbracket)

    if match(:at)
      name_token = consume_name_allowing_keywords("expected lifetime name after @")
      lifetime_params << "@#{name_token.lexeme}"
    else
      name_token = consume_name_allowing_keywords("expected type parameter name")
      if match(:colon)
        value_type = parse_type_ref
        type_params << AST::ValueTypeParam.new(
          name: name_token.lexeme,
          type: value_type,
          line: name_token.line,
          column: name_token.column,
          length: name_token.lexeme.length,
        )
      else
        constraints = parse_type_param_constraints
        type_params << AST::TypeParam.new(
          name: name_token.lexeme,
          constraints:,
          line: name_token.line,
          column: name_token.column,
          length: name_token.lexeme.length,
        )
      end
    end

    break unless match(:comma)
    next if check(:rbracket)
  end

  consume(:rbracket, "expected ']' after struct parameters")
  [lifetime_params, type_params]
end

#parse_struct_memberObject



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
459
# File 'lib/milk_tea/core/parser/declarations.rb', line 432

def parse_struct_member
  field_attributes = parse_attribute_applications
  visibility, visibility_token = parse_visibility

  if match(:event)
    return [:event, parse_event_decl(visibility:, attributes: field_attributes)]
  end

  if match(:struct)
    return [:nested_type, parse_struct_decl(visibility:, attributes: field_attributes)]
  end

  raise error(visibility_token, "public is only allowed on struct events") if visibility == :public

  field_token = consume_name_allowing_keywords("expected field name")
  field_name = field_token.lexeme
  consume(:colon, "expected ':' after field name")
  field_type = parse_type_ref
  consume_end_of_statement
  [:field, AST::Field.new(name: field_name, type: field_type, attributes: field_attributes, line: field_token.line, column: field_token.column)]
rescue ParseError => e
  raise unless @recovery_errors

  @recovery_errors << e
  synchronize_to_statement_boundary
  field_name_error = (field_name if defined?(field_name)) || "error"
  [:field, AST::Field.new(name: field_name_error, type: recovery_error_expr(e), attributes: field_attributes, line: e.token&.line || 1, column: e.token&.column || 1)]
end

#parse_type_alias_decl(visibility: :private) ⇒ Object



336
337
338
339
340
341
342
343
344
# File 'lib/milk_tea/core/parser/declarations.rb', line 336

def parse_type_alias_decl(visibility: :private)
  line = previous.line
  name_token = consume_name("expected type alias name")
  name = name_token.lexeme
  consume(:equal, "expected '=' after type alias name")
  target = parse_type_ref
  consume_end_of_statement
  AST::TypeAliasDecl.new(name:, target:, visibility:, line:, column: name_token.column)
end

#parse_union_decl(visibility: :private, attributes: []) ⇒ Object



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

def parse_union_decl(visibility: :private, attributes: [])
  line = previous.line
  name_token = consume_name("expected union name")
  name = name_token.lexeme
  c_name = parse_optional_explicit_c_name
  fields = parse_named_block do
                 field_name = consume_name_allowing_keywords("expected field name").lexeme
    consume(:colon, "expected ':' after field name")
    field_type = parse_type_ref
    consume_end_of_statement
    AST::Field.new(name: field_name, type: field_type)
  end
  AST::UnionDecl.new(name:, c_name:, fields:, visibility:, attributes:, line:, column: name_token.column)
end

#parse_var_decl(visibility: :private) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/milk_tea/core/parser/declarations.rb', line 297

def parse_var_decl(visibility: :private)
  line = previous.line
  name = nil
  var_type = nil
  name_token = consume_name("expected variable name")
  name = name_token.lexeme
  var_type = match(:colon) ? parse_type_ref : nil
  value = if match(:equal)
            parse_expression
          else
            raise error(name_token, "module variable without initializer requires a type") unless var_type

            nil
          end
  consume_end_of_statement
  AST::VarDecl.new(name:, type: var_type, value:, visibility:, line:, column: name_token.column)
rescue ParseError => e
  raise unless @recovery_errors && name

  @recovery_errors << e
  synchronize_to_statement_boundary
  AST::VarDecl.new(name:, type: var_type || recovery_error_expr(e), value: recovery_error_expr(e), visibility:, line:, column: name_token.column)
end

#parse_variant_decl(visibility: :private, attributes: []) ⇒ Object



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

def parse_variant_decl(visibility: :private, attributes: [])
  line = previous.line
  name_token = consume_name("expected variant name")
  name = name_token.lexeme
  type_params = parse_declaration_type_params
  arms = parse_named_block do
    arm_name = consume_name_allowing_keywords("expected variant arm name").lexeme
    fields = if match(:lparen)
               parsed = parse_comma_separated_until(:rparen) do
    field_name = consume_name_allowing_keywords("expected field name").lexeme
                 consume(:colon, "expected ':' after field name")
                 field_type = parse_type_ref
                 AST::Field.new(name: field_name, type: field_type)
               end
               consume(:rparen, "expected ')' after variant arm fields")
               parsed
             else
               []
             end
    consume_end_of_statement
    AST::VariantArm.new(name: arm_name, fields:)
  end
  AST::VariantDecl.new(name:, type_params:, arms:, visibility:, attributes:, line:, column: name_token.column)
end

#parse_visibilityObject



708
709
710
711
712
# File 'lib/milk_tea/core/parser/declarations.rb', line 708

def parse_visibility
  return [:public, previous] if match(:public)

  [:private, nil]
end

#raw_module_declaration_error_message(token) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/milk_tea/core/parser/declarations.rb', line 254

def raw_module_declaration_error_message(token)
  case token.type
  when :import
    "imports must appear before external directives and declarations"
  when :link, :include, :compiler_flag
    "#{token.lexeme} directives must appear before external declarations"
  when :attribute, :event, :var, :variant, :interface, :extending, :foreign, :function, :static_assert
    "#{token.lexeme} is not allowed in external files"
  when :async
    "async function is not allowed in external files"
  when :module
    "module headers are not allowed in external files"
  else
    "expected external declaration"
  end
end

#raw_module_directive_start?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/milk_tea/core/parser/declarations.rb', line 170

def raw_module_directive_start?
  check(:link) || check(:include) || check(:compiler_flag)
end