Class: KPeg::Grammar

Inherits:
Object
  • Object
show all
Defined in:
lib/kpeg/grammar.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGrammar

Returns a new instance of Grammar.



626
627
628
629
630
631
632
633
# File 'lib/kpeg/grammar.rb', line 626

def initialize
  @directives = {}
  @rules = {}
  @rule_order = []
  @setup_actions = []
  @foreign_grammars = {}
  @variables = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/kpeg/grammar.rb', line 720

def method_missing(meth, *args)
  meth_s = meth.to_s

  if meth_s[-1,1] == "="
    rule = args.first
    set(meth_s[0..-2], rule)
    return rule
  elsif !args.empty?
    super
  end

  # Hm, I guess this is fine. It might end up confusing people though.
  return ref(meth.to_s)
end

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



635
636
637
# File 'lib/kpeg/grammar.rb', line 635

def directives
  @directives
end

#foreign_grammarsObject (readonly)

Returns the value of attribute foreign_grammars.



636
637
638
# File 'lib/kpeg/grammar.rb', line 636

def foreign_grammars
  @foreign_grammars
end

#rule_orderObject (readonly)

Returns the value of attribute rule_order.



636
637
638
# File 'lib/kpeg/grammar.rb', line 636

def rule_order
  @rule_order
end

#rulesObject (readonly)

Returns the value of attribute rules.



636
637
638
# File 'lib/kpeg/grammar.rb', line 636

def rules
  @rules
end

#setup_actionsObject (readonly)

Returns the value of attribute setup_actions.



636
637
638
# File 'lib/kpeg/grammar.rb', line 636

def setup_actions
  @setup_actions
end

#variablesObject (readonly)

Returns the value of attribute variables.



637
638
639
# File 'lib/kpeg/grammar.rb', line 637

def variables
  @variables
end

Class Method Details

.resolve(obj) ⇒ Object



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/kpeg/grammar.rb', line 680

def self.resolve(obj)
  case obj
  when Operator
    return obj
  when Symbol
    return RuleReference.new(obj.to_s)
  when String
    return LiteralString.new(obj)
  when Array
    ops = []
    obj.each do |x|
      case x
      when Sequence
        ops.concat x.ops
      when Operator
        ops << x
      else
        ops << resolve(x)
      end
    end

    return Sequence.new(*ops)
  when Range
    return CharRange.new(obj.begin.to_s, obj.end.to_s)
  when Regexp
    return LiteralRegexp.new(obj)
  else
    raise "Unknown obj type - #{obj.inspect}"
  end
end

Instance Method Details

#[](rule) ⇒ Object

Use these to access the rules unambigiously



712
713
714
# File 'lib/kpeg/grammar.rb', line 712

def [](rule)
  ref(rule.to_s)
end

#[]=(name, rule) ⇒ Object



716
717
718
# File 'lib/kpeg/grammar.rb', line 716

def []=(name, rule)
  set(name, rule)
end

#action(action) ⇒ Object



842
843
844
# File 'lib/kpeg/grammar.rb', line 842

def action(action)
  Action.new action
end

#add_directive(name, body) ⇒ Object



639
640
641
642
643
644
645
# File 'lib/kpeg/grammar.rb', line 639

def add_directive(name, body)
  if @directives.include? name
    warn "directive #{name.inspect} appears more than once"
  end

  @directives[name] = body
end

#add_foreign_grammar(name, str) ⇒ Object



651
652
653
# File 'lib/kpeg/grammar.rb', line 651

def add_foreign_grammar(name, str)
  @foreign_grammars[name] = str
end

#add_setup(act) ⇒ Object



647
648
649
# File 'lib/kpeg/grammar.rb', line 647

def add_setup(act)
  @setup_actions << act
end

#andp(node) ⇒ Object



808
809
810
# File 'lib/kpeg/grammar.rb', line 808

def andp(node)
  AndPredicate.new Grammar.resolve(node)
end

#any(*nodes, &b) ⇒ Object



765
766
767
768
769
770
# File 'lib/kpeg/grammar.rb', line 765

def any(*nodes, &b)
  nodes.map! { |x| Grammar.resolve(x) }
  op = Choice.new(*nodes)
  op.set_action(b) if b
  op
end

#bounds(op) ⇒ Object



850
851
852
# File 'lib/kpeg/grammar.rb', line 850

def bounds(op)
  Bounds.new Grammar.resolve(op)
end

#collect(op) ⇒ Object



846
847
848
# File 'lib/kpeg/grammar.rb', line 846

def collect(op)
  Collect.new Grammar.resolve(op)
end

