Class: MilkTea::Parser

Defined Under Namespace

Classes: ParseRecoveryResult

Constant Summary

Constants included from MilkTea::Parse::Recovery

MilkTea::Parse::Recovery::TOP_LEVEL_RECOVERY_START_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, path: nil) ⇒ Parser

Returns a new instance of Parser.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/milk_tea/core/parser.rb', line 65

def initialize(tokens, path: nil)
  @tokens = tokens.is_a?(SyntaxTokenStream) ? tokens : SyntaxTokenStream.new(tokens)
  @path = path
  @current = 0
  @known_type_names = {}
  @known_import_aliases = {}
  @known_generic_callable_names = {}
  @current_type_param_names = []
  @in_inline_block_body = false
  seed_known_names
end

Class Method Details

.parse(source = nil, path: nil, tokens: nil) ⇒ Object



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

def self.parse(source = nil, path: nil, tokens: nil)
  token_stream = tokens || Lexer.lex(source, path: path)
  new(token_stream, path: path).parse
end

.parse_collecting_errors(source = nil, path: nil, tokens: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/milk_tea/core/parser.rb', line 54

def self.parse_collecting_errors(source = nil, path: nil, tokens: nil)
  if tokens
    return new(tokens, path: path).parse_collecting_errors
  end

  lex_errors = []
  token_stream = Lexer.lex(source, path: path, recovery_errors: lex_errors)
  parse_result = new(token_stream, path: path).parse_collecting_errors
  ParseRecoveryResult.new(ast: parse_result.ast, errors: lex_errors + parse_result.errors)
end

Instance Method Details

#parseObject



77
78
79
# File 'lib/milk_tea/core/parser.rb', line 77

def parse
  parse_source_file
end

#parse_collecting_errorsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/milk_tea/core/parser.rb', line 81

def parse_collecting_errors
  errors = @recovery_errors ? @recovery_errors.dup : []
  previous_recovery_errors = @recovery_errors
  @recovery_errors = errors
  ast = parse_source_file(errors:)
  ParseRecoveryResult.new(ast:, errors:)
rescue ParseError => e
  errors ||= []
  errors << e
  ParseRecoveryResult.new(ast: nil, errors:)
ensure
  @recovery_errors = previous_recovery_errors
end