From 0519e663487f07bcf4795aadb557488f550116e0 Mon Sep 17 00:00:00 2001 From: peefy Date: Tue, 5 Sep 2023 16:00:35 +0800 Subject: [PATCH] refactor: sync error with spec. --- kclvm/error/src/error.rs | 13 +++++++++- kclvm/error/src/error_codes/E1001.md | 27 ++++++++++++++++---- kclvm/error/src/error_codes/E1002.md | 29 ++++++++++++++++++++++ kclvm/error/src/error_codes/E1003.md | 29 ++++++++++++++++++++++ kclvm/error/src/error_codes/E1I37.md | 28 +++++++++++++++++++++ kclvm/error/src/error_codes/E2A31.md | 10 +++++--- kclvm/error/src/error_codes/E2D34.md | 30 ++++++++++++++-------- kclvm/error/src/error_codes/E2F04.md | 26 ++++++++++++++----- kclvm/error/src/error_codes/E2G22.md | 37 +++++++++++++++++++++++----- kclvm/error/src/error_codes/E2H13.md | 34 +++++++++++++++++++++++++ kclvm/error/src/error_codes/E2L28.md | 37 +++++++++++++++++++++------- 11 files changed, 259 insertions(+), 41 deletions(-) create mode 100644 kclvm/error/src/error_codes/E1002.md create mode 100644 kclvm/error/src/error_codes/E1003.md create mode 100644 kclvm/error/src/error_codes/E1I37.md create mode 100644 kclvm/error/src/error_codes/E2H13.md diff --git a/kclvm/error/src/error.rs b/kclvm/error/src/error.rs index 577d343c3..f3bf4d7e1 100644 --- a/kclvm/error/src/error.rs +++ b/kclvm/error/src/error.rs @@ -38,13 +38,19 @@ macro_rules! register_warnings { // Error messages for EXXXX errors. Each message should start and end with a // new line. register_errors! { + // E1XXX Syntax Errors E1001: ErrorKind::InvalidSyntax, include_str!("./error_codes/E1001.md"), + E1002: ErrorKind::TabError, include_str!("./error_codes/E1002.md"), + E1003: ErrorKind::IndentationError, include_str!("./error_codes/E1003.md"), + E1I37: ErrorKind::IllegalArgumentSyntax, include_str!("./error_codes/E1I37.md"), + // E2XXX Compile Errors E2G22: ErrorKind::TypeError, include_str!("./error_codes/E2G22.md"), E2F04: ErrorKind::CannotFindModule, include_str!("./error_codes/E2F04.md"), E2L23: ErrorKind::CompileError, include_str!("./error_codes/E2L23.md"), E2A31: ErrorKind::IllegalAttributeError, include_str!("./error_codes/E2A31.md"), E2L28: ErrorKind::UniqueKeyError, include_str!("./error_codes/E2L28.md"), E2D34: ErrorKind::IllegalInheritError, include_str!("./error_codes/E2D34.md"), + // E3XXX Runtime Errors E3M38: ErrorKind::EvaluationError, include_str!("./error_codes/E2D34.md"), } @@ -63,9 +69,12 @@ pub struct Error { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ErrorKind { + // Syntax Errors InvalidSyntax, TabError, - Indentation, + IndentationError, + IllegalArgumentSyntax, + // Compile Errors CannotFindModule, RecursiveLoad, FloatOverflow, @@ -83,6 +92,7 @@ pub enum ErrorKind { ValueError, KeyError, AttributeError, + // Runtime Errors AssertionError, ImmutableError, MultiInheritError, @@ -129,6 +139,7 @@ pub struct Warning { // Kind of KCL warning. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum WarningKind { + // Compile Warnings CompilerWarning, UnusedImportWarning, ReimportWarning, diff --git a/kclvm/error/src/error_codes/E1001.md b/kclvm/error/src/error_codes/E1001.md index 30c1ae3ca..60b9c6f42 100644 --- a/kclvm/error/src/error_codes/E1001.md +++ b/kclvm/error/src/error_codes/E1001.md @@ -1,9 +1,26 @@ +### InvalidSyntaxError (E1001) -This error indicates that the compiler syntax error has occurred. +KCL will report InvalidSyntaxError when KCL has a syntax error. -Erroneous code example: +The error code of InvalidSyntaxError is E1001. -```kcl,E1001 -x = f( - 7 ^ -> Expected one of ['all', 'any', 'bin_number', 'dec_number', '**', 'False', 'filter', 'float_number','hex_number', 'lambda', '{', '[', '(', 'long_string', 'not', 'map', '-', '*', 'name', 'None', '~', 'oct_number', '+',')', 'string', 'True', 'Undefined'] +For example: + +``` +a, b = 1, 2 # Multiple assign is illegal in KCL syntax ``` + +The KCL program will cause the following error message. + +```kcl,e1001 +error[E1001]: InvalidSyntax + --> /syntax_error/general/multiple_assign/case0/main.k:1:2 + | +1 | a, b = 1, 2 # Multiple assign is illegal in KCL syntax + | ^ expected statement + | +``` + +Possible resolution: + +Check and fix KCL syntax errors based on the KCL Language Standard diff --git a/kclvm/error/src/error_codes/E1002.md b/kclvm/error/src/error_codes/E1002.md new file mode 100644 index 000000000..41a437c40 --- /dev/null +++ b/kclvm/error/src/error_codes/E1002.md @@ -0,0 +1,29 @@ +### KCLTabError (E1002) + +KCL will report `KCLTabError` when KCL has a tab and white space syntax error. + +In KCL, it is forbidden to mix tabs and four spaces in one indentation block. And we recommend only using white spaces or tabs for indentation in the entire KCL project, don’t mix them. + +For example: + +```python +schema Person: + name: str # begin with a tab + age: int # begin with four white spaces, + # and four white spaces != tab in the env +``` + +The KCL program will cause the following error message. + +```shell +error[E1001]: InvalidSyntax + --> File /syntax_error/tab/tab_error_0/main.k:6:5 + | +3 | age: int = 1 + | ^ inconsistent use of tabs and spaces in indentation + | +``` + +Possible resolution: + +- Only use a tab or four white spaces in KCL, do not mix them. diff --git a/kclvm/error/src/error_codes/E1003.md b/kclvm/error/src/error_codes/E1003.md new file mode 100644 index 000000000..13b8625aa --- /dev/null +++ b/kclvm/error/src/error_codes/E1003.md @@ -0,0 +1,29 @@ +### IndentationError (E1003) + +KCL will report `KCLIndentationError` when KCL has an indentation syntax error. + +The KCL syntax includes indentation. A tab or four white spaces in KCL represents an indentation. The other cases will be regarded as syntax errors by KCL. + +For example: + +```python +schema Person: + name: str # a tab or four white spaces is legal. + age: int # three white spaces are illegal + info: str # two white spaces is illegal +``` + +The KCL program will cause the following error message. + +```shell +error[E1001]: InvalidSyntax + --> /syntax_error/indent/indent_error_0/main.k:3:4 + | +3 | age: int # three white spaces are illegal + | ^ unindent 3 does not match any outer indentation level + | +``` + +Possible resolution: + +- Only use a tab or four white spaces in the KCL program for indentation. diff --git a/kclvm/error/src/error_codes/E1I37.md b/kclvm/error/src/error_codes/E1I37.md new file mode 100644 index 000000000..298ec5846 --- /dev/null +++ b/kclvm/error/src/error_codes/E1I37.md @@ -0,0 +1,28 @@ +### IllegalArgumentSyntaxError (E1I37) + +KCL will report `IllegalArgumentSyntaxError` when KCL has an illegal argument in KCL syntax. + +For example: + +```python +# Parameters without default values +# must be in front of parameters with default values. +a = option(type="list", default={"key": "value"}, "key1") +``` + +The KCL program will cause the following error message. + +```shell +error[E1001]: InvalidSyntax + --> /option/type_convert_fail_2/main.k:3:57 + | +3 | a = option(type="list", default={"key": "value"}, "key1") + | ^ positional argument follows keyword argument + | +``` + +Possible resolution: + +```python +func(input_1, ..., input_n, param_with_key_1 = input_with_key_1, ..., param_with_key_n = input_with_key_n) +``` diff --git a/kclvm/error/src/error_codes/E2A31.md b/kclvm/error/src/error_codes/E2A31.md index 6d1d8dac2..9091357b2 100644 --- a/kclvm/error/src/error_codes/E2A31.md +++ b/kclvm/error/src/error_codes/E2A31.md @@ -4,8 +4,10 @@ This error indicates that the illegal attribute error has occurred. Erroneous code example: ```kcl,E2A31 -KCL Compile Error[E2A31] : Illegal attribute -1 |x = {None: None} - 6 ^ -> Failure -type 'NoneType' +error[E2A31]: IllegalAttributeError + --> /path/to/file.k:1:6 + | +1 | x = {None: None} + | ^ A attribute must be string type, got 'NoneType' + | ``` diff --git a/kclvm/error/src/error_codes/E2D34.md b/kclvm/error/src/error_codes/E2D34.md index 6ebda5fee..c1b2f2c4c 100644 --- a/kclvm/error/src/error_codes/E2D34.md +++ b/kclvm/error/src/error_codes/E2D34.md @@ -1,20 +1,30 @@ +### IllegalInheritError (E2D34) -This error indicates that invalid inheritance structure has occurred. +KCL will report `IllegalInheritError` when an illegal inheritance occurs in the schema. -Erroneous code example: +The `ewcode` of `IllegalInheritError` is `E2D34`. -```kcl +For example: + +```python schema FullnameMixin: fullName = "{} {}".format(firstName, lastName) -schema Scholar(FullnameMixin): +schema Scholar(FullnameMixin): # mixin inheritance is illegal school: str ``` -```kcl,E2D34 -KCL Complier Error[E2D34] : Illegal inheritance ----> File /schema/inherit/inherit_mixin_fail/main.k:8:1 -8 |schema Scholar(FullnameMixin): - 1 ^ -> Failure -mixin inheritance FullnameMixin is prohibited +The KCL program will cause the following error message. + +```shell +error[E2D34]: IllegalInheritError + --> /schema/inherit/inherit_mixin_fail/main.k:4:16 + | +4 | schema Scholar(FullnameMixin): + | ^ invalid schema inherit object type, expect schema, got 'FullnameMixin' + | ``` + +Possible resolution: + +- Schema supports single inheritance of schema in KCL. diff --git a/kclvm/error/src/error_codes/E2F04.md b/kclvm/error/src/error_codes/E2F04.md index 29f438dfd..4817d1c6e 100644 --- a/kclvm/error/src/error_codes/E2F04.md +++ b/kclvm/error/src/error_codes/E2F04.md @@ -1,10 +1,24 @@ +## CannotFindModule (E2F04) -This error indicates that the import module is not found. +KCL will report `CannotFindModule` when KCL imports a module that does not exist. -Erroneous code example: +The `ewcode` of `CannotFindModule` is `E2F04`. -```kcl,E2F04 -1 |import not_existed_pkg - 1 ^^^^^^^^^^^^^^^^^^^^^^ -> Failure -Cannot find the module not_existed_pkg from ./not_existed_pkg +For example: + +```python +import .some0.pkg1 as some00 # some0 not found in package + +Name1 = some00.Name # some0.pkg1.name ``` + +The KCL program will cause the following error message. + +```shell +error[E2F04]: CannotFindModule + --> import_abs_fail_0/app-main/main.k:1:1 + | +1 | import .some0.pkg1 as some00 # some0 not found in package + | Cannot find the module .some0.pkg1 + | +``` \ No newline at end of file diff --git a/kclvm/error/src/error_codes/E2G22.md b/kclvm/error/src/error_codes/E2G22.md index 16b1e0b22..c48127374 100644 --- a/kclvm/error/src/error_codes/E2G22.md +++ b/kclvm/error/src/error_codes/E2G22.md @@ -1,10 +1,35 @@ +### TypeError (E2G22) -This error indicates that the compiler type error has occurred. +KCL will report `TypeError` when a type error occurs in compiling type check. -Erroneous code example: +The `ewcode` of `TypeError` is `E2G22`. -```kcl,E2G22 -1 |a: int = "1" - 1 ^ -> got str(1) -expected int, got str(1) +For example: + +```python +schema Person: + firstName: str + lastName: int + +JohnDoe = Person { + "firstName": "John", + "lastName": "Doe" # Type Error,lastName: int,“Doe” is a string. +} +``` + +The KCL program will cause the following error message. + +```shell +error[E2G22]: TypeError + --> type/type_fail_0/main.k:7:5 + | +7 | "lastName": "Doe" # Type Error,lastName: int,“Doe” is a string. + | ^ expected int, got str(Doe) + | + + --> type/type_fail_0/main.k:3:5 + | +3 | lastName: int + | ^ variable is defined here, its type is int, but got str(Doe) + | ``` diff --git a/kclvm/error/src/error_codes/E2H13.md b/kclvm/error/src/error_codes/E2H13.md new file mode 100644 index 000000000..4c4908174 --- /dev/null +++ b/kclvm/error/src/error_codes/E2H13.md @@ -0,0 +1,34 @@ +### UnKnownDecoratorError (E2H13) + +KCL will report `UnKnownDecoratorError` when an unknown decorator is used in KCL. + +The `ewcode` of `UnKnownDecoratorError` is `E2H13`. + +For example: + +```python +@err_deprecated # It is an unknown decorator +schema Person: + firstName: str = "John" + lastName: str + name: str + +JohnDoe = Person { + name: "deprecated" +} +``` + +The KCL program will cause the following error message. + +```shell +error[E2L23]: CompileError + --> deprecated/unknown_fail_1/main.k:1:2 + | +1 | @err_deprecated # This is a error decorator + | ^ UnKnown decorator err_deprecated + | +``` + +Possible resolution: + +- Check whether the decorator exists. diff --git a/kclvm/error/src/error_codes/E2L28.md b/kclvm/error/src/error_codes/E2L28.md index d0c7cc8a2..9461f4a9b 100644 --- a/kclvm/error/src/error_codes/E2L28.md +++ b/kclvm/error/src/error_codes/E2L28.md @@ -1,21 +1,40 @@ +### UniqueKeyError (E2L28) -This error indicates that variables with the same name or duplicate definitions. +KCL will report `UniqueKeyError` when duplicate names appear in the KCL code. -Erroneous code example: +The `ewcode` of `UniqueKeyError` is `E2L28`. -```kcl +For example: + +```python schema Person: name: str = "kcl" age: int = 1 schema Person: aa: int + +x0 = Person{} +x1 = Person{age:101} ``` -```kcl,E2L28 -KCL Complier Error[E2L28] : Unique key error ----> File /schema/same_name/main.k:5:1 -5 |schema Person: - 1 ^ -> Failure -Variable name 'Person' must be unique in package context +The KCL program will cause the following error message. + +```shell +error[E2L28]: UniqueKeyError + --> /schema/same_name/main.k:5:8 + | +5 | schema Person: + | ^ Unique key error name 'Person' + | + + --> /schema/same_name/main.k:1:8 + | +1 | schema Person: + | ^ The variable 'Person' is declared here + | ``` + +Possible resolution: + +- Check if the name with error has been used.