#dot(&b) ⇒ Object



741
742
743
744
745
# File 'lib/kpeg/grammar.rb', line 741

def dot(&b)
  op = Dot.new
  op.set_action(b) if b
  op
end

#find(name) ⇒ Object



676
677
678
# File 'lib/kpeg/grammar.rb', line 676

def find(name)
  @rules[name]
end

#foreign_invoke(gram, name, args = nil) ⇒ Object

Invoke a rule defined on a foreign grammar

Parameters:

gram

The name of the grammar that the rule will be reference from

name

The name of the rule that will be invoked

args

Any arguements that should be passed to the rule

Returns:

A new ForeignInvokeRule


834
835
836
# File 'lib/kpeg/grammar.rb', line 834

def foreign_invoke(gram, name, args=nil)
  ForeignInvokeRule.new gram, name.to_s, args
end

#invoke(name, args = nil) ⇒ Object



820
821
822
# File 'lib/kpeg/grammar.rb', line 820

def invoke(name, args=nil)
  InvokeRule.new name.to_s, args
end

#kleene(node, &b) ⇒ Object



786
787
788
# File 'lib/kpeg/grammar.rb', line 786

def kleene(node, &b)
  multiple Grammar.resolve(node), 0, nil, &b
end

#lit(obj, &b) ⇒ Object



735
736
737
738
739
# File 'lib/kpeg/grammar.rb', line 735

def lit(obj, &b)
  op = Grammar.resolve(obj)
  op.set_action(b) if b
  op
end

#many(node, &b) ⇒ Object



782
783
784
# File 'lib/kpeg/grammar.rb', line 782

def many(node, &b)
  multiple Grammar.resolve(node), 1, nil, &b
end

#maybe(node, &b) ⇒ Object



778
779
780
# File 'lib/kpeg/grammar.rb', line 778

def maybe(node, &b)
  multiple Grammar.resolve(node), 0, 1, &b
end

#multiple(node, min, max, &b) ⇒ Object



772
773
774
775
776
# File 'lib/kpeg/grammar.rb', line 772

def multiple(node, min, max, &b)
  op = Multiple.new Grammar.resolve(node), min, max
  op.set_action(b) if b
  op
end

#notp(node) ⇒ Object



812
813
814
# File 'lib/kpeg/grammar.rb', line 812

def notp(node)
  NotPredicate.new Grammar.resolve(node)
end

#range(start, fin, &b) ⇒ Object



759
760
761
762
763
# File 'lib/kpeg/grammar.rb', line 759

def range(start, fin, &b)
  op = CharRange.new(start, fin)
  op.set_action(b) if b
  op
end

#ref(name, other_grammar = nil, args = nil) ⇒ Object



816
817
818
# File 'lib/kpeg/grammar.rb', line 816

def ref(name, other_grammar=nil, args=nil)
  RuleReference.new name.to_s, other_grammar, args
end

#reg(reg, opts = nil, &b) ⇒ Object



753
754
755
756
757
# File 'lib/kpeg/grammar.rb', line 753

def reg(reg, opts=nil, &b)
  op = LiteralRegexp.new reg, opts
  op.set_action(b) if b
  op
end

#rootObject



659
660
661
# File 'lib/kpeg/grammar.rb', line 659

def root
  @rules["root"]
end

#seq(*nodes, &b) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/kpeg/grammar.rb', line 790

def seq(*nodes, &b)
  ops = []
  nodes.each do |x|
    case x
    when Sequence
      ops.concat x.ops
    when Operator
      ops << x
    else
      ops << Grammar.resolve(x)
    end
  end

  op = Sequence.new(*ops)
  op.set_action(b) if b
  op
end

#set(name, op, args = nil) ⇒ Object



663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/kpeg/grammar.rb', line 663

def set(name, op, args=nil)
  if @rules.key? name
    raise "Already set rule named '#{name}'"
  end

  op = Grammar.resolve(op)

  @rule_order << name

  rule = Rule.new(name, op, args)
  @rules[name] = rule
end

#set_variable(name, val) ⇒ Object



655
656
657
# File 'lib/kpeg/grammar.rb', line 655

def set_variable(name, val)
  @variables[name] = val
end

#str(str, &b) ⇒ Object



747
748
749
750
751
# File 'lib/kpeg/grammar.rb', line 747

def str(str, &b)
  op = LiteralString.new str
  op.set_action(b) if b
  op
end

#t(op, name = nil) ⇒ Object



838
839
840
# File 'lib/kpeg/grammar.rb', line 838

def t(op, name=nil)
  Tag.new Grammar.resolve(op), name
end