Class: Hokusai::Ast

Inherits:
Object
  • Object
show all
Defined in:
ui/src/hokusai/ast.rb

Overview

Wrapper for interacting with asts produced by LibHokusai

Defined Under Namespace

Classes: Event, Func, Loop, Prop

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Ast

Returns a new instance of Ast.



123
124
125
126
127
128
129
130
# File 'ui/src/hokusai/ast.rb', line 123

def initialize(raw)
  @raw = raw
  @dirty = false
  @children = nil
  @siblings = nil
  @classes = nil
  @else_active = false
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



121
122
123
# File 'ui/src/hokusai/ast.rb', line 121

def parent
  @parent
end

#rawObject (readonly)

Returns the value of attribute raw.



120
121
122
# File 'ui/src/hokusai/ast.rb', line 120

def raw
  @raw
end

#targetObject

Returns the value of attribute target.



121
122
123
# File 'ui/src/hokusai/ast.rb', line 121

def target
  @target
end

#updaterObject

Returns the value of attribute updater.



121
122
123
# File 'ui/src/hokusai/ast.rb', line 121

def updater
  @updater
end

Class Method Details

.get(template) ⇒ Object



11
12
13
# File 'ui/src/hokusai/ast.rb', line 11

def self.get(template)
  registry[template]
end

.parse(template, type) ⇒ Hokusai::Ast

Parameters:

  • template (String)

    the template to parse

  • type (String)

    a name for this template (default root)

Returns:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'ui/src/hokusai/ast.rb', line 135

def self.parse(template, type)
  ast = LibHokusai.parse_template(type, template)
  errored = LibHokusai.hoku_errored_ast(ast)

  unless errored.null?
    begin
      raise Hokusai::Error.new(errored[:error]) 
    ensure
      # LibHokusai.hoku_ast_free(errored)
    end
  end

  new(ast)
end

.registryObject



7
8
9
# File 'ui/src/hokusai/ast.rb', line 7

def self.registry
  @registry ||= {}
end

.save(template, ast) ⇒ Object



15
16
17
# File 'ui/src/hokusai/ast.rb', line 15

def self.save(template, ast)
  registry[template] = ast
end

Instance Method Details

#childrenObject



254
255
256
257
258
259
260
261
262
263
# File 'ui/src/hokusai/ast.rb', line 254

def children
  return @children unless @children.nil?

  @children = []
  each_child do |child|
    @children << child
  end

  @children
end

#classesObject



243
244
245
246
247
248
249
250
251
252
# File 'ui/src/hokusai/ast.rb', line 243

def classes
  return @classes unless @classes.nil?

  @classes = []
  each_class do |child|
    @classes << child
  end

  @classes
end

#destroyProp?

Search for an event by name

def event(name)

event_ptr = FFI::MemoryPointer.new :pointer
code = LibHokusai.hoku_ast_prop_init(event_ptr, name)

raise Hokusai::Error.new("Failed to allocate prop") unless code.zero?

event_ptr2 = event_ptr.get_pointer(0)
event = LibHokusai::HmlAstEvent.new(event_ptr2)
response = LibHokusai.hoku_ast_get_event(raw, event)
event_ptr.free

return nil if response.null?

Event.new(response)

ensure

event_ptr.free

end

Parameters:

  • name (String)

    the name of the prop to find

Returns:

  • (Prop?)

    the prop if found



435
436
437
# File 'ui/src/hokusai/ast.rb', line 435

def destroy
  LibHokusai.hoku_ast_free(raw)
end

#dirty!Object



169
170
171
# File 'ui/src/hokusai/ast.rb', line 169

def dirty!
  @dirty = true
end

#dirty?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'ui/src/hokusai/ast.rb', line 173

def dirty?
  @dirty
end

#dumpObject

dumps this ast to STDOUT



440
441
442
# File 'ui/src/hokusai/ast.rb', line 440

def dump
  LibHokusai.hoku_dump(raw, 0)
end

#each_child {|Hokusai::Ast| ... } ⇒ Object

Iterate over each child node of this ast

Yields:



320
321
322
323
324
325
326
327
328
329
330
# File 'ui/src/hokusai/ast.rb', line 320

def each_child
  ast = raw[:relations][:next_child]
  i = 0

  while !ast.null?
    yield Ast.new(ast), i

    i += 1
    ast = ast[:relations][:next_sibling]
  end
end

#each_class {|String| ... } ⇒ Object

Iterate over all class names for this ast

Yields:

  • (String)

    the name of the class



335
336
337
338
339
340
341
342
343
344
345
# File 'ui/src/hokusai/ast.rb', line 335

def each_class
  ast = raw[:class_list]
  i = 0

  while !ast.null?
    yield(ast[:name], i)

    i += 1
    ast = ast[:next]
  end
end

#each_event {|Hokusai::Ast::Event| ... } ⇒ Object

Iterate over all events in this ast



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'ui/src/hokusai/ast.rb', line 350

def each_event
  i = FFI::MemoryPointer.new(:size_t)
  i.write(:size_t, 0)
  item = FFI::MemoryPointer.new :pointer

  while LibHokusai.hashmap_iter(raw[:events], i, item)
    event = item.get_pointer(0)
    event = LibHokusai::HmlAstEvent.new(event)

    yield Event.new(event)
  end

  item.free
  i.free
end

#each_prop {|Hokusai::Ast::Prop| ... } ⇒ Object

Iterate over all props in this ast

