Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const {
- char,
- mapTo,
- pipeParsers,
- parse,
- sequenceOf,
- str,
- letters,
- choice,
- possibly,
- sepBy,
- many,
- many1,
- anythingExcept,
- digits,
- whitespace,
- between,
- anyOfString,
- recursiveParser
- } = require('arcsecond');
- const tabspace = many (anyOfString (' \t')) .map (x => x.join(''));
- const whitespaceSurrounded = parser => between (whitespace) (whitespace) (parser);
- const tabspaceSurrounded = parser => between (tabspace) (tabspace) (parser);
- const spaceSurrounded = parser => between (char (' ')) (char (' ')) (parser);
- const parseValue = recursiveParser(() => choice ([
- parseWord,
- parseBlock,
- ]));
- const parseWord = pipeParsers([
- sequenceOf ([
- letters
- ]),
- mapTo(x => x.join())
- ]);
- const lineSeparated = sepBy (tabspaceSurrounded (char ('\n')));
- const semicolonSeparated = sepBy (tabspaceSurrounded (char (';')));
- const parseBlock =
- pipeParsers ([
- between
- (whitespaceSurrounded (char ('(')))
- (whitespaceSurrounded (char (')')))
- (lineSeparated (
- semicolonSeparated (
- parseValue
- )
- )),
- mapTo (function(x) {
- console.log('array: ' + x);
- return x;
- })
- ]);
- let result = parse (parseValue) (`(
- a;b
- (
- c;d
- ))`)
- console.log('result.value: ' + JSON.stringify(result.value, null, 2));
- if (false) {
- result = parse (parseValue) (`(
- a;b e
- (c;
- d
- ))`)
- console.log('result.value: ' + JSON.stringify(result.value, null, 2));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement