UNPKG

@fink/larix

Version:

A parser for generating fink's AST.

82 lines (80 loc) 1.91 kB
import { left_binding, non_binding } from "@fink/prattler/expressions.js"; import { curr_loc, curr_value } from "@fink/prattler/parser.js"; import { next_is_new_expr } from "./block/indentation.js"; import { single_expression } from "./block/expr.js"; export 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]; }; export 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; } } }); export const infix = (token_type, expr_type) => ({ ...maybe_left_binging(token_type), led: lbp => infix_led(expr_type, lbp + 1) }); export const infix_right = (token_type, expr_type) => ({ ...maybe_left_binging(token_type), led: lbp => infix_led(expr_type, lbp - 1) }); export 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]; } }); export 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]; } }); export const terminator = token_type => ({ ...maybe_left_binging(token_type) });