Class: Walrus::Grammar::RawText

Inherits:
Walrat::Node
  • Object
show all
Defined in:
lib/walrus/grammar/raw_text.rb

Instance Method Summary collapse

Instance Method Details

#compile(options = {}) ⇒ Object

Returns a string containing the compiled (Ruby) version of receiver. If options is true, instructs the receiver to strip the leading carriage return/line feed from the @lexeme prior to emitting the compiled output.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/walrus/grammar/raw_text.rb', line 33

def compile options = {}
  lexeme = options[:slurping] ? @lexeme.to_s.sub(/\A(\r\n|\r|\n)/, '') : @lexeme.to_s

  compiled  = ''
  first     = true
  lexeme.to_source_string.each do |line|
    newline = ''
    if line =~ /(\r\n|\r|\n)\z/       # check for literal newline at end of line
      line.chomp!                     # get rid of it
      newline = ' + ' + $~[0].dump    # include non-literal newline instead
    end

    if first
      compiled << "accumulate('%s'%s) # RawText\n" % [ line, newline ]
      first = false
    else
      compiled << "accumulate('%s'%s) # RawText (continued)\n" % [ line, newline ]
    end
  end
  compiled
end