Class: TRuby::ParserCombinator::StatementParser

Inherits:
Object
  • Object
show all
Includes:
TokenDSL
Defined in:
lib/t_ruby/parser_combinator/token/statement_parser.rb

Overview

Statement Parser - Parse statements into IR nodes

Instance Method Summary collapse

Methods included from TokenDSL

#keyword, #token

Constructor Details

#initializeStatementParser

Returns a new instance of StatementParser.



9
10
11
# File 'lib/t_ruby/parser_combinator/token/statement_parser.rb', line 9

def initialize
  @expression_parser = ExpressionParser.new
end

Instance Method Details

#parse_block(tokens, position = 0) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/t_ruby/parser_combinator/token/statement_parser.rb', line 43

def parse_block(tokens, position = 0)
  statements = []

  loop do
    position = skip_newlines(tokens, position)
    break if position >= tokens.length

    token = tokens[position]
    break if token.type == :eof
    break if i[end else elsif when rescue ensure].include?(token.type)

    result = parse_statement(tokens, position)
    break if result.failure?

    statements << result.value
    position = result.position
  end

  node = IR::Block.new(statements: statements)
  TokenParseResult.success(node, tokens, position)
end

#parse_statement(tokens, position = 0) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/t_ruby/parser_combinator/token/statement_parser.rb', line 13

def parse_statement(tokens, position = 0)
  return TokenParseResult.failure("End of input", tokens, position) if position >= tokens.length

  # Skip newlines
  position = skip_newlines(tokens, position)
  return TokenParseResult.failure("End of input", tokens, position) if position >= tokens.length

  token = tokens[position]

  case token.type
  when :return
    parse_return(tokens, position)
  when :if
    parse_if(tokens, position)
  when :unless
    parse_unless(tokens, position)
  when :while
    parse_while(tokens, position)
  when :until
    parse_until(tokens, position)
  when :case
    parse_case(tokens, position)
  when :begin
    parse_begin(tokens, position)
  else
    # Could be assignment or expression
    parse_assignment_or_expression(tokens, position)
  end
end