Class: Arcana::File

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



468
469
470
471
# File 'lib/arcana.rb', line 468

def initialize(path)
  @path = path
  @rules = parse
end

Instance Method Details

#parseObject



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/arcana.rb', line 481

def parse
  rules = []
  stack = []

  ::File.foreach(@path) do |line|
    if line.start_with?("#")
      # comment
    elsif line.match?(/\A\s+\z/)
      # blank
    elsif line.start_with?("!")
      if line =~ /\A!:([a-z]+)\s+(.*)\n\z/
        raise if stack.empty?
        stack.last.extras[$1] = $2
      else
        raise "couldn't parse #{line}"
      end
    else
      fields = line.chomp.split(/(?<![\\<>])\s+/, 4)
      offset, type, test, message = fields
      nesting = offset[/\A>*/].size

      stack = stack[0, nesting]

      offset = Offset.new offset[nesting..]
      pattern = Pattern.new(type, test)

      rule = Rule.new(offset, pattern, message)
      if stack.empty?
        rules << rule
      else
        stack.last.children << rule
      end
      stack << rule
    end
  end
  rules
end

#raw_rulesObject



473
474
475
# File 'lib/arcana.rb', line 473

def raw_rules
  @rules
end

#rulesObject



477
478
479
# File 'lib/arcana.rb', line 477

def rules
  RuleSet.new(@rules)
end