Module: Red

Defined in:
lib/red.rb,
lib/red/errors.rb,
lib/red/plugin.rb,
lib/red/nodes/call_nodes.rb,
lib/red/nodes/data_nodes.rb,
lib/red/nodes/control_nodes.rb,
lib/red/nodes/illegal_nodes.rb,
lib/red/nodes/literal_nodes.rb,
lib/red/nodes/variable_nodes.rb,
lib/red/nodes/assignment_nodes.rb,
lib/red/nodes/definition_nodes.rb

Overview

:nodoc:

Defined Under Namespace

Modules: RailsBase Classes: AssignmentNode, BuildError, CallNode, ControlNode, DataNode, DefinitionNode, IllegalNode, LiteralNode, VariableNode

Constant Summary collapse

VERSION =
'4.1.3'
ARRAY_NODES =
{
  :and          => LogicNode::Conjunction::And,
  :alias        => DefinitionNode::Alias,
  #:argscat      => IllegalNode::MultipleAssignmentNode,
  #:argspush     => IllegalNode::MultipleAssignmentNode,
  :array        => LiteralNode::Array,
  :attrasgn     => AssignmentNode::Attribute,
  :begin        => ControlNode::Begin,
  :block        => LiteralNode::Multiline,
  :block_arg    => CallNode::Block::Ampersand,
  :block_pass   => CallNode::Ampersand,
  :break        => ControlNode::Keyword::Break,
  :call         => CallNode::Method::ExplicitReceiver,
  :case         => LogicNode::Case,
  :class        => DefinitionNode::Class,
  :cdecl        => AssignmentNode::Constant,
  :colon2       => LiteralNode::Namespace,
  :colon3       => LiteralNode::Namespace::TopLevel,
  :const        => VariableNode::Constant,
  :cvar         => VariableNode::ClassVariable,
  :cvasgn       => AssignmentNode::ClassVariable,
  :cvdecl       => AssignmentNode::ClassVariable,
  :dasgn        => AssignmentNode::LocalVariable,
  :dasgn_curr   => AssignmentNode::LocalVariable,
  :defined      => CallNode::Defined,
  :defn         => DefinitionNode::Method::Instance,
  :defs         => DefinitionNode::Method::Singleton,
  :dot2         => LiteralNode::Range,
  :dot3         => LiteralNode::Range::Exclusive,
  :dregx        => LiteralNode::Regexp,
  :dregx_once   => IllegalNode::RegexEvaluationNode,
  :dstr         => LiteralNode::String,
  :dsym         => LiteralNode::Symbol,
  :dvar         => VariableNode::OtherVariable,
  :dxstr        => LiteralNode::Uninterpreted,
  :ensure       => ControlNode::Ensure,
  :evstr        => LiteralNode::String,
  :false        => LogicNode::Boolean::False,
  :fcall        => CallNode::Method::ImplicitReceiver,
  :flip2        => IllegalNode::FlipflopNode,
  :flip3        => IllegalNode::FlipflopNode,
  :for          => ControlNode::For,
  :gasgn        => AssignmentNode::GlobalVariable,
  :gvar         => VariableNode::OtherVariable,
  :hash         => LiteralNode::Hash,
  :iasgn        => AssignmentNode::InstanceVariable,
  :if           => LogicNode::If,
  :iter         => CallNode::Block,
  :ivar         => VariableNode::InstanceVariable,
  :lasgn        => AssignmentNode::LocalVariable,
  :lvar         => VariableNode::OtherVariable,
  :lit          => LiteralNode::Other,
  :match        => IllegalNode::MatchNode,
  :match2       => CallNode::Match,
  :match3       => CallNode::Match::Reverse,
  :masgn        => AssignmentNode::Multiple,
  :module       => DefinitionNode::Module,
  :next         => ControlNode::Keyword::Next,
  :nil          => VariableNode::Keyword::Nil,
  :not          => LogicNode::Not,
  :op_asgn1     => AssignmentNode::Operator::Bracket,
  :op_asgn2     => AssignmentNode::Operator::Dot,
  :op_asgn_and  => AssignmentNode::Operator::And,
  :op_asgn_or   => AssignmentNode::Operator::Or,
  :or           => LogicNode::Conjunction::Or,
  :postexe      => IllegalNode::PostexeNode,
  :redo         => ControlNode::Keyword::Redo,
  :regex        => LiteralNode::Regexp,
  :resbody      => ControlNode::RescueBody,
  :rescue       => ControlNode::Rescue,
  :retry        => IllegalNode::RetryNode,
  :return       => ControlNode::Return,
  :sclass       => DefinitionNode::SingletonClass,
  :scope        => DefinitionNode::Scope,
  :self         => VariableNode::Keyword::Self,
  :splat        => LiteralNode::Splat,
  :super        => CallNode::Super,
  :svalue       => LiteralNode::Other,
  :str          => LiteralNode::String,
  :sym          => LiteralNode::Symbol,
  :to_ary       => LiteralNode::Array,
  :true         => LogicNode::Boolean::True,
  :undef        => DefinitionNode::Undef,
  :until        => ControlNode::Loop::Until,
  :vcall        => CallNode::Method::ImplicitReceiver,
  :when         => LogicNode::Case::When,
  :while        => ControlNode::Loop::While,
  :xstr         => LiteralNode::Uninterpreted,
  :yield        => CallNode::Yield,
  :zarray       => LiteralNode::Array,
  :zsuper       => CallNode::Super::Delegate
}
DATA_NODES =
{
  Bignum        => DataNode::Numeric,
  Fixnum        => DataNode::Numeric,
  Float         => DataNode::Numeric,
  Range         => DataNode::Range,
  Regexp        => DataNode::Regexp,
  Symbol        => DataNode::Symbol,
  String        => DataNode::String,
  NilClass      => DataNode::Nil
}
METHOD_ESCAPE =
{
  :==           => :_eql2,
  :===          => :_eql3,
  :=~           => :_etld,
  :[]           => :_brac,
  :[]=          => :_breq,
  :<=           => :_lteq,
  :>=           => :_gteq,
  :<<           => :_ltlt,
  :>>           => :_gtgt,
  :<            => :_lthn,
  :>            => :_gthn,
  :'<=>'        => :_ltgt,
  :|            => :_pipe,
  :&            => :_ampe,
  :+            => :_plus,
  :+@           => :_posi,
  :-            => :_subt,
  :-@           => :_nega,
  :*            => :_star,
  :**           => :_str2,
  :/            => :_slsh,
  :%            => :_perc,
  :'^'          => :_care,
  :~            => :_tild
}
NATIVE_CONSTANTS =
%w{
  c$Object
  c$Module
  c$Class
  c$Comparable
  c$Enumerable
  c$Kernel
  c$Math
  c$Math.c$E
  c$Math.c$PI
  c$Array
  c$Exception
  c$StandardError
  c$ArgumentError
  c$IndexError
  c$RangeError
  c$RuntimeError
  c$TypeError
  c$FalseClass
  c$Hash
  c$MatchData
  c$NilClass
  c$Numeric
  c$Proc
  c$Range
  c$Regexp
  c$Regexp.c$IGNORECASE
  c$Regexp.c$EXTENDED
  c$Regexp.c$MULTILINE
  c$String
  c$Symbol
  c$Time
  c$TrueClass
}
INTERNAL_METHODS =
%w{
  []
  []=
  <=>
  ==
  ===
  allocate
  append_features
  backtrace
  block_given?
  call
  class
  each
  extend_object
  extended
  hash
  include
  included
  inherited
  initialize
  inspect
  is_a?
  join
  new
  raise
  sprintf
  superclass
  to_proc
  to_s
  to_str
}.map {|m| m.to_sym }
RAILS_LOADED =
true

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.already_updated?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/red/plugin.rb', line 3

