Class: Ibex::BisonImport::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/bison_import/importer.rb

Overview

Converts Bison declarations and productions into analysis-only Ibex source without parsing or executing C. rubocop:disable Metrics/ClassLength -- declaration and rule recovery share one positioned directive report.

Constant Summary collapse

DEFAULT_MAX_BYTES =

@rbs! type alternative = { items: Array, precedence: String? } type rule = { lhs: String, alternatives: Array }

20 * 1024 * 1024
DEFAULT_MAX_TOKENS =

: Integer

1_000_000
DEFAULT_MAX_RULES =

: Integer

50_000
DEFAULT_MAX_ACTIONS =

: Integer

100_000

Instance Method Summary collapse

Constructor Details

#initialize(source, file:, class_name: nil, max_bytes: DEFAULT_MAX_BYTES, max_tokens: DEFAULT_MAX_TOKENS, max_rules: DEFAULT_MAX_RULES, max_actions: DEFAULT_MAX_ACTIONS) ⇒ Importer

Returns a new instance of Importer.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ibex/bison_import/importer.rb', line 21

def initialize(source, file:, class_name: nil, max_bytes: DEFAULT_MAX_BYTES,
               max_tokens: DEFAULT_MAX_TOKENS, max_rules: DEFAULT_MAX_RULES,
               max_actions: DEFAULT_MAX_ACTIONS)
  @source = source
  @file = file
  @class_name = class_name
  @max_bytes = positive_limit(max_bytes, :max_bytes)
  @max_tokens = positive_limit(max_tokens, :max_tokens)
  @max_rules = positive_limit(max_rules, :max_rules)
  @max_actions = positive_limit(max_actions, :max_actions)
  @directives = [] #: Array[Directive]
  @actions = [] #: Array[Action]
  @token_entries = [] #: Array[[String, String?]]
  @terminal_names = {} #: Hash[String, String]
  @nonterminal_names = {} #: Hash[String, String]
  @precedence_levels = [] #: Array[[String, Array[String]]]
  @starts = [] #: Array[String]
  @expected_sr = nil #: Integer?
  @expected_rr = nil #: Integer?
end

Instance Method Details

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ibex/bison_import/importer.rb', line 43

def run
  validate_source
  declarations, grammar, grammar_line = split_sections
  parse_declarations(declarations)
  tokens = Tokenizer.new(grammar, start_line: grammar_line, max_tokens: @max_tokens).tokenize
  register_nonterminals(tokens)
  rules = parse_rules(tokens)
  source = render_source(rules)
  Result.new(
    source: source, file: @file, class_name: resolved_class_name,
    directives: @directives, actions: @actions, rule_count: rules.length,
    bounds: bounds
  )
end