Files
rexlang/tests/parse.rs
T
Your NameandClaude Opus 5.5 300d767124 Add bit operators, defer, list methods, rand, assert and rex test
- Bit operators & | ^ ~ << >> and their op= forms (Go precedence),
  plus hex, binary and octal literals (0xff, 0b1010, 0o755)
- defer stmt / defer { ... }: runs when the block ends, last first,
  also on return, ?, break and continue
- List methods insert, remove, clear, contains, index_of, reverse,
  sort and sort_by (stable merge sort)
- rand module: int(a, b), float(), seed(n)
- assert(cond[, msg]) and `rex test`, which runs each test_ function
  in its own process
- rex.toml marks a project's top folder: imports count from there
- Docs, formatter, LSP completions and editor grammars updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 23:30:05 +01:00

265 lines
8.5 KiB
Rust

use rex::ast::*;
use rex::parser::parse;
fn body_of(src: &str) -> Vec<Stmt> {
let p = parse(src).unwrap_or_else(|d| panic!("{}", d.render("test", src)));
match &p.items[0] {
Item::Func(f) => f.body.clone().unwrap().stmts,
other => panic!("expected func, got {other:?}"),
}
}
fn first_expr(src: &str) -> Expr {
match body_of(&format!("func f() {{\n{src}\n}}")).remove(0) {
Stmt::Expr(e) => e,
Stmt::Return { mut values, .. } => values.remove(0),
other => panic!("unexpected {other:?}"),
}
}
#[test]
fn all_examples_parse() {
let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/examples");
let mut n = 0;
for entry in std::fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.extension().map_or(false, |e| e == "rex") {
let src = std::fs::read_to_string(&path).unwrap();
parse(&src).unwrap_or_else(|d| panic!("{}", d.render(path.to_str().unwrap(), &src)));
n += 1;
}
}
assert!(n >= 4, "expected the example programs, found {n}");
}
#[test]
fn precedence() {
// 1 + 2 * 3 => 1 + (2 * 3)
let e = first_expr("1 + 2 * 3");
match e.kind {
ExprKind::Binary { op: BinOp::Add, rhs, .. } => {
assert!(matches!(rhs.kind, ExprKind::Binary { op: BinOp::Mul, .. }))
}
other => panic!("{other:?}"),
}
// a || b && c => a || (b && c)
let e = first_expr("a || b && c");
match e.kind {
ExprKind::Binary { op: BinOp::Or, rhs, .. } => {
assert!(matches!(rhs.kind, ExprKind::Binary { op: BinOp::And, .. }))
}
other => panic!("{other:?}"),
}
// subtraction is left-associative: 10 - 3 - 2 => (10 - 3) - 2
let e = first_expr("10 - 3 - 2");
match e.kind {
ExprKind::Binary { op: BinOp::Sub, lhs, .. } => {
assert!(matches!(lhs.kind, ExprKind::Binary { op: BinOp::Sub, .. }))
}
other => panic!("{other:?}"),
}
}
#[test]
fn ranges() {
assert!(matches!(first_expr("0..10").kind, ExprKind::Range { start: Some(_), end: Some(_) }));
assert!(matches!(first_expr("args[1..]").kind, ExprKind::Index { .. }));
let e = first_expr("xs[..3]");
match e.kind {
ExprKind::Index { index, .. } => {
assert!(matches!(index.kind, ExprKind::Range { start: None, end: Some(_) }))
}
other => panic!("{other:?}"),
}
}
#[test]
fn float_vs_range() {
assert!(matches!(first_expr("1.5").kind, ExprKind::Float(_)));
assert!(matches!(first_expr("1..5").kind, ExprKind::Range { .. }));
}
#[test]
fn interpolation() {
let e = first_expr(r#""hi {name}, you are {age + 1}!""#);
match e.kind {
ExprKind::Str(parts) => {
assert_eq!(parts.len(), 5);
assert!(matches!(parts[0], StrPart::Text(_)));
assert!(matches!(parts[1], StrPart::Expr(_)));
assert!(matches!(&parts[4], StrPart::Text(t) if t == "!"));
}
other => panic!("{other:?}"),
}
}
#[test]
fn escaped_brace_is_plain_text() {
let e = first_expr(r#""\{ not code \}""#);
match e.kind {
ExprKind::Str(parts) => assert_eq!(parts, vec![StrPart::Text("{ not code }".into())]),
other => panic!("{other:?}"),
}
}
#[test]
fn struct_literal_vs_block() {
// `if a == b {` must be a block, not `b { ... }` as a struct literal.
let stmts = body_of("func f() {\nif a == b { x = 1 }\n}");
assert!(matches!(stmts[0], Stmt::If { .. }));
// But a literal is fine on the right of `=` and inside parens in a header.
let stmts = body_of("func f() {\nvar p = Point{1, 2}\nif p == (Point{1, 2}) { x = 1 }\n}");
assert!(matches!(stmts[0], Stmt::Var(_)));
assert!(matches!(stmts[1], Stmt::If { .. }));
}
#[test]
fn for_forms() {
let stmts = body_of(
"func f() {\nfor { break }\nfor x < 3 { x += 1 }\nfor i in 0..10 step 2 { }\nfor i, x in xs { }\n}",
);
assert!(matches!(&stmts[0], Stmt::For { kind: ForKind::Infinite, .. }));
assert!(matches!(&stmts[1], Stmt::For { kind: ForKind::While(_), .. }));
match &stmts[2] {
Stmt::For { kind: ForKind::In { vars, step, .. }, .. } => {
assert_eq!(vars.len(), 1);
assert!(step.is_some());
}
other => panic!("{other:?}"),
}
match &stmts[3] {
Stmt::For { kind: ForKind::In { vars, .. }, .. } => assert_eq!(vars, &["i", "x"]),
other => panic!("{other:?}"),
}
}
#[test]
fn grouped_params() {
let p = parse("func f(a, b: int, s: string) {}").unwrap();
let Item::Func(f) = &p.items[0] else { panic!() };
let names: Vec<_> = f.params.iter().map(|p| p.name.as_str()).collect();
assert_eq!(names, ["a", "b", "s"]);
assert_eq!(f.params[0].ty, f.params[1].ty);
assert_ne!(f.params[1].ty, f.params[2].ty);
}
#[test]
fn struct_with_methods() {
let p = parse("pub struct P {\n x, y: f32\n pub func len(): f32 { return x }\n}").unwrap();
let Item::Struct(s) = &p.items[0] else { panic!() };
assert!(s.is_pub);
assert_eq!(s.fields.len(), 2);
assert_eq!(s.methods.len(), 1);
assert!(s.methods[0].is_pub);
}
#[test]
fn types() {
let p = parse("struct E {\n a: shared Doc\n b: weak Win?\n c: []int\n d: Stack[int]\n e: ui.Key\n}").unwrap();
let Item::Struct(s) = &p.items[0] else { panic!() };
assert!(matches!(s.fields[0].ty, Type::Shared(_)));
assert!(matches!(s.fields[1].ty, Type::Optional(_)));
assert!(matches!(s.fields[2].ty, Type::Slice(_)));
assert!(matches!(&s.fields[3].ty, Type::Named { args, .. } if args.len() == 1));
assert!(matches!(&s.fields[4].ty, Type::Named { path, .. } if path.len() == 2));
}
#[test]
fn multiple_returns() {
let p = parse("func f(): (int, error) { return 1, nil }").unwrap();
let Item::Func(f) = &p.items[0] else { panic!() };
assert!(matches!(&f.ret, Some(Type::Tuple(t)) if t.len() == 2));
}
#[test]
fn multi_line_call_and_literal() {
parse("func f() {\n g(\n 1,\n 2,\n)\n var p = P{\n x: 1,\n y: 2,\n }\n}").unwrap();
}
#[test]
fn else_if_chain() {
let stmts = body_of("func f() {\nif a { } else if b { } else { }\n}");
let Stmt::If { else_: Some(e), .. } = &stmts[0] else { panic!() };
assert!(matches!(**e, Stmt::If { .. }));
}
#[test]
fn semicolons_work() {
parse("func f() { var a = 1; var b = 2 }").unwrap();
}
// ---------- error messages ----------
fn err(src: &str) -> rex::error::Diag {
parse(src).unwrap_err()
}
#[test]
fn error_unclosed_brace_points_at_opener() {
let d = err("func main() {\n var x = 1\n");
assert!(d.msg.contains("never closed"), "{}", d.msg);
assert_eq!((d.span.line, d.span.col), (1, 13));
}
#[test]
fn error_unterminated_string() {
let d = err("func main() {\n var x = \"oops\n}");
assert!(d.msg.contains("never closed"), "{}", d.msg);
assert_eq!(d.span.line, 2);
}
#[test]
fn error_missing_param_type() {
let d = err("func f(a) {}");
assert!(d.msg.contains("`a` needs a type"), "{}", d.msg);
}
#[test]
fn error_global_var() {
let d = err("var x = 1");
assert!(d.msg.contains("global `var`"), "{}", d.msg);
}
#[test]
fn error_const_needs_value() {
assert!(err("func f() { const x: int }").msg.contains("const"));
}
#[test]
fn bit_operators_bind_like_go() {
// `&` and shifts go with `*`, `|` and `^` with `+`, all tighter than comparisons
let p = parse("func f() { var a = b & 1 == 0 }").unwrap();
let text = format!("{p:?}");
let eq = text.find("op: Eq").unwrap();
let and = text.find("op: BitAnd").unwrap();
assert!(eq < and, "`==` should be the outer operator: {text}");
let p = parse("func f() { var a = b | c << 2 }").unwrap();
let text = format!("{p:?}");
assert!(text.find("op: BitOr").unwrap() < text.find("op: Shl").unwrap(), "{text}");
}
#[test]
fn error_two_statements_on_one_line() {
let d = err("func f() { var a = 1 var b = 2 }");
assert!(d.msg.contains("end of line"), "{}", d.msg);
}
#[test]
fn render_shows_caret() {
let src = "func main() {\n var x = \n}";
let out = err(src).render("t.rex", src);
assert!(out.contains("t.rex:3:1"), "{out}");
assert!(out.contains("^"), "{out}");
}
#[test]
fn generic_bound_uses_a_colon() {
let p = parse("func max[T: Ordered](a, b: T): T { return a }").unwrap();
let Item::Func(f) = &p.items[0] else { panic!() };
assert_eq!(f.type_params[0].name, "T");
assert!(f.type_params[0].constraint.is_some());
assert!(parse("func max[T](a: T) { }").is_ok());
let e = parse("func max[T Ordered](a: T) { }").unwrap_err();
assert!(e.msg.contains("`:` and then the rule"), "{}", e.msg);
}