Class: Jejune::Macro

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/jejune/macro.rb

Constant Summary

Constants included from Constants

Constants::BROWSERS, Constants::CATCH_TYPES, Constants::CODE, Constants::DEFAULT_EXT, Constants::DELIMS, Constants::EXPRESSION_TYPES, Constants::FUNCTION_TYPES, Constants::OPEN_DELIM, Constants::OPERATOR_PRECEDENCE, Constants::PROPERTY_DEFINITION_TYPES, Constants::PROPERTY_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, tree) ⇒ Macro

Returns a new instance of Macro.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jejune/macro.rb', line 13

def initialize( manager, tree )
  @manager = manager
  #@tree    = tree
  @name    = tree[ 0 ].text
  
  if node = tree[ 1 ] and node.type == PARAMS
    @parameters = ParameterSet.study( node )
  else
    @parameters = ParameterSet.new
  end
  
  body_tokens = tree.last.tokens
  
  body_tokens.shift while t = body_tokens.first and t.type != LBRACE
  body_tokens.shift
  
  body_tokens.pop while t = body_tokens.last and t.type != RBRACE
  body_tokens.pop
  
  body_tokens.map! do | token |
    if token.type == ID and index = @parameters.include?( token.text )
      index
    else
      token.text
    end
  end
  
  @blueprint = body_tokens
end

Instance Attribute Details

#blueprintObject

Returns the value of attribute blueprint.



11
12
13
# File 'lib/jejune/macro.rb', line 11

def blueprint
  @blueprint
end

#managerObject

Returns the value of attribute manager.



11
12
13
# File 'lib/jejune/macro.rb', line 11

def manager
  @manager
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/jejune/macro.rb', line 11

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



11
12
13
# File 'lib/jejune/macro.rb', line 11

def parameters
  @parameters
end

Class Method Details

._load(serialized) ⇒ Object



43
44
45
46
47
# File 'lib/jejune/macro.rb', line 43

def self._load( serialized )
  macro = allocate
  macro.name, macro.parameters, macro.blueprint = Marshal.load( serialized )
  return macro
end

Instance Method Details

#_dump(depth) ⇒ Object



49
50
51
# File 'lib/jejune/macro.rb', line 49

def _dump( depth )
  Marshal.dump( [ @name, @parameters, @blueprint ] )
end

#apply(*params) ⇒ Object Also known as: expand



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jejune/macro.rb', line 53

def apply( *params )
  params =
    params.map do | i |
      if i.respond_to?( :tokens ) then i.tokens
      elsif TokenData::Token === i then [ i ]
      else i
      end
    end
  
  tokens = []
  for item in @blueprint
    case item
    when Fixnum
      tokens.concat( params[ item ] )
    else
      tokens << item
    end
  end
  
  @manager.translate( tokens.join( '' ) )
end