Module: Ibex::Frontend::GeneratedParserIncludes

Included in:
GeneratedParserBase
Defined in:
lib/ibex/frontend/generated_parser_includes.rb,
sig/ibex/frontend/generated_parser_includes.rbs

Overview

Semantic builders for fragment and include frontend nodes.

Instance Method Summary collapse

Instance Method Details

#build_fragment(keyword, declarations, rules, user_code) ⇒ AST::Fragment

RBS:

  • (Token keyword, Array[AST::declaration] declarations, Array[AST::Rule] rules, AST::user_code user_code) -> AST::Fragment

Parameters:

  • keyword (Token)
  • declarations (Array[AST::declaration])
  • rules (Array[AST::Rule])
  • user_code (AST::user_code)

Returns:



11
12
13
14
15
16
17
18
# File 'lib/ibex/frontend/generated_parser_includes.rb', line 11

def build_fragment(keyword, declarations, rules, user_code)
  # @type self: GeneratedParserBase
  extended_only!(keyword.location, "fragments")
  reject_fragment_root_declarations(declarations)
  user_block = user_code.values.flatten.first
  fail_at(user_block.loc, "user code is not allowed in fragments") if user_block
  AST::Fragment.new(declarations: declarations, rules: rules, loc: keyword.location)
end

#build_include(keyword, path) ⇒ AST::Include

RBS:

  • (Token keyword, Token path) -> AST::Include

Parameters:

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ibex/frontend/generated_parser_includes.rb', line 21

def build_include(keyword, path)
  # @type self: GeneratedParserBase
  label = token_string(keyword)
  extended_only!(keyword.location, "#{label}s")
  value = token_string(path)
  unless value.start_with?('"') && value.end_with?('"')
    fail_at(path.location, "#{label} path must use a double-quoted string")
  end

  AST::Include.new(path: value.undump, loc: keyword.location)
rescue RuntimeError => e
  fail_at(path.location, "invalid #{label} path: #{e.message}")
end

#fragment_root_declaration_name(declaration) ⇒ String?

RBS:

  • (AST::declaration declaration) -> String?

Parameters:

  • declaration (AST::declaration)

Returns:

  • (String, nil)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ibex/frontend/generated_parser_includes.rb', line 46

def fragment_root_declaration_name(declaration)
  return "options" if declaration.is_a?(AST::Options)
  return "expect" if declaration.is_a?(AST::Expect)
  return "%expect-rr" if declaration.is_a?(AST::ExpectRR)
  return "%param" if declaration.is_a?(AST::Parameter)
  return "start" if declaration.is_a?(AST::Start)
  return "%recover" if declaration.is_a?(AST::Recovery)
  return "%on_error_reduce" if declaration.is_a?(AST::OnErrorReduce)
  return "%test" if declaration.is_a?(AST::GrammarTest)
  return "lexer" if declaration.is_a?(AST::Lexer)

  nil
end

#reject_fragment_root_declarations(declarations) ⇒ void

This method returns an undefined value.

RBS:

  • (Array[AST::declaration] declarations) -> void

Parameters:

  • declarations (Array[AST::declaration])


36
37
38
39
40
41
42
43
# File 'lib/ibex/frontend/generated_parser_includes.rb', line 36

def reject_fragment_root_declarations(declarations)
  # @type self: GeneratedParserBase
  root_only = declarations.find { |declaration| fragment_root_declaration_name(declaration) }
  return unless root_only

  name = fragment_root_declaration_name(root_only)
  fail_at(root_only.loc, "#{name} declarations are not allowed in fragments")
end