Skip to main content

pagesift_syntax/scanner/
dockerfile.rs

1//! Dockerfile 外部扫描器,移植自 tree-sitter-dockerfile 的 `src/scanner.c`(MIT,Copyright (c) 2021 Camden Cheek)。
2
3use super::{MAX_SERIALIZED, ScanLexer, Scanner, is_space};
4
5const MAX_HEREDOCS: usize = 10;
6const DEL_SPACE: usize = 512;
7
8const HEREDOC_MARKER: usize = 0;
9const HEREDOC_LINE: usize = 1;
10const HEREDOC_END: usize = 2;
11const HEREDOC_NL: usize = 3;
12const ERROR_SENTINEL: usize = 4;
13
14#[derive(Default)]
15pub struct Dockerfile {
16    in_heredoc: bool,
17    stripping: bool,
18    /// 每项第一个字节是 `-`(缩进剥离)或空格,其后是分隔符。
19    heredocs: Vec<Vec<u8>>,
20}
21
22fn skip_whitespace(lx: &mut ScanLexer) {
23    while lx.lookahead() != '\0' && lx.lookahead() != '\n' && is_space(lx.lookahead()) {
24        lx.skip();
25    }
26}
27
28impl Dockerfile {
29    fn scan_marker(&mut self, lx: &mut ScanLexer) -> bool {
30        skip_whitespace(lx);
31        if lx.lookahead() != '<' {
32            return false;
33        }
34        lx.next();
35        if lx.lookahead() != '<' {
36            return false;
37        }
38        lx.next();
39        let mut stripping = false;
40        if lx.lookahead() == '-' {
41            stripping = true;
42            lx.next();
43        }
44        let mut quote = '\0';
45        if matches!(lx.lookahead(), '"' | '\'') {
46            quote = lx.lookahead();
47            lx.next();
48        }
49        let mut delim: Vec<u8> = vec![0];
50        // 记满之后继续前进但不再记录,保证标记本身解析正确
51        let mut recording = true;
52        while lx.lookahead() != '\0'
53            && (if quote != '\0' {
54                lx.lookahead() != quote
55            } else {
56                !is_space(lx.lookahead())
57            })
58        {
59            if lx.lookahead() == '\\' {
60                lx.next();
61                if lx.lookahead() == '\0' {
62                    return false;
63                }
64            }
65            if recording {
66                delim.push(lx.lookahead() as u32 as u8);
67            }
68            lx.next();
69            if recording && delim.len() >= DEL_SPACE - 2 {
70                recording = false;
71            }
72        }
73        if quote != '\0' {
74            if lx.lookahead() != quote {
75                return false;
76            }
77            lx.next();
78        }
79        if !recording {
80            lx.set_result(HEREDOC_MARKER as u16);
81            return true;
82        }
83        delim[0] = if stripping { b'-' } else { b' ' };
84        if self.heredocs.is_empty() {
85            self.heredocs.push(delim);
86            self.stripping = stripping;
87        } else if self.heredocs.len() < MAX_HEREDOCS {
88            self.heredocs.push(delim);
89        }
90        lx.set_result(HEREDOC_MARKER as u16);
91        true
92    }
93
94    fn scan_content(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
95        if self.heredocs.is_empty() {
96            self.in_heredoc = false;
97            return false;
98        }
99        self.in_heredoc = true;
100        if self.stripping {
101            skip_whitespace(lx);
102        }
103        if v[HEREDOC_END] {
104            let mut i = 1;
105            let first = &self.heredocs[0];
106            while i < first.len()
107                && lx.lookahead() != '\0'
108                && first[i] < 0x80
109                && lx.lookahead() == first[i] as char
110            {
111                lx.next();
112                i += 1;
113            }
114            if i == first.len() {
115                lx.set_result(HEREDOC_END as u16);
116                self.heredocs.remove(0);
117                match self.heredocs.first() {
118                    Some(h) => self.stripping = h[0] == b'-',
119                    None => self.in_heredoc = false,
120                }
121                return true;
122            }
123        }
124        if !v[HEREDOC_LINE] {
125            return false;
126        }
127        lx.set_result(HEREDOC_LINE as u16);
128        loop {
129            match lx.lookahead() {
130                '\0' => {
131                    if lx.eof() {
132                        self.in_heredoc = false;
133                        return true;
134                    }
135                    lx.next();
136                }
137                '\n' => return true,
138                _ => lx.next(),
139            }
140        }
141    }
142}
143
144impl Scanner for Dockerfile {
145    fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
146        if v[ERROR_SENTINEL] {
147            return if self.in_heredoc {
148                self.scan_content(lx, v)
149            } else {
150                self.scan_marker(lx)
151            };
152        }
153        if v[HEREDOC_NL] && !self.heredocs.is_empty() && lx.lookahead() == '\n' {
154            lx.set_result(HEREDOC_NL as u16);
155            lx.next();
156            return true;
157        }
158        if v[HEREDOC_MARKER] {
159            return self.scan_marker(lx);
160        }
161        if v[HEREDOC_LINE] || v[HEREDOC_END] {
162            return self.scan_content(lx, v);
163        }
164        false
165    }
166
167    fn serialize(&self, out: &mut Vec<u8>) {
168        out.push(self.in_heredoc as u8);
169        out.push(self.stripping as u8);
170        for h in &self.heredocs {
171            // 放不下的丢掉;还要给结尾的 0 留一个字节
172            if out.len() + h.len() + 1 + 1 > MAX_SERIALIZED {
173                break;
174            }
175            out.extend_from_slice(h);
176            out.push(0);
177        }
178        out.push(0);
179    }
180
181    fn deserialize(&mut self, d: &[u8]) {
182        self.heredocs.clear();
183        if d.len() < 2 {
184            self.in_heredoc = false;
185            self.stripping = false;
186            return;
187        }
188        self.in_heredoc = d[0] != 0;
189        self.stripping = d[1] != 0;
190        let mut p = 2;
191        for _ in 0..MAX_HEREDOCS {
192            let Some(len) = d.get(p..).and_then(|r| r.iter().position(|b| *b == 0)) else {
193                break;
194            };
195            if len == 0 {
196                break;
197            }
198            self.heredocs.push(d[p..p + len].to_vec());
199            p += len + 1;
200        }
201    }
202}