diff --git a/crates/aiken-lang/src/parser/expr/fail_todo.rs b/crates/aiken-lang/src/parser/expr/fail_todo_trace.rs similarity index 81% rename from crates/aiken-lang/src/parser/expr/fail_todo.rs rename to crates/aiken-lang/src/parser/expr/fail_todo_trace.rs index f7e15251..61b8f2f8 100644 --- a/crates/aiken-lang/src/parser/expr/fail_todo.rs +++ b/crates/aiken-lang/src/parser/expr/fail_todo_trace.rs @@ -34,13 +34,12 @@ pub fn parser<'a>( .map_with_span(UntypedExpr::fail), ))), just(Token::Trace) - .ignore_then(clause(expression.clone()).or_not().ignored().rewind()) .ignore_then(choice((string::hybrid(), expression.clone()))) - .then(sequence.clone()) + .then(sequence.clone().or_not()) .map_with_span(|(text, then_), span| UntypedExpr::Trace { kind: TraceKind::Trace, location: span, - then: Box::new(then_), + then: Box::new(then_.unwrap_or_else(|| UntypedExpr::todo(None, span))), text: Box::new(text), }), )) @@ -86,6 +85,24 @@ mod tests { ); } + #[test] + fn todo_empty() { + assert_expr!( + r#" + todo + "# + ); + } + + #[test] + fn todo_expr() { + assert_expr!( + r#" + todo string.join(["foo", "bar"]) + "# + ); + } + #[test] fn fail_expr() { assert_expr!( @@ -106,6 +123,16 @@ mod tests { #[test] fn trace_expr() { + assert_expr!( + r#" + trace string.join(["foo", "bar"]) + a + "# + ); + } + + #[test] + fn trace_expr_todo() { assert_expr!( r#" trace some_var diff --git a/crates/aiken-lang/src/parser/expr/mod.rs b/crates/aiken-lang/src/parser/expr/mod.rs index dd291ec4..91cf0ca2 100644 --- a/crates/aiken-lang/src/parser/expr/mod.rs +++ b/crates/aiken-lang/src/parser/expr/mod.rs @@ -7,7 +7,7 @@ pub mod assignment; mod block; pub(crate) mod bytearray; mod chained; -mod fail_todo; +mod fail_todo_trace; mod if_else; mod int; mod list; @@ -23,7 +23,7 @@ pub use anonymous_function::parser as anonymous_function; pub use block::parser as block; pub use bytearray::parser as bytearray; pub use chained::parser as chained; -pub use fail_todo::parser as fail_todo; +pub use fail_todo_trace::parser as fail_todo_trace; pub use if_else::parser as if_else; pub use int::parser as int; pub use list::parser as list; @@ -43,7 +43,7 @@ pub fn parser( ) -> impl Parser + '_ { recursive(|expression| { choice(( - fail_todo(expression.clone(), sequence.clone()), + fail_todo_trace(expression.clone(), sequence.clone()), pure_expression(sequence, expression), )) }) diff --git a/crates/aiken-lang/src/parser/expr/sequence.rs b/crates/aiken-lang/src/parser/expr/sequence.rs index 7ad30044..1920bdc6 100644 --- a/crates/aiken-lang/src/parser/expr/sequence.rs +++ b/crates/aiken-lang/src/parser/expr/sequence.rs @@ -1,34 +1,14 @@ use chumsky::prelude::*; use crate::{ - ast::TraceKind, expr::UntypedExpr, - parser::{ - error::ParseError, - expr::{block::parser as block, string}, - token::Token, - }, + parser::{error::ParseError, token::Token}, }; pub fn parser() -> impl Parser { recursive(|sequence| { - choice(( - // just(Token::Trace) - // .ignore_then(choice(( - // string::hybrid(), - // block(sequence.clone()), - // sequence.clone(), - // ))) - // .then(sequence.clone()) - // .map_with_span(|(text, then_), span| UntypedExpr::Trace { - // kind: TraceKind::Trace, - // location: span, - // then: Box::new(then_), - // text: Box::new(text), - // }), - super::parser(sequence.clone()) - .then(sequence.repeated()) - .foldl(|current, next| current.append_in_sequence(next)), - )) + super::parser(sequence.clone()) + .then(sequence.repeated()) + .foldl(|current, next| current.append_in_sequence(next)) }) } diff --git a/crates/aiken-lang/src/parser/expr/snapshots/error_basic.snap b/crates/aiken-lang/src/parser/expr/snapshots/error_basic.snap index 1f547cb1..5efe3038 100644 --- a/crates/aiken-lang/src/parser/expr/snapshots/error_basic.snap +++ b/crates/aiken-lang/src/parser/expr/snapshots/error_basic.snap @@ -1,12 +1,12 @@ --- -source: crates/aiken-lang/src/parser/expr/fail_todo.rs +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs description: "Code:\n\nfail @\"foo\"\n" --- Trace { kind: Error, - location: 0..11, + location: 5..11, then: ErrorTerm { - location: 0..11, + location: 5..11, }, text: String { location: 5..11, diff --git a/crates/aiken-lang/src/parser/expr/snapshots/error_sugar.snap b/crates/aiken-lang/src/parser/expr/snapshots/error_sugar.snap index aac19d17..5f5a60e4 100644 --- a/crates/aiken-lang/src/parser/expr/snapshots/error_sugar.snap +++ b/crates/aiken-lang/src/parser/expr/snapshots/error_sugar.snap @@ -1,12 +1,12 @@ --- -source: crates/aiken-lang/src/parser/expr/fail_todo.rs +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs description: "Code:\n\nfail \"foo\"\n" --- Trace { kind: Error, - location: 0..10, + location: 5..10, then: ErrorTerm { - location: 0..10, + location: 5..10, }, text: String { location: 5..10, diff --git a/crates/aiken-lang/src/parser/expr/snapshots/fail_empty.snap b/crates/aiken-lang/src/parser/expr/snapshots/fail_empty.snap new file mode 100644 index 00000000..e99d8a88 --- /dev/null +++ b/crates/aiken-lang/src/parser/expr/snapshots/fail_empty.snap @@ -0,0 +1,15 @@ +--- +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs +description: "Code:\n\nfail\n" +--- +Trace { + kind: Error, + location: 1..2, + then: ErrorTerm { + location: 1..2, + }, + text: String { + location: 1..2, + value: "aiken::error", + }, +} diff --git a/crates/aiken-lang/src/parser/expr/snapshots/fail_expr.snap b/crates/aiken-lang/src/parser/expr/snapshots/fail_expr.snap index e91ac371..a7223e61 100644 --- a/crates/aiken-lang/src/parser/expr/snapshots/fail_expr.snap +++ b/crates/aiken-lang/src/parser/expr/snapshots/fail_expr.snap @@ -1,63 +1,54 @@ --- -source: crates/aiken-lang/src/parser/expr/fail_todo.rs +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs description: "Code:\n\nfail str.join([@\"Some string \", some_params, @\" some string\"], @\"\")\n" --- -Sequence { - location: 0..67, - expressions: [ - Trace { - kind: Error, - location: 0..4, - then: ErrorTerm { - location: 0..4, - }, - text: String { - location: 0..4, - value: "aiken::error", - }, - }, - Call { - arguments: [ - CallArg { - label: None, +Trace { + kind: Error, + location: 5..67, + then: ErrorTerm { + location: 5..67, + }, + text: Call { + arguments: [ + CallArg { + label: None, + location: 14..61, + value: List { location: 14..61, - value: List { - location: 14..61, - elements: [ - String { - location: 15..30, - value: "Some string ", - }, - Var { - location: 32..43, - name: "some_params", - }, - String { - location: 45..60, - value: " some string", - }, - ], - tail: None, - }, - }, - CallArg { - label: None, - location: 63..66, - value: String { - location: 63..66, - value: "", - }, - }, - ], - fun: FieldAccess { - location: 5..13, - label: "join", - container: Var { - location: 5..8, - name: "str", + elements: [ + String { + location: 15..30, + value: "Some string ", + }, + Var { + location: 32..43, + name: "some_params", + }, + String { + location: 45..60, + value: " some string", + }, + ], + tail: None, }, }, - location: 5..67, + CallArg { + label: None, + location: 63..66, + value: String { + location: 63..66, + value: "", + }, + }, + ], + fun: FieldAccess { + location: 5..13, + label: "join", + container: Var { + location: 5..8, + name: "str", + }, }, - ], + location: 5..67, + }, } diff --git a/crates/aiken-lang/src/parser/expr/snapshots/todo_basic.snap b/crates/aiken-lang/src/parser/expr/snapshots/todo_basic.snap index 92394c5d..201c08bf 100644 --- a/crates/aiken-lang/src/parser/expr/snapshots/todo_basic.snap +++ b/crates/aiken-lang/src/parser/expr/snapshots/todo_basic.snap @@ -1,12 +1,12 @@ --- -source: crates/aiken-lang/src/parser/expr/error_todo.rs +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs description: "Code:\n\ntodo @\"foo\"\n" --- Trace { kind: Todo, - location: 0..11, + location: 5..11, then: ErrorTerm { - location: 0..11, + location: 5..11, }, text: String { location: 5..11, diff --git a/crates/aiken-lang/src/parser/expr/snapshots/todo_empty.snap b/crates/aiken-lang/src/parser/expr/snapshots/todo_empty.snap new file mode 100644 index 00000000..ae059379 --- /dev/null +++ b/crates/aiken-lang/src/parser/expr/snapshots/todo_empty.snap @@ -0,0 +1,15 @@ +--- +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs +description: "Code:\n\ntodo\n" +--- +Trace { + kind: Todo, + location: 1..2, + then: ErrorTerm { + location: 1..2, + }, + text: String { + location: 1..2, + value: "aiken::todo", + }, +} diff --git a/crates/aiken-lang/src/parser/expr/snapshots/todo_expr.snap b/crates/aiken-lang/src/parser/expr/snapshots/todo_expr.snap new file mode 100644 index 00000000..45117539 --- /dev/null +++ b/crates/aiken-lang/src/parser/expr/snapshots/todo_expr.snap @@ -0,0 +1,52 @@ +--- +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs +description: "Code:\n\ntodo string.join([\"foo\", \"bar\"])\n" +--- +Trace { + kind: Todo, + location: 5..32, + then: ErrorTerm { + location: 5..32, + }, + text: Call { + arguments: [ + CallArg { + label: None, + location: 17..31, + value: List { + location: 17..31, + elements: [ + ByteArray { + location: 18..23, + bytes: [ + 102, + 111, + 111, + ], + preferred_format: Utf8String, + }, + ByteArray { + location: 25..30, + bytes: [ + 98, + 97, + 114, + ], + preferred_format: Utf8String, + }, + ], + tail: None, + }, + }, + ], + fun: FieldAccess { + location: 5..16, + label: "join", + container: Var { + location: 5..11, + name: "string", + }, + }, + location: 5..32, + }, +} diff --git a/crates/aiken-lang/src/parser/expr/snapshots/todo_sugar.snap b/crates/aiken-lang/src/parser/expr/snapshots/todo_sugar.snap index ce2e4877..90e0777b 100644 --- a/crates/aiken-lang/src/parser/expr/snapshots/todo_sugar.snap +++ b/crates/aiken-lang/src/parser/expr/snapshots/todo_sugar.snap @@ -1,12 +1,12 @@ --- -source: crates/aiken-lang/src/parser/expr/error_todo.rs +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs description: "Code:\n\ntodo \"foo\"\n" --- Trace { kind: Todo, - location: 0..10, + location: 5..10, then: ErrorTerm { - location: 0..10, + location: 5..10, }, text: String { location: 5..10, diff --git a/crates/aiken-lang/src/parser/expr/snapshots/trace_expr.snap b/crates/aiken-lang/src/parser/expr/snapshots/trace_expr.snap new file mode 100644 index 00000000..783fa6e1 --- /dev/null +++ b/crates/aiken-lang/src/parser/expr/snapshots/trace_expr.snap @@ -0,0 +1,53 @@ +--- +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs +description: "Code:\n\ntrace string.join([\"foo\", \"bar\"])\na\n" +--- +Trace { + kind: Trace, + location: 0..35, + then: Var { + location: 34..35, + name: "a", + }, + text: Call { + arguments: [ + CallArg { + label: None, + location: 18..32, + value: List { + location: 18..32, + elements: [ + ByteArray { + location: 19..24, + bytes: [ + 102, + 111, + 111, + ], + preferred_format: Utf8String, + }, + ByteArray { + location: 26..31, + bytes: [ + 98, + 97, + 114, + ], + preferred_format: Utf8String, + }, + ], + tail: None, + }, + }, + ], + fun: FieldAccess { + location: 6..17, + label: "join", + container: Var { + location: 6..12, + name: "string", + }, + }, + location: 6..33, + }, +} diff --git a/crates/aiken-lang/src/parser/expr/snapshots/trace_expr_todo.snap b/crates/aiken-lang/src/parser/expr/snapshots/trace_expr_todo.snap new file mode 100644 index 00000000..c0ecd7ac --- /dev/null +++ b/crates/aiken-lang/src/parser/expr/snapshots/trace_expr_todo.snap @@ -0,0 +1,23 @@ +--- +source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs +description: "Code:\n\ntrace some_var \n" +--- +Trace { + kind: Trace, + location: 0..14, + then: Trace { + kind: Todo, + location: 0..14, + then: ErrorTerm { + location: 0..14, + }, + text: String { + location: 0..14, + value: "aiken::todo", + }, + }, + text: Var { + location: 6..14, + name: "some_var", + }, +} diff --git a/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_double_todo.snap b/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_double_todo.snap index b708e48a..0f9204ed 100644 --- a/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_double_todo.snap +++ b/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_double_todo.snap @@ -26,12 +26,12 @@ When { guard: None, then: Trace { kind: Todo, - location: 28..32, + location: 35..39, then: ErrorTerm { - location: 28..32, + location: 35..39, }, text: String { - location: 28..32, + location: 35..39, value: "aiken::todo", }, }, @@ -53,12 +53,12 @@ When { guard: None, then: Trace { kind: Todo, - location: 47..51, + location: 52..53, then: ErrorTerm { - location: 47..51, + location: 52..53, }, text: String { - location: 47..51, + location: 52..53, value: "aiken::todo", }, }, diff --git a/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_solo_error.snap b/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_solo_error.snap index 025955a4..85e7ae46 100644 --- a/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_solo_error.snap +++ b/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_solo_error.snap @@ -26,12 +26,12 @@ When { guard: None, then: Trace { kind: Error, - location: 28..32, + location: 33..34, then: ErrorTerm { - location: 28..32, + location: 33..34, }, text: String { - location: 28..32, + location: 33..34, value: "aiken::error", }, }, diff --git a/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_todo.snap b/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_todo.snap index 26c65eea..c0b59c19 100644 --- a/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_todo.snap +++ b/crates/aiken-lang/src/parser/expr/when/snapshots/when_clause_todo.snap @@ -46,9 +46,9 @@ When { guard: None, then: Trace { kind: Todo, - location: 47..68, + location: 52..68, then: ErrorTerm { - location: 47..68, + location: 52..68, }, text: String { location: 52..68, diff --git a/crates/aiken-lang/src/tests/snapshots/fail_expr.snap b/crates/aiken-lang/src/tests/snapshots/fail_expr.snap new file mode 100644 index 00000000..c2d4bfb4 --- /dev/null +++ b/crates/aiken-lang/src/tests/snapshots/fail_expr.snap @@ -0,0 +1,8 @@ +--- +source: crates/aiken-lang/src/tests/format.rs +description: "Code:\n\nfn foo() {\n fail some_var\n}\n" +--- +fn foo() { + fail some_var +} +