UNPKG

@fink/larix

Version:

A parser for generating fink's AST.

245 lines (206 loc) 4.42 kB
const { curr_loc, next_loc, next_is_end } = require("@fink/prattler/parser.js"); const { advance, has_errors } = require("@fink/prattler/parser.js"); const { add_error } = require("@fink/prattler/errors.js"); // TODO: remove? const end_of_block_indent = { end_of_block_indent: true }; exports.end_of_block_indent = end_of_block_indent; const end_of_code = `end`; exports.end_of_code = end_of_code; const curr_indentation = ({ blocks }) => { const [{ curr_ind }] = blocks; return curr_ind; }; exports.curr_indentation = curr_indentation; const min_indentation = ({ blocks }) => { const [{ min_ind }] = blocks; return min_ind; }; exports.min_indentation = min_indentation; const next_is_new_expr = ctx => { const { start: { column } } = next_loc(ctx); return column <= curr_indentation(ctx); }; exports.next_is_new_expr = next_is_new_expr; const next_is_unindented = ctx => { const min_ind = min_indentation(ctx); { const ˆvalue_1 = next_loc(ctx); /* istanbul ignore else */ if (ˆvalue_1 != null) { const { start: ˆp_3 } = ˆvalue_1; /* istanbul ignore else */ if (ˆp_3 != null) { const { column: ˆp_4 } = ˆp_3; /* istanbul ignore else */ if (ˆp_4 < min_ind) { return true; } } } { return false; } } }; exports.next_is_unindented = next_is_unindented; const next_is_indented = ctx => { const curr_ind = curr_indentation(ctx); const { end: { line: curr_line } } = curr_loc(ctx); const { start: { line: next_line, column: next_ind } } = next_loc(ctx); return curr_line < next_line && curr_ind < next_ind; }; exports.next_is_indented = next_is_indented; const pop_block = ctx => { const { blocks: [{ end_symbol }, ...blocks] } = ctx; const next_ctx = { ...ctx, blocks }; { const ˆvalue_5 = end_symbol; /* istanbul ignore else */ if (ˆvalue_5 === end_of_block_indent || ˆvalue_5 === end_of_code) { { const ˆvalue_7 = next_ctx; /* istanbul ignore else */ if (next_is_end(ˆvalue_7)) { return advance(next_ctx); } { return next_ctx; } } } { { const ˆvalue_9 = ctx; /* istanbul ignore else */ if (ˆvalue_9 != null) { const { next_token: ˆp_11 } = ˆvalue_9; /* istanbul ignore else */ if (ˆp_11 != null) { const { type: ˆp_12 } = ˆp_11; /* istanbul ignore else */ if (ˆp_12 === end_symbol) { return advance(next_ctx); } } } /* istanbul ignore else */ if (has_errors(ˆvalue_9)) { return ctx; } { { const ind = min_indentation(ctx); const [, err_ctx] = add_error(ctx, `Expected \`,\` or indented(>=${ind}) new line or \`${end_symbol}\`.`, ctx.next_token); return err_ctx; } } } } } }; exports.pop_block = pop_block; const push_block = (ctx, end_symbol) => { const { blocks } = ctx; const curr_ind = curr_indentation(ctx); let _do_result; ˆmatch_14: { const ˆvalue_13 = end_symbol; /* istanbul ignore else */ if (ˆvalue_13 === end_of_code) { _do_result = { end_symbol, curr_ind, min_ind: curr_ind }; break ˆmatch_14; } { _do_result = { end_symbol, curr_ind, min_ind: curr_ind + 1 }; break ˆmatch_14; } } const block = _do_result; _do_result = undefined; return { ...ctx, blocks: [block, ...blocks] }; }; exports.push_block = push_block; const update_indentation = ctx => { const { start: { column } } = next_loc(ctx); const { blocks: [block, ...blocks] } = ctx; const indented_block = { ...block, curr_ind: column, min_ind: column }; return { ...ctx, blocks: [indented_block, ...blocks] }; }; exports.update_indentation = update_indentation; const init_indentation = ctx => ({ ...ctx, blocks: [{ end_symbol: end_of_code, min_ind: 0, curr_ind: 0 }] }); exports.init_indentation = init_indentation;