functionalscript
Version:
FunctionalScript is a purely functional subset of JavaScript
90 lines (89 loc) • 1.77 kB
JavaScript
import { cp, join0, range, remove, repeat0, set, str } from "../bnf/func/module.f.js";
// space
const wsNoNewLineItem = () => set(' \t\r');
export const wsNoNewLine0 = () => [
[],
[wsNoNewLineItem, wsNoNewLine0],
];
const wsItem = () => [
[wsNoNewLineItem],
str('\n'),
];
export const ws0 = () => [
[],
[ws1],
];
export const ws1 = () => [
[wsItem, ws0]
];
//
export const json = () => [
[object],
[array],
[number],
[string],
str('true'),
str('false'),
str('null'),
];
//
const separator = () => [[cp(','), ws0]];
// object
const member = () => [[string, ws0, cp(':'), ws0, json, ws0]];
const object = () => [
[cp('{'), ws0, join0(member, separator), cp('}')]
];
// array
const element = () => [
[json, ws0]
];
const array = () => [
[cp('['), ws0, join0(element, separator), cp(']')]
];
// string
const character = () => [
...remove(unicode, [cp('"'), cp('\\')]),
[cp('\\'), escape],
];
const string = () => [[cp('"'), repeat0(character), cp('"')]];
export const unicode = [0x20, 0x10FFFF];
const escape = () => [
...set('"\\/bfnrt'),
[cp('u'), hex, hex, hex, hex] // 117
];
const hex = () => [
[digit],
[range('AF')],
[range('af')],
];
// number
const number = () => [
[uNumber],
[cp('-'), uNumber],
];
const uNumber = () => [
[uint, fraction0, exponent0],
];
const uint = () => [
str('0'),
[range('19'), digits0]
];
export const digit = () => [[range('09')]];
const digits0 = repeat0(digit);
const digits1 = () => [
[digit, digits0]
];
const fraction0 = () => [
[],
[cp('.'), digits1]
];
const exponent0 = () => [
[],
[e, sign, digits1],
];
const e = () => set('eE');
const sign = () => [
[],
[cp('+')],
[cp('-')],
];