Class: Ibex::Frontend::BootstrapParser

Inherits:
Object
  • Object
show all
Includes:
BootstrapParserDeclarations, BootstrapParserParameters, BootstrapParserRules
Defined in:
lib/ibex/frontend/bootstrap_parser.rb

Overview

Handwritten parser used only to regenerate the self-hosted frontend.

Constant Summary

Constants included from BootstrapParserRules

Ibex::Frontend::BootstrapParserRules::EXTENDED_SUFFIXES

Constants included from BootstrapParserDeclarations

Ibex::Frontend::BootstrapParserDeclarations::ASSOCIATIVITIES, Ibex::Frontend::BootstrapParserDeclarations::DECLARATIONS

Instance Method Summary collapse

Constructor Details

#initialize(source, file: "(grammar)", mode: :default) ⇒ BootstrapParser

Returns a new instance of BootstrapParser.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
# File 'lib/ibex/frontend/bootstrap_parser.rb', line 16

def initialize(source, file: "(grammar)", mode: :default)
  raise ArgumentError, "mode must be :default or :extended" unless %i[default extended].include?(mode)

  @tokens = source.is_a?(Array) ? source : Lexer.new(source, file: file).tokenize
  @index = 0
  @mode = mode
  @pragmas = {} #: Hash[String, bool]
end

Instance Method Details

#parseObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ibex/frontend/bootstrap_parser.rb', line 26

def parse
  location = expect_keyword("class").location
  class_name = parse_constant_path
  superclass = accept(:<) ? parse_constant_path : nil
  parse_pragmas
  declarations = parse_declarations
  expect_keyword("rule")
  rules = parse_rules
  expect_keyword("end") unless current.type == :user_code
  user_code = parse_user_code
  expect(:eof)
  AST::Root.new(class_name: class_name, superclass: superclass, declarations: declarations,
                rules: rules, user_code: user_code, loc: location,
                extended: @mode == :extended || @pragmas.key?("extended") || @pragmas.key?("cst"),
                cst: @pragmas.key?("cst"))
end

#parse_fragmentObject



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

def parse_fragment
  location = expect_keyword("fragment").location
  extended_only!(location, "fragments")
  declarations = parse_declarations
  reject_fragment_root_declarations(declarations)
  expect_keyword("rule")
  rules = keyword?("end") ? Array.new(0) : parse_rules #: Array[AST::Rule]
  expect_keyword("end")
  user_code = parse_user_code
  fail_at(user_code.values.flatten.first.loc, "user code is not allowed in fragments") unless user_code.empty?
  expect(:eof)
  AST::Fragment.new(declarations: declarations, rules: rules, loc: location)
end