Module: Ibex::Frontend::ParserConfigurationSupport

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

Overview

Shares the closed parser-declaration semantics between bootstrap and generated frontends.

Constant Summary collapse

PARSER_SETTING_VALUES =

RBS:

  • private def token_string: (Token) -> String
    private def fail_at: (Location, String) -> bot
    private def extended_only!: (Location, String) -> void

Returns:

  • (Hash[String, Array[String]])
{
  "algorithm" => %w[slr lalr ielr lr1].freeze,
  "entries" => %w[shared isolated].freeze,
  "cst_trivia" => %w[leading balanced drop].freeze
}.freeze

Instance Method Summary collapse

Instance Method Details

#build_parser_configuration(keyword, settings) ⇒ AST::ParserConfiguration

RBS:

  • (Token keyword, Array[AST::ParserSetting] settings) -> AST::ParserConfiguration

Parameters:

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ibex/frontend/parser_configuration_support.rb', line 35

def build_parser_configuration(keyword, settings)
  extended_only!(keyword.location, "parser declarations")
  seen = {} #: Hash[Symbol, bool]
  settings.each do |setting|
    fail_at(setting.loc, "duplicate parser setting #{setting.key}") if seen[setting.key]

    seen[setting.key] = true
  end
  fail_at(keyword.location, "parser declaration requires at least one setting") if settings.empty?

  AST::ParserConfiguration.new(settings: settings, loc: keyword.location)
end

#build_parser_setting(key, value) ⇒ AST::ParserSetting

RBS:

  • (Token key, Token value) -> AST::ParserSetting

Parameters:

Returns:



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

def build_parser_setting(key, value)
  name = token_string(key)
  admitted = PARSER_SETTING_VALUES[name]
  fail_at(key.location, "unknown parser setting #{name}") unless admitted

  selected = token_string(value)
  unless admitted.include?(selected)
    fail_at(value.location, "parser.#{name} must be one of #{admitted.join(', ')}; got #{selected}")
  end

  AST::ParserSetting.new(key: name.to_sym, value: selected.to_sym, loc: key.location)
end

#validate_root_parser_configuration(declarations) ⇒ void

This method returns an undefined value.

RBS:

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

Parameters:

  • declarations (Array[AST::declaration])


49
50
51
52
53
# File 'lib/ibex/frontend/parser_configuration_support.rb', line 49

def validate_root_parser_configuration(declarations)
  configurations = declarations.grep(AST::ParserConfiguration)
  duplicate = configurations.fetch(1, nil)
  fail_at(duplicate.loc, "duplicate parser declaration") if duplicate
end