Add more reserved keywords.

This commit is contained in:
Stephen Chung 2020-07-26 21:57:30 +08:00
parent 878ba0b794
commit ce20248792
3 changed files with 84 additions and 44 deletions

View File

@ -3,32 +3,65 @@ Keywords List
{{#include ../links.md}} {{#include ../links.md}}
| Keyword | Description | Not available under | | Keyword | Description | Inactive under |
| :-------------------: | ---------------------------------------- | :-----------------: | | :-------------------: | ---------------------------------------- | :-------------: |
| `true` | Boolean true literal | | | `true` | Boolean true literal | |
| `false` | Boolean false literal | | | `false` | Boolean false literal | |
| `let` | Variable declaration | | | `let` | Variable declaration | |
| `const` | Constant declaration | | | `const` | Constant declaration | |
| `if` | If statement | | | `if` | If statement | |
| `else` | else block of if statement | | | `else` | else block of if statement | |
| `while` | While loop | | | `while` | While loop | |
| `loop` | Infinite loop | | | `loop` | Infinite loop | |
| `for` | For loop | | | `for` | For loop | |
| `in` | Containment test, part of for loop | | | `in` | Containment test, part of for loop | |
| `continue` | Continue a loop at the next iteration | | | `continue` | Continue a loop at the next iteration | |
| `break` | Loop breaking | | | `break` | Loop breaking | |
| `return` | Return value | | | `return` | Return value | |
| `throw` | Throw exception | | | `throw` | Throw exception | |
| `import` | Import module | [`no_module`] | | `import` | Import module | [`no_module`] |
| `export` | Export variable | [`no_module`] | | `export` | Export variable | [`no_module`] |
| `as` | Alias for variable export | [`no_module`] | | `as` | Alias for variable export | [`no_module`] |
| `private` | Mark function private | [`no_function`] | | `private` | Mark function private | [`no_function`] |
| `fn` (lower-case `f`) | Function definition | [`no_function`] | | `fn` (lower-case `f`) | Function definition | [`no_function`] |
| `Fn` (capital `F`) | Function to create a [function pointer] | | | `Fn` (capital `F`) | Function to create a [function pointer] | |
| `call` | Call a [function pointer] | | | `call` | Call a [function pointer] | |
| `curry` | Curry a [function pointer] | | | `curry` | Curry a [function pointer] | |
| `this` | Reference to base object for method call | [`no_function`] | | `this` | Reference to base object for method call | [`no_function`] |
| `type_of` | Get type name of value | | | `type_of` | Get type name of value | |
| `print` | Print value | | | `print` | Print value | |
| `debug` | Print value in debug format | | | `debug` | Print value in debug format | |
| `eval` | Evaluate script | | | `eval` | Evaluate script | |
Reserved Keywords
-----------------
| Keyword | Potential usage |
| --------- | --------------------- |
| `var` | Variable declaration |
| `static` | Variable declaration |
| `do` | Looping |
| `each` | Looping |
| `then` | Conditional |
| `goto` | Jump |
| `switch` | Matching |
| `match` | Matching |
| `case` | Matching |
| `public` | Function/field access |
| `new` | Constructor |
| `try` | Trap exception |
| `catch` | Catch exception |
| `use` | Import namespace |
| `with` | Scope |
| `module` | Module |
| `package` | Package |
| `spawn` | Threading |
| `go` | Threading |
| `await` | Async |
| `async` | Async |
| `yield` | Async |
| `default` | Special value |
| `void` | Special value |
| `null` | Special value |
| `nil` | Special value |

View File

@ -5,18 +5,21 @@ Keywords
The following are reserved keywords in Rhai: The following are reserved keywords in Rhai:
| Keywords | Usage | Not available under feature | | Active keywords | Reserved keywords | Usage | Inactive under feature |
| ------------------------------------------------- | --------------------- | :-------------------------: | | ------------------------------------------------- | ---------------------------------------- | --------------------- | :--------------------: |
| `true`, `false` | Boolean constants | | | `true`, `false` | | Boolean constants | |
| `let`, `const` | Variable declarations | | | `let`, `const` | `var`, `static` | Variable declarations | |
| `if`, `else` | Control flow | | | `if`, `else` | `then`, `goto` | Control flow | |
| `while`, `loop`, `for`, `in`, `continue`, `break` | Looping | | | | `switch`, `match`, `case` | Matching | |
| `fn`, `private` | Functions | [`no_function`] | | `while`, `loop`, `for`, `in`, `continue`, `break` | `do`, `each` | Looping | |
| `return` | Return values | | | `fn`, `private` | `public`, `new` | Functions | [`no_function`] |
| `throw` | throw exceptions | | | `return` | | Return values | |
| `import`, `export`, `as` | Modules | [`no_module`] | | `throw` | `try`, `catch` | Throw exceptions | |
| `Fn`, `call` | Function pointers | | | `import`, `export`, `as` | `use`, `with`, `module`, `package` | Modules/packages | [`no_module`] |
| `type_of`, `print`, `debug`, `eval` | Special functions | | | `Fn`, `call`, `curry` | | Function pointers | |
| | `spawn`, `go`, `async`, `await`, `yield` | Threading/async | |
| `type_of`, `print`, `debug`, `eval` | | Special functions | |
| | `default`, `void`, `null`, `nil` | Special values | |
Keywords cannot become the name of a [function] or [variable], even when they are disabled.
Keywords cannot be the name of a [function] or [variable], unless the relevant feature is enabled.
For example, `fn` is a valid variable name under [`no_function`].

View File

@ -492,9 +492,13 @@ impl Token {
#[cfg(feature = "no_module")] #[cfg(feature = "no_module")]
"import" | "export" | "as" => Reserved(syntax.into()), "import" | "export" | "as" => Reserved(syntax.into()),
"===" | "!==" | "->" | "<-" | "=>" | ":=" | "::<" | "(*" | "*)" | "#" => { "===" | "!==" | "->" | "<-" | "=>" | ":=" | "::<" | "(*" | "*)" | "#" | "public"
| "new" | "use" | "module" | "package" | "var" | "static" | "with" | "do" | "each"
| "then" | "goto" | "switch" | "match" | "case" | "try" | "catch" | "default"
| "void" | "null" | "nil" | "spawn" | "go" | "async" | "await" | "yield" => {
Reserved(syntax.into()) Reserved(syntax.into())
} }
KEYWORD_PRINT | KEYWORD_DEBUG | KEYWORD_TYPE_OF | KEYWORD_EVAL | KEYWORD_FN_PTR KEYWORD_PRINT | KEYWORD_DEBUG | KEYWORD_TYPE_OF | KEYWORD_EVAL | KEYWORD_FN_PTR
| KEYWORD_FN_PTR_CALL | KEYWORD_FN_PTR_CURRY | KEYWORD_THIS => Reserved(syntax.into()), | KEYWORD_FN_PTR_CALL | KEYWORD_FN_PTR_CURRY | KEYWORD_THIS => Reserved(syntax.into()),