Lexing of bare regular text.

This is currently a bit of a hack but at least we're slowly getting there.
This commit is contained in:
Yorick Peterse 2014-03-13 00:42:12 +01:00
parent 2fbca93ae8
commit 34f8779c94
3 changed files with 30 additions and 7 deletions

View File

@ -120,6 +120,10 @@ module Oga
@text_buffer << text @text_buffer << text
} }
action emit_text_buffer {
emit_text_buffer
}
action buffer_string { action buffer_string {
@string_buffer << text @string_buffer << text
} }
@ -326,24 +330,30 @@ module Oga
*|; *|;
main := |* main := |*
newline => emit_newline; newline @emit_text_buffer => emit_newline;
doctype_start => { doctype_start @emit_text_buffer => {
t(:T_DOCTYPE_START) t(:T_DOCTYPE_START)
fcall doctype; fcall doctype;
}; };
cdata_start => { cdata_start @emit_text_buffer => {
t(:T_CDATA_START) t(:T_CDATA_START)
fcall cdata; fcall cdata;
}; };
comment_start => { comment_start @emit_text_buffer => {
t(:T_COMMENT_START) t(:T_COMMENT_START)
fcall comment; fcall comment;
}; };
element_start => open_element; element_start @emit_text_buffer => open_element;
any => {
@text_buffer << text
emit_text_buffer if @te == eof
};
*|; *|;
}%% }%%
end # Lexer end # Lexer

View File

@ -25,6 +25,14 @@ describe Oga::Lexer do
[:T_ELEM_CLOSE, nil, 1, 9] [:T_ELEM_CLOSE, nil, 1, 9]
] ]
end end
example 'lex text followed by a paragraph element' do
lex('Foo<p>').should == [
[:T_TEXT, 'Foo', 1, 1],
[:T_ELEM_OPEN, nil, 1, 4],
[:T_ELEM_NAME, 'p', 1, 5]
]
end
end end
context 'elements with attributes' do context 'elements with attributes' do

View File

@ -5,9 +5,7 @@ describe Oga::Lexer do
example 'lex regular text' do example 'lex regular text' do
lex('hello').should == [[:T_TEXT, 'hello', 1, 1]] lex('hello').should == [[:T_TEXT, 'hello', 1, 1]]
end end
end
context 'whitespace' do
example 'lex regular whitespace' do example 'lex regular whitespace' do
lex(' ').should == [[:T_TEXT, ' ', 1, 1]] lex(' ').should == [[:T_TEXT, ' ', 1, 1]]
end end
@ -22,5 +20,12 @@ describe Oga::Lexer do
[:T_TEXT, ' ', 2, 1] [:T_TEXT, ' ', 2, 1]
] ]
end end
example 'lex text followed by a newline' do
lex("foo\n").should == [
[:T_TEXT, 'foo', 1, 1],
[:T_TEXT, "\n", 1, 4]
]
end
end end
end end