Class: KPeg::Grammar

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGrammar

Returns a new instance of Grammar



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

def initialize
  @rules = {}
  @rule_order = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 780

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
  end

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

Instance Attribute Details

#rule_orderObject (readonly)

Returns the value of attribute rule_order



717
718
719
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 717

def rule_order
  @rule_order
end

#rulesObject (readonly)

Returns the value of attribute rules



717
718
719
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 717

def rules
  @rules
end

Class Method Details

.resolve(rule) ⇒ Object



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 740

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

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

Instance Method Details

#[](rule) ⇒ Object

Use these to access the rules unambigiously



772
773
774
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 772

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

#[]=(name, rule) ⇒ Object



776
777
778
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 776

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

#action(action) ⇒ Object



882
883
884
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 882

def action(action)
  Action.new action
end

#andp(node) ⇒ Object



866
867
868
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 866

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

#any(*nodes, &b) ⇒ Object



823
824
825
826
827
828
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 823

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

#dot(&b) ⇒ Object



799
800
801
802
803
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 799

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

#find(name) ⇒ Object



736
737
738
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 736

def find(name)
  @rules[name]
end

#kleene(node, &b) ⇒ Object



844
845
846
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 844

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

#lit(obj, &b) ⇒ Object



793
794
795
796
797
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 793

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

#many(node, &b) ⇒ Object



840
841
842
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 840

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

#maybe(node, &b) ⇒ Object



836
837
838
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 836

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

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



830
831
832
833
834
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 830

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

#notp(node) ⇒ Object



870
871
872
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 870

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

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



817
818
819
820
821
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 817

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

#ref(name, other_grammar = nil) ⇒ Object



874
875
876
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 874

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

#reg(reg, &b) ⇒ Object



811
812
813
814
815
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 811

def reg(reg, &b)
  rule = LiteralRegexp.new reg
  rule.set_action(b) if b
  rule
end

#rootObject



719
720
721
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 719

def root
  @rules["root"]
end

#seq(*nodes, &b) ⇒ Object



848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 848

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

  rule = Sequence.new(*rules)
  rule.set_action(b) if b
  rule
end

#set(name, rule) ⇒ Object



723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 723

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

  rule = Grammar.resolve(rule)

  @rule_order << name
  rule.name = name

  @rules[name] = rule
end

#str(str, &b) ⇒ Object



805
806
807
808
809
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 805

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

#t(rule, name = nil) ⇒ Object



878
879
880
# File 'lib/vendor/kpeg/lib/kpeg.rb', line 878

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