Don't use separate tokens/nodes for newlines.

Newlines are now lexed together with regular text. The line numbers are
advanced based on the amount of "\n" sequences in a text buffer.
This commit is contained in:
Yorick Peterse 2014-03-17 21:26:21 +01:00
parent 8898d08831
commit 274ab359ba
2 changed files with 16 additions and 31 deletions

View File

@ -81,8 +81,8 @@ module Oga
private private
def advance_line def advance_line(amount = 1)
@line += 1 @line += amount
@column = 1 @column = 1
end end
@ -113,9 +113,19 @@ module Oga
add_token(:T_TEXT, @text_buffer) add_token(:T_TEXT, @text_buffer)
lines = @text_buffer.count("\n")
advance_line(lines) if lines > 0
@text_buffer = '' @text_buffer = ''
end end
def buffer_text_until_eof(eof)
@text_buffer << text
emit_text_buffer if @te == eof
end
def emit_string_buffer def emit_string_buffer
add_token(:T_STRING, @string_buffer) add_token(:T_STRING, @string_buffer)
advance_column advance_column
@ -134,11 +144,6 @@ module Oga
newline = '\n' | '\r\n'; newline = '\n' | '\r\n';
whitespace = [ \t]; whitespace = [ \t];
action emit_newline {
t(:T_TEXT)
advance_line
}
# String processing # String processing
# #
# These actions/definitions can be used to process single and/or double # These actions/definitions can be used to process single and/or double
@ -300,16 +305,8 @@ module Oga
element_start = '<' element_name; element_start = '<' element_name;
element_text := |* element_text := |*
newline => { ^'<' => {
emit_text_buffer buffer_text_until_eof(eof)
t(:T_TEXT)
advance_line
};
^('<' | newline) => {
@text_buffer << text
emit_text_buffer if @te == eof
}; };
'<' => { '<' => {
@ -391,8 +388,6 @@ module Oga
*|; *|;
main := |* main := |*
newline @emit_text_buffer => emit_newline;
doctype_start @emit_text_buffer => { doctype_start @emit_text_buffer => {
t(:T_DOCTYPE_START) t(:T_DOCTYPE_START)
fcall doctype; fcall doctype;
@ -411,9 +406,7 @@ module Oga
element_start @emit_text_buffer => open_element; element_start @emit_text_buffer => open_element;
any => { any => {
@text_buffer << text buffer_text_until_eof(eof)
emit_text_buffer if @te == eof
}; };
*|; *|;
}%% }%%

View File

@ -14,17 +14,9 @@ describe Oga::Lexer do
lex("\n").should == [[:T_TEXT, "\n", 1, 1]] lex("\n").should == [[:T_TEXT, "\n", 1, 1]]
end end
example 'advance line numbers for newlines' do
lex("\n ").should == [
[:T_TEXT, "\n", 1, 1],
[:T_TEXT, ' ', 2, 1]
]
end
example 'lex text followed by a newline' do example 'lex text followed by a newline' do
lex("foo\n").should == [ lex("foo\n").should == [
[:T_TEXT, 'foo', 1, 1], [:T_TEXT, "foo\n", 1, 1]
[:T_TEXT, "\n", 1, 4]
] ]
end end
end end