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