An object-oriented language for parsing and pattern matching based on parsing expression grammars
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core functionality for creating and managing Ohm grammars from source definitions.
import { grammar, grammars, _buildGrammar } from "ohm-js";For TypeScript:
import { grammar, grammars, Grammar, Namespace, _buildGrammar } from "ohm-js";Creates a single Grammar instance from source code containing exactly one grammar definition.
/**
* Instantiate the Grammar defined by source. If specified, namespace is
* the Namespace to use when resolving external references in the grammar.
* @param source - String containing grammar definition
* @param namespace - Optional namespace for resolving external references
* @returns Grammar instance
* @throws Error if source contains zero or more than one grammar definition
*/
function grammar(source: string, namespace?: Namespace): Grammar;Usage Examples:
import { grammar } from "ohm-js";
// Create a simple grammar
const myGrammar = grammar(`
MyGrammar {
greeting = "hello" | "hi"
name = letter+
message = greeting " " name
}
`);
// Use with namespace for grammar extension
const baseGrammar = grammar(`
Base {
number = digit+
}
`);
const extendedGrammar = grammar(`
Extended <: Base {
decimal = number "." number
}
`, { Base: baseGrammar });Creates a Namespace containing Grammar instances for all grammars defined in the source.
/**
* Create a new Namespace containing Grammar instances for all of the
* grammars defined in source.
* If namespace is specified, it will be the prototype of the new Namespace.
* @param source - String containing one or more grammar definitions
* @param namespace - Optional parent namespace
* @returns Namespace containing all defined grammars
*/
function grammars(source: string, namespace?: Namespace): Namespace;Usage Examples:
import { grammars } from "ohm-js";
// Create multiple grammars at once
const myGrammars = grammars(`
Math {
number = digit+
expr = number ("+" number)*
}
String {
word = letter+
phrase = word (" " word)*
}
`);
// Access individual grammars
const mathGrammar = myGrammars.Math;
const stringGrammar = myGrammars.String;Container for multiple Grammar instances, indexed by grammar name.
/**
* A Namespace is a dictionary of Grammars
*/
interface Namespace {
[index: string]: Grammar;
}Usage Examples:
// Iterate over grammars in a namespace
for (const grammarName in myGrammars) {
const grammar = myGrammars[grammarName];
console.log(`Grammar: ${grammar.name}`);
}
// Create hierarchical namespaces
const childNamespace = grammars(childSource, parentNamespace);Internal function used by ohm-editor for incremental parsing support.
/**
* Internal function for building grammars (used by ohm-editor)
* @internal This is not part of the public API
* @param matchResult - MatchResult from parsing grammar source
* @param namespace - Namespace to build grammar into
* @returns The built namespace with grammars
*/
function _buildGrammar(matchResult: any, namespace?: Namespace): any;Usage Examples:
import { _buildGrammar, ohmGrammar } from "ohm-js";
// This is advanced internal usage - not recommended for general use
const grammarSource = `
MyGrammar {
rule = "hello"
}
`;
const match = ohmGrammar.match(grammarSource, 'Grammars');
if (match.succeeded()) {
const namespace = {};
_buildGrammar(match, namespace);
// namespace now contains the built grammar
}Note: This function is exported for ohm-editor compatibility but is not intended for general use. Most users should use grammar() or grammars() instead.
The grammar creation functions will throw errors in the following cases:
grammar() throws if source contains no grammar definitionsgrammar() throws if source contains more than one definitionError Example:
try {
const invalidGrammar = grammar("invalid syntax here");
} catch (error) {
console.error("Grammar compilation failed:", error.message);
// Error message includes line and column information
}interface Dict {
[index: string]: any;
}Install with Tessl CLI
npx tessl i tessl/npm-ohm-js