Class: WLang::RuleSet

Inherits:
Object
  • Object
show all
Defined in:
lib/wlang/rule_set.rb,
lib/wlang/dialects/sql_dialect.rb,
lib/wlang/dialects/yaml_dialect.rb,
lib/wlang/dialects/ruby_dialect.rb,
lib/wlang/dialects/xhtml_dialect.rb,
lib/wlang/rulesets/basic_ruleset.rb,
lib/wlang/rulesets/ruleset_utils.rb,
lib/wlang/dialects/hosted_dialect.rb,
lib/wlang/rulesets/context_ruleset.rb,
lib/wlang/rulesets/encoding_ruleset.rb,
lib/wlang/rulesets/buffering_ruleset.rb,
lib/wlang/dialects/plain_text_dialect.rb,
lib/wlang/rulesets/imperative_ruleset.rb

Overview

module XHtml

Defined Under Namespace

Modules: Basic, Buffering, Context, Encoding, Hosted, Imperative, PlainText, Ruby, SQL, Utils, XHtml, YAML

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RuleSet) initialize

Creates an new dialect rule set.



27
28
29
30
31
# File 'lib/wlang/rule_set.rb', line 27

def initialize() 
  @rules = {}
  @reuse = []
  @patterns = Hash.new{|h, k| h[k] = build_pattern(k)} 
end

Instance Attribute Details

- (Object) reuse (readonly)

Which modules are reused



22
23
24
# File 'lib/wlang/rule_set.rb', line 22

def reuse
  @reuse
end

Instance Method Details

- (Object) [](tag)

Returns the Rule associated with a given tag, nil if no such rule.



83
# File 'lib/wlang/rule_set.rb', line 83

def [](tag) @rules[tag];  end

- (Object) add_rule(tag, rule = nil, &block)

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). Otherwise rule is expected to be a Rule instance. This method check its arguments, raising an ArgumentError if incorrect.

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
# File 'lib/wlang/rule_set.rb', line 46

def add_rule(tag, rule=nil, &block) 
  if rule.nil? 
    raise(ArgumentError,"Block required") unless block_given?
    rule = Rule.new(&block)
  end
  raise(ArgumentError, "Rule expected") unless Rule===rule
  @rules[tag] = rule  
  @patterns.clear
end

- (Object) add_rules(mod, pairs = nil)

Add rules defined in a given RuleSet module.

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
# File 'lib/wlang/rule_set.rb', line 59

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)
  end
end

- (Object) each

Yields the block with name, rule pairs



34
35
36
# File 'lib/wlang/rule_set.rb', line 34

def each
  @rules.each_pair{|name,rule| yield(name, rule)}
end

- (Object) pattern(block_symbols)

Returns a Regexp instance with recognizes all tags installed in the rule set. The returned Regexp is backslashing aware (it matches \${ for example) as well as '{' and '}' aware. This pattern is used by WLang::Parser and is not intended to be used by users themselve.



76
77
78
# File 'lib/wlang/rule_set.rb', line 76

def pattern(block_symbols) 
  @patterns[block_symbols]
end