Class: KPeg::Grammar
- Inherits:
-
Object
show all
- Defined in:
- lib/kpeg/grammar.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](rule) ⇒ Object
Use these to access the rules unambigiously.
-
#[]=(name, rule) ⇒ Object
-
#action(action) ⇒ Object
-
#add_directive(name, body) ⇒ Object
-
#add_foreign_grammar(name, str) ⇒ Object
-
#add_setup(act) ⇒ Object
-
#andp(node) ⇒ Object
-
#any(*nodes, &b) ⇒ Object
-
#bounds(op) ⇒ Object
-
#collect(op) ⇒ Object
-
#dot(&b) ⇒ Object
-
#find(name) ⇒ Object
-
#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.
-
#initialize ⇒ Grammar
constructor
A new instance of Grammar.
-
#invoke(name, args = nil) ⇒ Object
-
#kleene(node, &b) ⇒ Object
-
#lit(obj, &b) ⇒ Object
-
#many(node, &b) ⇒ Object
-
#maybe(node, &b) ⇒ Object
-
#method_missing(meth, *args) ⇒ Object
-
#multiple(node, min, max, &b) ⇒ Object
-
#notp(node) ⇒ Object
-
#range(start, fin, &b) ⇒ Object
-
#ref(name, other_grammar = nil, args = nil) ⇒ Object
-
#reg(reg, opts = nil, &b) ⇒ Object
-
#root ⇒ Object
-
#seq(*nodes, &b) ⇒ Object
-
#set(name, op, args = nil) ⇒ Object
-
#set_variable(name, val) ⇒ Object
-
#str(str, &b) ⇒ Object
-
#t(op, name = nil) ⇒ Object
Constructor Details
Returns a new instance of Grammar.
636
637
638
639
640
641
642
643
|
# File 'lib/kpeg/grammar.rb', line 636
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
# File 'lib/kpeg/grammar.rb', line 730
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
return ref(meth.to_s)
end
|
Instance Attribute Details
#directives ⇒ Object
Returns the value of attribute directives.
645
646
647
|
# File 'lib/kpeg/grammar.rb', line 645
def directives
@directives
end
|
#foreign_grammars ⇒ Object
Returns the value of attribute foreign_grammars.
646
647
648
|
# File 'lib/kpeg/grammar.rb', line 646
def foreign_grammars
@foreign_grammars
end
|
#rule_order ⇒ Object
Returns the value of attribute rule_order.
646
647
648
|
# File 'lib/kpeg/grammar.rb', line 646
def rule_order
@rule_order
end
|
#rules ⇒ Object
Returns the value of attribute rules.
646
647
648
|
# File 'lib/kpeg/grammar.rb', line 646
def rules
@rules
end
|
#setup_actions ⇒ Object
Returns the value of attribute setup_actions.
646
647
648
|
# File 'lib/kpeg/grammar.rb', line 646
def setup_actions
@setup_actions
end
|
#variables ⇒ Object
Returns the value of attribute variables.
647
648
649
|
# File 'lib/kpeg/grammar.rb', line 647
def variables
@variables
end
|
Class Method Details
.resolve(obj) ⇒ Object
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
# File 'lib/kpeg/grammar.rb', line 690
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
722
723
724
|
# File 'lib/kpeg/grammar.rb', line 722
def [](rule)
ref(rule.to_s)
end
|
#[]=(name, rule) ⇒ Object
726
727
728
|
# File 'lib/kpeg/grammar.rb', line 726
def []=(name, rule)
set(name, rule)
end
|
#action(action) ⇒ Object
852
853
854
|
# File 'lib/kpeg/grammar.rb', line 852
def action(action)
Action.new action
end
|
#add_directive(name, body) ⇒ Object
649
650
651
652
653
654
655
|
# File 'lib/kpeg/grammar.rb', line 649
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
661
662
663
|
# File 'lib/kpeg/grammar.rb', line 661
def add_foreign_grammar(name, str)
@foreign_grammars[name] = str
end
|
#add_setup(act) ⇒ Object
657
658
659
|
# File 'lib/kpeg/grammar.rb', line 657
def add_setup(act)
@setup_actions << act
end
|
#any(*nodes, &b) ⇒ Object
775
776
777
778
779
780
|
# File 'lib/kpeg/grammar.rb', line 775
def any(*nodes, &b)
nodes.map! { |x| Grammar.resolve(x) }
op = Choice.new(*nodes)
op.set_action(b) if b
op
end
|
#dot(&b) ⇒ Object
751
752
753
754
755
|
# File 'lib/kpeg/grammar.rb', line 751
def dot(&b)
op = Dot.new
op.set_action(b) if b
op
end
|
#find(name) ⇒ Object
686
687
688
|
# File 'lib/kpeg/grammar.rb', line 686
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
844
845
846
|
# File 'lib/kpeg/grammar.rb', line 844
def foreign_invoke(gram, name, args=nil)
ForeignInvokeRule.new gram, name.to_s, args
end
|
#invoke(name, args = nil) ⇒ Object
830
831
832
|
# File 'lib/kpeg/grammar.rb', line 830
def invoke(name, args=nil)
InvokeRule.new name.to_s, args
end
|
#kleene(node, &b) ⇒ Object
796
797
798
|
# File 'lib/kpeg/grammar.rb', line 796
def kleene(node, &b)
multiple Grammar.resolve(node), 0, nil, &b
end
|
#lit(obj, &b) ⇒ Object
745
746
747
748
749
|
# File 'lib/kpeg/grammar.rb', line 745
def lit(obj, &b)
op = Grammar.resolve(obj)
op.set_action(b) if b
op
end
|
#many(node, &b) ⇒ Object
792
793
794
|
# File 'lib/kpeg/grammar.rb', line 792
def many(node, &b)
multiple Grammar.resolve(node), 1, nil, &b
end
|
#maybe(node, &b) ⇒ Object
788
789
790
|
# File 'lib/kpeg/grammar.rb', line 788
def maybe(node, &b)
multiple Grammar.resolve(node), 0, 1, &b
end
|
#multiple(node, min, max, &b) ⇒ Object
782
783
784
785
786
|
# File 'lib/kpeg/grammar.rb', line 782
def multiple(node, min, max, &b)
op = Multiple.new Grammar.resolve(node), min, max
op.set_action(b) if b
op
end
|
#range(start, fin, &b) ⇒ Object
769
770
771
772
773
|
# File 'lib/kpeg/grammar.rb', line 769
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
826
827
828
|
# File 'lib/kpeg/grammar.rb', line 826
def ref(name, other_grammar=nil, args=nil)
RuleReference.new name.to_s, other_grammar, args
end
|
#reg(reg, opts = nil, &b) ⇒ Object
763
764
765
766
767
|
# File 'lib/kpeg/grammar.rb', line 763
def reg(reg, opts=nil, &b)
op = LiteralRegexp.new reg, opts
op.set_action(b) if b
op
end
|
#root ⇒ Object
669
670
671
|
# File 'lib/kpeg/grammar.rb', line 669
def root
@rules["root"]
end
|
#seq(*nodes, &b) ⇒ Object
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
|
# File 'lib/kpeg/grammar.rb', line 800
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
673
674
675
676
677
678
679
680
681
682
683
684
|
# File 'lib/kpeg/grammar.rb', line 673
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
665
666
667
|
# File 'lib/kpeg/grammar.rb', line 665
def set_variable(name, val)
@variables[name] = val
end
|
#str(str, &b) ⇒ Object
757
758
759
760
761
|
# File 'lib/kpeg/grammar.rb', line 757
def str(str, &b)
op = LiteralString.new str
op.set_action(b) if b
op
end
|
#t(op, name = nil) ⇒ Object
848
849
850
|
# File 'lib/kpeg/grammar.rb', line 848
def t(op, name=nil)
Tag.new Grammar.resolve(op), name
end
|