lib/wlang/rulesets/ruleset_utils.rb in wlang-0.8.5 vs lib/wlang/rulesets/ruleset_utils.rb in wlang-0.9.1

- old
+ new

@@ -33,10 +33,13 @@ MULTI_AS = VAR + '(' + '\s*,\s*' + VAR + ')*' # USING string for as expression USING = VAR + '(' + '\s*,\s*' + VAR + ')*' + # SHARE string for expression + SHARE = '(all|root|none)' + # Basic blocks for building expressions BASIC_BLOCKS = { :dialect => {:str => DIALECT, :groups => 0, :decoder => nil}, :encoder => {:str => ENCODER, :groups => 0, :decoder => nil}, :qdialect => {:str => QDIALECT, :groups => 1, :decoder => nil}, @@ -44,11 +47,12 @@ :var => {:str => VAR, :groups => 0, :decoder => nil}, :expr => {:str => EXPR, :groups => 0, :decoder => :decode_expr}, :uri => {:str => URI, :groups => 0, :decoder => nil}, :with => {:str => WITH, :groups => 6, :decoder => :decode_with}, :multi_as => {:str => MULTI_AS, :groups => 1, :decoder => :decode_multi_as}, - :using => {:str => USING, :groups => 1, :decoder => :decode_using} + :using => {:str => USING, :groups => 1, :decoder => :decode_using}, + :share => {:str => SHARE, :groups => 1, :decoder => :decode_share} } # Regular expressions of built expressions REGEXPS = {} @@ -132,14 +136,31 @@ # Decodes a multi as expression def self.decode_using(expr, parser) expr.split(/\s*,\s*/).collect{|s| decode_expr(s, parser)} end + # Decodes a multi as expression + def self.decode_share(expr, parser) + expr.to_sym + end + + # Converts something to a usable hash + def self.to_hash(hash) + case hash + when Hash + hash.dup + when NilClass + {} + when Array + hash.inject({}){|memo, n| memo.merge!(to_hash(n))} + else + raise ArgumentError, "Unable to convert #{hash} to an hash" + end + end + # Builds a hash for 'using ... with ...' rules from a decoded expression - def self.context_from_using_and_with(decoded) - context = decoded[:using] - context = context.dup unless context.nil? - context = {} if context.nil? + def self.context_from_using_and_with(decoded, parser = nil) + context = to_hash(decoded[:using]) context.merge!(decoded[:with]) unless decoded[:with].nil? context end \ No newline at end of file