Class: ReVIEW::Compiler

Inherits:
Object show all
Defined in:
lib/review/compiler.rb

Defined Under Namespace

Classes: InlineSyntaxElement, SyntaxElement

Constant Summary collapse

SYNTAX =
{}
INLINE =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy) ⇒ Compiler

Returns a new instance of Compiler.



44
45
46
# File 'lib/review/compiler.rb', line 44

def initialize(strategy)
  @strategy = strategy
end

Instance Attribute Details

#strategyObject (readonly)

Returns the value of attribute strategy.



48
49
50
# File 'lib/review/compiler.rb', line 48

def strategy
  @strategy
end

Class Method Details

.defblock(name, argc, optional = false, &block) ⇒ Object



93
94
95
# File 'lib/review/compiler.rb', line 93

def Compiler.defblock(name, argc, optional = false, &block)
  defsyntax name, (optional ? :optional : :block), argc, &block
end

.definline(name) ⇒ Object



123
124
125
# File 'lib/review/compiler.rb', line 123

def Compiler.definline(name)
  INLINE[name] = InlineSyntaxElement.new(name)
end

.defsingle(name, argc, &block) ⇒ Object



97
98
99
# File 'lib/review/compiler.rb', line 97

def Compiler.defsingle(name, argc, &block)
  defsyntax name, :line, argc, &block
end

.defsyntax(name, type, argc, &block) ⇒ Object



101
102
103
# File 'lib/review/compiler.rb', line 101

def Compiler.defsyntax(name, type, argc, &block)
  SYNTAX[name] = SyntaxElement.new(name, type, argc, &block)
end

Instance Method Details

#compile(chap) ⇒ Object



50
51
52
53
54
# File 'lib/review/compiler.rb', line 50

def compile(chap)
  @chapter = chap
  do_compile
  @strategy.result
end

#inline_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/review/compiler.rb', line 127

def inline_defined?(name)
  INLINE.key?(name.to_sym)
end

#syntax_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/review/compiler.rb', line 105

def syntax_defined?(name)
  SYNTAX.key?(name.to_sym)
end

#syntax_descriptor(name) ⇒ Object



109
110
111
# File 'lib/review/compiler.rb', line 109

def syntax_descriptor(name)
  SYNTAX[name.to_sym]
end

#text(str) ⇒ Object



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/review/compiler.rb', line 509

def text(str)
  return '' if str.empty?
  words = str.split(/(@<\w+>\{(?:[^\}\\]|\\.)*?\})/, -1)
  words.each do |w|
    error "`@<xxx>' seen but is not valid inline op: #{w}" if w.scan(/@<\w+>/).size > 1 && !/\A@<raw>/.match(w)
  end
  result = @strategy.nofunc_text(words.shift)
  until words.empty?
    result << compile_inline(words.shift.gsub(/\\\}/, '}'))
    result << @strategy.nofunc_text(words.shift)
  end
  result
rescue => err
  error err.message
end