From 308512fab98efe510ccb2833a6482d7ab2fac3cb Mon Sep 17 00:00:00 2001 From: gluax <16431709+gluax@users.noreply.github.com> Date: Fri, 4 Mar 2022 10:26:34 -0800 Subject: [PATCH] add tests --- compiler/parser/src/tokenizer/lexer.rs | 4 ++++ leo/errors/src/parser/parser_errors.rs | 7 +++++++ .../parser/parser/circuits/self_not_first_fail.leo.out | 2 +- .../parser/parser/expression/literal/char_fail.leo.out | 2 ++ .../parser/functions/annotated_arg_not_ident.leo.out | 5 +++++ .../parser/parser/statement/hex_int_fail.leo.out | 7 +++++++ tests/parser/expression/literal/char_fail.leo | 2 ++ ...g_not_ident_int.leo => annotated_arg_not_ident.leo} | 6 ++++++ tests/parser/statement/hex_int_fail.leo | 10 ++++++++++ 9 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/expectations/parser/parser/functions/annotated_arg_not_ident.leo.out create mode 100644 tests/expectations/parser/parser/statement/hex_int_fail.leo.out rename tests/parser/functions/{annotated_arg_not_ident_int.leo => annotated_arg_not_ident.leo} (71%) create mode 100644 tests/parser/statement/hex_int_fail.leo diff --git a/compiler/parser/src/tokenizer/lexer.rs b/compiler/parser/src/tokenizer/lexer.rs index f97a3e1700..631e0f853d 100644 --- a/compiler/parser/src/tokenizer/lexer.rs +++ b/compiler/parser/src/tokenizer/lexer.rs @@ -161,7 +161,11 @@ impl Token { return Err(ParserError::lexer_eat_integer_leading_zero(String::from_utf8_lossy(input)).into()); } let mut i = 1; + while i < input.len() { + if i == 1 && input[0] == b'0' && input[i] == b'x' { + return Err(ParserError::lexer_hex_number_provided(&input_tendril[0..3]).into()); + } if !input[i].is_ascii_digit() { break; } diff --git a/leo/errors/src/parser/parser_errors.rs b/leo/errors/src/parser/parser_errors.rs index 79634004f4..9ad6bf1702 100644 --- a/leo/errors/src/parser/parser_errors.rs +++ b/leo/errors/src/parser/parser_errors.rs @@ -351,6 +351,13 @@ create_errors!( msg: format!("The escaped unicode char `{}` is greater than 0x10FFFF.", input), help: None, } + /// When a hex number is provided. + @backtraced + lexer_hex_number_provided { + args: (input: impl Display), + msg: format!("A hex number `{}..` was provided but hex is not allowed.", input), + help: None, + } /// When a function recieved a self argument outside the first argument. @backtraced diff --git a/tests/expectations/parser/parser/circuits/self_not_first_fail.leo.out b/tests/expectations/parser/parser/circuits/self_not_first_fail.leo.out index 12aa9c0dc5..5a73599380 100644 --- a/tests/expectations/parser/parser/circuits/self_not_first_fail.leo.out +++ b/tests/expectations/parser/parser/circuits/self_not_first_fail.leo.out @@ -2,4 +2,4 @@ namespace: Parse expectation: Fail outputs: - - "Error [EPAR0370040]: A function received a self argument as not the first argument." + - "Error [EPAR0370041]: A function received a self argument as not the first argument." diff --git a/tests/expectations/parser/parser/expression/literal/char_fail.leo.out b/tests/expectations/parser/parser/expression/literal/char_fail.leo.out index 971fb20b36..3378569ca7 100644 --- a/tests/expectations/parser/parser/expression/literal/char_fail.leo.out +++ b/tests/expectations/parser/parser/expression/literal/char_fail.leo.out @@ -36,4 +36,6 @@ outputs: - "Error [EPAR0370026]: Expected a valid escape character but found `117`." - "Error [EPAR0370026]: Expected a valid escape character but found `117`." - "Error [EPAR0370026]: Expected a valid escape character but found `117`." + - "Error [EPAR0370039]: The escaped unicode char `110000` is greater than 0x10FFFF." + - "Error [EPAR0370037]: There was no closing `}` after a escaped unicode `\\u{af🦀`." - "Error [EPAR0370028]: Expected a closed char but found `😭😂😘`." diff --git a/tests/expectations/parser/parser/functions/annotated_arg_not_ident.leo.out b/tests/expectations/parser/parser/functions/annotated_arg_not_ident.leo.out new file mode 100644 index 0000000000..35a7e35ca2 --- /dev/null +++ b/tests/expectations/parser/parser/functions/annotated_arg_not_ident.leo.out @@ -0,0 +1,5 @@ +--- +namespace: Parse +expectation: Fail +outputs: + - "Error [EPAR0370009]: unexpected string: expected 'ident', got '?'\n --> test:3:6\n |\n 3 | @foo(?, bar, ?)\n | ^\nError [EPAR0370009]: unexpected string: expected 'ident', got '?'\n --> test:3:14\n |\n 3 | @foo(?, bar, ?)\n | ^\nError [EPAR0370009]: unexpected string: expected 'ident', got '123'\n --> test:8:6\n |\n 8 | @bar(123) // ints not vali\n | ^^^\nError [EPAR0370017]: \"@context(...)\" is deprecated. Did you mean @test annotation?\n --> test:14:2\n |\n 14 | @context // recovery witness\n | ^^^^^^^" diff --git a/tests/expectations/parser/parser/statement/hex_int_fail.leo.out b/tests/expectations/parser/parser/statement/hex_int_fail.leo.out new file mode 100644 index 0000000000..c36f660260 --- /dev/null +++ b/tests/expectations/parser/parser/statement/hex_int_fail.leo.out @@ -0,0 +1,7 @@ +--- +namespace: ParseStatement +expectation: Fail +outputs: + - "Error [EPAR0370040]: A hex number `0x4..` was provided but hex is not allowed." + - "Error [EPAR0370040]: A hex number `0xA..` was provided but hex is not allowed." + - "Error [EPAR0370040]: A hex number `0xF..` was provided but hex is not allowed." diff --git a/tests/parser/expression/literal/char_fail.leo b/tests/parser/expression/literal/char_fail.leo index 982cc3f801..c5c0b94eb3 100644 --- a/tests/parser/expression/literal/char_fail.leo +++ b/tests/parser/expression/literal/char_fail.leo @@ -43,5 +43,7 @@ expectation: Fail '\u00000000' '\u01000000' '\u9999999' +'\u{110000}' +'\u{af🦀' '😭😂😘' diff --git a/tests/parser/functions/annotated_arg_not_ident_int.leo b/tests/parser/functions/annotated_arg_not_ident.leo similarity index 71% rename from tests/parser/functions/annotated_arg_not_ident_int.leo rename to tests/parser/functions/annotated_arg_not_ident.leo index 20780213f0..e927be8d5c 100644 --- a/tests/parser/functions/annotated_arg_not_ident_int.leo +++ b/tests/parser/functions/annotated_arg_not_ident.leo @@ -8,6 +8,12 @@ function x() { return (); } +@bar(123) // ints not vali +function x() { + return (); +} + + @context // recovery witness function x() { return (); diff --git a/tests/parser/statement/hex_int_fail.leo b/tests/parser/statement/hex_int_fail.leo new file mode 100644 index 0000000000..f8a4343770 --- /dev/null +++ b/tests/parser/statement/hex_int_fail.leo @@ -0,0 +1,10 @@ +/* +namespace: ParseStatement +expectation: Fail +*/ + +let x = 0x40u32; + +let y: u32 = 0xAAu32; + +let z = 0xFFu8; \ No newline at end of file