Work on error messages

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-18 09:59:36 +01:00
parent 7f7bf5c98d
commit 42698bb219
13 changed files with 333 additions and 160 deletions

View file

@ -7,11 +7,13 @@ fn check_files() {
for file in files {
let input = std::fs::read_to_string(file.unwrap().path()).unwrap();
let Ok(parsed) = nomo::lexer::parse(input.into()) else {
let input = nomo::input::NomoInput::from(input);
let Ok(parsed) = nomo::lexer::parse(input.clone()) else {
continue;
};
let Ok(ast) = nomo::parser::parse(parsed.tokens()) else {
let Ok(ast) = nomo::parser::parse(input, parsed.tokens()) else {
continue;
};

View file

@ -2,7 +2,7 @@
source: tests/file_tests.rs
expression: parsed
info:
input: "{{ if }} {{ end }}\n{{ if if }} {{ end }}\n{{ if if }} {{ for foo in bar }} {{ end }} {{ end }}\n{{ for in bar }} {{ end }}\n{{ for blah bar }} {{ end }}\n{{ else }}"
input: "{{ if }} {{ end }}\n{{ if if }} {{ end }}\n{{ if if }} {{ for foo in bar }} {{ end }} {{ end }}\n{{ for in bar }} {{ end }}\n{{ for blah bar }} {{ end }}\n{{ for blah}} {{ end }}\n{{ else }}"
context: {}
---
ParsedTemplate {
@ -99,8 +99,21 @@ ParsedTemplate {
[Whitespace]"\n" (149..150),
[LeftDelim]"{{" (150..152),
[Whitespace]" " (152..153),
[ConditionalElse]"else" (153..157),
[Whitespace]" " (157..158),
[RightDelim]"}}" (158..160),
[For]"for" (153..156),
[Whitespace]" " (156..157),
[Ident]"blah" (157..161),
[RightDelim]"}}" (161..163),
[Whitespace]" " (163..164),
[LeftDelim]"{{" (164..166),
[Whitespace]" " (166..167),
[End]"end" (167..170),
[Whitespace]" " (170..171),
[RightDelim]"}}" (171..173),
[Whitespace]"\n" (173..174),
[LeftDelim]"{{" (174..176),
[Whitespace]" " (176..177),
[ConditionalElse]"else" (177..181),
[Whitespace]" " (181..182),
[RightDelim]"}}" (182..184),
],
}

View file

@ -2,31 +2,50 @@
source: tests/file_tests.rs
expression: ast
info:
input: "{{ if }} {{ end }}\n{{ if if }} {{ end }}\n{{ if if }} {{ for foo in bar }} {{ end }} {{ end }}\n{{ for in bar }} {{ end }}\n{{ for blah bar }} {{ end }}\n{{ else }}"
input: "{{ if }} {{ end }}\n{{ if if }} {{ end }}\n{{ if if }} {{ for foo in bar }} {{ end }} {{ end }}\n{{ for in bar }} {{ end }}\n{{ for blah bar }} {{ end }}\n{{ for blah}} {{ end }}\n{{ else }}"
context: {}
---
error: Expected an expression after 'if'
 ╭▸ 
1 │ {{ if }} {{ end }}
 ━━
╰╴
╰╴ ━━
error: Expected an expression after 'if'
 ╭▸ 
2 │ {{ if if }} {{ end }}
╰╴ ━━
error: Expected an expression after 'if'
 ╭▸ 
3 │ {{ if if }} {{ for foo in bar }} {{ end }} {{ end }}
╰╴ ━━
error: Expected identifier here
error: Missing ident here
 ╭▸ 
4 │ {{ for in bar }} {{ end }}
╰╴ ━━━━━━
error: Missing `in` in `for _ in <expr>`
error: Missing `in` in `for .. in ` loop
 ╭▸ 
5 │ {{ for blah bar }} {{ end }}
╰╴ ━━━━━━━━
error: An error occurred while producing an Ast
│ ━━━━━━━━
╰╴
note: Try adding it
╭╴
5 │ {{ for blah in bar }} {{ end }}
╰╴ ++
error: Missing `in` in `for .. in ` loop
 ╭▸ 
6 │ {{ else }}
╰╴ ━━━━━━━
6 │ {{ for blah}} {{ end }}
│ ━━━━
╰╴
note: Try adding it
╭╴
6 │ {{ for blah in}} {{ end }}
╰╴ ++
error: Unexpected keyword
 ╭▸ 
7 │ {{ else }}
╰╴ ━━━━

View file

@ -5,4 +5,5 @@
{{ if if }} {{ for foo in bar }} {{ end }} {{ end }}
{{ for in bar }} {{ end }}
{{ for blah bar }} {{ end }}
{{ for blah}} {{ end }}
{{ else }}

View file

@ -5,6 +5,7 @@ use std::path::Path;
use nomo::Context;
use nomo::functions::FunctionMap;
use nomo::input::NomoInput;
test_each_file::test_each_path! { for ["nomo"] in "./tests/cases/" as cases => check_for_input }
@ -43,16 +44,18 @@ fn check_for_input([path]: [&Path; 1]) {
context.try_insert(k, v).unwrap();
}
let parsed = nomo::lexer::parse(input.into()).unwrap();
let input = NomoInput::from(input);
let parsed = nomo::lexer::parse(input.clone()).unwrap();
let _guard = settings.bind_to_scope();
insta::assert_debug_snapshot!(format!("{basename}.1-parsed"), parsed);
let ast = match nomo::parser::parse(parsed.tokens()) {
let ast = match nomo::parser::parse(input, parsed.tokens()) {
Ok(ast) => ast,
Err(err) => {
eprintln!("{}", err.to_report(input));
eprintln!("{}", err);
panic!("Could not evaluate ast");
}
};
@ -97,7 +100,9 @@ fn check_errors([path]: [&Path; 1]) {
let _guard = settings.bind_to_scope();
let parsed = nomo::lexer::parse(input.into()).map_err(|err| err.to_report());
let input = NomoInput::from(input);
let parsed = nomo::lexer::parse(input.clone()).map_err(|err| err.to_report());
match &parsed {
Ok(parsed) => insta::assert_debug_snapshot!(format!("{basename}.1-parsed"), parsed),
@ -108,7 +113,7 @@ fn check_errors([path]: [&Path; 1]) {
return;
};
let ast = nomo::parser::parse(parsed.tokens()).map_err(|err| err.to_report(input));
let ast = nomo::parser::parse(input, parsed.tokens()).map_err(|err| err.to_string());
match &ast {
Ok(ast) => insta::assert_debug_snapshot!(format!("{basename}.2-ast"), ast),