UNPKG

@fink/larix

Version:

A parser for generating fink's AST.

116 lines (97 loc) 2.13 kB
const { left_binding, non_binding } = require("@fink/prattler/expressions.js"); const { curr_loc, curr_value } = require("@fink/prattler/parser.js"); const { next_is_new_expr } = require("./block/indentation.js"); const { single_expression } = require("./block/expr.js"); const infix_led = (expr_type, lbp) => (ctx, left) => { const op = curr_value(ctx); const { loc: { start } } = left; const [right, next_ctx] = single_expression(ctx, lbp); const { loc: { end } } = right; return [{ type: expr_type, op, left, right, loc: { start, end } }, next_ctx]; }; exports.infix_led = infix_led; const maybe_left_binging = token_type => ({ ...left_binding(token_type), lbp: lbp => ctx => { const ˆvalue_1 = ctx; /* istanbul ignore else */ if (next_is_new_expr(ˆvalue_1)) { return 0; } { return lbp; } } }); exports.maybe_left_binging = maybe_left_binging; const infix = (token_type, expr_type) => ({ ...maybe_left_binging(token_type), led: lbp => infix_led(expr_type, lbp + 1) }); exports.infix = infix; const infix_right = (token_type, expr_type) => ({ ...maybe_left_binging(token_type), led: lbp => infix_led(expr_type, lbp - 1) }); exports.infix_right = infix_right; const prefix = (token_type, expr_type) => ({ ...non_binding(token_type), nud: lbp => ctx => { const { start } = curr_loc(ctx); const op = curr_value(ctx); const [right, next_ctx] = single_expression(ctx, lbp); const { end } = right.loc; return [{ type: expr_type, op, right, loc: { start, end } }, next_ctx]; } }); exports.prefix = prefix; const literal = (token_type, expr_type) => ({ ...non_binding(token_type), nud: () => ctx => { const loc = curr_loc(ctx); const value = curr_value(ctx); return [{ type: expr_type, value, loc }, ctx]; } }); exports.literal = literal; const terminator = token_type => ({ ...maybe_left_binging(token_type) }); exports.terminator = terminator;