@fink/larix
Version:
A parser for generating fink's AST.
90 lines (84 loc) • 2.06 kB
JavaScript
import { advance } from "@fink/prattler/parser.js";
import { add_named_block, indented_block } from "../block/init.js";
import { single_expression } from "../block/expr.js";
export const get_condition = ctx => {
const [left, next_ctx] = single_expression(ctx, 0);
{
const ˆvalue_1 = left;
/* istanbul ignore else */
if (ˆvalue_1 != null) {
const {
type: ˆp_3,
op: ˆp_4
} = ˆvalue_1;
/* istanbul ignore else */
if (ˆp_3 === `block`)
/* istanbul ignore else */
if (ˆp_4 === `else`) {
return [left, next_ctx];
}
}
{
return [left, advance(next_ctx)];
}
}
};
export const get_result = ctx => {
const [{
exprs,
comments,
loc
}, next_ctx] = indented_block(ctx);
return [{
type: `block`,
exprs,
comments,
loc
}, next_ctx];
};
export const cond_result_expr = ctx => {
// [{comments, ...left}, result_ctx] = get_condition ctx
const [left, result_ctx] = get_condition(ctx);
{
const ˆvalue_5 = left;
/* istanbul ignore else */
if (ˆvalue_5 != null) {
const {
type: ˆp_7,
op: ˆp_8
} = ˆvalue_5;
/* istanbul ignore else */
if (ˆp_7 === `block`)
/* istanbul ignore else */
if (ˆp_8 === `else`) {
return [left, result_ctx];
}
}
{
{
const [right, next_ctx] = get_result(result_ctx);
const {
start
} = left.loc;
const {
end
} = right.loc;
// [{type: 'match:expr', left, right, loc: {start, end}, comments}, next_ctx]
return [{
type: `match:expr`,
left,
right,
loc: {
start,
end
}
}, next_ctx];
}
}
}
};
export const add_conditionals = ctx => {
let ˆpipe_result_9 = ctx;
ˆpipe_result_9 = add_named_block(`match`, `block`, cond_result_expr)(ˆpipe_result_9);
return ˆpipe_result_9 = add_named_block(`else`)(ˆpipe_result_9);
};