Class: WLang::HostedLanguage

Inherits:
Object
  • Object
show all
Defined in:
lib/wlang/hosted_language.rb

Overview

Implements the hosted language abstraction of WLang. The hosted language is mainly responsible of evaluating expressions (see evaluate). This abstraction may be implemented by a user's own class, providing that it respected the evaluate method specification.

This default implementation implements the ruby hosted language. It works with ::WLang::HostedLanguage::DSL, which uses instance_eval for evaluating the expression. Calls to missing methods (without parameter and block) are converted in scope lookups. The DSL class is strictly private, as it uses a somewhat complex ruby introspection mechanism to ensure that scoping will not be perturbated by Kernel methods and Object private methods even if additional gems are loaded later.

If you want to intall low-priority variables and tools available in all wlang templates (global scoping) you can reopen the HostedLanguage class itself and override variable_missing. Note that “low-priority” means that these methods will be hidden if a user installs a variable with the same name in its template.

class WLang::HostedLanguage

  # Low-priority now variable
  def now
    Time.now
  end

  # Low-priority variables are checked before raising an UndefinedVariableError
  def variable_missing(name)
    case name
      when :who, ...
        self.send(name)
      else
        raise ::WLang::UndefinedVariableError.new(nil, nil, nil, name)
    end
  end 

end

This class is thread safe, meaning that the same hosting language instance may be safely shared by concurrent wlang parsers. Extending or re-opening this class and using instance variables will make it non thread-safe.

Defined Under Namespace

Classes: DSL

Instance Method Summary collapse

Instance Method Details

#evaluate(expression, parser_state) ⇒ Object

Evaluates a given expression in the context of a given parser state.

This method should always raise

  • an UndefinedVariableError when a given template variable cannot be found.

  • an EvalError when something more severe occurs



141
142
143
# File 'lib/wlang/hosted_language.rb', line 141

def evaluate(expression, parser_state)
  ::WLang::HostedLanguage::DSL.new(self, parser_state).__evaluate__(expression)
end

#variable_missing(name) ⇒ Object

Called when a variable cannot be found (name is a Symbol object). This default implementation raises an UndefinedVariableError. This method is intended to be overriden for handling such a situation more friendly or for installing low-priority global variables (see class documentation).



129
130
131
# File 'lib/wlang/hosted_language.rb', line 129

def variable_missing(name)
  raise ::WLang::UndefinedVariableError.new(nil, nil, nil, name)
end