Skip to content

Loads a ANTLR4 grammar file and compiles it to JavaScript

Notifications You must be signed in to change notification settings

tianyu/antlr4-webpack-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

antlr4-webpack-loader

Loads an ANTLR4 grammar file (.g4) and compiles it to JavaScript.

import {
  MyGrammarLexer,
  MyGrammarParser,
  MyGrammarListener,
  MyGrammarVisitor
} from './MyGrammar.g4';

Configuration

Add the ANTLR4 JavaScript runtime as a dependency: npm install --save antlr4.

Configure webpack.config.js by adding antlr4-webpack-loader:

module.exports = {
  module: {
    rules: [
      { test: /\.g4/, loader: 'antlr4-webpack-loader' }
    ]
  }
};

Usage

A grammar file named MyGrammar.g4 with a rule named myStartRule can be loaded in used in the following way:

import { InputStream, CommonTokenStream } from 'antlr4/index';
import { MyGrammarLexer, MyGrammarParser, MyGrammarVisitor } from './MyGrammar.g4';

class MyVisitor extends MyGrammarVisitor {

  visitMyStartRule(ctx) { ... }

  ...
}

const input = ...; // Load string content
const lexer = new MyGrammarLexer(new InputStream(input));
const parser = new MyGrammarParser(new CommonTokenStream(lexer));
const result = new MyVisitor().visit(parser.myStartRule());

Releases

No releases published

Packages

No packages published