• Stars
    star
    625
  • Rank 71,486 (Top 2 %)
  • Language
    TypeScript
  • License
    Other
  • Created almost 8 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Optimized TypeScript target for ANTLR 4

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.

๐Ÿ’ก 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
  1. Install antlr4ts-cli as a development dependency using your preferred package manager.
npm install antlr4ts-cli --save-dev
yarn add -D antlr4ts-cli
  1. Add a grammar to your project, e.g. path/to/MyGrammar.g4

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

    "scripts": {
      // ...
      "antlr4ts": "antlr4ts -visitor path/to/MyGrammar.g4"
    }
    
  3. 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.

    // ...
    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)

More Repositories

1

antlr4cs

The original, highly-optimized C# Target for ANTLR 4
Java
437
star
2

JavaForVS

Java Language Support extension for Visual Studio
C#
110
star
3

LangSvcV2

"Language Service V2" reference repository
C#
83
star
4

antlr4

The highly-optimized fork of ANTLR 4 (see README)
Java
73
star
5

ReferenceAssemblyAnnotator

IL weaver to add nullability annotations to .NET reference assemblies
C#
71
star
6

NOpenCL

.NET wrapper for OpenCL with abstraction
C#
63
star
7

antlrworks2

Tunnel Vision Labs' ANTLRWorks 2 IDE
Java
47
star
8

dotnet-trees

Efficient implementations of standard .NET collection interfaces using tree data structures
C#
44
star
9

antlr4-grammar-postgresql

An ANTLR 4 lexer for PostgreSQL
ANTLR
44
star
10

dotnet-threading

Utility library for writing asynchronous code targeting .NET 3.5 and newer
C#
33
star
11

MouseNavigation

A Visual Studio 2012+ extension to add support for mouse navigation buttons (back and forward)
C#
31
star
12

dotnet-uritemplate

A .NET library supporting RFC 6570 URI Templates (Level 4)
C#
23
star
13

dotnet-compatibility

A binary compatibility checking library for multiple versions of .NET assemblies
C#
20
star
14

InheritanceMargin

Show inheritance information in the glyph margin
C#
19
star
15

dotnet-httpclient35

A complete port of Mono's System.Net.Http assembly to .NET 3.5
C#
19
star
16

MPFProj10

Managed Package Framework for Visual Studio 2010 - 2013
C#
18
star
17

FindInSolutionExplorer

Adds the Find in Solution Explorer command to the context menu of open document tabs.
C#
16
star
18

goworks

Tunnel Vision Labs' GoWorks IDE
Java
16
star
19

vsxdeps

A collection of NuGet package specifications for Visual Studio reference assemblies
PowerShell
15
star
20

DebugCommandLine

A Visual Studio extension to quickly switch between recently used debug command lines
C#
14
star
21

vsbase

Base framework for Visual Studio extension development
C#
13
star
22

OpenInExternalBrowser

Visual Studio extension to open hyperlinks in an external browser instead of the in-process browser
C#
13
star
23

dotnet-collections

A complete build of System.Collections.Immutable for .NET 2.0+
C#
13
star
24

JustMyCodeToggle

Adds the Just My Code command button to Visual Studio
C#
12
star
25

java-threading

A port of Microsoft/vs-threading to Java 8
Java
10
star
26

WpfGraphing

WPF rendering engine for GraphViz
C#
8
star
27

MouseFastScroll

Mouse Fast Scroll extension for Visual Studio 2012+
C#
7
star
28

VsixWizardSample

Example Visual Studio Extension which includes a project template and a custom wizard
C#
7
star
29

nasmjit

C++
7
star
30

java-immutable

A port of System.Collections.Immutable to Java
Java
5
star
31

SHFB

This is an unofficial fork of the Sandcastle Help File Builder project, with the Git history manually constructed from the past project releases
C#
4
star
32

PresentationMode

A presentation mode for Visual Studio
C#
3
star
33

NuGetBuildTasks

MSBuild integration for generating NuGet packages from a manifest
C#
2
star