Skip to content

Commit

Permalink
Introduced a hosted dialect and used it in place of wlang/ruby everyw…
Browse files Browse the repository at this point in the history
…here.

Introduced a inclusion semantics for +{} in wlang/ruby dialect
  • Loading branch information
blambeau committed Jan 13, 2011
1 parent a353f48 commit a6622cb
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 24 deletions.
20 changes: 10 additions & 10 deletions doc/specification/specification.yml
@@ -1,6 +1,6 @@
---
title: WLang
version: 0.9.1
version: 0.9.3
sections:
- identifier: about
name: About
Expand Down Expand Up @@ -254,15 +254,15 @@ rulesets:
scope is provided by the Context ruleset. All are variants of 'saving previous instantiations' in
scope variables...
examples:
- ["wlang/*", '={name as n}{Hello +{n}}', "Hello O'Neil"]
- ["wlang/*", '={name as n}Hello +{n}', "Hello O'Neil"]
- ["wlang/*", '#={name}{blambeau}{Hello +{name}} and +{name}', "Hello blambeau and O'Neil"]
- ["wlang/*", '#={name}{blambeau}Hello +{name} and +{name}', "Hello blambeau and blambeau"]
- ["wlang/*", '={author as name}{Hello +{name}} and +{name}', "Hello blambeau and O'Neil"]
- ["wlang/*", '={author as name}Hello +{name} and +{name}', "Hello blambeau and blambeau"]
- ["wlang/*", '%={wlang/dummy as hello}{Hello +{name}}{+{hello}}', "Hello +{name}"]
- ["wlang/*", '^={plain-text/upcase as name}{+{author}}{Hello +{name}} and +{name}', "Hello BLAMBEAU and O'Neil"]
- ["wlang/*", '^={plain-text/upcase as name}{+{author}}Hello +{name} and +{name}', "Hello BLAMBEAU and BLAMBEAU"]
- ["wlang/*", '={name as n}{Hello !{n}}', "Hello O'Neil"]
- ["wlang/*", '={name as n}Hello !{n}', "Hello O'Neil"]
- ["wlang/*", '#={name}{blambeau}{Hello !{name}} and !{name}', "Hello blambeau and O'Neil"]
- ["wlang/*", '#={name}{blambeau}Hello !{name} and !{name}', "Hello blambeau and blambeau"]
- ["wlang/*", '={author as name}{Hello !{name}} and !{name}', "Hello blambeau and O'Neil"]
- ["wlang/*", '={author as name}Hello !{name} and !{name}', "Hello blambeau and blambeau"]
- ["wlang/*", '%={wlang/dummy as hello}{Hello !{name}}{!{hello}}', "Hello !{name}"]
- ["wlang/*", '^={plain-text/upcase as name}{!{author}}{Hello !{name}} and !{name}', "Hello BLAMBEAU and O'Neil"]
- ["wlang/*", '^={plain-text/upcase as name}{!{author}}Hello !{name} and !{name}', "Hello BLAMBEAU and BLAMBEAU"]

rules:
# ={wlang/hosted as x}{...}
Expand Down
2 changes: 1 addition & 1 deletion lib/wlang.rb
Expand Up @@ -20,7 +20,7 @@
module WLang

# Current version of WLang
VERSION = "0.9.2".freeze
VERSION = "0.9.3".freeze

######################################################################## About files and extensions

Expand Down
50 changes: 50 additions & 0 deletions lib/wlang/dialects/hosted_dialect.rb
@@ -0,0 +1,50 @@
module WLang
class EncoderSet

# Encoders for ruby
module Hosted

# Default encoders
DEFAULT_ENCODERS = {"main-encoding" => :main_encoding,
"single-quoting" => :single_quoting,
"double-quoting" => :double_quoting,
"regex-escaping" => :regex_escaping,
"method-case" => :method_case}

# No-op encoding here
def self.main_encoding(src, options); src; end

