2022-07-08 01:31:00 +03:00
|
|
|
//! Parse expressions and compare their results to expected values.
|
|
|
|
|
2023-07-27 18:25:45 +03:00
|
|
|
// === Features ===
|
|
|
|
#![feature(cell_update)]
|
2022-07-08 01:31:00 +03:00
|
|
|
// === Non-Standard Linter Configuration ===
|
|
|
|
#![allow(clippy::option_map_unit_fn)]
|
|
|
|
#![allow(clippy::precedence)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![deny(non_ascii_idents)]
|
|
|
|
#![deny(unconditional_recursion)]
|
|
|
|
#![warn(unsafe_code)]
|
|
|
|
#![warn(missing_copy_implementations)]
|
|
|
|
#![warn(missing_debug_implementations)]
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
#![warn(trivial_casts)]
|
|
|
|
#![warn(trivial_numeric_casts)]
|
|
|
|
#![warn(unused_import_braces)]
|
|
|
|
#![warn(unused_qualifications)]
|
|
|
|
|
2022-09-14 21:09:58 +03:00
|
|
|
|
|
|
|
|
2022-09-03 06:15:27 +03:00
|
|
|
mod metadata;
|
|
|
|
|
2022-10-12 20:40:16 +03:00
|
|
|
use enso_parser_debug::to_s_expr;
|
2022-07-08 01:31:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ===========================
|
|
|
|
// === Test support macros ===
|
|
|
|
// ===========================
|
|
|
|
|
2022-07-20 17:53:20 +03:00
|
|
|
/// Parses input as a sequence of S-expressions, and wraps it in a `BodyBlock`.
|
2022-07-08 01:31:00 +03:00
|
|
|
macro_rules! block {
|
2022-07-20 17:53:20 +03:00
|
|
|
( $($statements:tt)* ) => {
|
2022-10-12 20:40:16 +03:00
|
|
|
lexpr::sexp![(BodyBlock #( $( $statements )* ) )]
|
2022-07-08 01:31:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:19:12 +03:00
|
|
|
macro_rules! test {
|
|
|
|
( $code:expr, $($statements:tt)* ) => {
|
|
|
|
test($code, block![$( $statements )*]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 01:31:00 +03:00
|
|
|
|
|
|
|
|
2022-11-09 05:57:40 +03:00
|
|
|
// ================================
|
|
|
|
// === Language Construct Tests ===
|
|
|
|
// ================================
|
2022-07-08 01:31:00 +03:00
|
|
|
|
2022-07-20 17:53:20 +03:00
|
|
|
#[test]
|
|
|
|
fn nothing() {
|
2022-11-22 22:59:33 +03:00
|
|
|
test("", block![()]);
|
2022-07-20 17:53:20 +03:00
|
|
|
}
|
|
|
|
|
2022-07-08 01:31:00 +03:00
|
|
|
#[test]
|
|
|
|
fn application() {
|
|
|
|
test("a b c", block![(App (App (Ident a) (Ident b)) (Ident c))]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-10-05 07:45:31 +03:00
|
|
|
fn parentheses() {
|
|
|
|
test("(a b)", block![(Group (App (Ident a) (Ident b)))]);
|
|
|
|
test("x)", block![(App (Ident x) (Invalid))]);
|
|
|
|
test("(x", block![(App (Invalid) (Ident x))]);
|
2022-10-18 01:46:52 +03:00
|
|
|
test("(a) (b)", block![(App (Group (Ident a)) (Group (Ident b)))]);
|
2022-10-05 07:45:31 +03:00
|
|
|
#[rustfmt::skip]
|
2022-10-18 01:46:52 +03:00
|
|
|
test("((a b) c)", block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(Group
|
|
|
|
(App (Group (App (Ident a) (Ident b)))
|
2022-10-18 01:46:52 +03:00
|
|
|
(Ident c)))]);
|
2022-07-20 17:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn section_simple() {
|
2022-10-05 07:45:31 +03:00
|
|
|
let expected_lhs = block![(OprSectionBoundary 1 (OprApp () (Ok "+") (Ident a)))];
|
2022-07-20 17:53:20 +03:00
|
|
|
test("+ a", expected_lhs);
|
2022-10-05 07:45:31 +03:00
|
|
|
let expected_rhs = block![(OprSectionBoundary 1 (OprApp (Ident a) (Ok "+") ()))];
|
2022-07-20 17:53:20 +03:00
|
|
|
test("a +", expected_rhs);
|
|
|
|
}
|
|
|
|
|
2022-10-12 20:40:16 +03:00
|
|
|
#[test]
|
|
|
|
fn inline_if() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("if True then True else False", block![
|
|
|
|
(MultiSegmentApp #(((Ident if) (Ident True))
|
|
|
|
((Ident then) (Ident True))
|
|
|
|
((Ident else) (Ident False))))]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn then_block() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("if True then\n True", block![
|
|
|
|
(MultiSegmentApp #(((Ident if) (Ident True)) ((Ident then) (BodyBlock #((Ident True))))))]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn else_block() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("if True then True else\n False", block![
|
|
|
|
(MultiSegmentApp #(((Ident if) (Ident True))
|
|
|
|
((Ident then) (Ident True))
|
|
|
|
((Ident else) (BodyBlock #((Ident False))))))]);
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:17:25 +03:00
|
|
|
#[test]
|
|
|
|
fn if_then_else_chained_block() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("if True then True else False\n . to_text", block![
|
|
|
|
(OperatorBlockApplication
|
|
|
|
(MultiSegmentApp #(((Ident if) (Ident True))
|
|
|
|
((Ident then) (Ident True))
|
|
|
|
((Ident else) (Ident False)
|
|
|
|
)))
|
|
|
|
#(((Ok ".") (Ident to_text)))
|
|
|
|
#()
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_then_else_chained_block_with_group() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("(if True then True else False)\n . to_text", block![
|
|
|
|
(OperatorBlockApplication
|
|
|
|
(Group (MultiSegmentApp #(((Ident if) (Ident True))
|
|
|
|
((Ident then) (Ident True))
|
|
|
|
((Ident else) (Ident False)
|
|
|
|
))))
|
|
|
|
#(((Ok ".") (Ident to_text)))
|
|
|
|
#()
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_then_else_chained_block_multi2() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("if True then True else False\n . to_text\n . as_value", block![
|
|
|
|
(OperatorBlockApplication
|
|
|
|
(MultiSegmentApp #(((Ident if) (Ident True))
|
|
|
|
((Ident then) (Ident True))
|
|
|
|
((Ident else) (Ident False)
|
|
|
|
)))
|
|
|
|
#(((Ok ".") (Ident to_text)) ((Ok ".") (Ident as_value)))
|
|
|
|
#()
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_then_else_chained_block_multi3() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("if True then True else False\n . to_text\n . as_value\n . done 42", block![
|
|
|
|
(OperatorBlockApplication
|
|
|
|
(MultiSegmentApp #(((Ident if) (Ident True))
|
|
|
|
((Ident then) (Ident True))
|
|
|
|
((Ident else) (Ident False)
|
|
|
|
)))
|
|
|
|
#(((Ok ".") (Ident to_text)) ((Ok ".") (Ident as_value)) ((Ok ".") (App (Ident done) (Number () "42" ()))))
|
|
|
|
#()
|
|
|
|
)]);
|
|
|
|
}
|
2022-07-28 20:17:33 +03:00
|
|
|
|
2022-10-15 09:13:32 +03:00
|
|
|
// === Comments ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn plain_comments() {
|
2022-11-22 22:59:33 +03:00
|
|
|
test!("# a b c", ()());
|
2022-11-18 23:58:41 +03:00
|
|
|
test!("main = # define main\n 4",
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident main) #() () "=" (BodyBlock #(() (Number () "4" ())))));
|
2022-10-15 09:13:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn doc_comments() {
|
|
|
|
#[rustfmt::skip]
|
2024-03-25 02:45:55 +03:00
|
|
|
let lines = [
|
2022-10-15 09:13:32 +03:00
|
|
|
"## The Identity Function",
|
|
|
|
"",
|
|
|
|
" Arguments:",
|
|
|
|
" - x: value to do nothing to",
|
|
|
|
"id x = x",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test(&lines.join("\n"), block![
|
|
|
|
(Documented
|
2022-11-27 12:40:44 +03:00
|
|
|
(#((Section " The Identity Function") (Newline)
|
|
|
|
(Newline)
|
|
|
|
(Section "Arguments:") (Newline)
|
2022-10-15 09:13:32 +03:00
|
|
|
(Section "- x: value to do nothing to"))
|
2022-10-18 01:46:52 +03:00
|
|
|
#(()))
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident id) #((() (Ident x) () ())) () "=" (Ident x)))]);
|
2022-10-15 09:13:32 +03:00
|
|
|
#[rustfmt::skip]
|
2024-03-25 02:45:55 +03:00
|
|
|
let lines = [
|
2023-10-19 15:36:42 +03:00
|
|
|
"type Foo",
|
2023-01-26 15:53:20 +03:00
|
|
|
" ## Test indent handling",
|
|
|
|
" ",
|
|
|
|
" foo",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test!(&lines.join("\n"),
|
2023-10-19 15:36:42 +03:00
|
|
|
(TypeDef type Foo #() #(
|
|
|
|
(Documented
|
|
|
|
(#((Section " Test indent handling")) #(() ()))
|
|
|
|
(Ident foo)))));
|
2022-10-15 09:13:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-28 20:17:33 +03:00
|
|
|
// === Type Definitions ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_definition_no_body() {
|
2022-11-14 23:24:07 +03:00
|
|
|
test!("type Bool", (TypeDef type Bool #() #()));
|
|
|
|
test!("type Option a", (TypeDef type Option #((() (Ident a) () ())) #()));
|
|
|
|
test!("type Option (a)", (TypeDef type Option #((() (Ident a) () ())) #()));
|
|
|
|
test!("type Foo (a : Int)", (TypeDef type Foo #((() (Ident a) (":" (Ident Int)) ())) #()));
|
|
|
|
test!("type A a=0", (TypeDef type A #((() (Ident a) () ("=" (Number () "0" ())))) #()));
|
|
|
|
test!("type Existing_Headers (column_names : Vector Text)",
|
2022-10-05 07:45:31 +03:00
|
|
|
(TypeDef type Existing_Headers #(
|
2022-11-14 23:24:07 +03:00
|
|
|
(() (Ident column_names) (":" (App (Ident Vector) (Ident Text))) ())) #()));
|
2022-07-28 20:17:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_constructors() {
|
|
|
|
let code = [
|
|
|
|
"type Geo",
|
|
|
|
" Circle",
|
|
|
|
" radius",
|
2022-09-14 21:09:58 +03:00
|
|
|
" x",
|
2022-07-28 20:17:33 +03:00
|
|
|
" Rectangle width height",
|
|
|
|
" Point",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(TypeDef type Geo #()
|
2023-01-12 19:51:44 +03:00
|
|
|
#((ConstructorDefinition
|
|
|
|
Circle #() #(((() (Ident radius) () ())) ((() (Ident x) () ()))))
|
|
|
|
(ConstructorDefinition
|
|
|
|
Rectangle #((() (Ident width) () ()) (() (Ident height) () ())) #())
|
|
|
|
(ConstructorDefinition Point #() #())))];
|
2022-07-28 20:17:33 +03:00
|
|
|
test(&code.join("\n"), expected);
|
2022-10-18 01:46:52 +03:00
|
|
|
let code = "type Foo\n Bar (a : B = C.D)";
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2023-01-12 19:51:44 +03:00
|
|
|
(TypeDef type Foo #() #((ConstructorDefinition
|
2022-11-14 23:24:07 +03:00
|
|
|
Bar
|
|
|
|
#((() (Ident a) (":" (Ident B)) ("=" (OprApp (Ident C) (Ok ".") (Ident D)))))
|
2023-01-12 19:51:44 +03:00
|
|
|
#())))];
|
2022-10-18 01:46:52 +03:00
|
|
|
test(code, expected);
|
|
|
|
let code = "type Foo\n ## Bar\n Baz";
|
2023-01-12 19:51:44 +03:00
|
|
|
let expected = block![(TypeDef type Foo #() #(
|
|
|
|
(Documented (#((Section " Bar")) #(())) (ConstructorDefinition Baz #() #()))))];
|
2022-10-18 01:46:52 +03:00
|
|
|
test(code, expected);
|
|
|
|
let code = ["type A", " Foo (a : Integer, b : Integer)"];
|
|
|
|
#[rustfmt::skip]
|
2023-01-12 19:51:44 +03:00
|
|
|
let expected = block![(TypeDef type A #() #(
|
|
|
|
(ConstructorDefinition Foo #((() (Invalid) () ())) #())))];
|
2022-10-18 01:46:52 +03:00
|
|
|
test(&code.join("\n"), expected);
|
2022-07-28 20:17:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_methods() {
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["type Geo", " number =", " x", " area self = x + x"];
|
2022-07-28 20:17:33 +03:00
|
|
|
#[rustfmt::skip]
|
2022-10-05 07:45:31 +03:00
|
|
|
let expected = block![
|
2022-11-14 23:24:07 +03:00
|
|
|
(TypeDef type Geo #()
|
2023-12-12 17:48:44 +03:00
|
|
|
#((Function (Ident number) #() () "=" (BodyBlock #((Ident x))))
|
|
|
|
(Function (Ident area) #((() (Ident self) () ())) ()
|
2023-01-12 19:51:44 +03:00
|
|
|
"=" (OprApp (Ident x) (Ok "+") (Ident x)))))];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
let code = [
|
|
|
|
"type Problem_Builder",
|
|
|
|
" ## Returns a vector containing all reported problems, aggregated.",
|
|
|
|
" build_problemset : Vector",
|
|
|
|
" build_problemset self =",
|
|
|
|
" self",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(TypeDef type Problem_Builder #() #(
|
|
|
|
(Documented
|
|
|
|
(#((Section " Returns a vector containing all reported problems, aggregated.")) #(()))
|
|
|
|
(TypeSignature (Ident build_problemset) ":" (Ident Vector)))
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident build_problemset) #((() (Ident self) () ())) ()
|
2023-01-12 19:51:44 +03:00
|
|
|
"=" (BodyBlock #((Ident self))))))
|
|
|
|
];
|
2022-07-28 20:17:33 +03:00
|
|
|
test(&code.join("\n"), expected);
|
2022-07-08 01:31:00 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 07:45:31 +03:00
|
|
|
#[test]
|
|
|
|
fn type_operator_methods() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"type Foo",
|
|
|
|
" + : Foo -> Foo -> Foo",
|
|
|
|
" + self b = b",
|
2022-10-31 19:19:12 +03:00
|
|
|
" Foo.+ : Foo",
|
|
|
|
" Foo.+ self b = b",
|
2022-10-05 07:45:31 +03:00
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2022-11-14 23:24:07 +03:00
|
|
|
(TypeDef type Foo #()
|
2023-01-12 19:51:44 +03:00
|
|
|
#((TypeSignature (Ident #"+") ":"
|
|
|
|
(OprApp (Ident Foo) (Ok "->") (OprApp (Ident Foo) (Ok "->") (Ident Foo))))
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident #"+") #((() (Ident self) () ()) (() (Ident b) () ())) () "=" (Ident b))
|
2023-01-12 19:51:44 +03:00
|
|
|
(TypeSignature (OprApp (Ident Foo) (Ok ".") (Ident #"+")) ":" (Ident Foo))
|
|
|
|
(Function (OprApp (Ident Foo) (Ok ".") (Ident #"+"))
|
2023-12-12 17:48:44 +03:00
|
|
|
#((() (Ident self) () ()) (() (Ident b) () ())) () "=" (Ident b))))];
|
2022-10-05 07:45:31 +03:00
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2022-07-28 20:17:33 +03:00
|
|
|
#[test]
|
|
|
|
fn type_def_full() {
|
|
|
|
let code = [
|
|
|
|
"type Geo",
|
|
|
|
" Circle",
|
|
|
|
" radius : float",
|
2022-09-14 21:09:58 +03:00
|
|
|
" x",
|
2022-07-28 20:17:33 +03:00
|
|
|
" Rectangle width height",
|
|
|
|
" Point",
|
|
|
|
"",
|
|
|
|
" number =",
|
2022-09-14 21:09:58 +03:00
|
|
|
" x",
|
|
|
|
" area self = x + x",
|
2022-07-28 20:17:33 +03:00
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(TypeDef type Geo #()
|
2023-01-12 19:51:44 +03:00
|
|
|
#((ConstructorDefinition Circle #() #(
|
2022-10-05 07:45:31 +03:00
|
|
|
((() (Ident radius) (":" (Ident float)) ()))
|
2023-01-12 19:51:44 +03:00
|
|
|
((() (Ident x) () ()))))
|
|
|
|
(ConstructorDefinition
|
|
|
|
Rectangle #((() (Ident width) () ()) (() (Ident height) () ())) #())
|
|
|
|
(ConstructorDefinition Point #() #())
|
|
|
|
()
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident number) #() () "=" (BodyBlock #((Ident x))))
|
|
|
|
(Function (Ident area) #((() (Ident self) () ())) ()
|
2023-01-12 19:51:44 +03:00
|
|
|
"=" (OprApp (Ident x) (Ok "+") (Ident x)))))];
|
2022-07-28 20:17:33 +03:00
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2022-10-05 07:45:31 +03:00
|
|
|
#[test]
|
|
|
|
fn type_def_defaults() {
|
|
|
|
let code = ["type Result error ok=Nothing", " Ok value:ok = Nothing"];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(TypeDef type Result #((() (Ident error) () ())
|
|
|
|
(() (Ident ok) () ("=" (Ident Nothing))))
|
2023-01-12 19:51:44 +03:00
|
|
|
#((ConstructorDefinition Ok
|
|
|
|
#((() (Ident value) (":" (Ident ok)) ("=" (Ident Nothing)))) #())))];
|
2022-10-05 07:45:31 +03:00
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:09:20 +03:00
|
|
|
#[test]
|
|
|
|
fn type_def_nested() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"type Foo",
|
|
|
|
" type Bar",
|
|
|
|
" type Baz",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2022-11-14 23:24:07 +03:00
|
|
|
(TypeDef type Foo #()
|
2023-01-12 19:51:44 +03:00
|
|
|
#((TypeDef type Bar #() #())
|
|
|
|
(TypeDef type Baz #() #())))
|
2022-08-02 18:09:20 +03:00
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2022-07-28 20:17:33 +03:00
|
|
|
|
|
|
|
// === Variable Assignment ===
|
|
|
|
|
2022-07-08 01:31:00 +03:00
|
|
|
#[test]
|
|
|
|
fn assignment_simple() {
|
2022-09-14 21:09:58 +03:00
|
|
|
test("foo = x", block![(Assignment (Ident foo) "=" (Ident x))]);
|
2022-07-08 01:31:00 +03:00
|
|
|
}
|
|
|
|
|
2022-07-28 20:17:33 +03:00
|
|
|
|
|
|
|
// === Functions ===
|
|
|
|
|
2022-07-08 01:31:00 +03:00
|
|
|
#[test]
|
|
|
|
fn function_inline_simple_args() {
|
2023-12-12 17:48:44 +03:00
|
|
|
test!("foo a = x", (Function (Ident foo) #((() (Ident a) () ())) () "=" (Ident x)));
|
|
|
|
test!("foo a b = x",
|
|
|
|
(Function (Ident foo) #((() (Ident a) () ()) (() (Ident b) () ())) () "=" (Ident x)));
|
2022-09-15 08:32:28 +03:00
|
|
|
#[rustfmt::skip]
|
2023-12-12 17:48:44 +03:00
|
|
|
test!(
|
|
|
|
"foo a b c = x",
|
2022-10-12 20:40:16 +03:00
|
|
|
(Function (Ident foo)
|
2022-09-15 08:32:28 +03:00
|
|
|
#((() (Ident a) () ()) (() (Ident b) () ()) (() (Ident c) () ()))
|
2023-12-12 17:48:44 +03:00
|
|
|
() "=" (Ident x))
|
2022-09-15 08:32:28 +03:00
|
|
|
);
|
2023-12-12 17:48:44 +03:00
|
|
|
test!("foo _ = x", (Function (Ident foo) #((() (Wildcard -1) () ())) () "=" (Ident x)));
|
2022-07-08 01:31:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_block_noargs() {
|
2023-12-12 17:48:44 +03:00
|
|
|
test("foo =", block![(Function (Ident foo) #() () "=" ())]);
|
2022-07-08 01:31:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_block_simple_args() {
|
2023-12-12 17:48:44 +03:00
|
|
|
test("foo a =", block![(Function (Ident foo) #((() (Ident a) () ())) () "=" ())]);
|
2022-09-15 08:32:28 +03:00
|
|
|
#[rustfmt::skip]
|
2022-10-12 20:40:16 +03:00
|
|
|
test("foo a b =", block![(Function (Ident foo) #((() (Ident a) () ())
|
2023-12-12 17:48:44 +03:00
|
|
|
(() (Ident b) () ())) () "=" ())]);
|
2022-10-12 20:40:16 +03:00
|
|
|
#[rustfmt::skip]
|
2023-12-12 17:48:44 +03:00
|
|
|
test!("foo a b c =",
|
|
|
|
(Function (Ident foo)
|
|
|
|
#((() (Ident a) () ()) (() (Ident b) () ()) (() (Ident c) () ()))
|
|
|
|
()
|
|
|
|
"="
|
|
|
|
()));
|
2022-10-12 20:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_qualified() {
|
2023-12-12 17:48:44 +03:00
|
|
|
test!("Id.id x = x",
|
|
|
|
(Function (OprApp (Ident Id) (Ok ".") (Ident id))
|
|
|
|
#((() (Ident x) () ()))
|
|
|
|
() "=" (Ident x)));
|
2022-09-15 08:32:28 +03:00
|
|
|
}
|
|
|
|
|
2022-10-31 19:19:12 +03:00
|
|
|
#[test]
|
|
|
|
fn ignored_arguments() {
|
2023-12-12 17:48:44 +03:00
|
|
|
test!("f ~_ = x", (Function (Ident f) #(("~" (Wildcard -1) () ())) () "=" (Ident x)));
|
2022-10-31 19:19:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn foreign_functions() {
|
|
|
|
test!("foreign python my_method a b = \"42\"",
|
|
|
|
(ForeignFunction foreign python my_method
|
|
|
|
#((() (Ident a) () ()) (() (Ident b) () ()))
|
|
|
|
"="
|
|
|
|
(TextLiteral #((Section "42")))));
|
|
|
|
}
|
|
|
|
|
2023-12-12 17:48:44 +03:00
|
|
|
#[test]
|
|
|
|
fn function_inline_return_specification() {
|
|
|
|
// Typical usage
|
|
|
|
test!("id self that:Integer -> Integer = that",
|
|
|
|
(Function (Ident id)
|
|
|
|
#((() (Ident self) () ()) (() (Ident that) (":" (Ident Integer)) ()))
|
|
|
|
("->" (Ident Integer))
|
|
|
|
"=" (Ident that)));
|
|
|
|
// Edge case
|
|
|
|
test!("number -> Integer = 23",
|
|
|
|
(Function (Ident number) #()
|
|
|
|
("->" (Ident Integer))
|
|
|
|
"=" (Number () "23" ())));
|
|
|
|
// Edge case: Not an inline return specification
|
|
|
|
expect_invalid_node("f x : Integer -> Integer = 23");
|
|
|
|
}
|
|
|
|
|
2022-09-15 08:32:28 +03:00
|
|
|
|
|
|
|
// === Named arguments ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn named_arguments() {
|
|
|
|
let cases = [
|
2022-10-05 07:45:31 +03:00
|
|
|
("f x=y", block![(NamedApp (Ident f) x "=" (Ident y))]),
|
|
|
|
("f (x = y)", block![(NamedApp (Ident f) x "=" (Ident y))]),
|
2022-09-15 08:32:28 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// === Default arguments ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_app() {
|
2023-12-12 17:48:44 +03:00
|
|
|
test!("f default", (App (Ident f) (Ident default)));
|
2022-10-31 19:19:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn argument_named_default() {
|
|
|
|
test!("f default x = x",
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident f) #((() (Ident default) () ()) (() (Ident x) () ())) () "=" (Ident x)));
|
2022-10-31 19:19:12 +03:00
|
|
|
test!("f x default = x",
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident f) #((() (Ident x) () ()) (() (Ident default) () ())) () "=" (Ident x)));
|
2022-09-15 08:32:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_arguments() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let cases = [
|
2022-10-12 20:40:16 +03:00
|
|
|
("f x=1 = x", block![
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident f) #((() (Ident x) () ("=" (Number () "1" ())))) () "=" (Ident x))]),
|
2022-10-12 20:40:16 +03:00
|
|
|
("f (x = 1) = x", block![
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident f) #((() (Ident x) () ("=" (Number () "1" ())))) () "=" (Ident x))]),
|
2022-09-15 08:32:28 +03:00
|
|
|
// Pattern in LHS:
|
|
|
|
("f ~x=1 = x", block![
|
2022-10-12 20:40:16 +03:00
|
|
|
(Function (Ident f)
|
2022-10-05 07:45:31 +03:00
|
|
|
#(("~" (Ident x) () ("=" (Number () "1" ()))))
|
2023-12-12 17:48:44 +03:00
|
|
|
()
|
2022-09-15 08:32:28 +03:00
|
|
|
"=" (Ident x))]),
|
|
|
|
("f (~x = 1) = x", block![
|
2022-10-12 20:40:16 +03:00
|
|
|
(Function (Ident f)
|
2022-10-05 07:45:31 +03:00
|
|
|
#(("~" (Ident x) () ("=" (Number () "1" ()))))
|
2023-12-12 17:48:44 +03:00
|
|
|
()
|
2022-09-15 08:32:28 +03:00
|
|
|
"=" (Ident x))]),
|
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
2022-07-08 01:31:00 +03:00
|
|
|
}
|
|
|
|
|
2022-07-28 20:17:33 +03:00
|
|
|
|
|
|
|
// === Code Blocks ===
|
|
|
|
|
2022-07-20 17:53:20 +03:00
|
|
|
#[test]
|
|
|
|
fn code_block_body() {
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["main =", " x"];
|
2023-12-12 17:48:44 +03:00
|
|
|
test(&code.join("\n"), block![(Function (Ident main) #() () "=" (BodyBlock #((Ident x))))]);
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["main =", " ", " x"];
|
2023-12-12 17:48:44 +03:00
|
|
|
test(&code.join("\n"), block![(Function (Ident main) #() () "=" (BodyBlock #(() (Ident x))))]);
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["main =", " ", " x"];
|
2023-12-12 17:48:44 +03:00
|
|
|
test(&code.join("\n"), block![(Function (Ident main) #() () "=" (BodyBlock #(() (Ident x))))]);
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["main =", " ", " x"];
|
2023-12-12 17:48:44 +03:00
|
|
|
test(&code.join("\n"), block![(Function (Ident main) #() () "=" (BodyBlock #(() (Ident x))))]);
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["main =", "", " x"];
|
2023-12-12 17:48:44 +03:00
|
|
|
test(&code.join("\n"), block![(Function (Ident main) #() () "=" (BodyBlock #(() (Ident x))))]);
|
2022-07-20 17:53:20 +03:00
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"main =",
|
2022-09-14 21:09:58 +03:00
|
|
|
" +x",
|
|
|
|
" print x",
|
2022-07-20 17:53:20 +03:00
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expect = block![
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident main) #() () "=" (BodyBlock #(
|
2022-10-05 07:45:31 +03:00
|
|
|
(OprSectionBoundary 1 (OprApp () (Ok "+") (Ident x)))
|
2022-09-14 21:09:58 +03:00
|
|
|
(App (Ident print) (Ident x)))))
|
2022-07-20 17:53:20 +03:00
|
|
|
];
|
|
|
|
test(&code.join("\n"), expect);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn code_block_operator() {
|
|
|
|
let code = ["value = nums", " * each random", " + constant"];
|
|
|
|
let expect = block![
|
|
|
|
(Assignment (Ident value) "="
|
|
|
|
(OperatorBlockApplication (Ident nums)
|
|
|
|
#(((Ok "*") (App (Ident each) (Ident random)))
|
|
|
|
((Ok "+") (Ident constant)))
|
|
|
|
#()))
|
|
|
|
];
|
|
|
|
test(&code.join("\n"), expect);
|
|
|
|
}
|
|
|
|
|
2022-10-05 07:45:31 +03:00
|
|
|
#[test]
|
|
|
|
fn dot_operator_blocks() {
|
|
|
|
let code = ["rect1", " . width = 7", " . center", " + x"];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(OperatorBlockApplication (Ident rect1)
|
|
|
|
#(((Ok ".") (OprApp (Ident width) (Ok "=") (Number () "7" ())))
|
|
|
|
((Ok ".") (OperatorBlockApplication (Ident center)
|
|
|
|
#(((Ok "+") (Ident x))) #()))) #())];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2022-07-20 17:53:20 +03:00
|
|
|
#[test]
|
|
|
|
fn code_block_argument_list() {
|
2023-11-08 19:53:39 +03:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"foo",
|
|
|
|
" bar",
|
|
|
|
];
|
|
|
|
test!(&code.join("\n"), (ArgumentBlockApplication (Ident foo) #((Ident bar))));
|
|
|
|
|
2022-07-20 17:53:20 +03:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"value = foo",
|
|
|
|
" bar",
|
|
|
|
];
|
|
|
|
let expect = block![
|
|
|
|
(Assignment (Ident value) "=" (ArgumentBlockApplication (Ident foo) #((Ident bar))))
|
|
|
|
];
|
|
|
|
test(&code.join("\n"), expect);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"value = foo",
|
2022-09-14 21:09:58 +03:00
|
|
|
" +x",
|
2022-07-20 17:53:20 +03:00
|
|
|
" bar",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expect = block![
|
|
|
|
(Assignment (Ident value) "="
|
|
|
|
(ArgumentBlockApplication (Ident foo) #(
|
2022-10-05 07:45:31 +03:00
|
|
|
(OprSectionBoundary 1 (OprApp () (Ok "+") (Ident x)))
|
2022-07-20 17:53:20 +03:00
|
|
|
(Ident bar))))
|
|
|
|
];
|
|
|
|
test(&code.join("\n"), expect);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn code_block_empty() {
|
|
|
|
// The first line here should parse as a function with no body expression (which is an error).
|
|
|
|
// No input would parse as an empty `ArgumentBlock` or `OperatorBlock`, because those types are
|
|
|
|
// distinguished from a body continuation by the presence of non-empty indented lines.
|
|
|
|
let code = ["foo =", "bar"];
|
2023-12-12 17:48:44 +03:00
|
|
|
test(&code.join("\n"), block![(Function (Ident foo) #() () "=" ()) (Ident bar)]);
|
2022-07-20 17:53:20 +03:00
|
|
|
// This parses similarly to above; a line with no non-whitespace content does not create a code
|
|
|
|
// block.
|
|
|
|
let code = ["foo =", " ", "bar"];
|
2023-12-12 17:48:44 +03:00
|
|
|
test(&code.join("\n"), block![(Function (Ident foo) #() () "=" ()) () (Ident bar)]);
|
2022-07-20 17:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn code_block_bad_indents1() {
|
|
|
|
let code = ["main =", " foo", " bar", " baz"];
|
|
|
|
let expected = block![
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident main) #() () "=" (BodyBlock #((Ident foo) (Ident bar) (Ident baz))))
|
2022-07-20 17:53:20 +03:00
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn code_block_bad_indents2() {
|
|
|
|
let code = ["main =", " foo", " bar", "baz"];
|
|
|
|
let expected = block![
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident main) #() () "=" (BodyBlock #((Ident foo) (Ident bar))))
|
2022-07-20 17:53:20 +03:00
|
|
|
(Ident baz)
|
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn code_block_with_following_statement() {
|
|
|
|
let code = ["main =", " foo", "bar"];
|
|
|
|
let expected = block![
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident main) #() () "=" (BodyBlock #((Ident foo))))
|
2022-07-20 17:53:20 +03:00
|
|
|
(Ident bar)
|
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2022-10-05 07:45:31 +03:00
|
|
|
#[test]
|
|
|
|
fn operator_block_nested() {
|
|
|
|
let code = ["foo", " + bar", " - baz"];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(OperatorBlockApplication (Ident foo)
|
|
|
|
#(((Ok "+") (OperatorBlockApplication (Ident bar) #(((Ok "-") (Ident baz))) #())))
|
|
|
|
#())];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operator_section_in_operator_block() {
|
|
|
|
let code = ["foo", " + bar +"];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(OperatorBlockApplication (Ident foo)
|
|
|
|
#(((Ok "+") (OprSectionBoundary 1 (OprApp (Ident bar) (Ok "+") ()))))
|
|
|
|
#())];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2023-10-19 15:36:42 +03:00
|
|
|
#[test]
|
|
|
|
fn first_line_indented() {
|
|
|
|
expect_invalid_node(" a");
|
|
|
|
}
|
|
|
|
|
2022-07-08 01:31:00 +03:00
|
|
|
|
2022-07-28 20:17:33 +03:00
|
|
|
// === Binary Operators ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_operator_error() {
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["x + + x"];
|
2022-07-28 20:17:33 +03:00
|
|
|
let expected = block![
|
2022-09-14 21:09:58 +03:00
|
|
|
(OprApp (Ident x) (Err (#("+" "+"))) (Ident x))
|
2022-07-28 20:17:33 +03:00
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["x + + + x"];
|
2022-07-28 20:17:33 +03:00
|
|
|
let expected = block![
|
2022-09-14 21:09:58 +03:00
|
|
|
(OprApp (Ident x) (Err (#("+" "+" "+"))) (Ident x))
|
2022-07-28 20:17:33 +03:00
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn precedence() {
|
2022-10-15 09:13:32 +03:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let cases = [
|
|
|
|
("x * y + z", block![(OprApp (OprApp (Ident x) (Ok "*") (Ident y)) (Ok "+") (Ident z))]),
|
|
|
|
("x + y * z", block![(OprApp (Ident x) (Ok "+") (OprApp (Ident y) (Ok "*") (Ident z)))]),
|
|
|
|
("w + x + y * z", block![
|
|
|
|
(OprApp (OprApp (Ident w) (Ok "+") (Ident x)) (Ok "+")
|
|
|
|
(OprApp (Ident y) (Ok "*") (Ident z)))]),
|
2022-09-14 21:09:58 +03:00
|
|
|
];
|
2022-10-15 09:13:32 +03:00
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
2022-10-31 19:19:12 +03:00
|
|
|
test!("x - 1 + 2",
|
|
|
|
(OprApp (OprApp (Ident x) (Ok "-") (Number () "1" ())) (Ok "+") (Number () "2" ())));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dot_operator_precedence() {
|
|
|
|
test!("x y . f v", (App (OprApp (App (Ident x) (Ident y)) (Ok ".") (Ident f)) (Ident v)));
|
2022-09-14 21:09:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn right_associative_operators() {
|
2022-10-31 19:19:12 +03:00
|
|
|
test!("x --> y ---> z", (OprApp (Ident x) (Ok "-->") (OprApp (Ident y) (Ok "--->") (Ident z))));
|
|
|
|
test!("x <| y <<| z", (OprApp (Ident x) (Ok "<|") (OprApp (Ident y) (Ok "<<|") (Ident z))));
|
2022-07-28 20:17:33 +03:00
|
|
|
}
|
|
|
|
|
2022-10-15 09:13:32 +03:00
|
|
|
#[test]
|
|
|
|
fn left_associative_operators() {
|
2022-10-31 19:19:12 +03:00
|
|
|
test!("x + y + z", (OprApp (OprApp (Ident x) (Ok "+") (Ident y)) (Ok "+") (Ident z)));
|
2022-10-15 09:13:32 +03:00
|
|
|
}
|
|
|
|
|
2022-09-14 21:09:58 +03:00
|
|
|
#[test]
|
|
|
|
fn pipeline_operators() {
|
|
|
|
test("f <| a", block![(OprApp (Ident f) (Ok "<|") (Ident a))]);
|
|
|
|
test("a |> f", block![(OprApp (Ident a) (Ok "|>") (Ident f))]);
|
|
|
|
}
|
|
|
|
|
2022-10-05 07:45:31 +03:00
|
|
|
#[test]
|
|
|
|
fn accessor_operator() {
|
|
|
|
// Test that the accessor operator `.` is treated like any other operator.
|
|
|
|
let cases = [
|
|
|
|
("Console.", block![(OprSectionBoundary 1 (OprApp (Ident Console) (Ok ".") ()))]),
|
|
|
|
(".", block![(OprSectionBoundary 2 (OprApp () (Ok ".") ()))]),
|
|
|
|
(".log", block![(OprSectionBoundary 1 (OprApp () (Ok ".") (Ident log)))]),
|
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operator_sections() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test(".map (+2 * 3) *7", block![
|
|
|
|
(OprSectionBoundary 1
|
|
|
|
(App (App (OprApp () (Ok ".") (Ident map))
|
|
|
|
(Group
|
|
|
|
(OprSectionBoundary 1 (OprApp (OprApp () (Ok "+") (Number () "2" ()))
|
|
|
|
(Ok "*") (Number () "3" ())))))
|
|
|
|
(OprSectionBoundary 1 (OprApp () (Ok "*") (Number () "7" ())))))]);
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test(".sum 1", block![
|
|
|
|
(OprSectionBoundary 1 (App (OprApp () (Ok ".") (Ident sum)) (Number () "1" ())))]);
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("+1 + x", block![
|
|
|
|
(OprSectionBoundary 1 (OprApp (OprApp () (Ok "+") (Number () "1" ()))
|
|
|
|
(Ok "+") (Ident x)))]);
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("increment = 1 +", block![
|
|
|
|
(Assignment (Ident increment) "="
|
|
|
|
(OprSectionBoundary 1 (OprApp (Number () "1" ()) (Ok "+") ())))]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn template_functions() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("_.map (_+2 * 3) _*7", block![
|
|
|
|
(TemplateFunction 1
|
|
|
|
(App (App (OprApp (Wildcard 0) (Ok ".") (Ident map))
|
|
|
|
(Group
|
|
|
|
(TemplateFunction 1
|
|
|
|
(OprApp (OprApp (Wildcard 0) (Ok "+") (Number () "2" ()))
|
|
|
|
(Ok "*") (Number () "3" ())))))
|
|
|
|
(TemplateFunction 1 (OprApp (Wildcard 0) (Ok "*") (Number () "7" ())))))]);
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("_.sum 1", block![
|
|
|
|
(TemplateFunction 1 (App (OprApp (Wildcard 0) (Ok ".") (Ident sum)) (Number () "1" ())))]);
|
|
|
|
#[rustfmt::skip]
|
|
|
|
test("_+1 + x", block![
|
|
|
|
(TemplateFunction 1 (OprApp (OprApp (Wildcard 0) (Ok "+") (Number () "1" ()))
|
|
|
|
(Ok "+") (Ident x)))]);
|
|
|
|
}
|
|
|
|
|
2022-07-28 20:17:33 +03:00
|
|
|
|
|
|
|
// === Unary Operators ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unevaluated_argument() {
|
2022-09-14 21:09:58 +03:00
|
|
|
let code = ["main ~foo = x"];
|
2022-07-28 20:17:33 +03:00
|
|
|
let expected = block![
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident main) #(("~" (Ident foo) () ())) () "=" (Ident x))
|
2022-07-28 20:17:33 +03:00
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unary_operator_missing_operand() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("main ~ = x");
|
2022-07-28 20:17:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unary_operator_at_end_of_expression() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("foo ~");
|
2022-07-28 20:17:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-07-27 18:25:45 +03:00
|
|
|
fn unspaced_operator_sequence() {
|
2023-10-19 15:36:42 +03:00
|
|
|
// Add a negated value.
|
|
|
|
test!("x = y+-z",
|
|
|
|
(Assignment (Ident x) "=" (OprApp (Ident y) (Ok "+") (UnaryOprApp "-" (Ident z)))));
|
|
|
|
// Create an operator section that adds a negated value to its input.
|
|
|
|
test!("x = +-z",
|
|
|
|
(Assignment (Ident x) "=" (OprSectionBoundary 1
|
|
|
|
(OprApp () (Ok "+") (UnaryOprApp "-" (Ident z))))));
|
|
|
|
// Create an operator section that adds its input, negated, to a value.
|
|
|
|
test!("x = y+-",
|
|
|
|
(Assignment (Ident x) "=" (OprSectionBoundary 1
|
|
|
|
(OprApp (Ident y) (Ok "+") (UnaryOprApp "-" ())))));
|
|
|
|
// Assign a negative number to x.
|
|
|
|
test!("x=-1", (Assignment (Ident x) "=" (UnaryOprApp "-" (Number () "1" ()))));
|
|
|
|
// Assign a negated value to x.
|
|
|
|
test!("x=-y", (Assignment (Ident x) "=" (UnaryOprApp "-" (Ident y))));
|
2022-07-28 20:17:33 +03:00
|
|
|
}
|
|
|
|
|
2022-08-09 23:31:23 +03:00
|
|
|
#[test]
|
|
|
|
fn minus_binary() {
|
|
|
|
let cases = [
|
2022-09-14 21:09:58 +03:00
|
|
|
("x - x", block![(OprApp (Ident x) (Ok "-") (Ident x))]),
|
|
|
|
("x-x", block![(OprApp (Ident x) (Ok "-") (Ident x))]),
|
2022-08-09 23:31:23 +03:00
|
|
|
("x.-y", block![(OprApp (Ident x) (Ok ".") (UnaryOprApp "-" (Ident y)))]),
|
|
|
|
("x.~y", block![(OprApp (Ident x) (Ok ".") (UnaryOprApp "~" (Ident y)))]),
|
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn minus_section() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let cases = [
|
2022-10-05 07:45:31 +03:00
|
|
|
("- x", block![(OprSectionBoundary 1 (OprApp () (Ok "-") (Ident x)))]),
|
|
|
|
("(- x)", block![(Group (OprSectionBoundary 1 (OprApp () (Ok "-") (Ident x))))]),
|
2022-09-14 21:09:58 +03:00
|
|
|
("- (x * x)", block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(OprSectionBoundary 1 (OprApp () (Ok "-")
|
|
|
|
(Group (OprApp (Ident x) (Ok "*") (Ident x)))))]),
|
2022-08-09 23:31:23 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn minus_unary() {
|
2022-10-31 19:19:12 +03:00
|
|
|
test!("f -x", (App (Ident f) (UnaryOprApp "-" (Ident x))));
|
|
|
|
test!("-x", (UnaryOprApp "-" (Ident x)));
|
|
|
|
test!("(-x)", (Group (UnaryOprApp "-" (Ident x))));
|
|
|
|
test!("-(x * x)", (UnaryOprApp "-" (Group (OprApp (Ident x) (Ok "*") (Ident x)))));
|
|
|
|
test!("x=-x", (Assignment (Ident x) "=" (UnaryOprApp "-" (Ident x))));
|
|
|
|
test!("-x+x", (OprApp (UnaryOprApp "-" (Ident x)) (Ok "+") (Ident x)));
|
|
|
|
test!("-x*x", (OprApp (UnaryOprApp "-" (Ident x)) (Ok "*") (Ident x)));
|
2023-03-17 21:53:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn minus_unary_decimal() {
|
2022-10-31 19:19:12 +03:00
|
|
|
test!("-2.1", (UnaryOprApp "-" (Number () "2" ("." "1"))));
|
2023-03-17 21:53:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn minus_unary_in_method_app() {
|
|
|
|
test!("-1.x", (OprApp (UnaryOprApp "-" (Number () "1" ())) (Ok ".") (Ident x)));
|
|
|
|
test!("-1.up_to 100",
|
|
|
|
(App (OprApp (UnaryOprApp "-" (Number () "1" ())) (Ok ".") (Ident up_to))
|
|
|
|
(Number () "100" ())));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_app_in_minus_unary() {
|
|
|
|
test!("-Number.positive_infinity",
|
|
|
|
(UnaryOprApp "-" (OprApp (Ident Number) (Ok ".") (Ident positive_infinity))));
|
2022-08-09 23:31:23 +03:00
|
|
|
}
|
|
|
|
|
2024-03-12 22:31:16 +03:00
|
|
|
#[test]
|
|
|
|
fn autoscope_operator() {
|
|
|
|
test!("x : ..True", (TypeSignature (Ident x) ":" (AutoscopedIdentifier ".." True)));
|
|
|
|
test!("x = ..True", (Assignment (Ident x) "=" (AutoscopedIdentifier ".." True)));
|
|
|
|
test!("x = f ..True",
|
|
|
|
(Assignment (Ident x) "=" (App (Ident f) (AutoscopedIdentifier ".." True))));
|
|
|
|
expect_invalid_node("x = ..not_a_constructor");
|
|
|
|
expect_invalid_node("x = case a of ..True -> True");
|
|
|
|
expect_invalid_node("x = ..4");
|
|
|
|
expect_invalid_node("x = ..Foo.Bar");
|
|
|
|
expect_invalid_node("x = f .. True");
|
|
|
|
expect_invalid_node("x = f(.. ..)");
|
|
|
|
expect_invalid_node("x = f(.. *)");
|
|
|
|
expect_invalid_node("x = f(.. True)");
|
|
|
|
expect_multiple_operator_error("x = ..");
|
|
|
|
expect_multiple_operator_error("x = .. True");
|
|
|
|
expect_multiple_operator_error("x : .. True");
|
|
|
|
}
|
|
|
|
|
2022-07-28 20:17:33 +03:00
|
|
|
|
2022-09-03 09:38:06 +03:00
|
|
|
// === Import/Export ===
|
2022-08-02 18:09:20 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn import() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let cases = [
|
|
|
|
("import project.IO", block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(Import () () ((Ident import) (OprApp (Ident project) (Ok ".") (Ident IO))) () () ())]),
|
2022-08-02 18:09:20 +03:00
|
|
|
("import Standard.Base as Enso_List", block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(Import () ()
|
2022-08-02 18:09:20 +03:00
|
|
|
((Ident import) (OprApp (Ident Standard) (Ok ".") (Ident Base)))
|
2022-10-05 07:45:31 +03:00
|
|
|
()
|
2022-08-02 18:09:20 +03:00
|
|
|
((Ident as) (Ident Enso_List))
|
|
|
|
())]),
|
|
|
|
("from Standard.Base import all", block![
|
|
|
|
(Import ()
|
|
|
|
((Ident from) (OprApp (Ident Standard) (Ok ".") (Ident Base)))
|
2022-10-05 07:45:31 +03:00
|
|
|
((Ident import) ())
|
|
|
|
all () ())]),
|
2022-08-02 18:09:20 +03:00
|
|
|
("from Standard.Base import all hiding Number, Boolean", block![
|
|
|
|
(Import ()
|
|
|
|
((Ident from) (OprApp (Ident Standard) (Ok ".") (Ident Base)))
|
2022-10-05 07:45:31 +03:00
|
|
|
((Ident import) ())
|
|
|
|
all
|
2022-08-02 18:09:20 +03:00
|
|
|
()
|
2022-09-03 09:38:06 +03:00
|
|
|
((Ident hiding) (OprApp (Ident Number) (Ok ",") (Ident Boolean))))]),
|
2022-08-02 18:09:20 +03:00
|
|
|
("polyglot java import java.lang.Float", block![
|
|
|
|
(Import
|
|
|
|
((Ident polyglot) (Ident java))
|
|
|
|
()
|
|
|
|
((Ident import)
|
|
|
|
(OprApp (OprApp (Ident java) (Ok ".") (Ident lang)) (Ok ".") (Ident Float)))
|
2022-10-05 07:45:31 +03:00
|
|
|
() () ())]),
|
2022-08-02 18:09:20 +03:00
|
|
|
("polyglot java import java.net.URI as Java_URI", block![
|
|
|
|
(Import
|
|
|
|
((Ident polyglot) (Ident java))
|
|
|
|
()
|
|
|
|
((Ident import)
|
|
|
|
(OprApp (OprApp (Ident java) (Ok ".") (Ident net)) (Ok ".") (Ident URI)))
|
2022-10-05 07:45:31 +03:00
|
|
|
()
|
2022-08-02 18:09:20 +03:00
|
|
|
((Ident as) (Ident Java_URI))
|
|
|
|
())]),
|
2022-10-31 19:19:12 +03:00
|
|
|
("from Standard.Base import Foo, Bar, Baz", block![
|
|
|
|
(Import ()
|
|
|
|
((Ident from) (OprApp (Ident Standard) (Ok ".") (Ident Base)))
|
|
|
|
((Ident import) (OprApp (OprApp (Ident Foo) (Ok ",") (Ident Bar)) (Ok ",") (Ident Baz)))
|
|
|
|
() () ())]),
|
2022-08-02 18:09:20 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("from Standard.Base.Data.Array import new as array_new");
|
2022-08-02 18:09:20 +03:00
|
|
|
}
|
|
|
|
|
2022-09-03 09:38:06 +03:00
|
|
|
#[test]
|
|
|
|
fn export() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let cases = [
|
2022-09-14 21:09:58 +03:00
|
|
|
("export prj.Data.Foo", block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(Export ()
|
2022-09-14 21:09:58 +03:00
|
|
|
((Ident export)
|
|
|
|
(OprApp (OprApp (Ident prj) (Ok ".") (Ident Data)) (Ok ".") (Ident Foo)))
|
2022-10-05 07:45:31 +03:00
|
|
|
() () ())]),
|
2022-09-03 09:38:06 +03:00
|
|
|
("export Foo as Bar", block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(Export () ((Ident export) (Ident Foo)) () ((Ident as) (Ident Bar)) ())]),
|
2022-09-03 09:38:06 +03:00
|
|
|
("from Foo export Bar, Baz", block![
|
|
|
|
(Export
|
|
|
|
((Ident from) (Ident Foo))
|
|
|
|
((Ident export) (OprApp (Ident Bar) (Ok ",") (Ident Baz)))
|
2022-10-05 07:45:31 +03:00
|
|
|
() () ())]),
|
2022-09-03 09:38:06 +03:00
|
|
|
("from Foo export all hiding Bar, Baz", block![
|
|
|
|
(Export
|
|
|
|
((Ident from) (Ident Foo))
|
2022-10-05 07:45:31 +03:00
|
|
|
((Ident export) ())
|
|
|
|
all
|
2022-09-03 09:38:06 +03:00
|
|
|
()
|
|
|
|
((Ident hiding) (OprApp (Ident Bar) (Ok ",") (Ident Baz))))]),
|
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:09:20 +03:00
|
|
|
|
2022-09-03 06:15:27 +03:00
|
|
|
// === Metadata ===
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn metadata_raw() {
|
|
|
|
let code = [
|
2022-09-14 21:09:58 +03:00
|
|
|
"x",
|
2023-02-10 21:05:40 +03:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2022-09-03 06:15:27 +03:00
|
|
|
"#### METADATA ####",
|
|
|
|
r#"[[{"index":{"value":7},"size":{"value":8}},"5bad897e-099b-4b00-9348-64092636746d"]]"#,
|
|
|
|
];
|
|
|
|
let code = code.join("\n");
|
|
|
|
let (_meta, code) = enso_parser::metadata::parse(&code).unwrap();
|
|
|
|
let expected = block![
|
2022-09-14 21:09:58 +03:00
|
|
|
(Ident x)
|
2022-09-03 06:15:27 +03:00
|
|
|
()
|
|
|
|
];
|
|
|
|
test(code, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn metadata_parsing() {
|
|
|
|
let code = metadata::ORDERS_WITH_METADATA;
|
|
|
|
let (meta, code) = enso_parser::metadata::parse(code).unwrap();
|
2023-10-19 15:36:42 +03:00
|
|
|
let _ast = parse(code);
|
2022-09-03 06:15:27 +03:00
|
|
|
let _meta: enso_parser::metadata::Metadata = meta.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// === Type annotations and signatures ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_signatures() {
|
2022-10-18 01:46:52 +03:00
|
|
|
#[rustfmt::skip]
|
2022-09-03 06:15:27 +03:00
|
|
|
let cases = [
|
2022-10-12 20:40:16 +03:00
|
|
|
("val : Bool", block![(TypeSignature (Ident val) ":" (Ident Bool))]),
|
|
|
|
("val : List Int", block![(TypeSignature (Ident val) ":" (App (Ident List) (Ident Int)))]),
|
2022-10-18 01:46:52 +03:00
|
|
|
("foo : [Integer | Text] -> (Integer | Text)", block![
|
|
|
|
(TypeSignature (Ident foo) ":"
|
|
|
|
(OprApp (Array (OprApp (Ident Integer) (Ok "|") (Ident Text)) #())
|
|
|
|
(Ok "->")
|
|
|
|
(Group (OprApp (Ident Integer) (Ok "|") (Ident Text)))))]),
|
2022-09-03 06:15:27 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_annotations() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let cases = [
|
2022-09-14 21:09:58 +03:00
|
|
|
("val = x : Int", block![
|
|
|
|
(Assignment (Ident val) "=" (TypeAnnotated (Ident x) ":" (Ident Int)))]),
|
|
|
|
("val = foo (x : Int)", block![
|
2022-09-03 06:15:27 +03:00
|
|
|
(Assignment (Ident val) "="
|
|
|
|
(App (Ident foo)
|
2022-10-05 07:45:31 +03:00
|
|
|
(Group (TypeAnnotated (Ident x) ":" (Ident Int)))))]),
|
|
|
|
("(x : My_Type _)", block![
|
2024-03-12 22:31:16 +03:00
|
|
|
(Group
|
|
|
|
(TypeAnnotated (Ident x)
|
|
|
|
":"
|
|
|
|
(App (Ident My_Type) (TemplateFunction 1 (Wildcard 0)))))]),
|
2022-10-05 07:45:31 +03:00
|
|
|
("x : List Int -> Int", block![
|
2022-10-12 20:40:16 +03:00
|
|
|
(TypeSignature (Ident x) ":"
|
|
|
|
(OprApp (App (Ident List) (Ident Int)) (Ok "->") (Ident Int)))]),
|
2022-09-03 06:15:27 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-03 09:38:06 +03:00
|
|
|
// === Text Literals ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_text_literals() {
|
2022-11-14 23:24:07 +03:00
|
|
|
test!(r#""I'm an inline raw text!""#, (TextLiteral #((Section "I'm an inline raw text!"))));
|
|
|
|
test!(r#"zero_length = """#, (Assignment (Ident zero_length) "=" (TextLiteral #())));
|
|
|
|
test!(r#""type""#, (TextLiteral #((Section "type"))));
|
|
|
|
test!(r#"unclosed = ""#, (Assignment (Ident unclosed) "=" (TextLiteral #())));
|
|
|
|
test!(r#"unclosed = "a"#, (Assignment (Ident unclosed) "=" (TextLiteral #((Section "a")))));
|
|
|
|
test!(r#"'Other quote type'"#, (TextLiteral #((Section "Other quote type"))));
|
|
|
|
test!(r#""Non-escape: \n""#, (TextLiteral #((Section "Non-escape: \\n"))));
|
|
|
|
test!(r#""Non-escape: \""#, (TextLiteral #((Section "Non-escape: \\"))));
|
|
|
|
test!(r#"'String with \' escape'"#,
|
2024-04-18 16:21:05 +03:00
|
|
|
(TextLiteral #((Section "String with ") (Escape 0x27) (Section " escape"))));
|
2022-11-14 23:24:07 +03:00
|
|
|
test!(r#"'\u0915\u094D\u0937\u093F'"#, (TextLiteral
|
2024-04-18 16:21:05 +03:00
|
|
|
#((Escape 0x0915) (Escape 0x094D) (Escape 0x0937) (Escape 0x093F))));
|
|
|
|
test!(r#"('\n')"#, (Group (TextLiteral #((Escape 0x0A)))));
|
2022-11-14 23:24:07 +03:00
|
|
|
test!(r#"`"#, (Invalid));
|
|
|
|
test!(r#"(")")"#, (Group (TextLiteral #((Section ")")))));
|
2024-04-18 16:21:05 +03:00
|
|
|
test!(r#"'\x'"#, (TextLiteral #((Escape 0xFFFFFFFFu32))));
|
|
|
|
test!(r#"'\u'"#, (TextLiteral #((Escape 0xFFFFFFFFu32))));
|
|
|
|
test!(r#"'\U'"#, (TextLiteral #((Escape 0xFFFFFFFFu32))));
|
2022-09-03 09:38:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiline_text_literals() {
|
2022-10-05 07:45:31 +03:00
|
|
|
test("'''", block![(TextLiteral #())]);
|
|
|
|
let code = r#""""
|
2022-09-03 09:38:06 +03:00
|
|
|
part of the string
|
|
|
|
3-spaces indented line, part of the Text Block
|
|
|
|
this does not end the string -> '''
|
|
|
|
|
2022-10-05 07:45:31 +03:00
|
|
|
`also` part of the string
|
2022-09-03 09:38:06 +03:00
|
|
|
|
2022-09-14 21:09:58 +03:00
|
|
|
x"#;
|
2022-09-03 09:38:06 +03:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(TextLiteral
|
2022-11-27 12:40:44 +03:00
|
|
|
#((Section "part of the string") (Newline)
|
|
|
|
(Section " 3-spaces indented line, part of the Text Block") (Newline)
|
|
|
|
(Section "this does not end the string -> '''") (Newline)
|
|
|
|
(Newline)
|
2022-10-31 19:19:12 +03:00
|
|
|
(Section "`also` part of the string")))
|
|
|
|
()
|
2022-10-05 07:45:31 +03:00
|
|
|
(Ident x)
|
|
|
|
];
|
|
|
|
test(code, expected);
|
|
|
|
let code = r#""""
|
|
|
|
multiline string that doesn't end in a newline
|
|
|
|
x"#;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(TextLiteral #((Section "multiline string that doesn't end in a newline")))
|
2022-09-14 21:09:58 +03:00
|
|
|
(Ident x)
|
2022-09-03 09:38:06 +03:00
|
|
|
];
|
2022-10-05 07:45:31 +03:00
|
|
|
test(code, expected);
|
2023-10-19 15:36:42 +03:00
|
|
|
let code = "x = \"\"\"\n Indented multiline\nx";
|
2022-10-15 09:13:32 +03:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(Assignment (Ident x) "=" (TextLiteral #((Section "Indented multiline"))))
|
|
|
|
(Ident x)
|
|
|
|
];
|
|
|
|
test(code, expected);
|
|
|
|
let code = "'''\n \\nEscape at start\n";
|
2024-04-18 16:21:05 +03:00
|
|
|
test!(code, (TextLiteral #((Escape 0x0A) (Section "Escape at start"))) ());
|
2022-10-15 09:13:32 +03:00
|
|
|
let code = "x =\n x = '''\n x\nx";
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2023-12-12 17:48:44 +03:00
|
|
|
(Function (Ident x) #() () "="
|
2022-10-15 09:13:32 +03:00
|
|
|
(BodyBlock #((Assignment (Ident x) "=" (TextLiteral #((Section "x")))))))
|
|
|
|
(Ident x)
|
|
|
|
];
|
|
|
|
test(code, expected);
|
2022-10-31 19:19:12 +03:00
|
|
|
test!("foo = bar '''\n baz",
|
|
|
|
(Assignment (Ident foo) "=" (App (Ident bar) (TextLiteral #((Section "baz"))))));
|
2024-04-18 16:21:05 +03:00
|
|
|
test!("'''\n \\t'", (TextLiteral #((Escape 0x09) (Section "'"))));
|
2022-10-31 19:19:12 +03:00
|
|
|
test!("'''\n x\n \\t'",
|
2024-04-18 16:21:05 +03:00
|
|
|
(TextLiteral #((Section "x") (Newline) (Escape 0x09) (Section "'"))));
|
2022-09-03 09:38:06 +03:00
|
|
|
}
|
|
|
|
|
2022-09-15 08:32:28 +03:00
|
|
|
#[test]
|
|
|
|
fn interpolated_literals_in_inline_text() {
|
2023-11-08 19:53:39 +03:00
|
|
|
test!(r#"'Simple case.'"#, (TextLiteral #((Section "Simple case."))));
|
|
|
|
test!(r#"'With a `splice`.'"#, (TextLiteral
|
|
|
|
#((Section "With a ")
|
|
|
|
(Splice (Ident splice))
|
|
|
|
(Section "."))));
|
|
|
|
test!(r#"'` SpliceWithLeadingWhitespace`'"#,
|
|
|
|
(TextLiteral #((Splice (Ident SpliceWithLeadingWhitespace)))));
|
|
|
|
test!(r#"'String with \n escape'"#,
|
2024-04-18 16:21:05 +03:00
|
|
|
(TextLiteral #((Section "String with ") (Escape 0x0A) (Section " escape"))));
|
|
|
|
test!(r#"'\x0Aescape'"#, (TextLiteral #((Escape 0x0A) (Section "escape"))));
|
|
|
|
test!(r#"'\u000Aescape'"#, (TextLiteral #((Escape 0x0A) (Section "escape"))));
|
|
|
|
test!(r#"'\u{0000A}escape'"#, (TextLiteral #((Escape 0x0A) (Section "escape"))));
|
|
|
|
test!(r#"'\U0000000Aescape'"#, (TextLiteral #((Escape 0x0A) (Section "escape"))));
|
2022-09-15 08:32:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn interpolated_literals_in_multiline_text() {
|
2022-10-05 07:45:31 +03:00
|
|
|
let code = r#"'''
|
|
|
|
`splice` at start"#;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(TextLiteral #((Splice (Ident splice)) (Section " at start")))];
|
|
|
|
test(code, expected);
|
|
|
|
|
|
|
|
let code = r#"'''
|
2022-09-15 08:32:28 +03:00
|
|
|
text with a `splice`
|
|
|
|
and some \u000Aescapes\'"#;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(TextLiteral
|
2022-11-27 12:40:44 +03:00
|
|
|
#((Section "text with a ") (Splice (Ident splice)) (Newline)
|
2024-04-18 16:21:05 +03:00
|
|
|
(Section "and some ") (Escape 0x0A) (Section "escapes") (Escape 0x27)))];
|
2022-10-05 07:45:31 +03:00
|
|
|
test(code, expected);
|
2022-09-15 08:32:28 +03:00
|
|
|
}
|
|
|
|
|
2022-09-03 09:38:06 +03:00
|
|
|
|
2022-09-14 21:09:58 +03:00
|
|
|
// === Lambdas ===
|
|
|
|
|
|
|
|
#[test]
|
2022-10-05 07:45:31 +03:00
|
|
|
fn new_lambdas() {
|
2022-09-14 21:09:58 +03:00
|
|
|
let cases = [
|
2022-10-05 07:45:31 +03:00
|
|
|
(r#"\v -> v"#, block![(Lambda "\\" (OprApp (Ident v) (Ok "->") (Ident v)))]),
|
|
|
|
(r#"\a b -> x"#, block![
|
|
|
|
(Lambda "\\" (OprApp (App (Ident a) (Ident b)) (Ok "->") (Ident x)))]),
|
2022-09-14 21:09:58 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
2022-10-05 07:45:31 +03:00
|
|
|
#[test]
|
|
|
|
fn old_lambdas() {
|
2022-10-18 01:46:52 +03:00
|
|
|
test("x -> y", block![(OprApp (Ident x) (Ok "->") (Ident y))]);
|
|
|
|
test("x->y", block![(OprApp (Ident x) (Ok "->") (Ident y))]);
|
|
|
|
test("x-> y", block![(OprApp (Ident x) (Ok "->") (Ident y))]);
|
|
|
|
test("x->\n y", block![(OprApp (Ident x) (Ok "->") (BodyBlock #((Ident y))))]);
|
|
|
|
test("x ->\n y", block![(OprApp (Ident x) (Ok "->") (BodyBlock #((Ident y))))]);
|
|
|
|
test("f x->\n y", block![
|
|
|
|
(App (Ident f) (OprApp (Ident x) (Ok "->") (BodyBlock #((Ident y)))))]);
|
|
|
|
test("x->y-> z", block![(OprApp (Ident x) (Ok "->") (OprApp (Ident y) (Ok "->") (Ident z)))]);
|
2022-10-05 07:45:31 +03:00
|
|
|
}
|
|
|
|
|
2022-09-14 21:09:58 +03:00
|
|
|
|
|
|
|
// === Pattern Matching ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pattern_irrefutable() {
|
|
|
|
let code = "Point x_val = my_point";
|
|
|
|
let expected = block![(Assignment (App (Ident Point) (Ident x_val)) "=" (Ident my_point))];
|
|
|
|
test(code, expected);
|
|
|
|
|
2022-10-05 07:45:31 +03:00
|
|
|
test("Vector _ = x", block![(Assignment (App (Ident Vector) (Wildcard -1)) "=" (Ident x))]);
|
2022-09-14 21:09:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_expression() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"case a of",
|
|
|
|
" Some -> x",
|
2022-10-18 23:37:36 +03:00
|
|
|
" Int -> x",
|
2022-09-14 21:09:58 +03:00
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(CaseOf (Ident a) #(
|
2022-10-18 23:37:36 +03:00
|
|
|
((() (Ident Some) "->" (Ident x)))
|
|
|
|
((() (Ident Int) "->" (Ident x)))))
|
2022-09-14 21:09:58 +03:00
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"case a of",
|
|
|
|
" Vector_2d x y -> x",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(CaseOf (Ident a) #(
|
2022-10-18 23:37:36 +03:00
|
|
|
((() (App (App (Ident Vector_2d) (Ident x)) (Ident y)) "->" (Ident x)))))];
|
2022-09-14 21:09:58 +03:00
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"case self of",
|
|
|
|
" Vector_2d -> x",
|
|
|
|
" _ -> x",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2022-10-05 07:45:31 +03:00
|
|
|
(CaseOf (Ident self) #(
|
2022-10-18 23:37:36 +03:00
|
|
|
((() (Ident Vector_2d) "->" (Ident x)))
|
|
|
|
((() (Wildcard -1) "->" (Ident x)))))];
|
2022-10-05 07:45:31 +03:00
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"case foo of",
|
|
|
|
" v:My_Type -> x",
|
|
|
|
" v:(My_Type _ _) -> x",
|
2022-09-14 21:09:58 +03:00
|
|
|
];
|
2022-10-05 07:45:31 +03:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(CaseOf (Ident foo) #(
|
2022-10-18 23:37:36 +03:00
|
|
|
((() (TypeAnnotated (Ident v) ":" (Ident My_Type)) "->" (Ident x)))
|
|
|
|
((() (TypeAnnotated (Ident v) ":"
|
2024-03-12 22:31:16 +03:00
|
|
|
(Group (App
|
|
|
|
(App
|
|
|
|
(Ident My_Type)
|
|
|
|
(TemplateFunction 1 (Wildcard 0)))
|
|
|
|
(TemplateFunction 1 (Wildcard 0)))))
|
|
|
|
"->" (Ident x)))))
|
|
|
|
];
|
2022-09-14 21:09:58 +03:00
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2023-10-19 15:36:42 +03:00
|
|
|
#[test]
|
|
|
|
fn case_documentation() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"case a of",
|
|
|
|
" ## The Some case",
|
|
|
|
" Some -> x",
|
|
|
|
" ## The Int case",
|
|
|
|
" Int -> x",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
|
|
|
(CaseOf (Ident a) #(
|
|
|
|
(((#((Section " The Some case")) #()) () () ()))
|
|
|
|
((() (Ident Some) "->" (Ident x)))
|
|
|
|
(((#((Section " The Int case")) #()) () () ()))
|
|
|
|
((() (Ident Int) "->" (Ident x)))))
|
|
|
|
];
|
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:46:52 +03:00
|
|
|
#[test]
|
|
|
|
fn case_by_type() {
|
|
|
|
macro_rules! test_case {
|
|
|
|
( $code:expr, $case:tt ) => {
|
|
|
|
test(&format!("case foo of\n {}", $code), block![(CaseOf (Ident foo) #(($case)))]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test_case!("f:A->B -> x",
|
2022-10-18 23:37:36 +03:00
|
|
|
(() (TypeAnnotated (Ident f) ":" (OprApp (Ident A) (Ok "->") (Ident B))) "->" (Ident x)));
|
2022-10-18 01:46:52 +03:00
|
|
|
test_case!("f : A->B -> x",
|
2022-10-18 23:37:36 +03:00
|
|
|
(() (TypeAnnotated (Ident f) ":" (OprApp (Ident A) (Ok "->") (Ident B))) "->" (Ident x)));
|
2022-10-18 01:46:52 +03:00
|
|
|
test_case!("v : A -> x->x",
|
2022-10-18 23:37:36 +03:00
|
|
|
(() (TypeAnnotated (Ident v) ":" (Ident A)) "->" (OprApp (Ident x) (Ok "->") (Ident x))));
|
2022-10-18 01:46:52 +03:00
|
|
|
test_case!("v : A -> x -> x",
|
2022-10-18 23:37:36 +03:00
|
|
|
(() (TypeAnnotated (Ident v) ":" (Ident A)) "->" (OprApp (Ident x) (Ok "->") (Ident x))));
|
2022-10-18 01:46:52 +03:00
|
|
|
test_case!("v:A->x->x",
|
2022-10-18 23:37:36 +03:00
|
|
|
(() (TypeAnnotated (Ident v) ":" (Ident A)) "->" (OprApp (Ident x) (Ok "->") (Ident x))));
|
|
|
|
test_case!("v:A->x", (() (TypeAnnotated (Ident v) ":" (Ident A)) "->" (Ident x)));
|
2022-10-18 01:46:52 +03:00
|
|
|
test_case!("v : A -> _ + x",
|
2022-10-18 23:37:36 +03:00
|
|
|
(() (TypeAnnotated (Ident v) ":" (Ident A)) "->"
|
2022-10-18 01:46:52 +03:00
|
|
|
(TemplateFunction 1 (OprApp (Wildcard 0) (Ok "+") (Ident x)))));
|
|
|
|
}
|
|
|
|
|
2022-09-14 21:09:58 +03:00
|
|
|
#[test]
|
2024-03-12 22:31:16 +03:00
|
|
|
fn pattern_match_suspended_default_arguments() {
|
2022-09-14 21:09:58 +03:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let code = [
|
|
|
|
"case self of",
|
|
|
|
" Vector_2d ... -> x",
|
|
|
|
];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let expected = block![
|
2024-03-12 22:31:16 +03:00
|
|
|
(CaseOf (Ident self) #(((() (App (Ident Vector_2d) (SuspendedDefaultArguments)) "->" (Ident x)))))];
|
2022-09-14 21:09:58 +03:00
|
|
|
test(&code.join("\n"), expected);
|
|
|
|
}
|
|
|
|
|
2023-10-04 13:33:10 +03:00
|
|
|
// === Private (project-private) keyword ===
|
|
|
|
#[test]
|
|
|
|
fn private_keyword() {
|
|
|
|
test("private", block![(Private())]);
|
|
|
|
test("private func", block![(Private (Ident func))]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn private_is_first_statement() {
|
|
|
|
// Comments and empty lines are allowed before `private`.
|
|
|
|
#[rustfmt::skip]
|
2024-03-25 02:45:55 +03:00
|
|
|
let lines = [
|
2023-10-04 13:33:10 +03:00
|
|
|
"# Some comment",
|
|
|
|
"# Other comment",
|
|
|
|
"",
|
|
|
|
"private"
|
|
|
|
];
|
|
|
|
test(&lines.join("\n"), block![()()()(Private)]);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
2024-03-25 02:45:55 +03:00
|
|
|
let lines = [
|
2023-10-04 13:33:10 +03:00
|
|
|
"type T",
|
|
|
|
"",
|
|
|
|
"private"
|
|
|
|
];
|
|
|
|
expect_invalid_node(&lines.join("\n"));
|
|
|
|
}
|
2022-09-14 21:09:58 +03:00
|
|
|
|
|
|
|
// === Array/tuple literals ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn array_literals() {
|
|
|
|
let cases = [
|
2022-10-05 07:45:31 +03:00
|
|
|
("[]", block![(Array () #())]),
|
|
|
|
("[x]", block![(Array (Ident x) #())]),
|
|
|
|
("[x, y]", block![(Array (Ident x) #(("," (Ident y))))]),
|
|
|
|
("[x, y, z]", block![(Array (Ident x) #(("," (Ident y)) ("," (Ident z))))]),
|
|
|
|
("[ x , y ]", block![(Array (Ident x) #(("," (Ident y))))]),
|
|
|
|
("[ x , y , z ]", block![(Array (Ident x) #(("," (Ident y)) ("," (Ident z))))]),
|
2022-09-14 21:09:58 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tuple_literals() {
|
|
|
|
let cases = [
|
2022-10-05 07:45:31 +03:00
|
|
|
("{}", block![(Tuple () #())]),
|
|
|
|
("{x}", block![(Tuple (Ident x) #())]),
|
|
|
|
("{x, y}", block![(Tuple (Ident x) #(("," (Ident y))))]),
|
2022-09-14 21:09:58 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// === Numeric literals ===
|
|
|
|
|
2023-10-19 15:36:42 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod numbers {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn with_decimal() {
|
|
|
|
test!("1 . 0", (OprApp (Number () "1" ()) (Ok ".") (Number () "0" ())));
|
|
|
|
test!("1 .0",
|
|
|
|
(App (Number () "1" ()) (OprSectionBoundary 1 (OprApp () (Ok ".") (Number () "0" ())))));
|
|
|
|
test!("1. 0",
|
|
|
|
(OprSectionBoundary 1 (App (OprApp (Number () "1" ()) (Ok ".") ()) (Number () "0" ()))));
|
|
|
|
test!("pi = 3.14", (Assignment (Ident pi) "=" (Number () "3" ("." "14"))));
|
|
|
|
test!("0.0.x", (OprApp (Number () "0" ("." "0")) (Ok ".") (Ident x)));
|
|
|
|
}
|
2022-10-31 19:19:12 +03:00
|
|
|
|
2023-10-19 15:36:42 +03:00
|
|
|
#[test]
|
|
|
|
fn with_base() {
|
|
|
|
test!("0b10101010", (Number "0b" "10101010" ()));
|
|
|
|
test!("0o122137", (Number "0o" "122137" ()));
|
|
|
|
test!("0xAE2F14", (Number "0x" "AE2F14" ()));
|
|
|
|
}
|
2022-10-31 19:19:12 +03:00
|
|
|
|
2023-10-19 15:36:42 +03:00
|
|
|
#[test]
|
|
|
|
fn base_only() {
|
|
|
|
test!("0x", (Number "0x" () ()));
|
|
|
|
test!("0b", (Number "0b" () ()));
|
|
|
|
test!("0o", (Number "0o" () ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
// This syntax cannot be used until we remove old-nondecimal number support, which is
|
|
|
|
// needed for compatibility until the old parser is fully replaced.
|
|
|
|
#[ignore]
|
|
|
|
fn new_delimited() {
|
|
|
|
test!("100_000", (Number () "100_000" ()));
|
|
|
|
test!("10_000.99", (Number () "10_000" ("." "99")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn old_nondecimal() {
|
|
|
|
test!("2_01101101", (Number "2_" "01101101" ()));
|
|
|
|
test!("-2_01101101", (UnaryOprApp "-" (Number "2_" "01101101" ())));
|
|
|
|
test!("16_17ffffffffffffffa", (Number "16_" "17ffffffffffffffa" ()));
|
|
|
|
}
|
2022-10-05 07:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// === Whitespace ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trailing_whitespace() {
|
|
|
|
let cases = [
|
|
|
|
("a ", block![(Ident a) ()]),
|
|
|
|
("a \n", block![(Ident a) ()]),
|
2023-12-12 17:48:44 +03:00
|
|
|
("a = \n x", block![(Function (Ident a) #() () "=" (BodyBlock #((Ident x))))]),
|
2022-09-14 21:09:58 +03:00
|
|
|
];
|
|
|
|
cases.into_iter().for_each(|(code, expected)| test(code, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 10:09:01 +03:00
|
|
|
// === Annotations ===
|
|
|
|
|
|
|
|
#[test]
|
2023-01-04 20:50:38 +03:00
|
|
|
fn at_operator() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("foo@bar");
|
|
|
|
expect_invalid_node("foo @ bar");
|
2022-10-10 10:09:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-01-04 20:50:38 +03:00
|
|
|
fn attributes() {
|
|
|
|
test!("@on_problems P.g\nTable.select_columns : Text -> Table",
|
|
|
|
(Annotated "@" on_problems
|
|
|
|
(OprApp (Ident P) (Ok ".") (Ident g))
|
|
|
|
#(())
|
|
|
|
(TypeSignature (OprApp (Ident Table) (Ok ".") (Ident select_columns))
|
|
|
|
":"
|
|
|
|
(OprApp (Ident Text) (Ok "->") (Ident Table)))));
|
|
|
|
test!("@a z\n@b\nx", (Annotated "@" a (Ident z) #(()) (Annotated "@" b () #(()) (Ident x))));
|
|
|
|
test!("@a\n@b\nx", (Annotated "@" a () #(()) (Annotated "@" b () #(()) (Ident x))));
|
|
|
|
}
|
|
|
|
|
2023-01-12 19:51:44 +03:00
|
|
|
#[test]
|
|
|
|
fn attributes_in_types() {
|
|
|
|
test!("type A\n @a z\n @b\n x",
|
|
|
|
(TypeDef type A #() #(
|
|
|
|
(Annotated "@" a (Ident z) #(()) (Annotated "@" b () #(()) (Ident x))))));
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:50:38 +03:00
|
|
|
#[test]
|
|
|
|
fn inline_builtin_annotations() {
|
|
|
|
test!("@Tail_Call go t", (AnnotatedBuiltin "@" Tail_Call #() (App (Ident go) (Ident t))));
|
|
|
|
test!("@Tail_Call go\n a\n b",
|
|
|
|
(AnnotatedBuiltin "@" Tail_Call #()
|
|
|
|
(ArgumentBlockApplication (Ident go) #((Ident a) (Ident b)))));
|
2022-10-10 10:09:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-01-04 20:50:38 +03:00
|
|
|
fn multiline_builtin_annotations() {
|
2022-11-14 23:24:07 +03:00
|
|
|
test!("@Builtin_Type\ntype Date",
|
2023-01-04 20:50:38 +03:00
|
|
|
(AnnotatedBuiltin "@" Builtin_Type #(()) (TypeDef type Date #() #())));
|
2022-10-10 10:09:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-20 20:32:59 +03:00
|
|
|
// === SKIP and FREEZE ===
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn freeze() {
|
|
|
|
test!("FREEZE x", (MultiSegmentApp #(((Ident FREEZE) (Ident x)))));
|
|
|
|
test!("FREEZE x + y", (MultiSegmentApp
|
|
|
|
#(((Ident FREEZE) (OprApp (Ident x) (Ok "+") (Ident y))))));
|
|
|
|
test!("FREEZE x.f", (MultiSegmentApp
|
|
|
|
#(((Ident FREEZE) (OprApp (Ident x) (Ok ".") (Ident f))))));
|
|
|
|
test!("FREEZE x.f y", (MultiSegmentApp #(((Ident FREEZE)
|
|
|
|
(App (OprApp (Ident x) (Ok ".") (Ident f)) (Ident y))))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip() {
|
|
|
|
test!("SKIP x", (MultiSegmentApp #(((Ident SKIP) (Ident x)))));
|
|
|
|
test!("SKIP x + y", (MultiSegmentApp #(((Ident SKIP) (OprApp (Ident x) (Ok "+") (Ident y))))));
|
|
|
|
test!("SKIP x.f", (MultiSegmentApp #(((Ident SKIP) (OprApp (Ident x) (Ok ".") (Ident f))))));
|
|
|
|
test!("SKIP x.f y", (MultiSegmentApp #(((Ident SKIP)
|
|
|
|
(App (OprApp (Ident x) (Ok ".") (Ident f)) (Ident y))))));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-08 01:31:00 +03:00
|
|
|
|
2023-09-19 17:26:42 +03:00
|
|
|
// =========================
|
|
|
|
// === Scalability Tests ===
|
|
|
|
// =========================
|
|
|
|
|
|
|
|
/// Test an input that caused a stack overflow in a version of the parser that used recursion to
|
|
|
|
/// resolve macro segments.
|
|
|
|
#[test]
|
|
|
|
fn big_array() {
|
|
|
|
let mut big_array = "[".to_owned();
|
|
|
|
// This value was chosen to be large enough to cause a stack overflow, but not so large that it
|
|
|
|
// would take a long time to do so.
|
|
|
|
let array_length = 1000;
|
|
|
|
for _ in 0..array_length {
|
|
|
|
big_array.push_str(
|
|
|
|
r#"[{"index":{"value":1},"size":{"value":8}},"6063e6d3-3341-40f4-b4fb-7e986eb31ae8"],"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
big_array.push_str("1]");
|
|
|
|
expect_valid(&big_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-11-09 05:57:40 +03:00
|
|
|
// ==========================
|
|
|
|
// === Syntax Error Tests ===
|
|
|
|
// ==========================
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn space_required() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("foo = if cond.x else.y");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn incomplete_type_definition() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("type");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_case() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("foo = case x of\n 4");
|
|
|
|
expect_invalid_node("foo = case x of\n 4 ->");
|
|
|
|
expect_invalid_node("foo = case x of\n 4->");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn malformed_sequence() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("(1, )");
|
|
|
|
expect_invalid_node("foo = (1, )");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unmatched_delimiter() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("(");
|
|
|
|
expect_invalid_node(")");
|
|
|
|
expect_invalid_node("[");
|
|
|
|
expect_invalid_node("]");
|
|
|
|
expect_invalid_node("foo = (");
|
|
|
|
expect_invalid_node("foo = )");
|
|
|
|
expect_invalid_node("foo = [");
|
|
|
|
expect_invalid_node("foo = ]");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unexpected_special_operator() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("foo = 1, 2");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn malformed_import() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("import");
|
|
|
|
expect_invalid_node("import as Foo");
|
|
|
|
expect_invalid_node("import Foo as Foo, Bar");
|
|
|
|
expect_invalid_node("import Foo as Foo.Bar");
|
|
|
|
expect_invalid_node("import Foo as");
|
|
|
|
expect_invalid_node("import Foo as Bar.Baz");
|
|
|
|
expect_invalid_node("import Foo hiding");
|
|
|
|
expect_invalid_node("import Foo hiding X,");
|
|
|
|
expect_invalid_node("polyglot import Foo");
|
|
|
|
expect_invalid_node("polyglot java import");
|
|
|
|
expect_invalid_node("from import all");
|
|
|
|
expect_invalid_node("from Foo import all hiding");
|
|
|
|
expect_invalid_node("from Foo import all hiding X.Y");
|
|
|
|
expect_invalid_node("export");
|
|
|
|
expect_invalid_node("export as Foo");
|
|
|
|
expect_invalid_node("export Foo as Foo, Bar");
|
|
|
|
expect_invalid_node("export Foo as Foo.Bar");
|
|
|
|
expect_invalid_node("export Foo as");
|
|
|
|
expect_invalid_node("export Foo as Bar.Baz");
|
|
|
|
expect_invalid_node("export Foo hiding");
|
|
|
|
expect_invalid_node("export Foo hiding X,");
|
|
|
|
expect_invalid_node("from export all");
|
|
|
|
expect_invalid_node("from Foo export all hiding");
|
|
|
|
expect_invalid_node("from Foo export all hiding X.Y");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_token() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("`");
|
|
|
|
expect_invalid_node("splice_outside_text = `");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn illegal_foreign_body() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("foreign 4");
|
|
|
|
expect_invalid_node("foreign 4 * 4");
|
|
|
|
expect_invalid_node("foreign foo = \"4\"");
|
|
|
|
expect_invalid_node("foreign js foo = 4");
|
2022-11-09 05:57:40 +03:00
|
|
|
}
|
|
|
|
|
2023-02-10 02:00:29 +03:00
|
|
|
#[test]
|
|
|
|
fn unexpected_tokens_in_inner_macro_segment() {
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_invalid_node("from Foo import all What_Is_This_Doing_Here hiding Bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_unspaced_operator_sequence() {
|
|
|
|
// Typically, a sequence of operator identifiers is lexed as a single operator. However, an
|
|
|
|
// exception is made for some sequences of operator characters ending in the `-` character: An
|
|
|
|
// expression such as `x+-x` is accepted, and read equivalently to `x + -x` (see
|
|
|
|
// [`unspaced_operator_sequence`]).
|
|
|
|
//
|
|
|
|
// Due to this special case, there is no reasonable way to interpret this type of expression as
|
|
|
|
// valid when spaces are added in the following way:
|
|
|
|
expect_multiple_operator_error("x = y +- z");
|
|
|
|
expect_multiple_operator_error("x =- y");
|
|
|
|
//
|
|
|
|
// Treating the `-` as a unary operator applied to `z` would be confusing, as it would be in
|
|
|
|
// contradiction to the associativity implied by the whitespace rules.
|
|
|
|
//
|
|
|
|
// However, it would also be confusing to lex a sequence of characters like `+-` as a single
|
|
|
|
// operator in spaced expressions, but as two operators in unspaced expressions.
|
|
|
|
//
|
|
|
|
// Lacking any reasonable valid interpretation, we treat this case as a multiple-operator error.
|
|
|
|
// This is the only case in which we yield a multiple-operator error when there are no spaces
|
|
|
|
// between the operators.
|
|
|
|
//
|
|
|
|
// Similar expressions with missing operands should be treated likewise:
|
|
|
|
expect_multiple_operator_error("x = y +-");
|
|
|
|
expect_multiple_operator_error("x = +- z");
|
|
|
|
expect_multiple_operator_error("x =-");
|
|
|
|
expect_multiple_operator_error("=- y");
|
|
|
|
expect_multiple_operator_error("=-");
|
2023-02-10 02:00:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-09 05:57:40 +03:00
|
|
|
|
|
|
|
|
2022-07-08 01:31:00 +03:00
|
|
|
// ====================
|
|
|
|
// === Test Support ===
|
|
|
|
// ====================
|
|
|
|
|
2023-07-27 18:25:45 +03:00
|
|
|
|
|
|
|
// === Testing helpers ===
|
|
|
|
|
|
|
|
/// Check that the given [`Tree`] is a valid representation of the given source code:
|
|
|
|
/// - Assert that the given [`Tree`] is composed of tokens that concatenate back to the given source
|
|
|
|
/// code.
|
|
|
|
/// - Assert that the given [`Tree`] can be serialized and deserialized without error.
|
|
|
|
fn expect_tree_representing_code(code: &str, ast: &enso_parser::syntax::Tree) {
|
|
|
|
assert_eq!(ast.code(), code, "{:?}", &ast);
|
|
|
|
let serialized = enso_parser::serialization::serialize_tree(ast).unwrap();
|
|
|
|
let deserialized = enso_parser::serialization::deserialize_tree(&serialized);
|
|
|
|
deserialized.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// === Testing valid inputs ===
|
|
|
|
|
2022-07-08 01:31:00 +03:00
|
|
|
/// Given a block of input Enso code, test that:
|
|
|
|
/// - The given code parses to the AST represented by the given S-expression.
|
|
|
|
/// - The AST pretty-prints back to the original code.
|
2022-07-25 17:24:21 +03:00
|
|
|
/// - Rust's deserialization is compatible with Rust's serialization for the type. (The Java format
|
|
|
|
/// tests check Java's deserialization against Rust's deserialization).
|
2022-07-08 01:31:00 +03:00
|
|
|
///
|
|
|
|
/// The S-expression format is as documented for [`enso_metamodel_lexpr`], with some
|
|
|
|
/// postprocessing:
|
|
|
|
/// - For concision, field names are stripped (as if all structs were tuple structs).
|
|
|
|
/// - Most token types are represented as their contents, rather than as a token struct. For
|
|
|
|
/// example, a `token::Number` may be represented like: `sexp![10]`, and a `token::Ident` may look
|
|
|
|
/// like `sexp![foo]`.
|
2022-10-12 20:40:16 +03:00
|
|
|
fn test(code: &str, expect: lexpr::Value) {
|
2023-10-19 15:36:42 +03:00
|
|
|
let ast = parse(code);
|
2022-07-08 01:31:00 +03:00
|
|
|
let ast_s_expr = to_s_expr(&ast, code);
|
2022-07-20 17:53:20 +03:00
|
|
|
assert_eq!(ast_s_expr.to_string(), expect.to_string(), "{:?}", &ast);
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_tree_representing_code(code, &ast);
|
|
|
|
}
|
|
|
|
|
2023-10-19 15:36:42 +03:00
|
|
|
fn parse(code: &str) -> enso_parser::syntax::tree::Tree {
|
|
|
|
let ast = enso_parser::Parser::new().run(code);
|
|
|
|
let expected_span = 0..(code.encode_utf16().count() as u32);
|
2023-11-08 19:53:39 +03:00
|
|
|
let mut locations = enso_parser::source::code::debug::LocationCheck::new();
|
|
|
|
enso_parser_debug::validate_spans(&ast, expected_span, &mut locations);
|
|
|
|
locations.check(code);
|
2023-10-19 15:36:42 +03:00
|
|
|
ast
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:25:45 +03:00
|
|
|
|
|
|
|
// === Testing inputs containing syntax errors ===
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Default, Copy, Clone)]
|
|
|
|
struct Errors {
|
|
|
|
invalid_node: bool,
|
|
|
|
multiple_operator: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Errors {
|
|
|
|
fn collect(code: &str) -> Self {
|
2023-10-19 15:36:42 +03:00
|
|
|
let ast = parse(code);
|
2023-07-27 18:25:45 +03:00
|
|
|
expect_tree_representing_code(code, &ast);
|
|
|
|
let errors = core::cell::Cell::new(Errors::default());
|
2024-04-11 07:27:19 +03:00
|
|
|
ast.visit_trees(|tree| match &*tree.variant {
|
2023-07-27 18:25:45 +03:00
|
|
|
enso_parser::syntax::tree::Variant::Invalid(_) => {
|
|
|
|
errors.update(|e| Self { invalid_node: true, ..e });
|
|
|
|
}
|
|
|
|
enso_parser::syntax::tree::Variant::OprApp(opr_app) if opr_app.opr.is_err() => {
|
|
|
|
errors.update(|e| Self { multiple_operator: true, ..e });
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
});
|
|
|
|
errors.into_inner()
|
|
|
|
}
|
2022-07-08 01:31:00 +03:00
|
|
|
}
|
2022-10-18 01:46:52 +03:00
|
|
|
|
|
|
|
/// Checks that an input contains an `Invalid` node somewhere.
|
2023-07-27 18:25:45 +03:00
|
|
|
fn expect_invalid_node(code: &str) {
|
|
|
|
let errors = Errors::collect(code);
|
|
|
|
assert!(errors.invalid_node, "{:?}", enso_parser::Parser::new().run(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that an input contains a multiple-operator error somewhere.
|
|
|
|
fn expect_multiple_operator_error(code: &str) {
|
|
|
|
let errors = Errors::collect(code);
|
|
|
|
assert!(errors.multiple_operator, "{:?}", enso_parser::Parser::new().run(code));
|
2022-10-18 01:46:52 +03:00
|
|
|
}
|
2023-09-19 17:26:42 +03:00
|
|
|
|
|
|
|
/// Check that the input can be parsed, and doesn't yield any `Invalid` nodes.
|
|
|
|
fn expect_valid(code: &str) {
|
|
|
|
let errors = Errors::collect(code);
|
|
|
|
assert!(!errors.invalid_node);
|
|
|
|
}
|