Class: Reco::Preprocessor
- Inherits:
-
Object
- Object
- Reco::Preprocessor
- Defined in:
- lib/reco/preprocessor.rb
Defined Under Namespace
Classes: Error
Class Method Summary collapse
Instance Method Summary collapse
- #begin_code(options) ⇒ Object
- #dedent ⇒ Object
- #execute ⇒ Object
- #fail(message) ⇒ Object
- #indent(capture = nil) ⇒ Object
-
#initialize(source) ⇒ Preprocessor
constructor
A new instance of Preprocessor.
- #print_string(string) ⇒ Object
- #record(line) ⇒ Object
- #record_code(code) ⇒ Object
Constructor Details
#initialize(source) ⇒ Preprocessor
Returns a new instance of Preprocessor.
10 11 12 13 14 15 16 |
# File 'lib/reco/preprocessor.rb', line 10 def initialize(source) @scanner = Scanner.new source @output = '' @level = 0 = {} @captures = [] end |
Class Method Details
.preprocess(source) ⇒ Object
6 7 8 |
# File 'lib/reco/preprocessor.rb', line 6 def self.preprocess(source) new(source).execute end |
Instance Method Details
#begin_code(options) ⇒ Object
32 33 34 |
# File 'lib/reco/preprocessor.rb', line 32 def begin_code() = end |
#dedent ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/reco/preprocessor.rb', line 55 def dedent @level -= 1 fail "unexpected dedent" if @level < 0 if @captures[0] == @level @captures.shift dedent end end |
#execute ⇒ Object
18 19 20 21 |
# File 'lib/reco/preprocessor.rb', line 18 def execute @scanner.scan { |token| send token.shift, *token } until @scanner.done? @output end |
#fail(message) ⇒ Object
64 65 66 |
# File 'lib/reco/preprocessor.rb', line 64 def fail() raise Error, end |
#indent(capture = nil) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/reco/preprocessor.rb', line 46 def indent(capture = nil) @level += 1 if capture record "__capture #{capture}" @captures.unshift @level indent end end |
#print_string(string) ⇒ Object
28 29 30 |
# File 'lib/reco/preprocessor.rb', line 28 def print_string(string) record "__out.push #{Reco::Util.inspect_string(string)}" unless string.empty? end |
#record(line) ⇒ Object
23 24 25 26 |
# File 'lib/reco/preprocessor.rb', line 23 def record(line) @output += @level.times.collect { " " }.join @output += "#{line}\n" end |
#record_code(code) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/reco/preprocessor.rb', line 36 def record_code(code) if code != 'end' if [:print] record [:safe] ? "__out.push #{code}" : "__out.push __sanitize #{code}" else record code end end end |