Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
The moo npm package is a tokenizer for compilers and other tools that operate on code. It takes a string of text and breaks it down into tokens that can be easily parsed or analyzed. This is useful for creating programming languages, interpreters, compilers, and other syntax-aware utilities.
Tokenization
This code sample demonstrates how to define a simple lexer with the moo package. It includes rules for whitespace, comments, numbers, strings, parentheses, keywords, and newlines. The lexer is then used to tokenize a sample string.
{"lexer": "const moo = require('moo');\nlet lexer = moo.compile({\n WS: /[ \t]+/,\n comment: /\/\/.*?$/,\n number: /\d+/,\n string: /\"(\\\\.|[^\\\\\"])*\"/,\n lparen: '('\n rparen: ')',\n keyword: ['while', 'if', 'else', 'moo'],\n NL: { match: /\n/, lineBreaks: true },\n});\nlexer.reset('if (42) moo');\nconsole.log(lexer.next()); // -> { type: 'keyword', value: 'if' }"}
Custom Error Handling
This code sample shows how to handle errors in tokenization with moo. An 'error' token type is defined using moo.error, which is used to throw an exception when the lexer encounters an invalid syntax.
{"lexerWithErrorHandling": "const moo = require('moo');\nlet lexer = moo.compile({\n WS: /[ \t]+/,\n comment: /\/\/.*?$/,\n number: /\d+/,\n string: /\"(\\\\.|[^\\\\\"])*\"/,\n error: moo.error,\n});\nlexer.reset('invalid input');\ntry {\n while (true) {\n let token = lexer.next();\n if (!token) break;\n if (token.type === 'error') throw Error('Invalid syntax');\n }\n} catch (e) {\n console.error(e.message);\n}"}
Chevrotain is a JavaScript parser building toolkit which is similar to moo but more powerful and complex. It provides not only tokenization but also parsing capabilities, allowing users to define both the lexer and the parser for their language. Chevrotain is typically used when you need a full parser rather than just a tokenizer.
Nearley is a simple, fast, and powerful parsing toolkit for JavaScript. It is similar to moo in that it can be used to tokenize input strings, but it also includes a parser that is generated from a grammar specification. Nearley is often used for more complex parsing tasks where a grammar-based approach is beneficial.
PEG.js is a parser generator for JavaScript based on the parsing expression grammar formalism. It generates parsers with excellent performance and error reporting from a grammar. While moo focuses on tokenization, PEG.js provides a full parsing solution with a focus on creating parsers from a domain-specific language (DSL).
FAQs
Optimised tokenizer/lexer generator! 🐄 Much performance. Moo!
The npm package moo receives a total of 3,095,154 weekly downloads. As such, moo popularity was classified as popular.
We found that moo 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
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.