Merge pull request #406 from schungx/master

Fix hanging when parsing unterminated statements block.
This commit is contained in:
Stephen Chung 2021-04-20 11:37:01 +08:00 committed by GitHub
commit f7d523e983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 10 deletions

View File

@ -4,11 +4,27 @@ Testing Scripts
Testing scripts written in Rhai. Testing scripts written in Rhai.
Install `rhai-run` Tool
-----------------------
Use the following command to install the `rhai-run` tool:
```sh
cargo install --path . --bin rhai-run
```
How to Run How to Run
---------- ----------
Run scripts using the `rhai-run` tool: Run scripts using the `rhai-run` tool:
```sh
rhai-run ./scripts/test_script_to_run.rhai
```
or
```sh ```sh
cargo run --bin rhai-run ./scripts/test_script_to_run.rhai cargo run --bin rhai-run ./scripts/test_script_to_run.rhai
``` ```

View File

@ -10,9 +10,10 @@ fn new_mat(x, y) {
matrix matrix
} }
fn mat_gen(n) { fn mat_gen() {
let m = new_mat(n, n); const n = global::SIZE;
const tmp = 1.0 / n / n; const tmp = 1.0 / n / n;
let m = new_mat(n, n);
for i in range(0, n) { for i in range(0, n) {
for j in range(0, n) { for j in range(0, n) {
@ -39,9 +40,7 @@ fn mat_mul(a, b) {
c[i][j] = 0.0; c[i][j] = 0.0;
for z in range(0, a[i].len) { for z in range(0, a[i].len) {
let x = a[i][z]; c[i][j] += a[i][z] * b2[j][z];
let y = b2[j][z];
c[i][j] += x * y;
} }
} }
} }
@ -51,8 +50,8 @@ fn mat_mul(a, b) {
const now = timestamp(); const now = timestamp();
const a = mat_gen(SIZE); const a = mat_gen();
const b = mat_gen(SIZE); const b = mat_gen();
const c = mat_mul(a, b); const c = mat_mul(a, b);
/* /*

View File

@ -2417,7 +2417,23 @@ fn parse_block(
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let prev_mods_len = state.modules.len(); let prev_mods_len = state.modules.len();
while !match_token(input, Token::RightBrace).0 { loop {
// Terminated?
match input.peek().unwrap() {
(Token::RightBrace, _) => {
eat_token(input, Token::RightBrace);
break;
}
(Token::EOF, pos) => {
return Err(PERR::MissingToken(
Token::RightBrace.into(),
"to terminate this block".into(),
)
.into_err(*pos));
}
_ => (),
}
// Parse statements inside the block // Parse statements inside the block
settings.is_global = false; settings.is_global = false;
@ -2443,11 +2459,13 @@ fn parse_block(
eat_token(input, Token::SemiColon); eat_token(input, Token::SemiColon);
} }
// { ... { stmt } ; // { ... { stmt } ;
(Token::SemiColon, _) if !need_semicolon => (), (Token::SemiColon, _) if !need_semicolon => {
eat_token(input, Token::SemiColon);
}
// { ... { stmt } ??? // { ... { stmt } ???
(_, _) if !need_semicolon => (), (_, _) if !need_semicolon => (),
// { ... stmt <error> // { ... stmt <error>
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)), (Token::LexError(err), err_pos) => return Err(err.clone().into_err(*err_pos)),
// { ... stmt ??? // { ... stmt ???
(_, pos) => { (_, pos) => {
// Semicolons are not optional between statements // Semicolons are not optional between statements