Yields:



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'ui/src/hokusai/ast.rb', line 369

def each_prop
  i = FFI::MemoryPointer.new :size_t
  i.write(:size_t, 0)
  item = FFI::MemoryPointer.new :pointer

  while LibHokusai.hashmap_iter(raw[:props], i, item)
    prop = item.get_pointer(0)
    prop = LibHokusai::HmlAstProp.new(prop)

    yield Prop.new(prop)
  end

  item.free
  i.free
end

#else_active=(val) ⇒ Object



207
208
209
# File 'ui/src/hokusai/ast.rb', line 207

def else_active=(val)
  @else_active = val
end

#else_astObject



211
212
213
214
215
# File 'ui/src/hokusai/ast.rb', line 211

def else_ast
  return nil unless has_else_condition?

  Ast.new(raw[:else_relations][:next_child])
end

#else_childrenObject



280
281
282
283
284
285
286
287
288
289
290
291
# File 'ui/src/hokusai/ast.rb', line 280

def else_children
  children = []

  ast = raw[:else_relations][:next_child]
  while !ast.null?
    children << ast

    ast = ast[:relations][:next_sibling]
  end

  children
end

#else_condition_active?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'ui/src/hokusai/ast.rb', line 203

def else_condition_active?
  has_else_condition? && @else_active == 1
end

#event(name) ⇒ Hokusai::Ast::Prop?

Search for a prop by name

def prop(name)

prop_ptr = FFI::MemoryPointer.new :pointer
code = LibHokusai.hoku_ast_prop_init(prop_ptr, name)

raise Hokusai::Error.new("Failed to allocate prop") unless code.zero?

prop_ptr2 = prop_ptr.get_pointer(0)
prop = LibHokusai::HmlAstProp.new(prop_ptr2)
response = LibHokusai.hoku_ast_get_prop(raw, prop)
return nil if response.null?

Prop.new(response)

ensure

prop_ptr.free

end

Parameters:

  • name (String)

    the name of the prop to find

Returns:



409
410
411
# File 'ui/src/hokusai/ast.rb', line 409

def event(name)
  events[name]
end

#eventsObject



305
306
307
308
309
310
311
312
313
314
315
# File 'ui/src/hokusai/ast.rb', line 305

def events
  return @events unless @events.nil?

  @events = {}

  each_event do |event|
    @events[event.name] = event
  end

  @events
end

#has_else_condition?Boolean

Returns:

  • (Boolean)


197
198
199
200
201
# File 'ui/src/hokusai/ast.rb', line 197

def has_else_condition?
  @else_condition = !raw[:else_relations].null? if @else_condition.nil?

  @else_condition
end

#has_if_condition?Boolean

Returns:

  • (Boolean)


191
192
193
194
195
# File 'ui/src/hokusai/ast.rb', line 191

def has_if_condition?
  @if_condition = !raw[:cond].null? if @if_condition.nil?

  @if_condition
end

#idObject



239
240
241
# File 'ui/src/hokusai/ast.rb', line 239

def id
  @id ||= raw[:id].nil? ? "(null)" : raw[:id]
end

#ifObject



223
224
225
226
227
228
229
230
231
232
233
# File 'ui/src/hokusai/ast.rb', line 223

def if
  @cond ||= raw[:cond]

  return nil if @cond.null?

  @call ||= @cond[:call]

  return nil if @call.null?

  @func ||= Func.new(@call)
end

#loopObject



217
218
219
220
221
# File 'ui/src/hokusai/ast.rb', line 217

def loop
  return nil unless loop?

  @loop ||= Loop.new(raw[:loop])
end

#loop?Boolean

Returns:

  • (Boolean)


185
186
187
188
189
# File 'ui/src/hokusai/ast.rb', line 185

def loop?
  @loop_condition = !raw[:loop].null? if @loop_condition.nil?

  @loop_condition
end

#prop(name) ⇒ Object



385
386
387
# File 'ui/src/hokusai/ast.rb', line 385

def prop(name)
  props[name]
end

#propsObject



293
294
295
296
297
298
299
300
301
302
303
# File 'ui/src/hokusai/ast.rb', line 293

def props
  return @props unless @props.nil?

  @props = {}

  each_prop do |prop|
    @props[prop.name] = prop
  end

  @props
end

#siblingsObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'ui/src/hokusai/ast.rb', line 265

def siblings
  return @siblings unless @siblings.nil?

  @siblings = []

  ast = raw[:relations][:next_sibling]
  while !ast.null?
    @siblings << Ast.new(ast)

    ast = ast[:relations][:next_sibling]
  end

  @siblings
end

#slot?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'ui/src/hokusai/ast.rb', line 177

def slot?
  type == "slot"
end

#styleObject



150
151
152
# File 'ui/src/hokusai/ast.rb', line 150

def style
  @style ||= Style.parse_styles(raw[:styles]) unless raw[:styles].null?
end

#style_listObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'ui/src/hokusai/ast.rb', line 154

def style_list
  @style_list ||= begin
    list = []

    head = raw[:style_list]
    until head.null?
      list << head[:name]

      head = head[:next]
    end

    list
  end
end

#typeObject



235
236
237
# File 'ui/src/hokusai/ast.rb', line 235

def type
  @type ||= raw[:type].nil? ? "(null)" : raw[:type]
end

#virtual?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'ui/src/hokusai/ast.rb', line 181

def virtual?
  type == "virtual"
end