Lexing of various primitive CSS tokens.

This includes brackes, commas, pipes (used for namespaces) and more.
This commit is contained in:
Yorick Peterse 2014-09-19 01:07:06 +02:00
parent aa60115c0a
commit 2ede705f1b
2 changed files with 59 additions and 5 deletions

View File

@ -132,9 +132,12 @@ module Oga
whitespace = [\t ]+;
action emit_space {
add_token(:T_SPACE)
}
comma = ',' @{ add_token(:T_COMMA) };
hash = '#' @{ add_token(:T_HASH) };
dot = '.' @{ add_token(:T_DOT) };
pipe = '|' @{ add_token(:T_PIPE) };
lbrack = '[' @{ add_token(:T_LBRACK) };
rbrack = ']' @{ add_token(:T_RBRACK) };
# Identifiers
#
@ -148,8 +151,9 @@ module Oga
}
main := |*
whitespace | comma | hash | dot | pipe | lbrack | rbrack;
identifier => emit_identifier;
whitespace => emit_space;
any;
*|;

View File

@ -9,9 +9,59 @@ describe Oga::CSS::Lexer do
example 'lex a path with two members' do
lex_css('div h3').should == [
[:T_IDENT, 'div'],
[:T_SPACE, nil],
[:T_IDENT, 'h3']
]
end
example 'lex two paths' do
lex_css('foo, bar').should == [
[:T_IDENT, 'foo'],
[:T_COMMA, nil],
[:T_IDENT, 'bar']
]
end
example 'lex a path selecting an ID' do
lex_css('#foo').should == [
[:T_HASH, nil],
[:T_IDENT, 'foo']
]
end
example 'lex a path selecting a class' do
lex_css('.foo').should == [
[:T_DOT, nil],
[:T_IDENT, 'foo']
]
end
example 'lex a wildcard path' do
lex_css('*').should == [[:T_IDENT, '*']]
end
example 'lex a path containing a namespace name' do
lex_css('foo|bar').should == [
[:T_IDENT, 'foo'],
[:T_PIPE, nil],
[:T_IDENT, 'bar']
]
end
example 'lex a path containing a namespace wildcard' do
lex_css('*|foo').should == [
[:T_IDENT, '*'],
[:T_PIPE, nil],
[:T_IDENT, 'foo']
]
end
example 'lex a path containing a simple predicate' do
lex_css('foo[bar]').should == [
[:T_IDENT, 'foo'],
[:T_LBRACK, nil],
[:T_IDENT, 'bar'],
[:T_RBRACK, nil]
]
end
end
end