UNPKG

subscript

Version:

Modular expression parser & evaluator

31 lines (27 loc) 943 B
// try/catch/finally/throw statements - parse half // AST (faithful): ['try', body, ['catch', param, handler]?, ['finally', cleanup]?] import { parse, keyword, parens, expr, word, skip, peek } from '../parse.js'; import { block } from './if.js'; const STATEMENT = 5; // try { body } [catch (param) { handler }] [finally { cleanup }] keyword('try', STATEMENT + 1, () => { const node = ['try', block()]; parse.space(); if (word('catch')) { skip(5); parse.space(); // ES2019 optional catch binding: `catch { ... }` (no parameter) node.push(['catch', peek() === 40 ? parens() : null, block()]); } parse.space(); if (word('finally')) { skip(7); node.push(['finally', block()]); } return node; }); keyword('throw', STATEMENT + 1, () => { parse.asi && (parse.newline = false); parse.space(); if (parse.newline) throw SyntaxError('Unexpected newline after throw'); return ['throw', expr(STATEMENT)]; });