def already_updated?
  @@red_updated ||= false
end

.debugObject



232
233
234
# File 'lib/red.rb', line 232

def self.debug
  false
end

.init(file = '') ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/red.rb', line 218

def self.init(file = '')
  @@red_filepath    = File.dirname(File.expand_path(file))
  @@namespace_stack = []
  @@red_constants   = NATIVE_CONSTANTS
  @@red_methods     = INTERNAL_METHODS
  @@red_function    = nil
  @@red_singleton   = nil
  @@red_block_arg   = nil
  @@red_import      = false
  @@red_boolean     = '`'
  @@red_required    = []
  return true
end

.railsObject



255
256
257
# File 'lib/red.rb', line 255

def self.rails
  require 'red/plugin'
end

.update?(filename) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/red/plugin.rb', line 26

def update?(filename)
  if File.exists?("public/javascripts/#{filename}.js")
    (File.mtime("public/javascripts/red/#{filename}.red") rescue File.mtime("public/javascripts/red/#{filename}.rb")) > File.mtime("public/javascripts/#{filename}.js")
  else
    return true
  end
end

.update_javascriptsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/red/plugin.rb', line 7

def update_javascripts
  @@red_updated = true
  Red.init
  red_dir = 'public/javascripts/red/'
  Dir.glob("#{red_dir}**/*.red").each do |filepath|
    if self.update?(filename = filepath.gsub(red_dir,'').gsub(/.[rb|red]+$/,'')) || true
      js_output = (File.read(filepath).translate_to_sexp_array.red! || '')
      
      filename.split('/')[0...-1].inject('public/javascripts') do |string,dir|
        new_dir = string << '/' << dir
        Dir.mkdir(new_dir) unless File.exists?(new_dir)
        string
      end
      
      File.open("public/javascripts/#{filename}.js", 'w') { |f| f.write(js_output) }
    end
  end
end

Instance Method Details

#is_sexp?(*sexp_types) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/red.rb', line 251

def is_sexp?(*sexp_types)
  self.is_a?(Array) && sexp_types.include?(self.first)
end

#red!(options = {}, reset = false) ⇒ Object



236
237
238
239
240
241
242
243
244
# File 'lib/red.rb', line 236

def red!(options = {}, reset = false)
  case self
  when Array
    raise(BuildError::UnknownNode, "Don't know how to handle sexp type :#{self.first}") unless ARRAY_NODES[self.first]
    return ARRAY_NODES[self.first].new(*(self[1..-1] + [options]))
  else
    return DATA_NODES[self.class].new(self, options)
  end
end

#translate_to_sexp_arrayObject

Raises:



246
247
248
249
# File 'lib/red.rb', line 246

def translate_to_sexp_array
  raise TypeError, "Can only translate Strings" unless self.is_a?(String)
  ParseTree.translate(self)
end