Class: RPrec::DSL

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

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.



21
22
23
# File 'lib/rprec/dsl.rb', line 21

def initialize
  @precs = {}
end

Instance Attribute Details

#precsHash{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.

Returns:



29
30
31
# File 'lib/rprec/dsl.rb', line 29

def precs
  @precs
end

Class Method Details

.build(main = :main, &block) ⇒ RPrec::Grammar

Parameters:

  • main (Symbol) (defaults to: :main)
  • block (Proc)

Returns:



12
13
14
15
16
17
18
# File 'lib/rprec/dsl.rb', line 12

def self.build(main = :main, &block)
  context = DSL.new
  context.instance_eval(&block)
  grammar = Grammar.new(main, context.precs)
  grammar.setup
  grammar
end

Instance Method Details

#prec(name_and_succs, &block) ⇒ void

This method returns an undefined value.

Parameters:

  • name_and_succs (Symbol, Hash{Symbol => Symbol}, Hash{Symbol => Array<Symbol>})
  • block (Proc)

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rprec/dsl.rb', line 34

def prec(name_and_succs, &block)
  # @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, &block)
end