require 'wlang/rulesets/ruleset_utils'
module WLang
class RuleSet
#
# 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}[link://files/specification.html].
#
module Basic
U=WLang::RuleSet::Utils
# Default mapping between tag symbols and methods
DEFAULT_RULESET = {'!' => :execution, '%' => :modulation, '^' => :encoding,
'+' => :inclusion, '$' => :injection, '%!' => :recursive_application}
# Rule implementation of !{wlang/ruby}.
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
# Rule implementation of %{wlang/active-string}{...}.
def self.modulation(parser, offset)
dialect, reached = parser.parse(offset, "wlang/active-string")
result, reached = parser.parse_block(reached, dialect)
[result, reached]
end
# Rule implementation of ^{wlang/active-string}{...}
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
# Rule implementation of ${wlang/ruby}
def self.injection(parser, offset)
execution(parser, offset)
end
# Rule implementation of +{wlang/ruby}
def self.inclusion(parser, offset)
execution(parser, offset)
end
# Rule implementation of %!{wlang/ruby using ... with ...}{...}
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
end # module Basic
end
end