Class: Bijou::Parse::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/bijou/backend.rb

Overview

The backend is used by the parser to generate a Ruby representation of a Bijou component.

Defined Under Namespace

Classes: ParseContext

Instance Method Summary collapse

Constructor Details

#initialize(diagnostics, trace, use_markers) ⇒ Backend

Returns a new instance of Backend.



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/bijou/backend.rb', line 280

def initialize(diagnostics, trace, use_markers)
  @trace = trace
  @use_markers = use_markers

  @buffer = ''
  @parseContext = ParseContext::Normal
  
  @diagnostics = diagnostics

  @currentComponent = @component = Bijou::Parse::Component.new(use_markers)
  @argsCollection = nil
  @isDefContext = false

  # TODO: Make these installable.
  @filters = {
    'u' => Bijou::EncodeURL.new,
    'h' => Bijou::EncodeHTML.new,
    'a' => Bijou::EncodeAttributeValue.new,
    't' => Bijou::EncodeTrim.new,
    'w' => Bijou::EncodeWiki.new,
  }
end

Instance Method Details

#add_argument(argName, argValue, filename, line, column) ⇒ Object



529
530
531
532
533
534
535
536
537
538
# File 'lib/bijou/backend.rb', line 529

def add_argument(argName, argValue, filename, line, column)
  if @argsCollection
    if @argsCollection.args.has_key?(argName)
      error("argument '#{argName}' overrides previous delcaration", line, column)
    end
    @argsCollection.add_argument(argName, argValue, filename, line)
  else
    error("unexpected argument", line, column)
  end
end

#buffer(s) ⇒ Object

Used to accumulate raw text from the parser.



304
305
306
# File 'lib/bijou/backend.rb', line 304

def buffer(s)
  @buffer << s
end

#call_tag(identifier, args, indirect, filename, line) ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/bijou/backend.rb', line 488

def call_tag(identifier, args, indirect,  filename, line)
  # Convert the argument list [[arg1, val1], [arg2, val2], ...] into
  # a list of formatted string values.
  list = []

  args.each {|item|
    list.push "'#{item[0]}' => #{item[1]}"
  }

  # Join the list of strings into a formatted hash.
  text = "{#{list.join(', ')}}"

  @currentComponent.render_marker(line, filename) if @use_markers

  if indirect
    # REVIEW: The indirection method currently shares the arguments with the
    # method to be invoked. This may not be desirable. We would need an 
    # different call syntax to do otherwise. If different arguments are 
    # required, invoke may be called directly in a code block.
    @currentComponent.render_code("    @context.invoke(#{identifier}(#{text}), #{text})")
  else
    @currentComponent.render_code("    @context.invoke('#{identifier}', #{text})")
  end
end

#directive_tag(hash, line, column) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/bijou/backend.rb', line 513

