Class: RPrec::DSL
- Inherits:
-
Object
- Object
- RPrec::DSL
- Defined in:
- lib/rprec/dsl.rb
Overview
DSL
provides a DSL to construct RPrec::Grammar
objects.
It is also a context of blocks of the RPrec::DSL.build
method.
Defined Under Namespace
Classes: PrecDSL
Instance Attribute Summary collapse
- #precs ⇒ Hash{Symbol => RPrec::Prec} readonly private
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ DSL
constructor
private
A new instance of DSL.
- #prec(name_and_succs) ⇒ void
Constructor Details
#initialize ⇒ DSL
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of DSL.
19 20 21 |
# File 'lib/rprec/dsl.rb', line 19 def initialize @precs = {} end |
Instance Attribute Details
#precs ⇒ Hash{Symbol => RPrec::Prec} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
27 28 29 |
# File 'lib/rprec/dsl.rb', line 27 def precs @precs end |
Class Method Details
.build(main = :main) ⇒ RPrec::Grammar
10 11 12 13 14 15 16 |
# File 'lib/rprec/dsl.rb', line 10 def self.build(main = :main, &) context = DSL.new context.instance_eval(&) grammar = Grammar.new(main, context.precs) grammar.setup grammar end |
Instance Method Details
#prec(name_and_succs) ⇒ void
This method returns an undefined value.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rprec/dsl.rb', line 31 def prec(name_and_succs, &) # @type var name: Symbol # @type var succs: Array[Symbol] name, succs = if name_and_succs.is_a?(Hash) name_first, succs_first = name_and_succs.first if name_and_succs.size != 1 || !name_first || !succs_first raise ArgumentError, '`prec` should be `prec name => succs` form' end [name_first, succs_first.is_a?(Array) ? succs_first : [succs_first]] else [name_and_succs, []] end raise ArgumentError, "A prec '#{name}' is already defined" if @precs.include?(name) @precs[name] = PrecDSL.build(name, succs, &) end |