From 191875a85b70f3a3833267d8ad55abbbeb1aeea7 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 26 Dec 2009 10:59:47 -0800 Subject: [PATCH] got a lexer working along the lines of what kamatsu proposes --- lib/coffee_script/lexer.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/coffee_script/lexer.rb b/lib/coffee_script/lexer.rb index abd45592..3890d8a2 100644 --- a/lib/coffee_script/lexer.rb +++ b/lib/coffee_script/lexer.rb @@ -132,17 +132,24 @@ module CoffeeScript return literal_token if size == @indent if size > @indent token(:INDENT, size - @indent) + @indents << size - @indent @indent = size - @indents << @indent else - token(:OUTDENT, @indent - size) - @indents.pop while @indents.last && ((@indents.last || 0) > size) - @indent = @indents.last || 0 + outdent_token(@indent - size) end @line += 1 @i += (size + 1) end + def outdent_token(move_out) + while move_out > 0 + last_indent = @indents.pop + token(:OUTDENT, last_indent) + move_out -= last_indent + end + @indent = @indents.last || 0 + end + # Matches and consumes non-meaningful whitespace. def whitespace_token return false unless whitespace = @chunk[WHITESPACE, 1] @@ -214,9 +221,7 @@ module CoffeeScript # Close up all remaining open blocks. def close_indentation - while indent = @indents.pop - token(:OUTDENT, @indents.first || 0) - end + outdent_token(@indent) end end