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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! 表达式解析测试。

use super::parse;
use crate::core::{BuiltinVar, ElementKind, Expr, ExprKind, RangeKind, StrPart};
use crate::span::Span;
use crate::template::{Configuration, Template};
use crate::value::TNumber;
use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use std::rc::Rc;
use std::str::FromStr;

fn cfg() -> Rc<Configuration> {
    Rc::new(Configuration::new())
}

fn parse_ok(src: &str) -> Template {
    parse(&cfg(), "t", src).unwrap_or_else(|e| panic!("parse failed for {src:?}: {e}"))
}

#[allow(dead_code)]
fn parse_err(src: &str) -> String {
    match parse(&cfg(), "t", src) {
        Err(e) => e.to_string(),
        Ok(_) => panic!("expected parse error for {src:?}"),
    }
}

fn expr_of(src: &str) -> ExprKind {
    let t = parse_ok(&format!("${{{src}}}"));
    match &t.root[0].kind {
        ElementKind::Interpolation { expr, .. } => expr.kind.clone(),
        k => panic!("expected interpolation, got {k:?}"),
    }
}

fn num(v: TNumber) -> ExprKind {
    ExprKind::Num(v)
}

fn ident(n: &str) -> ExprKind {
    ExprKind::Ident(n.to_string())
}

fn strlit(s: &str) -> ExprKind {
    ExprKind::Str(s.to_string())
}

#[test]
fn number_literal_mapping() {
    assert_eq!(expr_of("1"), num(TNumber::Int(1)));
    assert_eq!(expr_of("1L"), num(TNumber::Long(1)));
    assert_eq!(expr_of("1F"), num(TNumber::Float(1.0)));
    assert_eq!(expr_of("1D"), num(TNumber::Double(1.0)));
    assert_eq!(
        expr_of("1.5"),
        num(TNumber::Decimal(BigDecimal::from_str("1.5").unwrap()))
    );
    assert_eq!(
        expr_of("1e3"),
        num(TNumber::Decimal(BigDecimal::from_str("1000").unwrap()))
    );
    assert_eq!(
        expr_of("1.5e-2"),
        num(TNumber::Decimal(BigDecimal::from_str("0.015").unwrap()))
    );
    assert_eq!(expr_of("0x1A"), num(TNumber::Int(26)));
    assert_eq!(expr_of("0x10L"), num(TNumber::Long(16)));
    // 超 i64 整数 → BigInt
    assert_eq!(
        expr_of("99999999999999999999"),
        num(TNumber::BigInt(
            BigInt::from_str("99999999999999999999").unwrap()
        ))
    );
    // 大数 Long 后缀回退 BigInt
    assert_eq!(
        expr_of("99999999999999999999L"),
        num(TNumber::BigInt(
            BigInt::from_str("99999999999999999999").unwrap()
        ))
    );
}

// -----------------------------------------------------------------------
// 字符串字面量(StringLiteral:转义 + 插值)
// -----------------------------------------------------------------------

