Class: FixtureFox::Tokenizer
- Inherits:
-
Object
- Object
- FixtureFox::Tokenizer
- Defined in:
- lib/fixture_fox/tokenizer.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Minimum path to source file.
-
#lines ⇒ Object
readonly
Array of tokenized lines.
Instance Method Summary collapse
- #call ⇒ Object
- #dump(long: false) ⇒ Object
- #error(msg) ⇒ Object
-
#initialize(file_or_text) ⇒ Tokenizer
constructor
A new instance of Tokenizer.
Constructor Details
#initialize(file_or_text) ⇒ Tokenizer
Returns a new instance of Tokenizer.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fixture_fox/tokenizer.rb', line 12 def initialize(file_or_text) @lines = [] # FIXME: Why @dev_proc ? if file_or_text =~ /\s/ if caller[0] =~ /^(.*):(\d+):in `.*/ file, @lineno = $1, $2.to_i - 1 @file = Pathname.new(Pathname.new(file).).relative_path_from(Pathname.getwd).to_s @dev_proc = lambda { |&block| StringIO.open(file_or_text, &block) } else raise "Oops" end else @lineno = 0 @file = file_or_text @dev_proc = lambda { |&block| File.open(@file, "r", &block) } end end |
Instance Attribute Details
#file ⇒ Object (readonly)
Minimum path to source file. Note that this is a ruby file if the source was inline
7 8 9 |
# File 'lib/fixture_fox/tokenizer.rb', line 7 def file @file end |
#lines ⇒ Object (readonly)
Array of tokenized lines
10 11 12 |
# File 'lib/fixture_fox/tokenizer.rb', line 10 def lines @lines end |
Instance Method Details
#call ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fixture_fox/tokenizer.rb', line 31 def call # Read source source = @dev_proc.call { |dev| dev.readlines.take_while { |l| l !~ /^__END__\s*$/ } } # Find initial indent while !source.empty? && source.first =~ /^\s*(?:#.*)?$/ source.shift @lineno += 1 end !source.empty? or return [] @initial_indent = source.first[/\A */].size while !source.empty? line = source.shift @lineno += 1 # Ignore blank lines and comments next if line =~ /^\s*(?:#.*)?$/ # Find indent indent = line[/\A */].size - @initial_indent indent >= 0 or error("Illegal indent") # Strip prefixed and suffixed blanks line.strip! # Create if line =~ /^@/ @lines << DirectiveLine.new(@file, @lineno, @initial_indent, indent, line) else @lines << Line.new(@file, @lineno, @initial_indent, indent, line) end end @lines end |
#dump(long: false) ⇒ Object
70 71 72 |
# File 'lib/fixture_fox/tokenizer.rb', line 70 def dump(long: false) @lines.each { |l| puts l.to_s(long: long) } end |
#error(msg) ⇒ Object
74 75 76 |
# File 'lib/fixture_fox/tokenizer.rb', line 74 def error(msg) raise ParseError.new(@file, @lineno, @initial_indent, msg) end |