Class: Depager::Generator

Inherits:
Object
  • Object
show all
Includes:
Utils::CommonMethods
Defined in:
lib/depager.rb

Direct Known Subclasses

ExtensionGenerator, ParserGenerator

Constant Summary collapse

TEMPLATES_DIR =
File.expand_path("depager/ruby/templates", __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::CommonMethods

#error_exit, #error_message, #expanded_code_delimiter, #file, #full_target_name, #input_path, #inspect, #target_name, #target_namespace, #warning

Constructor Details

#initialize(d_parser) ⇒ Generator

Returns a new instance of Generator.



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/depager.rb', line 366

def initialize(d_parser)
  @d_parser = d_parser
  @g_parser = d_parser.g_parser
  @parsing_method = nil

  @options      = {}
  @decorators   = []
  @requirements = []
  @extensions   = []
  @inner_code   = ""
  @outer_code   = ""
  @setup_code   = ""
  @precs        = []
  @mixins       = []
end

Instance Attribute Details

#d_parserObject (readonly)

Returns the value of attribute d_parser.



363
364
365
# File 'lib/depager.rb', line 363

def d_parser
  @d_parser
end

#decoratorsObject

Returns the value of attribute decorators.



364
365
366
# File 'lib/depager.rb', line 364

def decorators
  @decorators
end

#g_parserObject (readonly)

Returns the value of attribute g_parser.



363
364
365
# File 'lib/depager.rb', line 363

def g_parser
  @g_parser
end

#inner_codeObject

Returns the value of attribute inner_code.



364
365
366
# File 'lib/depager.rb', line 364

def inner_code
  @inner_code
end

#optionsObject

Returns the value of attribute options.



364
365
366
# File 'lib/depager.rb', line 364

def options
  @options
end

#outer_codeObject

Returns the value of attribute outer_code.



364
365
366
# File 'lib/depager.rb', line 364

def outer_code
  @outer_code
end

#parsing_methodObject (readonly)

Returns the value of attribute parsing_method.



363
364
365
# File 'lib/depager.rb', line 363

def parsing_method
  @parsing_method
end

#requirementsObject

Returns the value of attribute requirements.



364
365
366
# File 'lib/depager.rb', line 364

def requirements
  @requirements
end

#setup_codeObject

Returns the value of attribute setup_code.



364
365
366
# File 'lib/depager.rb', line 364

def setup_code
  @setup_code
end

Instance Method Details

#generate_code(template) ⇒ Object



382
383
384
# File 'lib/depager.rb', line 382

def generate_code(template)
  ERB.new(template || "", trim_mode: "-").result(binding)
end

#parse_block(raw: false) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/depager.rb', line 405

def parse_block(raw: false)
  lineno = file.lineno
  val = ""
  until file.eof?
    line = file.gets
    break if /^%\}/.match?(line)

    val.concat line
  end
  return val if raw

  delimiter = expanded_code_delimiter
  <<~EXPANDED_CODE
    module_eval <<'#{delimiter}', '#{file.path}', #{lineno + 1}
    #{val}
    #{delimiter}
  EXPANDED_CODE
end

#parse_common(line) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/depager.rb', line 424

def parse_common(line)
  case line
  when /^\s*(#.*)?$/
    # skip space and comment.
  when /^%decorate\s+(.*?)\s*\((.*)\)\s*$/
    @decorators.push $1
    @requirements.push $2
  when /^%decorate\s+(.*?)\s*$/
    @decorators.push $1
  when /^%extend\s+(.*?)\s*\(\s*'(.*)'\s*\)\s*$/
    @extensions.push [$2.strip, $1] unless @extensions.find { |i| i[1] == $1 }
  when /^%option\s+([\w:]+).(\w+)\s+(.*)\s*$/
    if $1 == "Generator"
      Depager.configuration[$2.to_sym] = $3
    else
      @options[$1] ||= []
      @options[$1] << [$2, $3]
    end
  when /^%require\s+'(.+?)'\s*$/
    @requirements.push "'#{$1}'"
  when /^%mixin\s+(\S+)\s*(\((.+)\)\s*)?$/
    @mixins.push $1
    @requirements.push $3 if $3
  when /^%inner\s*\{\s*$/
    @inner_code << parse_block
  when /^%prec\s*\{\s*$/
    @precs = parse_prec
  when /^%expanded_code_delimiter\s+(\S+)\s*$/
    Depager.configuration[:expanded_code_delimiter] = $1
  when /^%abort_behavior\s+(pending\s+)?(.*)$/
    @inner_code << "    def do_abort_driver; end\n" if $1
    @inner_code << "    def abort_driver; #{$2}; end\n" unless $2.strip.empty?
  else
    warning "syntax error(declaration).\n> #{line}"
  end
end

#parse_precObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/depager.rb', line 386

def parse_prec
  precs = []
  until file.eof?
    case line = file.gets
    when /^\s*$/, /^\s*#.*$/
      # skip
    when /^\s*(left|right|nonassoc)\s+(.*)$/
      dir = $1.upcase.intern
      syms = $2.split.map { |i| i =~ /'(.+?)'/ ? $1 : i.intern }
      precs.push [dir, syms]
    when /^%\}\s*$/
      break
    else
      error_exit "syntax error(prec).\n> #{line}"
    end
  end
  precs
end