freemarker 0.1.1

Apache FreeMarker 语义兼容的 Rust 模板引擎(freemarker-core 迁移)
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! 表达式词法 —— next_expr_token 主循环与核心分发表(FM_EXPRESSION 家族状态)。
//!
//! `ctx` 决定 `>`/`]`/`}` 在深度 0 时的语义(标签结束/插值结束/错误);
//! `>` 语义随方括号/角度语法区分(ExprCtx::Tag.square),详见主文件头注释。

use super::{is_ident_start, ExprCtx, Lexer, Tok};
use crate::error::Result;

impl Lexer {
    /// 表达式 token 扫描(FM_EXPRESSION 家族状态)。
    /// `ctx` 决定 `>`/`]`/`}` 在深度 0 时的语义(标签结束/插值结束/错误)。
    /// 返回 (token, 起始行, 起始列, 结束行, 结束列) —— 结束位置供解析器做
    /// token 相邻性判定(`<@callee>` 的 NO_SPACE_EXPRESSION 语义,见 grammar.rs)。
    pub(crate) fn next_expr_token(&mut self, ctx: ExprCtx) -> Result<(Tok, u32, u32, u32, u32)> {
        loop {
            self.skip_ws();
            let (line, col) = self.line_col();
            let c = match self.peek() {
                None => return Ok((Tok::Eof, line, col, line, col)),
                Some(c) => c,
            };
            // 表达式内注释 `<#-- -->` / `<!-- -->` / `[#-- --]` / `[!-- --]`(EXPRESSION_COMMENT)
            if c == '<' || c == '[' {
                let n1 = self.peek_at(1);
                let n2 = self.peek_at(2);
                if matches!(n1, Some('#') | Some('!'))
                    && n2 == Some('-')
                    && self.peek_at(3) == Some('-')
                {
                    self.bump();
                    self.bump();
                    self.bump();
                    self.bump();
                    self.skip_expr_comment()?;
                    continue;
                }
            }
            let (tok, _, _) = self.scan_expr_token_after_ws(ctx, line, col)?;
            let (el, ec) = self.line_col();
            return Ok((tok, line, col, el, ec));
        }
    }

    /// 表达式注释正文(EXPRESSION_COMMENT SKIP:直到 `-->` 或 `--]`)
    fn skip_expr_comment(&mut self) -> Result<()> {
        loop {
            match self.peek() {
                None => {
                    return Err(self.err(self.line, self.col, "Unclosed comment in expression."))
                }
                Some('-') if self.peek_at(1) == Some('-') => match self.peek_at(2) {
                    Some('>') | Some(']') => {
                        self.bump();
                        self.bump();
                        self.bump();
                        return Ok(());
                    }
                    _ => {
                        self.bump();
                    }
                },
                _ => {
                    self.bump();
                }
            }
        }
    }