# Single-quoting encoding
def self.single_quoting(src, options); src.gsub(/([^\\])'/,%q{\1\\\'}); end

# Double-quoting encoding
def self.double_quoting(src, options); src.gsub('"','\"'); end

# Regexp-escaping encoding
def self.regex_escaping(src, options); Regexp.escape(src); end

# Converts any source to a typical ruby method name
def self.method_case(src, options)
src.strip.gsub(/[^a-zA-Z0-9\s]/," ").
gsub(/([A-Z])/){ " " + $1.downcase}.
strip.
gsub(/^([^a-z])/){ "_" + $1 }.
gsub(/\s+/){"_"}
end


end # module Hosted

end
class RuleSet

# Defines rulset of the wlang/ruby dialect
module Hosted

# Default mapping between tag symbols and methods
DEFAULT_RULESET = {}

end # module Hosted

end # class RuleSet
end # module WLang
4 changes: 2 additions & 2 deletions lib/wlang/dialects/plain_text_dialect.rb
Expand Up @@ -47,7 +47,7 @@ module PlainText

# Upcase rule as <tt>+{wlang/hosted}</tt>
def self.upcase(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")
value = parser.evaluate(expression)
value = value.nil? ? "" : value.to_s
result = WLang::EncoderSet::PlainText.upcase(value)
Expand All @@ -56,7 +56,7 @@ def self.upcase(parser, offset)

# Downcase rule as <tt>-{wlang/hosted}</tt>
def self.downcase(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")
value = parser.evaluate(expression)
value = value.nil? ? "" : value.to_s
result = EncoderSet::PlainText.downcase(value)
Expand Down
4 changes: 2 additions & 2 deletions lib/wlang/dialects/ruby_dialect.rb
Expand Up @@ -106,7 +106,7 @@ module Ruby

# Rule implementation of <tt>+{wlang/ruby}</tt>.
def self.inclusion(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")
value = parser.evaluate(expression)
result = WLang::EncoderSet::Ruby.to_literal(value)
[result, reached]
Expand All @@ -115,4 +115,4 @@ def self.inclusion(parser, offset)
end # module Ruby

end # class RuleSet
end # module WLang
end # module WLang
12 changes: 12 additions & 0 deletions lib/wlang/dialects/standard_dialects.rb
Expand Up @@ -72,6 +72,18 @@
dialect("dummy") do
end

# wlang/ruby dialect
dialect("hosted") do
ruby_require "wlang/dialects/hosted_dialect" do
encoders WLang::EncoderSet::Hosted
rules WLang::RuleSet::Basic
rules WLang::RuleSet::Encoding
rules WLang::RuleSet::Imperative
rules WLang::RuleSet::Context
rules WLang::RuleSet::Hosted
end
end

# wlang/active-string dialect
dialect("active-string") do
rules WLang::RuleSet::Basic
Expand Down
2 changes: 1 addition & 1 deletion lib/wlang/rulesets/basic_ruleset.rb
Expand Up @@ -19,7 +19,7 @@ module Basic

# Rule implementation of <tt>!{wlang/ruby}</tt>.
def self.execution(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")
value = parser.evaluate(expression)
result = value.nil? ? "" : value.to_s
[result, reached]
Expand Down
6 changes: 3 additions & 3 deletions lib/wlang/rulesets/context_ruleset.rb
Expand Up @@ -18,7 +18,7 @@ module Context

# Rule implementation of <tt>={wlang/hosted as x}{...}</tt>
def self.assignment(parser, offset)
expr, reached = parser.parse(offset, "wlang/ruby")
expr, reached = parser.parse(offset, "wlang/hosted")

# decode expression
decoded = U.decode_expr_as(expr)
Expand Down Expand Up @@ -62,7 +62,7 @@ def self.block_assignment(parser, offset)

# Rule implementation of <tt>%={wlang/active-string as x}{...}{...}</tt>
def self.modulo_assignment(parser, offset)
dialect_as, reached = parser.parse(offset, "wlang/ruby")
dialect_as, reached = parser.parse(offset, "wlang/hosted")

# decode expression
decoded = U.decode_qdialect_as(dialect_as)
Expand All @@ -84,7 +84,7 @@ def self.modulo_assignment(parser, offset)

# Rule implementation of <tt>^={wlang/active-string as x}{...}{...}</tt>
def self.encoding_assignment(parser, offset)
encoding_as, reached = parser.parse(offset, "wlang/ruby")
encoding_as, reached = parser.parse(offset, "wlang/hosted")

# decode expression
decoded = U.decode_qencoder_as(encoding_as)
Expand Down
6 changes: 3 additions & 3 deletions lib/wlang/rulesets/encoding_ruleset.rb
Expand Up @@ -45,23 +45,23 @@ def self.double_quoting(parser, offset)

# Injection as <tt>${wlang/ruby}</tt>
def self.injection(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")
result = parser.evaluate(expression)
encoded = parser.encode(result.to_s, "main-encoding")
[encoded, reached]
end

# Single-quoted as <tt>'{wlang/ruby}</tt>
def self.single_quoted(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")
result = parser.evaluate(expression)
encoded = parser.encode(result.to_s, "single-quoting")
["'" << encoded, reached]
end

# Double-quoted as <tt>"{wlang/ruby}</tt>
def self.double_quoted(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")
result = parser.evaluate(expression)
encoded = parser.encode(result.to_s, "double-quoting")
['"' << encoded, reached]
Expand Down
4 changes: 2 additions & 2 deletions lib/wlang/rulesets/imperative_ruleset.rb
Expand Up @@ -27,7 +27,7 @@ module Imperative
# otherwise instantiates #3 if present.
#
def self.conditional(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")
value = parser.evaluate(expression)
if value
then_block, reached = parser.parse_block(reached)
Expand Down Expand Up @@ -64,7 +64,7 @@ def self.merge_each_args(names, args)
# name x in the scope). If #3 is present, it is instantiated between elements.
#
def self.enumeration(parser, offset)
expression, reached = parser.parse(offset, "wlang/ruby")
expression, reached = parser.parse(offset, "wlang/hosted")

# decode 'wlang/hosted using each as x' expression
hash = U.expr(:expr,
Expand Down

0 comments on commit a6622cb

Please sign in to comment.