def directive_tag(hash, line, column)
  trace "\nDirectives:"
  hash.each { |key, value|
    trace "#{key} => #{value}"

    if value =~ /^[\'\"](.*)[\'\"]$/
      value = $1
    end

    if @component.directives.has_key?(key)
      warning("directive '#{key}' overrides previous definition", line, column)
    end
    @component.directives[key] = value
  }
end

#error(s, line = nil, column = nil) ⇒ Object



345
346
347
348
349
350
# File 'lib/bijou/backend.rb', line 345

def error(s, line=nil, column=nil)
  m = Bijou::Parse::Error.new
  m.at(line, column)
  m << s
  @diagnostics.add_error(m)
end

#inline_tag(text, filename, line) ⇒ Object



466
467
468
469
# File 'lib/bijou/backend.rb', line 466

def inline_tag(text, filename, line)
  @currentComponent.render_marker(line, filename) if @use_markers
  @currentComponent.render_code(text)
end

#markup_section(buffer, filename, line) ⇒ Object



376
377
378
379
380
381
382
383
384
# File 'lib/bijou/backend.rb', line 376

def markup_section(buffer, filename, line)
  @currentComponent.render_marker(line, filename) if @use_markers

  if @isDefContext
    @currentComponent.render_code(buffer)
  else
    @currentComponent.render_part(buffer)
  end
end

#message(s, line = nil, column = nil) ⇒ Object



331
332
333
334
335
336
# File 'lib/bijou/backend.rb', line 331

def message(s, line=nil, column=nil)
  m = Bijou::Parse::Message.new
  m.at(line, column)
  m << s
  @diagnostics.add_message(m)
end

#named_end_tag(prefix) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/bijou/backend.rb', line 443

def named_end_tag(prefix)
  if @argsCollection
    @argsCollection = nil;
    return
  end

  case @parseContext
  when ParseContext::MethodTag;
    @component.add_method(@currentComponent)
    @currentComponent = @component
    
    @parseContext = ParseContext::Normal
    @isDefContext = false
  when 'args'
    if !@argsCollection
      error("unexpected args end tag", line, column)
    end

    @argsCollection = nil
  else
  end
end

#named_start_tag(prefix, name, line, column) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/bijou/backend.rb', line 392

def named_start_tag(prefix, name, line, column)
  case prefix
  when 'method'
    if @parseContext != ParseContext::Normal
      error("method declared at nested scope", line, column)
      return
    end

    @parseContext = ParseContext::MethodTag
    @currentComponent = Bijou::Parse::Method.new(name)
    @isDefContext = false

  when 'init'
    if @parseContext != ParseContext::Normal
      error("init declared at nested scope", line, column)
      return
    end

    @parseContext = ParseContext::MethodTag
    @currentComponent = Bijou::Parse::Method.new(prefix)
    @isDefContext = true

  when 'fini'
    if @parseContext != ParseContext::Normal
      error("fini declared at nested scope", line, column)
      return
    end

    @parseContext = ParseContext::MethodTag
    @currentComponent = Bijou::Parse::Method.new(prefix)
    @isDefContext = true

  when 'args'
    if @argsCollection
      error("args may not be declared within another args section.", line, column)
      return
    end

    # NOTE: We don't change parse context.
    @argsCollection = Bijou::Parse::ArgumentCollection.new

    if @currentComponent.args
      warning("args section overrides previous args section.", line, column)
    end

    @currentComponent.args = @argsCollection
  else
    raise "unexpected named tag #{prefix}"
  end
end

#output_tag(text, filters, filename, line) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/bijou/backend.rb', line 471

def output_tag(text, filters, filename, line)
  filtered = "(#{text}).to_s"

  if filters
    filters.each{|filter|
      if @filters.has_key? filter
        filtered = @filters[filter].render(filtered)
      else
        warning("unrecognized output tag filter '#{filter}'", filename, line)
      end
    }
  end

  @currentComponent.render_marker(line, filename) if @use_markers
  @currentComponent.render_expr(filtered)
end

Flushes the buffer to the output stream without a newline. Often used before an inline tag <% … %>.



321
322
323
324
325
326
327
328
329
# File 'lib/bijou/backend.rb', line 321

def print_buffer()
  if @isDefContext
    @currentComponent.render_code_(@buffer)
  else
    @currentComponent.render_part(@buffer)
  end

  @buffer = ''
end

#puts_bufferObject

Used to flush the buffer to the output stream with a newline



309
310
311
312
313
314
315
316
317
# File 'lib/bijou/backend.rb', line 309

def puts_buffer()
  if @isDefContext
    @currentComponent.render_code(@buffer)
  else
    @currentComponent.render_line(@buffer)
  end

  @buffer = ''
end

#render(component, source_filename = nil, cache_filename = nil, component_base = nil, require_list = nil) ⇒ Object

Parser methods

++



362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/bijou/backend.rb', line 362

def render(component, source_filename=nil, cache_filename=nil, 
           component_base=nil, require_list=nil)
  @component.component = component
  @component.source_filename = source_filename
  @component.cache_filename = cache_filename
  if component_base
    @component.component_base = component_base
  end
  if require_list
    @component.require_list = require_list
  end
  @component.to_s
end

#tag_open(tagStart) ⇒ Object



386
387
388
389
390
# File 'lib/bijou/backend.rb', line 386

def tag_open(tagStart)
  print_buffer

  trace "\nopentag: #{tagStart}" 
end

#trace(s) ⇒ Object



352
353
354
# File 'lib/bijou/backend.rb', line 352

def trace(s)
  puts s if @trace
end

#warning(s, line = nil, column = nil) ⇒ Object



338
339
340
341
342
343
# File 'lib/bijou/backend.rb', line 338

def warning(s, line=nil, column=nil)
  m = Bijou::Parse::Warning.new
  m.at(line, column)
  m << s
  @diagnostics.add_warning(m)
end