#[test]
fn string_literal_escapes() {
    assert_eq!(expr_of(r#""abc""#), strlit("abc"));
    assert_eq!(expr_of(r#""a\n\t\\\'\"b""#), strlit("a\n\t\\'\"b"));
    // \l \g \a 转义(FTL 特有)
    assert_eq!(expr_of(r#""\l\g\a""#), strlit("<>&"));
    // \xHH 与 \uXXXX
    assert_eq!(expr_of(r#""\x41""#), strlit("A"));
    assert_eq!(expr_of(r#""\u0041\u00e9""#), strlit("Aé"));
}

#[test]
fn string_interpolation_parts() {
    assert_eq!(
        expr_of(r#""a${x}b""#),
        ExprKind::InterpStr(vec![
            StrPart::Text("a".to_string()),
            // 插值内表达式由子解析器解析,位置相对插值正文(Java parseValue 子解析语义)
            StrPart::Interp(Box::new(Expr::new(ident("x"), Span::new(1, 1)))),
            StrPart::Text("b".to_string()),
        ])
    );
    assert_eq!(
        expr_of(r#""${x}""#),
        ExprKind::InterpStr(vec![StrPart::Interp(Box::new(Expr::new(
            ident("x"),
            Span::new(1, 1)
        )))])
    );
    // 嵌套字符串插值(外层用单引号:双引号字符串内不能再有未转义双引号,
    // Java 词法同样在 `"x ${"` 处截断)
    assert_eq!(
        expr_of(r#"'x ${"a${y}b"} z'"#),
        ExprKind::InterpStr(vec![
            StrPart::Text("x ".to_string()),
            // 嵌套字符串字面量同样由子解析器处理(位置相对)
            StrPart::Interp(Box::new(Expr::new(
                ExprKind::InterpStr(vec![
                    StrPart::Text("a".to_string()),
                    StrPart::Interp(Box::new(Expr::new(ident("y"), Span::new(1, 1)))),
                    StrPart::Text("b".to_string()),
                ]),
                Span::new(1, 1)
            ))),
            StrPart::Text(" z".to_string()),
        ])
    );
    // `$${` 在字符串内:第一个 $ 是文本(Java indexOf 语义)
    assert_eq!(
        expr_of(r#""$${x}""#),
        ExprKind::InterpStr(vec![
            StrPart::Text("$".to_string()),
            StrPart::Interp(Box::new(Expr::new(ident("x"), Span::new(1, 1)))),
        ])
    );
    // 原始字符串:不插值、不解码
    assert_eq!(expr_of(r#"r"a${x}\n""#), strlit("a${x}\\n"));
}

#[test]
fn string_literal_unclosed() {
    // Java TokenMgrError 词法错误格式(jar 实测 parse_unclosed_string 基线)
    let msg = parse_err(r#"${"abc}"#);
    assert!(
        msg.contains("Lexical error: encountered <EOF> after \"\\\"abc}\"."),
        "{msg}"
    );
}

#[test]
fn invalid_escape_sequence() {
    let msg = parse_err(r#"${"a\qb"}"#);
    assert!(msg.contains("Invalid escape sequence"), "{msg}");
}

// -----------------------------------------------------------------------
// 布尔 / 标识符 / 内置变量
// -----------------------------------------------------------------------

#[test]
fn boolean_and_identifier() {
    assert_eq!(expr_of("true"), ExprKind::Bool(true));
    assert_eq!(expr_of("false"), ExprKind::Bool(false));
    assert_eq!(expr_of("now"), ExprKind::BuiltinVar(BuiltinVar::Now));
    assert_eq!(expr_of("fooBar_$1"), ident("fooBar_$1"));
    assert_eq!(expr_of("français"), ident("français"));
    // `.now` 内置变量形式(Java BuiltinVariable 产生式)
    assert_eq!(expr_of(".now"), ExprKind::BuiltinVar(BuiltinVar::Now));
}

// -----------------------------------------------------------------------
// 后缀操作:点 / 动态键 / 方法调用 / 内建 / 默认值 / 存在性
// -----------------------------------------------------------------------

#[test]
fn postfix_operations() {
    assert_eq!(
        expr_of("a.b"),
        ExprKind::Dot {
            target: Box::new(Expr::new(ident("a"), Span::new(1, 3))),
            name: "b".to_string(),
        }
    );
    // 链式点
    assert_eq!(
        expr_of("a.b.c"),
        ExprKind::Dot {
            target: Box::new(Expr::new(
                ExprKind::Dot {
                    target: Box::new(Expr::new(ident("a"), Span::new(1, 3))),
                    name: "b".to_string(),
                },
                Span::new(1, 3)
            )),
            name: "c".to_string(),
        }
    );
    assert_eq!(
        expr_of(r#"a["k"]"#),
        ExprKind::DynKey {
            target: Box::new(Expr::new(ident("a"), Span::new(1, 3))),
            key: Box::new(Expr::new(strlit("k"), Span::new(1, 5))),
        }
    );
    // 关键字作成员名(DotVariable 产生式)
    assert_eq!(
        expr_of("a.in"),
        ExprKind::Dot {
            target: Box::new(Expr::new(ident("a"), Span::new(1, 3))),
            name: "in".to_string(),
        }
    );
}

#[test]
fn method_call() {
    assert_eq!(
        expr_of("f(x, y)"),
        ExprKind::Call {
            callee: Box::new(Expr::new(ident("f"), Span::new(1, 3))),
            args: vec![
                Expr::new(ident("x"), Span::new(1, 5)),
                Expr::new(ident("y"), Span::new(1, 8)),
            ],
        }
    );
}

#[test]
fn builtin_variants() {
    assert_eq!(
        expr_of("x?upper_case"),
        ExprKind::BuiltIn {
            target: Box::new(Expr::new(ident("x"), Span::new(1, 3))),
            name: "upper_case".to_string(),
            args: None,
        }
    );
    assert_eq!(
        expr_of(r#"x?string("0.##")"#),
        ExprKind::BuiltIn {
            target: Box::new(Expr::new(ident("x"), Span::new(1, 3))),
            name: "string".to_string(),
            args: Some(vec![Expr::new(strlit("0.##"), Span::new(1, 12))]),
        }
    );
}

#[test]
fn exists_and_default_to() {
    assert_eq!(
        expr_of("x??"),
        ExprKind::Exists(Box::new(Expr::new(ident("x"), Span::new(1, 3))))
    );
    assert_eq!(
        expr_of("x!"),
        ExprKind::Default {
            target: Box::new(Expr::new(ident("x"), Span::new(1, 3))),
            default: None,
        }
    );
    assert_eq!(
        expr_of("x!y"),
        ExprKind::Default {
            target: Box::new(Expr::new(ident("x"), Span::new(1, 3))),
            default: Some(Box::new(Expr::new(ident("y"), Span::new(1, 5)))),
        }
    );
    // 带默认值的完整表达式(Java DefaultTo 前瞻语义:x!y+z → x!(y+z))
    assert_eq!(
        expr_of("x!y + z"),
        ExprKind::Default {
            target: Box::new(Expr::new(ident("x"), Span::new(1, 3))),
            default: Some(Box::new(Expr::new(
                ExprKind::Add(
                    Box::new(Expr::new(ident("y"), Span::new(1, 5))),
                    Box::new(Expr::new(ident("z"), Span::new(1, 9))),
                ),
                Span::new(1, 5)
            ))),
        }
    );
    // `x! &&y`:`&&` 不是表达式开头 → 无默认值
    assert_eq!(
        expr_of("x! && y"),
        ExprKind::And(
            Box::new(Expr::new(
                ExprKind::Default {
                    target: Box::new(Expr::new(ident("x"), Span::new(1, 3))),
                    default: None,
                },
                Span::new(1, 3)
            )),
            Box::new(Expr::new(ident("y"), Span::new(1, 9))),
        )
    );
}

// -----------------------------------------------------------------------
// lambda(LocalLambdaExpression)
// -----------------------------------------------------------------------

#[test]
fn lambda_expression() {
    assert_eq!(
        expr_of("x?filter(y -> y > 1)"),
        ExprKind::BuiltIn {
            target: Box::new(Expr::new(ident("x"), Span::new(1, 3))),
            name: "filter".to_string(),
            args: Some(vec![Expr::new(
                ExprKind::Lambda {
                    params: vec!["y".to_string()],
                    body: Box::new(Expr::new(
                        ExprKind::Gt(
                            Box::new(Expr::new(ident("y"), Span::new(1, 17))),
                            Box::new(Expr::new(num(TNumber::Int(1)), Span::new(1, 21))),
                        ),
                        Span::new(1, 17)
                    )),
                },
                Span::new(1, 12)
            )]),
        }
    );
    // 括号形式 (y) ->
    assert_eq!(
        expr_of("x?map((y) -> y * 2)"),
        ExprKind::BuiltIn {
            target: Box::new(Expr::new(ident("x"), Span::new(1, 3))),
            name: "map".to_string(),
            args: Some(vec![Expr::new(
                ExprKind::Lambda {
                    params: vec!["y".to_string()],
                    body: Box::new(Expr::new(
                        ExprKind::Mul(
                            Box::new(Expr::new(ident("y"), Span::new(1, 16))),
                            Box::new(Expr::new(num(TNumber::Int(2)), Span::new(1, 20))),
                        ),
                        Span::new(1, 16)
                    )),
                },
                Span::new(1, 9)
            )]),
        }
    );
}

// -----------------------------------------------------------------------
// 列表 / 哈希字面量
// -----------------------------------------------------------------------

#[test]
fn list_and_hash_literals() {
    assert_eq!(
        expr_of("[1, 2]"),
        ExprKind::ListLit(vec![
            Expr::new(num(TNumber::Int(1)), Span::new(1, 4)),
            Expr::new(num(TNumber::Int(2)), Span::new(1, 7)),
        ])
    );
    assert_eq!(expr_of("[]"), ExprKind::ListLit(vec![]));
    assert_eq!(
        expr_of(r#"{"a": 1}"#),
        ExprKind::HashLit(vec![(
            Expr::new(strlit("a"), Span::new(1, 4)),
            Expr::new(num(TNumber::Int(1)), Span::new(1, 9)),
        )])
    );
    // 逗号分隔键值对(Java HashLiteral 的 (<COMMA>|<COLON>) 形式)
    assert_eq!(
        expr_of(r#"{"a", 1}"#),
        ExprKind::HashLit(vec![(
            Expr::new(strlit("a"), Span::new(1, 4)),
            Expr::new(num(TNumber::Int(1)), Span::new(1, 9)),
        )])
    );
    // 非字符串键 → 解析错误(Java stringLiteralOnly:数字字面量作键,
    // 消息逐字 "Found number literal: 1. Expecting string",jar 实测)
    let msg = parse_err(r#"${ {1: 2} }"#);
    assert!(
        msg.contains("Found number literal: 1. Expecting string"),
        "{msg}"
    );
}

// -----------------------------------------------------------------------
// 括号 / 一元 / 优先级
// -----------------------------------------------------------------------

#[test]
fn parenthesis_and_unary() {
    assert_eq!(
        expr_of("(x)"),
        ExprKind::Paren(Box::new(Expr::new(ident("x"), Span::new(1, 4))))
    );
    assert_eq!(
        expr_of("-x"),
        ExprKind::UnaryMinus(Box::new(Expr::new(ident("x"), Span::new(1, 4))))
    );
    // `+x` 无 AST 节点(Java UnaryPlusMinusExpression(isMinus=false) 语义)
    assert_eq!(expr_of("+x"), ident("x"));
    assert_eq!(
        expr_of("!x"),
        ExprKind::Not(Box::new(Expr::new(ident("x"), Span::new(1, 4))))
    );
    assert_eq!(
        expr_of("!!x"),
        ExprKind::Not(Box::new(Expr::new(
            ExprKind::Not(Box::new(Expr::new(ident("x"), Span::new(1, 5)))),
            Span::new(1, 4)
        )))
    );
    assert_eq!(
        expr_of("-1"),
        ExprKind::UnaryMinus(Box::new(Expr::new(num(TNumber::Int(1)), Span::new(1, 4))))
    );
}

#[test]
fn precedence_and_associativity() {
    // 乘法优先于加法
    assert_eq!(
        expr_of("1 + 2 * 3"),
        ExprKind::Add(
            Box::new(Expr::new(num(TNumber::Int(1)), Span::new(1, 3))),
            Box::new(Expr::new(
                ExprKind::Mul(
                    Box::new(Expr::new(num(TNumber::Int(2)), Span::new(1, 7))),
                    Box::new(Expr::new(num(TNumber::Int(3)), Span::new(1, 11))),
                ),
                Span::new(1, 7)
            )),
        )
    );
    // 括号覆盖
    assert_eq!(
        expr_of("(1 + 2) * 3"),
        ExprKind::Mul(
            Box::new(Expr::new(
                ExprKind::Paren(Box::new(Expr::new(
                    ExprKind::Add(
                        Box::new(Expr::new(num(TNumber::Int(1)), Span::new(1, 4))),
                        Box::new(Expr::new(num(TNumber::Int(2)), Span::new(1, 8))),
                    ),
                    Span::new(1, 4)
                ))),
                Span::new(1, 3)
            )),
            Box::new(Expr::new(num(TNumber::Int(3)), Span::new(1, 13))),
        )
    );
    // 左结合
    assert_eq!(
        expr_of("a + b - c"),
        ExprKind::Sub(
            Box::new(Expr::new(
                ExprKind::Add(
                    Box::new(Expr::new(ident("a"), Span::new(1, 3))),
                    Box::new(Expr::new(ident("b"), Span::new(1, 7))),
                ),
                Span::new(1, 3)
            )),
            Box::new(Expr::new(ident("c"), Span::new(1, 11))),
        )
    );
    // 逻辑优先级:&& 高于 ||
    assert_eq!(
        expr_of("a && b || c"),
        ExprKind::Or(
            Box::new(Expr::new(
                ExprKind::And(
                    Box::new(Expr::new(ident("a"), Span::new(1, 3))),
                    Box::new(Expr::new(ident("b"), Span::new(1, 8))),
                ),
                Span::new(1, 3)
            )),
            Box::new(Expr::new(ident("c"), Span::new(1, 13))),
        )
    );
    // equality 高于 and
    assert_eq!(
        expr_of("a == b && c"),
        ExprKind::And(
            Box::new(Expr::new(
                ExprKind::Eq(
                    Box::new(Expr::new(ident("a"), Span::new(1, 3))),
                    Box::new(Expr::new(ident("b"), Span::new(1, 8))),
                ),
                Span::new(1, 3)
            )),
            Box::new(Expr::new(ident("c"), Span::new(1, 13))),
        )
    );
    // 一元 not 优先于 &&
    assert_eq!(
        expr_of("!a && b"),
        ExprKind::And(
            Box::new(Expr::new(
                ExprKind::Not(Box::new(Expr::new(ident("a"), Span::new(1, 4)))),
                Span::new(1, 3)
            )),
            Box::new(Expr::new(ident("b"), Span::new(1, 9))),
        )
    );
    // equality 高于 relational
    assert_eq!(
        expr_of("x > 1 == y"),
        ExprKind::Eq(
            Box::new(Expr::new(
                ExprKind::Gt(
                    Box::new(Expr::new(ident("x"), Span::new(1, 3))),
                    Box::new(Expr::new(num(TNumber::Int(1)), Span::new(1, 7))),
                ),
                Span::new(1, 3)
            )),
            Box::new(Expr::new(ident("y"), Span::new(1, 12))),
        )
    );
    // 非结合:a == b == c 报错(Java EqualityExpression 单一可选)
    let msg = parse_err("${a == b == c}");
    assert!(msg.contains("line 1, column 10"), "{msg}");
    assert!(msg.contains("Expected \"}\""), "{msg}");
}

#[test]
fn range_expressions() {
    assert_eq!(
        expr_of("1..5"),
        ExprKind::Range {
            start: Box::new(Expr::new(num(TNumber::Int(1)), Span::new(1, 3))),
            end: Some(Box::new(Expr::new(num(TNumber::Int(5)), Span::new(1, 6)))),
            kind: RangeKind::Inclusive,
        }
    );
    assert_eq!(expr_of("1..<5").kind_of_range_kind(), RangeKind::Exclusive);
    assert_eq!(
        expr_of("1..*5").kind_of_range_kind(),
        RangeKind::SizeLimited
    );
    assert_eq!(
        expr_of("1.."),
        ExprKind::Range {
            start: Box::new(Expr::new(num(TNumber::Int(1)), Span::new(1, 3))),
            end: None,
            kind: RangeKind::SizeLimited, // Java END_UNBOUND 无契约槽位(文档化偏差)
        }
    );
}

trait RangeHelper {
    fn kind_of_range_kind(&self) -> RangeKind;
}
impl RangeHelper for ExprKind {
    fn kind_of_range_kind(&self) -> RangeKind {
        match self {
            ExprKind::Range { kind, .. } => *kind,
            _ => panic!("expected range"),
        }
    }
}

#[test]
fn relational_in_parens() {
    // 括号内 `>` 是比较符(IN_PAREN 词法状态)
    assert_eq!(
        expr_of("(a > b)"),
        ExprKind::Paren(Box::new(Expr::new(
            ExprKind::Gt(
                Box::new(Expr::new(ident("a"), Span::new(1, 4))),
                Box::new(Expr::new(ident("b"), Span::new(1, 8))),
            ),
            Span::new(1, 4)
        )))
    );
}

// -----------------------------------------------------------------------
// 指令(If / List / Assign / Macro / Call / ...)