UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

134 lines (133 loc) 4.45 kB
import { dfa, run, toRange, toUnion } from "./module.f.js"; import { union } from "../types/byte_set/module.f.js"; import { sort, fromEntries } from "../types/object/module.f.js"; import { stringify } from "../json/module.f.js"; import { identity } from "../types/function/module.f.js"; import { toArray } from "../types/list/module.f.js"; import { stringToList } from "../text/utf16/module.f.js"; const stringifyIdentity = stringify(identity); const buildDfa = () => { const lowercaseAlpha = toRange('az'); const uppercaseAlpha = toRange('AZ'); const alpha = union(lowercaseAlpha)(uppercaseAlpha); const idSymbol = toUnion('_$'); const idBegin = union(alpha)(idSymbol); const digit = toRange('09'); const idNext = union(idBegin)(digit); const dot = toUnion('.'); const grammar = [ ['', digit, 'int'], ['int', digit, 'int'], ['', digit, 'floatBegin'], ['floatBegin', digit, 'floatBegin'], ['floatBegin', dot, 'floatDot'], ['floatDot', digit, 'float'], ['float', digit, 'float'], ['', idBegin, 'id'], ['id', idNext, 'id'] ]; return dfa(grammar); }; export default { dfa: () => { const dfa = buildDfa(); const entries = Object.entries(dfa); const sortedEntries = sort(entries); const obj = fromEntries(sortedEntries); const result = stringifyIdentity(obj); const expectedObj = { '[""]': [ ['[]', 35], ['["id"]', 36], ['[]', 47], ['["floatBegin","int"]', 57], ['[]', 64], ['["id"]', 90], ['[]', 94], ['["id"]', 95], ['[]', 96], ['["id"]', 122] ], '["float"]': [['[]', 47], ['["float"]', 57]], '["floatBegin","int"]': [ ['[]', 45], ['["floatDot"]', 46], ['[]', 47], ['["floatBegin","int"]', 57] ], '["floatDot"]': [['[]', 47], ['["float"]', 57]], '["id"]': [ ['[]', 35], ['["id"]', 36], ['[]', 47], ['["id"]', 57], ['[]', 64], ['["id"]', 90], ['[]', 94], ['["id"]', 95], ['[]', 96], ['["id"]', 122] ], '[]': [] }; const expectedResult = stringifyIdentity(expectedObj); if (result !== expectedResult) { throw result; } }, run: [ () => { const dfa = buildDfa(); const input = stringToList('a1'); const result = stringifyIdentity(toArray(run(dfa)(input))); const expectedOutput = [ '["id"]', '["id"]' ]; const expectedResult = stringifyIdentity(expectedOutput); if (result !== expectedResult) { throw result; } }, () => { const dfa = buildDfa(); const input = stringToList('0.1'); const result = stringifyIdentity(toArray(run(dfa)(input))); const expectedOutput = [ '["floatBegin","int"]', '["floatDot"]', '["float"]' ]; const expectedResult = stringifyIdentity(expectedOutput); if (result !== expectedResult) { throw result; } }, () => { const dfa = buildDfa(); const input = stringToList('//'); const result = stringifyIdentity(toArray(run(dfa)(input))); const expectedOutput = [ '[]', '[]' ]; const expectedResult = stringifyIdentity(expectedOutput); if (result !== expectedResult) { throw result; } }, () => { const dfa = buildDfa(); const input = stringToList('::'); const result = stringifyIdentity(toArray(run(dfa)(input))); const expectedOutput = [ '[]', '[]' ]; const expectedResult = stringifyIdentity(expectedOutput); if (result !== expectedResult) { throw result; } } ] };