
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Ohm is a parser generator for building languages and interpreters. It provides a way to define grammars and parse text according to those grammars. Ohm is particularly useful for creating domain-specific languages, interpreters, and compilers.
Defining Grammars
This feature allows you to define a grammar using Ohm's syntax. The example defines a simple arithmetic grammar that can parse expressions involving addition, subtraction, multiplication, and division.
const ohm = require('ohm-js');
const grammar = ohm.grammar(`
Arithmetic {
Exp = AddExp
AddExp = AddExp "+" MulExp -- plus
| AddExp "-" MulExp -- minus
| MulExp
MulExp = MulExp "*" PriExp -- times
| MulExp "/" PriExp -- divide
| PriExp
PriExp = "(" Exp ")" -- paren
| number
number = digit+
}
`);
Parsing Input
Once a grammar is defined, you can use it to parse input strings. This example parses an arithmetic expression and checks if the parsing succeeded.
const input = '3 + 5 * (10 - 4)';
const matchResult = grammar.match(input);
if (matchResult.succeeded()) {
console.log('Parsing succeeded!');
} else {
console.log('Parsing failed.');
}
Semantic Actions
Ohm allows you to define semantic actions that can be performed on the parse tree. This example defines an 'eval' operation to evaluate arithmetic expressions parsed by the grammar.
const semantics = grammar.createSemantics().addOperation('eval', {
Exp: function(e) { return e.eval(); },
AddExp_plus: function(a, _, b) { return a.eval() + b.eval(); },
AddExp_minus: function(a, _, b) { return a.eval() - b.eval(); },
MulExp_times: function(a, _, b) { return a.eval() * b.eval(); },
MulExp_divide: function(a, _, b) { return a.eval() / b.eval(); },
PriExp_paren: function(_1, e, _2) { return e.eval(); },
number: function(digits) { return parseInt(this.sourceString, 10); }
});
const result = semantics(matchResult).eval();
console.log(result);
PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. Like Ohm, it allows you to define grammars and parse text, but it uses Parsing Expression Grammars (PEG) instead of Ohm's custom syntax.
ANTLR (Another Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It is more feature-rich and supports multiple target languages, making it more versatile than Ohm, but also more complex to use.
Nearley is a simple, fast, and powerful parser toolkit for JavaScript. It uses Earley parsing, which can handle more complex grammars than PEG-based parsers like PEG.js. Nearley is more flexible but can be more difficult to use for simple grammars compared to Ohm.
FAQs
An object-oriented language for parsing and pattern matching
We found that ohm-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.