1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! CSS 外部扫描器,移植自 tree-sitter-css 的 `src/scanner.c`(MIT,Copyright (c) 2018 Max Brunsfeld)。
//! 无状态:序列化为空。
use super::{ScanLexer, Scanner, is_space};
const DESCENDANT_OP: usize = 0;
const PSEUDO_CLASS_SELECTOR_COLON: usize = 1;
const ERROR_RECOVERY: usize = 2;
#[derive(Default)]
pub struct Css;
impl Scanner for Css {
fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
if v[ERROR_RECOVERY] {
return false;
}
if is_space(lx.lookahead()) && v[DESCENDANT_OP] {
lx.set_result(DESCENDANT_OP as u16);
lx.skip();
while is_space(lx.lookahead()) {
lx.skip();
}
lx.mark_end();
let c = lx.lookahead();
if matches!(c, '#' | '.' | '[' | '-' | '*') || c.is_alphanumeric() {
return true;
}
if c == ':' {
lx.next();
if is_space(lx.lookahead()) {
return false;
}
loop {
if matches!(lx.lookahead(), ';' | '}') || lx.eof() {
return false;
}
if lx.lookahead() == '{' {
return true;
}
lx.next();
}
}
}
if v[PSEUDO_CLASS_SELECTOR_COLON] {
while is_space(lx.lookahead()) {
lx.skip();
}
if lx.lookahead() == ':' {
lx.next();
if lx.lookahead() == ':' {
return false;
}
lx.mark_end();
lx.set_result(PSEUDO_CLASS_SELECTOR_COLON as u16);
// 需要 `{` 才是伪类选择器,`;` 说明是属性;注释里的字符不算
let mut in_comment = false;
while !matches!(lx.lookahead(), ';' | '}') && !lx.eof() {
lx.next();
if lx.lookahead() == '{' && !in_comment {
return true;
}
if lx.lookahead() == '/' && !in_comment {
lx.next();
if lx.lookahead() == '*' {
in_comment = true;
}
} else if lx.lookahead() == '*' && in_comment {
lx.next();
if lx.lookahead() == '/' {
in_comment = false;
}
}
}
// 到了输入末尾也返回伪类选择器,错误恢复时更好
return lx.eof();
}
}
false
}
fn serialize(&self, _out: &mut Vec<u8>) {}
fn deserialize(&mut self, _data: &[u8]) {}
}