functionalscript
Version:
FunctionalScript is a purely functional subset of JavaScript
242 lines (241 loc) • 6.78 kB
JavaScript
// JSON: https://www.json.org/json-en.html
{
// null, [0x09, 0x0A], 0x0D, 0x20
const ws = () => ({ or: [
[],
['\t', ws], // 0x09
['\n', ws], // 0x0A
['\r', ws], // 0x0D
[' ', ws], // 0x20
] });
// null, 0x2B, 0x2D
const sign = { or: [
[],
'+', // 0x2B
'-', // 0x2D
] };
// null, [0x30, 0x39]
const digits1 = () => ({ or: [
[],
digits // [0x30, 0x39]
] });
// [0x30, 0x39]
const digits = () => [digit, digits1];
// null, 0x45, 0x65
const exponent = { or: [
[],
['E', sign, digits], // 0x45
['e', sign, digits], // 0x65
] };
// null, 0x2E
const fraction = { or: [
[],
['.', digits] // 0x2E
] };
// [0x31, 0x39]
const onenine = [0x31, 0x39];
// [0x30, 0x39]
const digit = { or: [
'0', // 0x30
onenine, // [0x31, 0x39]
] };
// null, 0x2C
const members2 = () => ({ or: [
[],
[',', ws, members1], // 0x2C
] });
// 0x22
const members1 = () => [member1, members2];
// 0x22, 0x7D
const object1 = { or: [
[members1, '}'], // 0x22
'}', // 0x7D
] };
// 0x7B
const object = ['{', ws, object1];
// 0x22, 0x2D, [0x30, 0x39], 0x5B, 0x5D, 0x66, 0x6E, 0x74, 0x7B
const array1 = () => ({ or: [
[element1, ']'], // 0x22, 0x2D, [0x30, 0x39], 0x5B, 0x66, 0x6E, 0x74, 0x7B
']', // 0x5D
] });
// 0x5B
const array = ['[', ws, array1];
// [0x30, 0x39], [0x41, 0x46], [0x61, 0x66]
const hex = { or: [
digit, // [0x30, 0x39]
[0x41, 0x46], // A..F
[0x61, 0x66], // a..f
] };
// 0x22, 0x2F, 0x5C, 0x62, 0x66, 0x6E, 0x72, [0x74, 0x75]
const escape = { or: [
'"', // 0x22
'/', // 0x2F
'\\', // 0x5C
'b', // 0x62
'f', // 0x66
'n', // 0x6E
'r', // 0x72
't', // 0x74
['u', hex, hex, hex, hex] // 0x75
] };
// [0x20, 0x21], [0x23, 0x10FFFF]
const character = { or: [
[0x20, 0x21], // exclude '"' 0x22
[0x23, 0x5B], // exclude '\' 0x5C
[0x5D, 0x10FFFF], //
['\\', escape], // 0x5C
] };
// null, [0x20, 0x21], [0x23, 0x10FFFF]
const characters = () => ({ or: [
[],
[character, characters]
] });
// 0x22
const string = ['"', characters, '"'];
// [0x30, 0x39]
const integer1 = { or: [
'0',
[onenine, digits1],
] };
// 0x2D, [0x30, 0x39]
const integer = { or: [
['-', integer1], // 0x2D
'0', // 0x30
[onenine, digits1], // [0x31, 0x39]
] };
// 0x2D, [0x30, 0x39]
const number = [integer, fraction, exponent];
// 0x22, 0x2D, [0x30, 0x39], 0x5B, 0x66, 0x6E, 0x74, 0x7B
const value = { or: [
string, // 0x22
number, // 0x2D, [0x30, 0x39]
array, // 0x5B
'false', // 0x66
'null', // 0x6E
'true', // 0x74
object, // 0x7B
] };
// 0x22, 0x2D, [0x30, 0x39], 0x5B, 0x66, 0x6E, 0x74, 0x7B
const element1 = [value, ws];
// [0x09, 0x0A], 0x0D, 0x20, 0x22, 0x2D, [0x30, 0x39], 0x5B, 0x66, 0x6E, 0x74, 0x7B
const element = [ws, element1];
// 0x22
const member1 = [string, ws, ':', element];
const json = element;
}
// serializable
{
//
const element = { id: 'element' };
const ws = { id: 'ws' };
const value = { id: 'value' };
const object = { id: 'object' };
const array = { id: 'array' };
const string = { id: 'string' };
const number = { id: 'number' };
const members = { id: 'members' };
const characters = { id: 'characters' };
const integer = { id: 'integer' };
const fraction = { id: 'fraction' };
const exponent = { id: 'exponent' };
const member = { id: 'member' };
const character = { id: 'character' };
const digit = { id: 'digit' };
const onenine = { id: 'onenine' };
const digits = { id: 'digits' };
const sign = { id: 'sign' };
const escape = { id: 'escape' };
const hex = { id: 'hex' };
const map = {
json: element,
element: [ws, value, ws],
ws: { or: [
[],
[' ', ws],
['\t', ws],
['\n', ws],
['\r', ws],
] },
value: { or: [
object,
array,
string,
number,
'true',
'false',
'null'
] },
object: { or: [
['{', ws, '}'],
['{', members, '}'],
] },
array: { or: [
['[', ws, ']'],
['[', element, ']'],
] },
string: ['"', characters, '"'],
number: [integer, fraction, exponent],
members: { or: [
member,
[member, ',', members],
] },
characters: { or: [
[],
[character, characters]
] },
integer: { or: [
digit,
[onenine, digits],
['-', digit],
['-', onenine, digits],
] },
fraction: { or: [
[],
['.', digits]
] },
exponent: { or: [
[],
['E', sign, digits],
['e', sign, digits],
] },
member: [ws, string, ws, ':', element],
character: { or: [
[0x20, 0x21], // exclude '"' 0x22
[0x23, 0x5B], // exclude '\' 0x5C
[0x5D, 0x10FFFF],
['\\', escape],
] },
digit: { or: [
'0',
onenine,
] },
onenine: [0x31, 0x39],
digits: { or: [
digit,
[digit, digits]
] },
sign: { or: [
[],
'+',
'-',
] },
escape: { or: [
'"',
'\\',
'/',
'b',
'f',
'n',
'r',
't',
['u', hex, hex, hex, hex]
] },
hex: { or: [
digit,
[0x41, 0x46], // A..F
[0x61, 0x66], // a..f
] },
};
const _map = map;
}
export {};