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

- (Grammar) initialize

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

- (Object) method_missing(meth, *args)



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

- (Object) rule_order (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

- (Object) rules (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

+ (Object) resolve(rule)



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

- (Object) [](rule)

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

- (Object) []=(name, rule)



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

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

- (Object) action(action)



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

def action(action)
  Action.new action
end

- (Object) andp(node)



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

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

- (Object) any(*nodes, &b)



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

- (Object) dot(&b)



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

- (Object) find(name)



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

def find(name)
  @rules[name]
end

- (Object) kleene(node, &b)



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

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

- (Object) lit(obj, &b)



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

- (Object) many(node, &b)



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

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

- (Object) maybe(node, &b)



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

- (Object) multiple(node, min, max, &b)



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

- (Object) notp(node)



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

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

- (Object) range(start, fin, &b)



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

- (Object) ref(name, other_grammar = nil)



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

- (Object) reg(reg, &b)



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

- (Object) root



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

def root
  @rules["root"]
end

- (Object) seq(*nodes, &b)



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

- (Object) set(name, rule)



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

- (Object) str(str, &b)



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

- (Object) t(rule, name = nil)



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

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