Module: Ibex::NormalizeParserConfiguration

Included in:
Normalizer
Defined in:
lib/ibex/normalize/parser_configuration.rb,
sig/ibex/normalize/parser_configuration.rbs

Overview

Converts a root parser declaration into the explicit current Grammar IR contract.

Instance Method Summary collapse

Instance Method Details

#normalized_parser_contractIR::ParserContract?

RBS:

  • () -> IR::ParserContract?

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ibex/normalize/parser_configuration.rb', line 18

def normalized_parser_contract
  # @type self: Normalizer
  declaration = @parser_configuration
  return unless declaration

  entries = {} #: Hash[Symbol, IR::ParserContract::Entry]
  declaration.settings.each do |setting|
    validated_parser_setting_definition(setting)
    fail_at(setting.loc, "duplicate parser setting #{setting.key}") if entries.key?(setting.key)

    if setting.key == :cst_trivia && @options[:cst] != true
      fail_at(setting.loc, "parser.cst_trivia requires pragma cst")
    end

    entries[setting.key] = IR::ParserContract::Entry.new(
      setting.key, value: setting.value, location: parser_setting_location(setting), explicit: true
    )
  end
  validate_isolated_entries(declaration)
  IR::ParserContract.new(**entries)
end

#parser_setting_location(setting) ⇒ Location

RBS:

  • (Frontend::AST::ParserSetting setting) -> Location

Parameters:

Returns:



64
65
66
67
68
# File 'lib/ibex/normalize/parser_configuration.rb', line 64

def parser_setting_location(setting)
  # @type self: Normalizer
  location = setting.loc
  Location.new(file: location.file, line: location.line, column: location.column)
end

#read_parser_configuration(declaration) ⇒ void

This method returns an undefined value.

RBS:

  • (Frontend::AST::ParserConfiguration declaration) -> void

Parameters:



9
10
11
12
13
14
15
# File 'lib/ibex/normalize/parser_configuration.rb', line 9

def read_parser_configuration(declaration)
  # @type self: Normalizer
  fail_at(declaration.loc, "parser declarations require extended mode") unless @mode == :extended
  fail_at(declaration.loc, "duplicate parser declaration") if @parser_configuration

  @parser_configuration = declaration
end

#validate_isolated_entries(declaration) ⇒ void

This method returns an undefined value.

RBS:

  • (Frontend::AST::ParserConfiguration declaration) -> void

Parameters:



55
56
57
58
59
60
61
# File 'lib/ibex/normalize/parser_configuration.rb', line 55

def validate_isolated_entries(declaration)
  # @type self: Normalizer
  setting = declaration.settings.find { |candidate| candidate.key == :entries && candidate.value == :isolated }
  return unless setting && @start_names.length < 2

  fail_at(setting.loc, "parser.entries isolated requires at least two start symbols")
end

#validated_parser_setting_definition(setting) ⇒ Hash[Symbol, Object?]

RBS:

  • (Frontend::AST::ParserSetting setting) -> Hash[Symbol, Object?]

Parameters:

Returns:

  • (Hash[Symbol, Object?])


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ibex/normalize/parser_configuration.rb', line 41

def validated_parser_setting_definition(setting)
  # @type self: Normalizer
  definition = IR::ParserContract::DEFINITIONS[setting.key]
  unless %i[algorithm entries cst_trivia].include?(setting.key) && definition
    fail_at(setting.loc, "unknown parser setting #{setting.key}")
  end
  unless definition.fetch(:values).include?(setting.value)
    allowed = definition.fetch(:values).join(", ")
    fail_at(setting.loc, "parser.#{setting.key} must be one of #{allowed}; got #{setting.value}")
  end
  definition
end