Module: WLang::RuleSet::Basic

Defined in:
lib/wlang/rulesets/basic_ruleset.rb

Overview

Basic ruleset, commonly included by any wlang dialect (but some tags, like ${...} may be overriden). This ruleset is often installed conjointly with WLang::RuleSet::Encoding which provides interresting overridings of ${...} and +{...}.

For an overview of this ruleset, see the wlang specification file.

Constant Summary

U =
WLang::RuleSet::Utils
DEFAULT_RULESET =

Default mapping between tag symbols and methods

{'!' => :execution, '%' => :modulation, '^' => :encoding,
'+' => :inclusion, '$' => :injection, '%!' => :recursive_application}

Class Method Summary collapse

Class Method Details

.encoding(parser, offset) ⇒ Object

Rule implementation of ^{wlang/active-string}{...}



36
37
38
39
40
41
# File 'lib/wlang/rulesets/basic_ruleset.rb', line 36

def self.encoding(parser, offset)
  encoder, reached = parser.parse(offset, "wlang/active-string")
  result, reached = parser.parse_block(reached)
  result = parser.encode(result, encoder)
  [result, reached]
end

.execution(parser, offset) ⇒ Object

Rule implementation of !{wlang/ruby}.



21
22
23
24
25
26
# File 'lib/wlang/rulesets/basic_ruleset.rb', line 21

def self.execution(parser, offset)
  expression, reached = parser.parse(offset, "wlang/hosted")
  value = parser.evaluate(expression)
  result = value.nil? ? "" : value.to_s
  [result, reached]
end

.inclusion(parser, offset) ⇒ Object

Rule implementation of +{wlang/ruby}



49
50
51
# File 'lib/wlang/rulesets/basic_ruleset.rb', line 49

def self.inclusion(parser, offset)
  execution(parser, offset)
end

.injection(parser, offset) ⇒ Object

Rule implementation of ${wlang/ruby}



44
45
46
# File 'lib/wlang/rulesets/basic_ruleset.rb', line 44

def self.injection(parser, offset)
  execution(parser, offset)
end

.modulation(parser, offset) ⇒ Object

Rule implementation of %{wlang/active-string}{...}.



29
30
31
32
33
# File 'lib/wlang/rulesets/basic_ruleset.rb', line 29

def self.modulation(parser, offset)
  dialect, reached = parser.parse(offset, "wlang/active-string")
  result, reached = parser.parse_block(reached, dialect)
  [result, reached]
end

.recursive_application(parser, offset) ⇒ Object

Rule implementation of %!{wlang/ruby using ... with ...}{...}



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wlang/rulesets/basic_ruleset.rb', line 54

def self.recursive_application(parser, offset)
  dialect, reached = parser.parse(offset, "wlang/active-string")
  text, reached = parser.parse_block(reached)
    
  # decode expression
  decoded = U.expr(:qdialect, 
                   ["share", :share, false],
                   ["using", :expr, false], 
                   ["with",  :with, false]).decode(dialect, parser)
  parser.syntax_error(offset) if decoded.nil?
    
  # build context
  dialect2 = decoded[:qdialect]
  shared  = decoded[:share].nil? ? :root : decoded[:share]
  context  = U.context_from_using_and_with(decoded)
  
  # TODO: refactor me!!
  parser.branch(:template => WLang::template(text, dialect2),
                :offset   => 0,
                :shared   => shared,
                :scope    => context) {
    instantiated, forget = parser.instantiate(true)
    [instantiated, reached]
  }
end