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

jslex

v0.0.1

Published

Javascript Lexical Analyzers Auto Generator

Downloads

14

Readme

JSLex

Javascript Lexical Analyzers("scanners" or "lexers") Generator

See chinese version README here(查看中文版本README)

Install

npm install -g jslex

Usage

jslex lex_file [-t template_file] [-o output_file]

  • lex_file lex file to convert.
  • template_file template file to use. default use nodejs template file, see template/node.tpl.txt
  • output_file generated file to save. default use same filename as lex file.

Example

1.define your lex rules and actions. see demo/find_jslex.lex

$case_ignore    true
$lexer_name     MyLexer

QUOTE           \"
JS              js
LE              lex
KEY_TO_FIND     {JS}_{LE}
OTHER           [\d\D]

$$

QUOTE  {
    this.yygoto(QUOTE_STATE);
}

<QUOTE_STATE> KEY_TO_FIND {
    console.log("Found '" + this.yytxt + "' in quotes at " + this.yyidx);
    this.q_number++;
    this.total_number++;
}

<QUOTE_STATE>QUOTE {
    this.yygoto(DEFAULT);
}

<QUOTE_STATE> OTHER   {
    // do nothing
}

KEY_TO_FIND     {
    console.log("Found '" + this.yytxt + "' at " + this.yyidx);
    this.total_number++;
}

OTHER   {}

$$
$start       {
    console.log("Finding word 'js_lex' with ignore case option.\n");
}
$construct   {
    this.q_number = 0;
    this.total_number = 0;
}
$finish     {
    console.log("\nDone. Found word 'js_lex' " + this.q_number + " times in quotes and " + this.total_number + " total times!")
}
$unknow     {
    console.warn("unknow char:", String.fromCharCode(this.chr));
}
$unmatch    {
    console.warn("unmath char:", String.fromCharCode(this.chr));
}

2.Use command jslex find_jslex.lex, file find_jslex.js will be created. 3.Use command ./find_jslex.js test_find_jslex.txt. content of test_find_jslex.txt is bellow:

Hello everyone, I'm js_Lex.

"You are js_lex?!"

葛

Yes, I'm js_lex, no one else can be JS_LEX.

4.Final, you can see the output is:

Finding word 'js_lex' with ignore case option.

Found 'js_Lex' at 20
Found 'js_lex' in quotes at 38
unknow char: 葛
Found 'js_lex' at 61
Found 'JS_LEX' at 88

Done. Found word 'js_lex' 1 times in quotes and 4 total times!

Lex Rule

Same as classic Lex. JSLex support some pre-defines like regex:

  • \d digit, 0-9
  • \D not digit
  • \s empty char, include space and \f\n\r\t\v
  • \S not empty char
  • \w word, a-zA-Z_
  • \W not word
  • \a alpha letter, a-zA-Z
  • \A not alpha letter
  • \u upper letter, A-Z
  • \U not upper letter
  • \l lower case letter, a-z
  • \L not lower case letter
  • . any char except \n

JSLex also support combine defined rules. surround it by { and }. For example:

DIGIT   \d
INT     {DIGIT}+
FLOAT   {INT}(\.{INT})?

JSLex support state, so it can generate more than just a lexer. See example above.

Argument

you can specify argument at the begining of lex file. jslex support follow arguments:

  • $case_ignore ignore letter lower or upper case, default is false.
  • $lexer_name lexer_name, default is JSLexer
  • $argument usage $argument key value, store key-value pair for later use.
  • $include_dir jslex support multiple files. we will discuss it later.

Multiple Files

JSLex support deal with multiple files. So you can make your project modularization.

//todo: complete doc

Todo

  • Much more functions.
  • Combine with jison, to make it's lex function more strong.(currently jison only support origin javascript RegExp to deal with lex?)