Class: PSD::EngineData

Inherits:
Object
  • Object
show all
Includes:
DocumentHelpers
Defined in:
lib/psd/enginedata.rb,
lib/psd/enginedata/node.rb,
lib/psd/enginedata/text.rb,
lib/psd/enginedata/errors.rb,
lib/psd/enginedata/version.rb,
lib/psd/enginedata/instruction.rb,
lib/psd/enginedata/document_helpers.rb,
lib/psd/enginedata/instructions/noop.rb,
lib/psd/enginedata/instructions/number.rb,
lib/psd/enginedata/instructions/string.rb,
lib/psd/enginedata/instructions/boolean.rb,
lib/psd/enginedata/instructions/hash_end.rb,
lib/psd/enginedata/instructions/property.rb,
lib/psd/enginedata/instructions/hash_start.rb,
lib/psd/enginedata/instructions/single_line_array.rb,
lib/psd/enginedata/instructions/property_with_data.rb,
lib/psd/enginedata/instructions/number_with_decimal.rb,
lib/psd/enginedata/instructions/multi_line_array_end.rb,
lib/psd/enginedata/instructions/multi_line_array_start.rb

Overview

:nodoc:

Defined Under Namespace

Modules: DocumentHelpers Classes: Instruction, Node, Text, TokenNotFound

Constant Summary collapse

INSTRUCTIONS =

All of the instructions as defined by the EngineData spec.

[
  Instruction::HashStart,
  Instruction::HashEnd,
  Instruction::SingleLineArray,
  Instruction::MultiLineArrayStart,
  Instruction::MultiLineArrayEnd,
  Instruction::Property,
  Instruction::PropertyWithData,
  Instruction::String,
  Instruction::NumberWithDecimal,
  Instruction::Number,
  Instruction::Boolean,
  Instruction::Noop
]
VERSION =
"1.1.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DocumentHelpers

#reset_node, #set_node, #set_property, #stack_pop, #stack_push, #update_node

Constructor Details

#initialize(text) ⇒ EngineData

Create a new Text instance and initialize our parsing stacks.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/psd/enginedata.rb', line 44

def initialize(text)
  @text = Text.new(text)

  @property_stack = []
  @node_stack = []

  @property = :root
  @node = nil

  @parsed = false
end

Instance Attribute Details

#nodeObject Also known as: result

Returns the value of attribute node.



17
18
19
# File 'lib/psd/enginedata.rb', line 17

def node
  @node
end

#node_stackObject

Returns the value of attribute node_stack.



17
18
19
# File 'lib/psd/enginedata.rb', line 17

def node_stack
  @node_stack
end

#propertyObject

Returns the value of attribute property.



17
18
19
# File 'lib/psd/enginedata.rb', line 17

def property
  @property
end

#property_stackObject

Returns the value of attribute property_stack.



17
18
19
# File 'lib/psd/enginedata.rb', line 17

def property_stack
  @property_stack
end

#textObject (readonly)

Returns the value of attribute text.



16
17
18
# File 'lib/psd/enginedata.rb', line 16

def text
  @text
end

Class Method Details

.load(file) ⇒ Object

Read a file containing EngineData markup and initialize a new instance.



39
40
41
# File 'lib/psd/enginedata.rb', line 39

def self.load(file)
  self.new File.new(file, 'rb').read
end

Instance Method Details

#parse!Object

Parses the full document.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/psd/enginedata.rb', line 57

def parse!
  return if parsed?

  while true
    line = @text.current
    @parsed = true and return if line.nil?

    parse_tokens(line)
    @text.next!
  end
end

#parse_tokens(text) ⇒ Object

Go through each instruction until a token match is found, then parse the matches.

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/psd/enginedata.rb', line 76

def parse_tokens(text)
  INSTRUCTIONS.each do |inst|
    match = inst.match(text)
    return inst.new(self, text).execute! if match
  end

  # This is a hack for the Japanese character rules that the format embeds
  match = Instruction::String.match(text + @text.next)
  if match
    text += @text.next!
    return Instruction::String.new(self, text).execute!
  end

  raise TokenNotFound.new("Text = #{text.dump}, Line = #{@text.line + 1}")
end

#parsed?Boolean

Has the document been parsed yet?

Returns:

  • (Boolean)


70
71
72
# File 'lib/psd/enginedata.rb', line 70

def parsed?
  @parsed
end