Socket
Socket
Sign inDemoInstall

escript-antlr4

Package Overview
Dependencies
4
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    escript-antlr4

An Escript lexer & parser that provides both visitor and listener patterns to traverse the parse tree.


Version published
Maintainers
1
Install size
451 kB
Created

Readme

Source

escript-antlr4

Getting started

  1. Install escript-antlr4 and antlr4ts as dependencies using your preferred package manager.
npm install escript-antlr4 antlr4ts --save
yarn add escript-antlr4 antlr4ts
  1. Use your grammar in TypeScript (or JavaScript)
import { EscriptLexer, EscriptParser } from 'escript-antlr4';
import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts';

const program = `
program main(arg0, arg1)
    Print(arg0);
endprogram

function f1() endfunction
function f2() endfunction
`;

const inputStream = new ANTLRInputStream(program);

const lexer = new EscriptLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new EscriptParser(tokenStream);

/**
 * Parse the input:
 * 	- A source file (.src or .inc) has entry point `compilationUnit`.
 *  - A module file (.em) has entry point `moduleUnit()`.
 */

const tree = parser.compilationUnit();

The two main ways to inspect the tree are by using a listener or a visitor, you can read about the differences between the two here.

Listener Approach
// ...
import { EscriptParserListener, ProgramDeclarationContext } from 'escript-antlr4';
import { ParseTreeWalker } from 'antlr4ts/tree';

class EnterProgramListener implements EscriptParserListener {
    enterProgramDeclaration(context: ProgramDeclarationContext) {
        console.log(`Program name: ${context.IDENTIFIER().text}`);
        // ...
    }

    // other enterX functions...
}

// Create the listener
const listener: EscriptParserListener = new EnterProgramListener();
// Use the entry point for listeners
ParseTreeWalker.DEFAULT.walk(listener, tree);
Visitor Approach
// ...
import { EscriptParserVisitor, FunctionDeclarationContext } from 'escript-antlr4';
import { AbstractParseTreeVisitor } from 'antlr4ts/tree';

// Extend the AbstractParseTreeVisitor to get default visitor behaviour
class CountElementsVisitor
    extends AbstractParseTreeVisitor<number>
    implements EscriptParserVisitor<number> {

    defaultResult() {
        return 0;
    }

    aggregateResult(aggregate: number, nextResult: number) {
        return aggregate + nextResult;
    }

    visitFunctionDeclaration(context: FunctionDeclarationContext): number {
        return 1 + super.visitChildren(context);
    }
}

// Create the visitor
const countElementsVisitor = new CountElementsVisitor();
// Use the visitor entry point
const count = countElementsVisitor.visit(tree);
console.log(`There are ${count} function declarations`);

FAQs

Last updated on 20 Feb 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc