Skip to content

Commit

Permalink
Fix degenerate case of case with a single when and an else
Browse files Browse the repository at this point in the history
  • Loading branch information
toggledbits committed Nov 17, 2023
1 parent 3cd1b76 commit 0f9958c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

**NOTE:** In order to *build* lexpjs with Unicode-friendly identifiers enabled (if, for some reason, the included pre-built `lexp.js` file doesn't suit your needs), you first need to modify *jison-lex* to allow Unicode property escapes in its *RegExp*s. See `README-lexer.md` for details.

## 1.0.23321

* Fix degenerate case of `case` statement with a single `when` and an `else` (which is really an `if` made complex).

## 1.0.23297

* Add `range()` function with more flexibility than short-cut range operator (`..`).
Expand Down
4 changes: 3 additions & 1 deletion grammar.jison
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
%start expressions

%{
/* Grammar 23296 */
/* Grammar 23321 */
var buffer = "", qsep = "";
Expand Down Expand Up @@ -309,6 +309,8 @@ when_list
{ $1.expr.push( atom( 'if', { test: $3, tc: $5, fc: $7, locs: [@3, @5, @7] } ) ); $$ = $1; }
| when_list WHEN e COLON e
{ $1.expr.push( atom( 'if', { test: $3, tc: $5, locs: [@3, @5] } ) ); $$ = $1; }
| WHEN e COLON e ELSE e
{ $$ = atom( 'list', { expr: [ atom( 'if', { test: $2, tc: $4, fc: $6, locs: [@2, @4, @5] } ) ] } ); }
| WHEN e COLON e
{ $$ = atom( 'list', { expr: [ atom( 'if', { test: $2, tc: $4, locs: [@2, @4] } ) ] } ); }
;
Expand Down
77 changes: 40 additions & 37 deletions lexp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lexpjs",
"version": "1.0.23297",
"version": "1.0.23321",
"description": "An expression parser written in JavaScript",
"main": "cli.js",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const version = 23297;
const version = 23321;

const verbose = false; // If true, all tests and results printed; otherwise just errors.

Expand Down Expand Up @@ -420,7 +420,8 @@ var test_expr = [
, { expr: 't=12, case when t==1: "one" when t==2: "two" when t==12: "twelve" end', expect: "twelve" }
, { expr: 't=9, case when t==1: "one" when t==2: "two" when t==12: "twelve" end', expect: null }
, { expr: 't=11, case when t==1: "one" when t==2: "two" when t==12: "twelve" else "unknown" end', expect: "unknown" }

, { expr: 'case when t==1: "one" end', expect: null } // abbreviated syntax (nonsense, use 'if', but supported)
, { expr: 'case when t==1: "one" else "two" end', expect: "two" } // abbreviated syntax (nonsense, use 'if', but supported)

/* Iteration */
, { expr: "each item in [1,2,3,4,5]: 2*item", expect: [ 2,4,6,8,10 ] }
Expand Down
2 changes: 1 addition & 1 deletion umd-preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* SOFTWARE.
*/

const version = 23297;
const version = 23321;

const FEATURE_MONTH_BASE = 1; /* 1 = months 1-12; set to 0 if you prefer JS semantics where 0=Jan,11=Dec */
const MAX_RANGE = 1000; /* Maximum number of elements in a result range op result array */
Expand Down

0 comments on commit 0f9958c

Please sign in to comment.