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

tscc-compiler

v1.1.3

Published

An LALR(1) compiler compiler written in Typescript

Downloads

5

Readme

tscc-compiler

An LALR(1) compiler generator written in typescript. It generates both the tokenizer and the parser. Currently it can generate parsers written in javascript and typescript. More target languages will be supported in future releases.

Check out wiki for tscc-compiler!

Installation

To use it in node, you can install tscc-compiler via npm:

npm install tscc-compiler -g

Or, to use it in browsers, just download the file tscc.js or tscc.min.js and reference them with a script tag. The latter is a compressed version of the former.

Usage

From command line interface (CLI)

To generate the corresponding parser whose grammar is specified in test.y, for example, you could use the following command:

tscc-compiler test.y

The output files actually depends on the target language. For js and ts, test.ts or test.js would be generated respectively, plus a report file test.output, which contains the lexical DFA tables and LALR parse table.

Options for CLI

| Option | Description| Argument| |:---------------|:----------|:---------------| |-o, --output |Specify the output file to print DFA and parse table|Name of the output file| |-t, --test|Parse (no lexical analyse) the given input string. Parsing process will be printed. See below for explanation.|The string to be parsed.| |-d, --detail-time|Print a detailed list of time costs of different generation phases.|No| |-h, --help|Print help message and quit|No|

From module

This project uses module bundler rollup to create a source file tscc.js that contains the entail source code for tscc-compiler. You may import it as a module by var tscc = require('tscc-compiler'); or include tscc.js with a script tag in browsers. A simple way to invoke tscc-compiler is calling tscc.main with the argument being an object that contains various options. It returns 0 if no error ocurrs, otherwise it returns -1. Options are listed below:

|Option |Required|Type|Description| |:------|:-------|:---|:----------| |inputFile|Yes|string|Name of the input file| |input|Yes|string|Content of the input file| |outputFile|No|string|Name of output file (not to be confused with output parser). If not specified, the output file won't be generated.| |stdout|Yes|tscc.io.OutputStream|An interface object to output all the messages. This object must contain write(s) and writeln(s).| |writeFile|Yes|(path: string, content: string) => any|A callback to write files.| |testInput|No|string|Test input. If specified, the result will be printed. See below for explanation.| |printDetailedTime|Yes|boolean|Whether to print the detailed time cost list.| |printDFA|No|boolean|Whether to print lexical DFA tables in the output file.| |showlah|No|boolean|Whether to show look-ahead tokens of items when printing parse table.| |showFullItemsets|No|boolean|Whether to show full item sets when printing parse table. If not specified or set to false, only kernel items will be printed.|

Where type notations in Typescript are used.

Here's a simple example:

var fs = require('fs');
var tscc = require('tscc-compiler').main;
tscc({
    inputFile: 'example.y',
    input: fs.readFileSync('example.y', 'utf-8'),
    outputFile: 'example.output',
    stdout: {
    	write: function(s){ process.stdout.write(s); },
        writeln: function(s){ console.log(s || ''); }
    },
    writeFile: function(path, content){
        fs.writeFileSync(path, content);
    },
    printDetailedTime: true
});

The module also provides a more flexible way to use it.

Test input

You can give the tscc-compiler a test input string to test if the grammar works. Input string consists of the following two elements, seperated by spaces:

  • An identifier parenthesised by <> is a token, referenced by its name;
  • A raw string is also a token, but referenced by alias;

For example, to test the calculator grammar (see examples/calculator/):

tscc-compiler caculator.y -t "<CONST> + <CONST> * <CONST>"

The output should be:

preparing for test
| <CONST> "+" <CONST> "*" <CONST> 
<CONST> | "+" <CONST> "*" <CONST> 
expr | "+" <CONST> "*" <CONST> 
expr "+" | <CONST> "*" <CONST> 
expr "+" <CONST> | "*" <CONST> 
expr "+" expr | "*" <CONST> 
expr "+" expr "*" | <CONST> 
expr "+" expr "*" <CONST> | 
expr "+" expr "*" expr | 
expr "+" expr | 
expr | 
start | 
accepted!
compilation done in 0.071s
0 warning(s), 0 error(s)

Grammar file

The syntax of the grammar specifying file used by tscc is similiar to yacc. Checkout wiki for tscc-compiler for a specification of grammar file, and examples/ for explicit usages.

Syntax highlight

A syntax highlight mode of grammar file for CodeMirror can be found at web-demo/lib/tscc-highlight-codemirror.js. Feel free to check it out and use it.

Demo

An online demo for tscc-compiler can be found here.

License

MIT.