Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

moo

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moo

Optimised tokenizer/lexer generator! 🐄 Much performance. Moo!

  • 0.4.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.7M
decreased by-6.88%
Maintainers
2
Weekly downloads
 
Created

What is moo?

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.

What are moo's main functionalities?

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}"}

Other packages similar to moo

FAQs

Package last updated on 14 Sep 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc