![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
coffee-lex
Advanced tools
Stupid lexer for CoffeeScript.
$ npm install [--save] coffee-lex
The main lex
function simply returns a list of tokens:
import lex from 'coffee-lex';
let source = 'a?(b: c)';
let tokens = lex(source);
// Print each token object.
tokens.forEach(token => console.log(token));
// SourceToken { type: SourceType { name: 'IDENTIFIER' }, start: 0, end: 1 }
// SourceToken { type: SourceType { name: 'EXISTENCE' }, start: 1, end: 2 }
// SourceToken { type: SourceType { name: 'LPAREN' }, start: 2, end: 3 }
// SourceToken { type: SourceType { name: 'IDENTIFIER' }, start: 3, end: 4 }
// SourceToken { type: SourceType { name: 'COLON' }, start: 4, end: 5 }
// SourceToken { type: SourceType { name: 'IDENTIFIER' }, start: 6, end: 7 }
// SourceToken { type: SourceType { name: 'RPAREN' }, start: 7, end: 8 }
// Print tokens along with their source.
tokens.forEach(token =>
console.log(
token.type.name,
JSON.stringify(source.slice(token.start, token.end))
)
);
// IDENTIFIER "a"
// EXISTENCE "?"
// LPAREN "("
// IDENTIFIER "b"
// COLON ":"
// IDENTIFIER "c"
// RPAREN ")"
You can also get more fine control of what you'd like to lex by using the
stream
function:
import { stream, EOF } from 'coffee-lex';
let source = 'a?(b: c)';
let step = stream(source);
let location;
do {
location = step();
console.log(location);
} while (location.type !== EOF);
// SourceLocation { type: SourceType { name: 'IDENTIFIER' }, index: 0 }
// SourceLocation { type: SourceType { name: 'EXISTENCE' }, index: 1 }
// SourceLocation { type: SourceType { name: 'LPAREN' }, index: 2 }
// SourceLocation { type: SourceType { name: 'IDENTIFIER' }, index: 3 }
// SourceLocation { type: SourceType { name: 'COLON' }, index: 4 }
// SourceLocation { type: SourceType { name: 'SPACE' }, index: 5 }
// SourceLocation { type: SourceType { name: 'IDENTIFIER' }, index: 6 }
// SourceLocation { type: SourceType { name: 'RPAREN' }, index: 7 }
// SourceLocation { type: SourceType { name: 'EOF' }, index: 8 }
This function not only lets you control how far into the source you'd like to go, it also gives you information about source code that wouldn't become part of a token, such as spaces.
The official CoffeeScript lexer does a lot of pre-processing, even with
rewrite: false
. That makes it good for building an AST, but bad for
identifying parts of the source code that aren't part of the final AST, such as
the location of operators. One good example of this is string interpolation. The
official lexer turns it into a series of string tokens separated by (virtual)
+
tokens, but they have no reality in the original source code. Here's what
the official lexer generates for "a#{b}c"
:
[ [ 'STRING_START',
'(',
{ first_line: 0, first_column: 0, last_line: 0, last_column: 0 },
origin: [ 'STRING', null, [Object] ] ],
[ 'STRING',
'"a"',
{ first_line: 0, first_column: 0, last_line: 0, last_column: 1 } ],
[ '+',
'+',
{ first_line: 0, first_column: 3, last_line: 0, last_column: 3 } ],
[ '(',
'(',
{ first_line: 0, first_column: 3, last_line: 0, last_column: 3 } ],
[ 'IDENTIFIER',
'b',
{ first_line: 0, first_column: 4, last_line: 0, last_column: 4 },
variable: true ],
[ ')',
')',
{ first_line: 0, first_column: 5, last_line: 0, last_column: 5 },
origin: [ '', 'end of interpolation', [Object] ] ],
[ '+',
'+',
{ first_line: 0, first_column: 6, last_line: 0, last_column: 6 } ],
[ 'STRING',
'"c"',
{ first_line: 0, first_column: 6, last_line: 0, last_column: 7 } ],
[ 'STRING_END',
')',
{ first_line: 0, first_column: 7, last_line: 0, last_column: 7 } ],
[ 'TERMINATOR',
'\n',
{ first_line: 0, first_column: 8, last_line: 0, last_column: 8 } ] ]
Here's what coffee-lex generates for the same source:
[ SourceToken { type: SourceType { name: 'STRING_START' }, start: 0, end: 1 },
SourceToken { type: SourceType { name: 'STRING_CONTENT' }, start: 1, end: 2 },
SourceToken { type: SourceType { name: 'INTERPOLATION_START' }, start: 2, end: 4 },
SourceToken { type: SourceType { name: 'IDENTIFIER' }, start: 4, end: 5 },
SourceToken { type: SourceType { name: 'INTERPOLATION_END' }, start: 5, end: 6 },
SourceToken { type: SourceType { name: 'STRING_CONTENT' }, start: 6, end: 7 },
SourceToken { type: SourceType { name: 'STRING_END' }, start: 7, end: 8 } ]
MIT
FAQs
Stupid lexer for CoffeeScript.
The npm package coffee-lex receives a total of 7,984 weekly downloads. As such, coffee-lex popularity was classified as popular.
We found that coffee-lex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.