Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcisbee/nanolex
🪁 Parser grammar builder
https://github.com/marcisbee/nanolex
grammar lexer parser-library tokenizer
Last synced: about 2 months ago
JSON representation
🪁 Parser grammar builder
- Host: GitHub
- URL: https://github.com/marcisbee/nanolex
- Owner: Marcisbee
- License: mit
- Created: 2024-02-27T16:56:05.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-10-14T10:05:03.000Z (3 months ago)
- Last Synced: 2024-11-12T19:43:49.321Z (about 2 months ago)
- Topics: grammar, lexer, parser-library, tokenizer
- Language: TypeScript
- Homepage:
- Size: 45.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nanolex
@TODO readme
Very experimental parser building library
## Usage
```sh
npm i nanolex
``````ts
import { createToken, EOF, getComposedTokens, nanolex } from "nanolex";// Define tokens
const Whitespace = createToken(/[ \t\n\r]+/, "WhiteSpace");
const LParen = createToken("(");
const RParen = createToken(")");
const Comma = createToken(",");
const Integer = createToken(/-?\d+/, "Integer");
const Identifier = createToken(/\w+/, "Identifier");// List of tokenizable tokens
const tokens = getComposedTokens([
Whitespace,
LParen,
RParen,
Comma,
Integer,
]);// Define the usage of your parser
export function parser(value: string) {
// Initiate grammar
const {
consume,
zeroOrOne,
zeroOrMany,
zeroOrManySep,
and,
or,
patternToSkip,
throwIfError,
} = nanolex(value, tokens);// Write patterns to skip, in this case ignore whitespace
// Pattern is just grammar
patternToSkip(consume(Whitespace));// Write parser grammar patterns here
function FUNCTION() {
return and([
consume(Identifier),
consume(LParen),
PARAMS,
consume(RParen),
], transform)();function transform([name, _, params]) {
return {
type: "function",
name,
params,
};
}
}function PARAMS() {
return zeroOrManySep(
VALUE,
consume(Comma),
)();
}function VALUE() {
return or([
consume(Integer, Number),
FUNCTION,
])();
}// Run the grammar
const [output] = throwIfError(and([FUNCTION, consume(EOF)]));return output;
}
``````ts
import { parser } from "./parser.ts";parser("SUM(1, SUM(2, 3))");
/*
{
"type": "function",
"name": "SUM",
"params": [
1,
{
"type": "function",
"name": "SUM",
"params": [2, 3]
}
]
}
*/
```