0.5.0-alpha.4 • Published 5 years ago

antlr4ts v0.5.0-alpha.4

Weekly downloads
34,572
License
BSD-3-Clause
Repository
github
Last release
5 years ago

antlr4ts - TypeScript/JavaScript target for ANTLR 4

Join the chat at https://gitter.im/tunnelvisionlabs/antlr4ts

Build status

License

Overview

  • Releases: See the GitHub Releases page for release notes and links to the distribution
  • Feedback: Use GitHub Issues for issues (bugs, enhancements, features, and questions)

Requirements

This project has separate requirements for developers and end users.

:bulb: The requirements listed on this page only cover user scenarios - that is, scenarios where developers wish to use ANTLR 4 for parsing tasks inside of a TypeScript application. If you are interested in contributing to ANTLR 4 itself, see CONTRIBUTING.md for contributor documentation.

End user requirements

Parsers generated by the ANTLR 4 TypeScript target have a runtime dependency on the antlr4ts package. The package is tested and known to work with Node.js 6.7.

Development requirements

The tool used to generate TypeScript code from an ANTLR 4 grammar is written in Java. To fully utilize the ANTLR 4 TypeScript target (including the ability to regenerate code from a grammar file after changes are made), a Java Runtime Environment (JRE) needs to be installed on the developer machine. The generated code itself uses several features new to TypeScript 2.0.

  • Java Runtime Environment 1.6+ (1.8+ recommended)
  • TypeScript 2.0+

Getting started

  1. Install antlr4ts as a runtime dependency using your preferred package manager.

    npm install antlr4ts --save
    yarn add antlr4ts
  2. Install antlr4ts-cli as a development dependency using your preferred package manager.

    npm install antlr4ts-cli --save-dev
    yarn add -D antlr4ts-cli
  3. Add a grammar to your project, e.g. path/to/MyGrammar.g4

  4. Add a script to package.json for compiling your grammar to TypeScript

    "scripts": {
      // ...
      "antlr4ts": "antlr4ts -visitor path/to/MyGrammar.g4"
    }
  5. Use your grammar in TypeScript

    import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts';
    
    // Create the lexer and parser
    let inputStream = new ANTLRInputStream("text");
    let lexer = new MyGrammarLexer(inputStream);
    let tokenStream = new CommonTokenStream(lexer);
    let parser = new MyGrammarParser(tokenStream);
    
    // Parse the input, where `compilationUnit` is whatever entry point you defined
    let 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 { MyGrammarParserListener } from './MyGrammarParserListener'
    import { FunctionDeclarationContext } from './MyGrammarParser'
    import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker'
class EnterFunctionListener implements MyGrammarParserListener {
  // Assuming a parser rule with name: `functionDeclaration`
  enterFunctionDeclaration(context: FunctionDeclarationContext) {
    console.log(`Function start line number ${context._start.line}`)
    // ...
  }

  // other enterX functions...
}

// Create the listener
const listener: MyGrammarParserListener = new EnterFunctionListener();
// Use the entry point for listeners
ParseTreeWalker.DEFAULT.walk(listener, tree)
```

###### Visitor Approach

Note you must pass the `-visitor` flag to antlr4ts to get the generated visitor file.

```typescript
// ...
import { MyGrammarParserVisitor } from './MyGrammarParserVisitor'
import { AbstractParseTreeVisitor } from 'antlr4ts/tree/AbstractParseTreeVisitor'

// Extend the AbstractParseTreeVisitor to get default visitor behaviour
class CountFunctionsVisitor extends AbstractParseTreeVisitor<number> implements MyGrammarParserVisitor<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 countFunctionsVisitor = new CountFunctionsVisitor()
// Use the visitor entry point
countFunctionsVisitor.visit(tree)
```
@kpainter/antlr4-c3@zakjan/gget@suited-grpc/generator@dev-sam/samlang-parser-generated-ts@junkio/brkt@kipper/baseralph-parser-tsssralph-parser-ttralph-parser-ttt@vandycarlos/tiny-lang@everything-registry/sub-chunk-1145aurebeshcel-in-jstblktest-monaco-editorxdoc-parsersubjektstack-packerspark-sql-language-serversuscipitrerumswql2cypherthrift-parser-tsthrift-parser-typescriptvrtxvb6-antlr4veloce-sfdx-v3vertexqlvf-parser@coalescesoftware/shared@cubejs-backend-json-clone/schema-compiler@cubejs-backend/schema-compiler@d1ll0n/solidity-parser@borealisswap/borealis-swap-lib@xon/astjabuti-dsl-language-antlr-v3jabuti-dsl-language-antlrjonscriptjava-astjavadoc_evaluatorjsoniq-language-serverjust-mcflash-compilertmdl-language-servicestmdl-parser-typescriptsole-langtybscrits-antlr4-scannertvm-solidity-parsertw-excel-formula-monacotime-analyzertgr-wdn-query-langtoybox-formulatodolangeditor@apexdevtools/apex-ls@apexdevtools/apex-parser@apexdevtools/vf-parser@apollo21/firewall-rule-parser@amplitude/ampli@atlaskit/jql-ast@atlaskit/jql-autocomplete@atlaskit/jql-editor@atlaskit/jql-parser@atlassianlabs/jql-ast@atlassianlabs/jql-autocomplete@atlassianlabs/jql-editor@atlassianlabs/jql-parser@atala/prism-wallet-sdk@atomist/antlr@aurelianoa/metadataupdatable@alexa/acdl@blending_jake/jtools@bz8085/schema-compiler-with-json@chatbot-mgap/chat_response_parser@chrimc62/adaptive-expressions@chrimc62/botbuilder-lg@christopheranderson/botbuilder-lg@christopheranderson/botframework-expressions@bowogfc/bbpolymer@designliquido/deleguayilia_editoryajson-streamviking-langvipul-dashboardunicoen.tsts-aspcore2-parserts-mysql-parserts-mysql-autocompleteuparser_jsuqm-files-parsers@hyperledger/identus-edge-agent-sdk@heswell/datagrid-parsers@higrow/raql.js@humafara/evmhuma@flxbl-io/sfp@fwio/ts-mysql-parser@fornax-ai/prompt-parser@informalsystems/quint@infinitebrahmanuniverse/nolb-antl@innoswap/core@latticexyz/solidity-parser
0.5.0-alpha.4

5 years ago

0.5.0-dev

5 years ago

0.5.0-alpha.3

6 years ago

0.5.0-alpha.2

6 years ago

0.5.0-alpha.1

7 years ago

0.5.0-burt.0

8 years ago

0.4.1-alpha.0

8 years ago

0.4.0-burt.2

8 years ago

0.4.0-burt.1

8 years ago

0.4.0-burt

8 years ago

0.4.0-alpha.4

8 years ago

0.4.0-alpha.3

8 years ago

0.4.0-alpha.2

9 years ago

0.4.0-alpha.1

9 years ago