Class: Ibex::Frontend::BootstrapParser
- Inherits:
-
Object
- Object
- Ibex::Frontend::BootstrapParser
- 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
-
#initialize(source, file: "(grammar)", mode: :default) ⇒ BootstrapParser
constructor
A new instance of BootstrapParser.
- #parse ⇒ Object
- #parse_fragment ⇒ Object
Constructor Details
#initialize(source, file: "(grammar)", mode: :default) ⇒ BootstrapParser
Returns a new instance of BootstrapParser.
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
#parse ⇒ Object
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_fragment ⇒ Object
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 |