    /// 跳过空白后的 token 分发(核心词法表)
    fn scan_expr_token_after_ws(
        &mut self,
        ctx: ExprCtx,
        line: u32,
        col: u32,
    ) -> Result<(Tok, u32, u32)> {
        let c = self.peek().unwrap();
        // Java OPEN_MISPLACED_INTERPOLATION(FTL.jj :1409-1421):表达式模式中
        // `${`/`#{`/`[=` 是词法错误(TokenMgrError LEXICAL_ERROR,位置=起始符列;
        // jar 实测 parse_needless_interpolation 基线)。注意 `$` 本身是合法标识符
        // 起始字符(`$foo` 变量名),仅 `${` 组合报错
        if (c == '$' || c == '#') && self.peek_at(1) == Some('{')
            || c == '[' && self.peek_at(1) == Some('=')
        {
            let img = if c == '[' {
                "[="
            } else if c == '$' {
                "${"
            } else {
                "#{"
            };
            let closer = if c == '[' { "]" } else { "}" };
            self.bump();
            self.bump();
            return Err(self.err(
                line,
                col,
                format!(
                    "You can't use {img}...{closer} (an interpolation) here as you are \
                     already in FreeMarker-expression-mode. Thus, instead of {img}myExpression{closer}, \
                     just write myExpression. ({img}...{closer} is only used where otherwise static \
                     text is expected, i.e., outside FreeMarker tags and interpolations, or inside \
                     string literals.)"
                ),
            ));
        }
        let tok = match c {
            '<' => {
                // LESS_THAN / LESS_THAN_EQUALS(表达式内 `<` 恒为比较符;
                // 标签结束只可能是 `>`,不存在歧义)
                self.bump();
                if self.peek() == Some('=') {
                    self.bump();
                    Tok::Lte
                } else {
                    Tok::Lt
                }
            }
            '>' => {
                if self.paren_depth > 0 {
                    // IN_PAREN:NATURAL_GT / NATURAL_GTE
                    self.bump();
                    if self.peek() == Some('=') {
                        self.bump();
                        Tok::Gte
                    } else {
                        Tok::Gt
                    }
                } else {
                    match ctx {
                        // 角度语法 DIRECTIVE_END:`>` 结束标签(`>=` 不可能 —— `>` 已结束标签)
                        ExprCtx::Tag { square: false } => {
                            self.bump();
                            Tok::TagEnd
                        }
                        // 方括号语法:标签仅由 `]` 结束,故 `>`/`>=` 为 NATURAL_GT/GTE。
                        // 插值内 `>` 同样是 NATURAL_GT(DIRECTIVE_END 动作的 postInterpolation 分支)。
                        ExprCtx::Tag { square: true } | ExprCtx::Interp => {
                            self.bump();
                            if self.peek() == Some('=') {
                                self.bump();
                                Tok::Gte
                            } else {
                                Tok::Gt
                            }
                        }
                    }
                }
            }
            ']' => {
                if self.bracket_depth > 0 {
                    self.bump();
                    self.bracket_depth -= 1;
                    Tok::CloseBracket
                } else {
                    match ctx {
                        ExprCtx::Tag { square: true } => {
                            // 方括号语法下 `]` 在深度 0 结束标签(CLOSE_BRACKET 的 DIRECTIVE_END 分支)
                            self.bump();
                            Tok::TagEnd
                        }
                        _ => {
                            // 2.3.28+:角度语法下无配对 `]` → 报错(newUnexpectedClosingTokenException)
                            return Err(self.err(
                                line,
                                col,
                                "You can't have a \"]\" here, as there's nothing open that it could close.",
                            ));
                        }
                    }
                }
            }
            '}' => {
                if self.curly_depth > 0 {
                    self.bump();
                    self.curly_depth -= 1;
                    Tok::CloseCurly
                } else {
                    match ctx {
                        ExprCtx::Interp => {
                            self.bump();
                            Tok::InterpEnd
                        }
                        ExprCtx::Tag { .. } => {
                            return Err(self.err(
                                line,
                                col,
                                "You can't have a \"}\" here, as there's nothing open that it could close.",
                            ));
                        }
                    }
                }
            }
            ')' => {
                self.bump();
                self.paren_depth = self.paren_depth.saturating_sub(1);
                Tok::CloseParen
            }
            '(' => {
                self.bump();
                self.paren_depth += 1;
                Tok::OpenParen
            }
            '[' => {
                self.bump();
                self.bracket_depth += 1;
                Tok::OpenBracket
            }
            '{' => {
                self.bump();
                self.curly_depth += 1;
                Tok::OpenCurly
            }
            '=' => {
                self.bump();
                if self.peek() == Some('=') {
                    self.bump();
                }
                Tok::Eq
            }
            '!' => {
                self.bump();
                if self.peek() == Some('=') {
                    self.bump();
                    Tok::NotEq
                } else {
                    Tok::Exclam
                }
            }
            '?' => {
                self.bump();
                if self.peek() == Some('?') {
                    self.bump();
                    Tok::Exists
                } else {
                    Tok::Builtin
                }
            }
            '+' => {
                self.bump();
                match self.peek() {
                    Some('+') => {
                        self.bump();
                        Tok::PlusPlus
                    }
                    Some('=') => {
                        self.bump();
                        Tok::PlusEq
                    }
                    _ => Tok::Plus,
                }
            }
            '-' => {
                self.bump();
                match self.peek() {
                    Some('-') => {
                        self.bump();
                        Tok::MinusMinus
                    }
                    Some('=') => {
                        self.bump();
                        Tok::MinusEq
                    }
                    Some('>') => {
                        self.bump();
                        Tok::LambdaArrow
                    }
                    Some('&')
                        if self.peek_at(1) == Some('g')
                            && self.peek_at(2) == Some('t')
                            && self.peek_at(3) == Some(';') =>
                    {
                        // `-&gt;` 也是 lambda 箭头
                        self.bump();
                        self.bump();
                        self.bump();
                        self.bump();
                        Tok::LambdaArrow
                    }
                    _ => Tok::Minus,
                }
            }
            '*' => {
                self.bump();
                match self.peek() {
                    Some('*') => {
                        self.bump();
                        Tok::DoubleStar
                    }
                    Some('=') => {
                        self.bump();
                        Tok::TimesEq
                    }
                    _ => Tok::Times,
                }
            }
            '/' => {
                self.bump();
                if self.peek() == Some('=') {
                    self.bump();
                    Tok::DivEq
                } else if matches!(ctx, ExprCtx::Tag { .. })
                    && matches!(self.peek(), Some('>') | Some(']'))
                {
                    // `/>` / `/]`:自闭合标签结束(EMPTY_DIRECTIVE_END,最长匹配优先于 DIVIDE)
                    self.bump();
                    Tok::EmptyTagEnd
                } else {
                    Tok::Divide
                }
            }
            '%' => {
                self.bump();
                if self.peek() == Some('=') {
                    self.bump();
                    Tok::ModEq
                } else {
                    Tok::Percent
                }
            }
            '&' => {
                // `&lt;` / `&lt;=` / `&gt;` / `&gt;=` / `&amp;&amp;` / `&` / `&&`
                if self.peek_at(1) == Some('l')
                    && self.peek_at(2) == Some('t')
                    && self.peek_at(3) == Some(';')
                {
                    self.bump();
                    self.bump();
                    self.bump();
                    self.bump();
                    if self.peek() == Some('=') {
                        self.bump();
                        Tok::Lte
                    } else {
                        Tok::Lt
                    }
                } else if self.peek_at(1) == Some('g')
                    && self.peek_at(2) == Some('t')
                    && self.peek_at(3) == Some(';')
                {
                    self.bump();
                    self.bump();
                    self.bump();
                    self.bump();
                    if self.peek() == Some('=') {
                        self.bump();
                        Tok::Gte
                    } else {
                        Tok::Gt
                    }
                } else if self.peek_at(1) == Some('a')
                    && self.peek_at(2) == Some('m')
                    && self.peek_at(3) == Some('p')
                    && self.peek_at(4) == Some(';')
                    && self.peek_at(5) == Some('&')
                    && self.peek_at(6) == Some('a')
                    && self.peek_at(7) == Some('m')
                    && self.peek_at(8) == Some('p')
                    && self.peek_at(9) == Some(';')
                {
                    // `&amp;&amp;`
                    for _ in 0..10 {
                        self.bump();
                    }
                    Tok::And
                } else {
                    self.bump();
                    if self.peek() == Some('&') {
                        self.bump();
                    }
                    Tok::And
                }
            }
            '|' => {
                self.bump();
                if self.peek() == Some('|') {
                    self.bump();
                }
                Tok::Or
            }
            ',' => {
                self.bump();
                Tok::Comma
            }
            ';' => {
                self.bump();
                Tok::Semicolon
            }
            ':' => {
                self.bump();
                Tok::Colon
            }
            '.' => {
                // `...` / `..<` / `..!` / `..*` / `..` / `.`
                self.bump();
                if self.peek() == Some('.') {
                    self.bump();
                    if self.peek() == Some('.') {
                        self.bump();
                        Tok::Ellipsis
                    } else if matches!(self.peek(), Some('<') | Some('!')) {
                        // `..<`(排端范围,FTL.jj DOT_DOT_LESS)与 `..!`(兼容分支)同 token
                        self.bump();
                        Tok::DotDotLess
                    } else if self.peek() == Some('*') {
                        self.bump();
                        Tok::DotDotStar
                    } else {
                        Tok::DotDot
                    }
                } else {
                    Tok::Dot
                }
            }
            '\\' => {
                // 转义标识符起始字符 `\-` `\.` `\:` `\#`(ESCAPED_ID_CHAR)
                if matches!(
                    self.peek_at(1),
                    Some('-') | Some('.') | Some(':') | Some('#')
                ) {
                    Tok::Ident(self.scan_ident())
                } else {
                    // 转义运算符:`\and` / `\lt` / `\lte` / `\gt` / `\gte`
                    let n1 = self.peek_at(1);
                    match n1 {
                        Some('a')
                            if self.peek_at(2) == Some('n') && self.peek_at(3) == Some('d') =>
                        {
                            for _ in 0..4 {
                                self.bump();
                            }
                            Tok::And
                        }
                        Some('l') => {
                            if self.peek_at(2) == Some('t') {
                                self.bump();
                                self.bump();
                                if self.peek() == Some('e') {
                                    self.bump();
                                    Tok::Lte
                                } else {
                                    Tok::Lt
                                }
                            } else {
                                return Err(self.err(
                                    line,
                                    col,
                                    format!("Unexpected character \"\\\\{n1:?}\"."),
                                ));
                            }
                        }
                        Some('g') if self.peek_at(2) == Some('t') => {
                            self.bump();
                            self.bump();
                            if self.peek() == Some('e') {
                                self.bump();
                                Tok::Gte
                            } else {
                                Tok::Gt
                            }
                        }
                        _ => {
                            return Err(self.err(
                                line,
                                col,
                                format!("Unexpected character \"\\\\{n1:?}\"."),
                            ));
                        }
                    }
                }
            }
            '"' | '\'' => {
                let (tok, _) = self.scan_string_token()?;
                tok
            }
            'r' if matches!(self.peek_at(1), Some('"') | Some('\'')) => {
                // RAW_STRING:`r"..."` / `r'...'`
                self.bump(); // r
                let quote = self.peek().unwrap(); // 引号(已确认存在)
                self.bump();
                let mut s = String::new();
                loop {
                    match self.peek() {
                        None => {
                            return Err(self.err(line, col, "Unclosed raw string literal."));
                        }
                        Some(q) if q == quote => {
                            self.bump();
                            break;
                        }
                        Some(q) => {
                            s.push(q);
                            self.bump();
                        }
                    }
                }
                Tok::RawStr(s)
            }
            c if c.is_ascii_digit() => {
                let raw = self.scan_number_raw();
                Tok::Number(raw)
            }
            c if is_ident_start(c) => {
                let name = self.scan_ident();
                match name.as_str() {
                    "true" => Tok::True,
                    "false" => Tok::False,
                    "in" => Tok::In,
                    "as" => Tok::As,
                    "using" => Tok::Using,
                    "lt" => Tok::Lt,
                    "lte" => Tok::Lte,
                    "gt" => Tok::Gt,
                    "gte" => Tok::Gte,
                    _ => Tok::Ident(name),
                }
            }
            c => {
                return Err(self.err(line, col, format!("Unexpected character \"{c}\".")));
            }
        };
        Ok((tok, line, col))
    }
}