Skip to content

Commit

Permalink
fix error message for extra close parenthesis #263
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jan 14, 2024
1 parent 8ff26cd commit 76a7dc0
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 136 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* fix odd? even? on non integers
* fix object literals with null value [#264](https://github.com/jcubic/lips/issues/264)
* fix version and date in executable [#261](https://github.com/jcubic/lips/issues/261)
* fix error on extra close parenthesis [#263](https://github.com/jcubic/lips/issues/263)

## 1.0.0-beta.17
### Breaking
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![npm](https://img.shields.io/badge/npm-1.0.0%E2%80%93beta.17.3-blue.svg)](https://www.npmjs.com/package/@jcubic/lips)
![1.0.0 Complete](https://img.shields.io/github/milestones/progress-percent/jcubic/lips/1?label=1.0.0%20Complete)
[![Build and test](https://github.com/jcubic/lips/actions/workflows/build.yaml/badge.svg?branch=devel&event=push)](https://github.com/jcubic/lips/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&915345bb7efef31d29f7c395edf02656)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&588ba056f11c7ab86ecec631717742c3)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Join Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jcubic/lips)
![NPM Download Count](https://img.shields.io/npm/dm/@jcubic/lips)
![JSDelivr Download count](https://img.shields.io/jsdelivr/npm/hm/@jcubic/lips)
Expand Down
8 changes: 4 additions & 4 deletions dist/lips.ems.min.js

Large diffs are not rendered by default.

50 changes: 33 additions & 17 deletions dist/lips.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 42 additions & 62 deletions dist/lips.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 11 additions & 47 deletions dist/lips.min.js

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -1531,11 +1531,17 @@ class Parser {
balanced() {
return this._state.parentheses === 0;
}
ballancing_error(expr) {
ballancing_error(expr, prev) {
const count = this._state.parentheses;
const e = new Error('Parser: expected parenthesis but eof found');
const re = new RegExp(`\\){${count}}$`);
e.__code__ = [expr.toString().replace(re, '')];
let e;
if (count < 0) {
e = new Error('Parser: unexpected parenthesis');
e.__code__ = [prev.toString() + ')'];
} else {
e = new Error('Parser: expected parenthesis but eof found');
const re = new RegExp(`\\){${count}}$`);
e.__code__ = [expr.toString().replace(re, '')];
}
throw e;
}
// Cover This function (array and object branch)
Expand Down Expand Up @@ -1658,6 +1664,9 @@ class Parser {
this.skip();
this._refs[ref_label] = this._read_object();
return this._refs[ref_label];
} else if (this.is_close(token)) {
this.skip();
// invalid state, we don't need to return anything
} else if (this.is_open(token)) {
this.skip();
return this.read_list();
Expand Down Expand Up @@ -1698,14 +1707,16 @@ async function* parse(arg, env) {
}
const parser = new Parser(arg, { env });
let i = 100000;
let prev;
while (true) {
const expr = await parser.read_object();
if (!parser.balanced()) {
parser.ballancing_error(expr);
parser.ballancing_error(expr, prev);
}
if (expr === eof) {
break;
}
prev = expr;
yield expr;
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@
(let ((x '#3=(1 2 . #3#)))
(t.is (eq? x (cddr x)) true))))

(test "parser: should throw an error on extra close paren"
(lambda (t)
(t.is (try
(lips.exec "(define x 10))")
(catch (e)
e.message))
"Parser: unexpected parenthesis")))

(test "tokenizer: should create tokens for simple list"
(lambda (t)
(t.is (lips.tokenize "(foo bar baz)")
Expand Down

0 comments on commit 76a7dc0

Please sign in to comment.