Schach - Parsing

lsbasi_part7_astprecedence_02.png


Schach - Parsing | v1.1.0 | by Masked​


Introduction

This is a core library for text parsing. This script does not provide any functionality by itself, only functions that ought to be used by other scripts to make sense of generic user input.

For more details on motivation and advantages, see my previous post on it.


Instructions

Please read the online documentation for details on the available functions.

Here goes a quick usage example:

JavaScript:
const { expression, evaluate } = Schach.Parsing.Arithmetic;

const { parsed: expr } = expression().run("#x^4 - log2(#y)")
evaluate(expr, { variables: { x: 5, y: 32 } }) // returns 5^4 - log2(32) = 625 - 5 = 620


Download: Github


Source Code:
Github


License:
Zlib
Sorry for double posting, but I updated the plugin to v1.1.

There was an issue with the model for the operator expression on the arithmetic parser that made it unserializable. The update breaks compatibility with v1.0 if you depend directly on the expression model, so if that's the case please update the plugin.

Also, I took some time to write a more intricate usage example, this time for parsing XML-like strings:
JavaScript:
const { Parser, pure, many, many1 } = Schach.Parsing;
const { number } = Schach.Parsing.Arithmetic;
const { char, predicate, spaces, string } = Schach.Parsing.Text;

const ESCAPE = {
    'b': "\b",
    'f': "\f",
    'n': "\n",
    'r': "\r",
    't': "\t",
    'v': "\v",
    "'": "'",
    '"': '"',
    '\\': '\\'
};

const escapeSequence = char("\\")
    .dropThen(
        predicate(c => String.fromCodePoint(c) in ESCAPE, 'valid escape character')
        .map(String.fromCodePoint)
        .map(c => ESCAPE[c]));

const stringValue = char('"')
    .dropThen(
        many(
            escapeSequence
            .or(
                predicate(c => c != '"'.codePointAt(0) && c != '\\'.codePointAt(0), 'not " or \\')
                .map(String.fromCodePoint))))
    .thenDrop(char('"'))
    .map(l => l.join(''));

const value = stringValue.or(number())
    .mapError(({ actual }) => ({ actual, expected: "string or number" }));

const property = () =>
    many1(
        predicate(c => c != 32 && c != '='.codePointAt(0), 'neither space or =')
        .map(String.fromCodePoint))
    .map(l => l.join(''))
    .thenDrop(spaces())
    .thenDrop(char('='))
    .thenDrop(spaces())
    .zip(value)
    .map(([name, value]) => ({ name, value }));

const openTag =
    char('<')
    .dropThen(
        many1(
            predicate(
                c => c != 32 && c != '>'.codePointAt(0), 'neither space or >')
            .map(String.fromCodePoint))
        .map(l => l.join('')))
    .thenDrop(spaces())
    .zip(many(property().thenDrop(spaces())))
    .thenDrop(char('>'))
    .map(([tag, props]) => ({ tag, props }));

const closeTag = (tag) => string(`</${tag}>`);

const myXML = Parser.of(() => {
    const tag =
        openTag.flatMap(({ tag, props }) =>
            myXML().or(pure(undefined))
            .map(content => ({ tag, props, content }))
            .thenDrop(closeTag(tag)));
    return many1(tag.or(value));
});

And here's an example running it:
JavaScript:
myXML().run('<abc><def ghi="asd">123</def></abc>')

I forgot to make it support linebreaks, sorry haha

And the corresponding output:
JavaScript:
[{
    "tag": "abc",
    "props": [],
    "content": [{
        "tag": "def",
        "props": [{
            "name": "ghi",
            "value": "asd"
        }],
        "content": [123]
    }]
}]

It might look a bit confusing at first, but it's basically a complete XML parser in less than 70 lines of code! It also uses most of the core functionality the plugin provides, so it makes a good reference if you need to do something with it.

Original thread: https://forums.rpgmakerweb.com/threads/schach-parsing.128288/