npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-antlr4

v0.4.6

Published

Gulp plugin running ANTLR 4

Downloads

13

Readme

gulp-antlr4

Gulp plugin running ANTLR 4

Background

First you should get familiar with ANTLR4 and how to write grammars. This plugin won't save you from getting fluent with that powerhouse of a tool.

But once you've done that, you will be able to make excitingly complex transforms of your files.

ANTLR4 comes with a Javascript runtime, but the documentation on the web is almost exclusively in a JAVA context. This Gulp plugin aims at eliminating all frictions in the Javascript context between your grammars and your intents.

The normal Gulp way is to source your data files and get downstream your translations or your interpretations. Just point the plugin to your custom translator or your custom visitor and it will do the rest.

But the plugin can also recognize your grammar files and generate for you the base parser/lexer files from which you will derive your translator/visitor. You need for that Java installed and the ANTLR4 Jar installed. The plugin will refuse to work if your environment is not properly set up, since if you can't generate anew from your updated grammars, you can't develop any ANTLR4 application anyway.

Usage

Generating a listener parser

Convenience usage, but not the Gulp way. See Using a listener parser for the Gulp way.

import gulp from 'gulp';
import antlr4 from 'gulp-antlr4';

gulp.task('generate-my-listener-files', () => {
  return gulp.src('path/to/MyGrammar.g4') // Relies on .g4 extension!
    .pipe(antlr4('output-dir'));// Where to put the generated Parser files
}); // Streams down the unchanged grammars

Generating a visitor parser

Convenience usage, but not the Gulp way. See Using a visitor parser for the Gulp way.

import gulp from 'gulp';
import antlr4 from 'gulp-antlr4';

gulp.task('generate-my-visitor-files', () => {
  return gulp.src('path/to/MyGrammar.g4') // Relies on .g4 extension!
    .pipe(antlr4({
      parserDir: 'output-dir', // Where to put the generated Parser files
      visitor: true,
    })); // Streams down the unchanged grammars
});

Using a listener parser

import gulp from 'gulp';
import antlr4 from 'gulp-antlr4';

gulp.task('translate-my-files', () => {
  return gulp.src('data-glob/**/*') // If the glob contains .g4 files,
  // they will be processed first and the data files buffered, then the new
  // generated translator will process them
    .pipe(antlr4({
      grammar: 'MyGrammar', // Stem for all the generated Parser file names
      parserDir: 'some-dir', // Where to find/put the generated Parser files
      listener: 'MyListener', // Your custom child of MyGrammarListener
      listenerDir: 'another-dir', // Where to find the above; may be
        // omitted if it is the same as parserDir
      rule: 'init', // Starting rule for parsing data files
    })); // Streams down the translated data files
});

Using a visitor parser

import gulp from 'gulp';
import antlr4 from 'gulp-antlr4';

gulp.task('interprete-my-files', () => {
  return gulp.src('data-glob/**/*') // If the glob contains .g4 files,
  // they will be processed first and the data files buffered, then the new
  // generated interpreter will process them
    .pipe(antlr4({
      grammar: 'MyGrammar', // Stem for all the generated Parser file names
      parserDir: 'some-dir', // Where to find/put the generated Parser files
      visitor: 'MyVisitor', // Your custom child of MyGrammarVisitor
      visitorDir: 'another-dir', // Where to find the above; may be
        // omitted if it is the same as parserDir
      rule: 'init', // Starting rule for parsing data files
    })); // Streams down the interpretation of the data files, e.g. the results
    // of a series of arithmetical operations if your interpreter is a
    // calculator of some sort
});

Real life Gulpfile example

import gulp from 'gulp';
import antlr4 from 'gulp-antlr4';

const grammarGlob = [
  'src/static/antlr4/grammars/**/*.g4'
];
const parserDir = 'src/static/antlr4/parsers';
const dataGlob = [
  'src/static/data/**/*.*'
];
const grammar = 'Calc';
const rule = 'prog';
const listener = 'Translator';
const listenerDir = 'build/src/static/antlr4/custom';
const visitor = 'Interpreter';
const visitorDir = 'build/src/static/antlr4/custom';
const outputDir = 'src/static/results';

export const makeParser = () => {
  if (require && require.cache) {
    // Remove parser files from require cache
    Object.keys(require.cache).filter(key => {
      return key.includes(parserDir) ||
        key.includes(listenerDir) ||
        key.includes(visitorDir);
    }).forEach(key => {
      delete require.cache[key];
    });
  }

  return gulp.src(grammarGlob)
    .pipe(antlr4({
      parserDir,
      listener: true,
      visitor: true
    }));
};

export const translate = () => {
  return gulp.src(dataGlob)
    .pipe(antlr4({
      grammar, parserDir, listener, listenerDir, rule,
    }))
    .pipe(gulp.dest(outputDir));
};

gulp.task('translate', gulp.series(makeParser, translate));

export const calculate = () => {
  return gulp.src(dataGlob)
    .pipe(antlr4({
      grammar, parserDir, visitor, visitorDir, rule,
    }))
    .pipe(gulp.dest(outputDir));
};

gulp.task('calculate', gulp.series(makeParser, calculate));

License

gulp-antlr4 is MIT licensed.

© 2017-2019 Jason Lenoble