Handle errors in readExpr

This commit is contained in:
John J. Camilleri
2021-10-08 12:54:36 +02:00
parent a27cf6a17b
commit 71536e8e37
2 changed files with 75 additions and 64 deletions

View File

@@ -201,6 +201,9 @@ function newNGF (abstract_name: string, path?: string): PGFGrammar {
function readExpr (str: string): Expr { function readExpr (str: string): Expr {
const txt = PgfText_FromString(str) const txt = PgfText_FromString(str)
const expr = runtime.pgf_read_expr(txt, unmarshaller.ref()) const expr = runtime.pgf_read_expr(txt, unmarshaller.ref())
if (ref.isNull(expr)) {
throw new PGFError('unable to parse expression')
}
return ref.readObject(expr) as Expr return ref.readObject(expr) as Expr
} }

View File

@@ -210,6 +210,13 @@ describe('abstract syntax', () => {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
describe('expressions', () => { describe('expressions', () => {
test('invalid', () => {
expect(() => {
PGF.readExpr('->')
}).toThrow(PGF.PGFError)
})
describe('literals', () => {
test('small integer', () => { test('small integer', () => {
const e1 = PGF.readExpr('123') const e1 = PGF.readExpr('123')
const e2 = new PGF.ExprLit(123) const e2 = new PGF.ExprLit(123)
@@ -286,3 +293,4 @@ describe('expressions', () => {
expect(e1).not.toEqual(e3) expect(e1).not.toEqual(e3)
}) })
}) })
})