lib/wlang/rule_set.rb in wlang-0.8.5 vs lib/wlang/rule_set.rb in wlang-0.9.1

- old
+ new

@@ -16,15 +16,27 @@ # end # # == Detailed API class RuleSet + # Which modules are reused + attr_reader :reuse + # # Creates an new dialect rule set. # - def initialize() @rules, @pattern = {}, nil; end + def initialize() + @rules = {} + @reuse = [] + @patterns = Hash.new{|h, k| h[k] = build_pattern(k)} + end + # Yields the block with name, rule pairs + def each + @rules.each_pair{|name,rule| yield(name, rule)} + end + # # Adds a tag matching rule to this rule set. _tag_ must be a String with the # tag associated to the rule (without the '{', that is '$' for the tag ${...} # for example. If rule is ommited and a block is given, a new Rule instance is # created on the fly with _block_ as implementation (see Rule#new). @@ -36,18 +48,19 @@ raise(ArgumentError,"Block required") unless block_given? rule = Rule.new(&block) end raise(ArgumentError, "Rule expected") unless Rule===rule @rules[tag] = rule - @pattern = nil + @patterns.clear end # # Add rules defined in a given RuleSet module. # def add_rules(mod, pairs=nil) raise(ArgumentError,"Module expected") unless Module===mod + reuse << mod pairs = mod::DEFAULT_RULESET if pairs.nil? pairs.each_pair do |symbol,method| meth = mod.method(method) raise(ArgumentError,"No such method: #{method}") if meth.nil? add_rule(symbol, &meth.to_proc) @@ -59,10 +72,10 @@ # The returned Regexp is backslashing aware (it matches <tt>\${</tt> for example) # as well as '{' and '}' aware. This pattern is used by WLang::Parser and is # not intended to be used by users themselve. # def pattern(block_symbols) - build_pattern(block_symbols); + @patterns[block_symbols] end # # Returns the Rule associated with a given tag, _nil_ if no such rule. # \ No newline at end of file