UNPKG

derw

Version:

An Elm-inspired language that transpiles to TypeScript

146 lines (145 loc) 4.35 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.prefixLines = prefixLines; exports.destructureLength = destructureLength; exports.patternGapPositions = patternGapPositions; exports.patternHasGaps = patternHasGaps; const List = __importStar(require("../stdlib/List")); function prefixLines(body, indent) { function lineFn(line) { if (line.trim() === "") { return line; } else { return `${" ".repeat(indent)}${line}`; } } return (function (y) { return y.join("\n"); })(List.map(lineFn, body.split("\n"))); } function partLength(part, index) { switch (part.kind) { case "Destructure": { return 1; } case "StringValue": { return 1; } case "FormatStringValue": { return 1; } case "EmptyList": { return 0; } case "Value": { if (index === 0) { return 1; } else { return 0; } ; } } } function destructureLength(pattern) { return List.foldl(function (x, y) { return x + y; }, 0, List.indexedMap(partLength, pattern.parts)); } function GapPositionInfo(args) { return { ...args, }; } function patternGapPositions(pattern) { function folder(part, info) { switch (part.kind) { case "Value": { if (info.i > 0) { return { i: info.i - 1, positions: [info.i, ...info.positions], }; } else { return { ...info, i: info.i - 1 }; } ; } default: { return { ...info, i: info.i - 1 }; } } } return (function (y) { return y.positions; })(List.foldr(folder, { i: pattern.parts.length - 1, positions: [], }, pattern.parts)); } function patternHasGaps(pattern) { function hasGap(index, xs) { switch (xs.length) { case xs.length: { if (xs.length >= 1) { const [x, ...ys] = xs; switch (x.kind) { case "Value": { if (index > 0) { return true; } else { return hasGap(index + 1, ys); } ; } default: { return hasGap(index + 1, ys); } } ; } } default: { return false; } } } return hasGap(0, pattern.parts); }