Skip to content
/ parser Public

A robust parser that performs syntactic analysis and constructs an Abstract Syntax Tree (AST).

License

Notifications You must be signed in to change notification settings

je-es/parser

Repository files navigation


parser

A robust tool for syntactic analysis.
It consumes tokens generated by a lexer and constructs an AST for interpretation or compilation.

line
  • Install

    npm install @je-es/parser
    import * as Parser from "@je-es/parser";

    line
  • Usage

    // [1] create parser rules
    const parser_rules : Parser.Rules = [
    
        Parser.createRule('Root',
            Parser.oneOrMore(Parser.rule('Stmt')),
            {
                build: (data: Parser.Result) => {
                    const arr   = data.getRepeatResult()!;
                    const stmts = arr.map((x) => x.getCustomData()! as AST.StmtNode);
                    return Parser.Result.createAsCustom('passed', 'root', stmts, data.span);
                }
            }
        ),
    
        Parser.createRule('Ident',
            Parser.token('ident'),
            {
                build: (data: Parser.Result) => {
                    const identResult = data.getTokenData()!;
    
                    return Parser.Result.createAsCustom('passed', 'ident',
                        AST.IdentNode.create( identResult.span, identResult.value!, false),
                        data.span
                    );
                },
    
                errors: [ Parser.error(0, "Expected identifier") ]
            }
        ),
    
        // Include required rules
        ...Type,
        ...Expr,
        ...Stmt,
    ];
    // [2] create parser settings
    const parser_settings : Parser.ParserSettings = {
        startRule       : 'Root',
        errorRecovery   : { mode: 'resilient', maxErrors: 99 },
        ignored         : ['ws', 'comment'],
        debug           : 'off',
        maxDepth        : 9999,
        maxCacheSize    : 1024, // 1GB
    };
    // [3] parse tokens using your rules
    const parser_result = Parser.parse([<tokens>], parser_rules, parser_settings);
    
    // Hint: to get tokens use `@je-es/lexer`

    line
  • Related


line

About

A robust parser that performs syntactic analysis and constructs an Abstract Syntax Tree (AST).

Topics

Resources

License

Stars

Watchers

Forks