Skip to content

Commit

Permalink
Expressions: raise error on unexpected operator
Browse files Browse the repository at this point in the history
  • Loading branch information
phensley committed Jan 6, 2021
1 parent c9f478d commit fad5694
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
23 changes: 22 additions & 1 deletion __tests__/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ASN,
SEMI,
SEQ,
VarToken,
ExprOptions,
} from '../src/math';
import { splitVariable } from '../src/util';
Expand All @@ -43,7 +44,7 @@ const build = (s: string) => {
const parse = (s: string) => new Expr(s).tokens.elems;

const call = (value: string) => ({ type: ExprTokenType.CALL, value });
const varn = (value: string) => ({
const varn = (value: string): VarToken => ({
type: ExprTokenType.VARIABLE,
value: splitVariable(value),
});
Expand Down Expand Up @@ -267,6 +268,26 @@ test('unsupported values', () => {
expect(reduce('bool(arr)', c)).toEqual(undefined);
});

test('unsupported operators', () => {
let c: Context = new Context(new Node({}));
let e: Expr;

// Reduce invalid expressions to ensure that unexpected operators are caught
// during evaluation.

e = new Expr('');
e.reduceExpr(c, [num(1), num(2), SEMI]);
expect(e.errors[0]).toContain('Unexpected operator');

e = new Expr('');
e.reduceExpr(c, [num(1), num(2), LPRN]);
expect(e.errors[0]).toContain('Unexpected operator');

e = new Expr('');
e.reduceExpr(c, [varn('@foo'), num(1), num(2), ADD, LPRN, ASN]);
expect(e.errors[0]).toContain('Unexpected operator');
});

test('strings', () => {
const c = context({});

Expand Down
14 changes: 9 additions & 5 deletions src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ const E_INVALID_HEX = `Invalid 2-char hex escape found`;
const E_INVALID_UNICODE = `Invalid unicode escape found`;
// const E_INVALID_HEX_NUM = 'Invalid hex number sequence';
// const E_INVALID_DEC_NUM = 'Invalid decimal number sequence';
const E_MISMATCHED_OP = `Mismatched operator found: `;
const E_MISMATCHED_OP = `Mismatched operator found:`;
const E_UNEXPECTED_OPERATOR = `Unexpected operator found during evaluation:`;

// Operator associativity
export const enum Assoc {
Expand Down Expand Up @@ -695,7 +696,7 @@ const mul = (a: Token, b: Token): Token => num(asnum(a) * asnum(b));
const matcher = new ExprMatcherImpl('');

// Uncomment to debug tokens
// const debug = (t: Token | undefined): string => {
// export const debug = (t: Token | undefined): string => {
// if (!t) {
// return 'undefined';
// }
Expand Down Expand Up @@ -879,9 +880,8 @@ export class Expr {
// Validate operator args are present and valid
if (a === undefined || b === undefined) {
// Invalid arguments to operator, bail out.
const op = OPERATORS[t.value.type].desc;
ctx.error(
expressionReduce(this.raw, `Invalid arguments to operator ${op}`)
expressionReduce(this.raw, `Invalid arguments to operator ${t.value.desc}`)
);
break loop;
}
Expand Down Expand Up @@ -974,7 +974,11 @@ export class Expr {
case OperatorType.LOR:
r = bool(asbool(a) || asbool(b));
break;
}
default:
this.errors.push(`${E_UNEXPECTED_OPERATOR} ${t.value.desc}`);
stack.top = undefined;
break loop;
}
stack.push(r!);
}
}
Expand Down

0 comments on commit fad5694

Please sign in to comment.