miniohm

package module
v0.0.0-...-8c9f535 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2025 License: MIT Imports: 8 Imported by: 0

README

minohm-go

A Go implementation of the minohm interface, for using Ohm grammars from Go.

A grammar blob is an Ohm grammar that has been compiled to .wasm via the @ohm-js/wasm NPM package. To use a grammar blob to match some input, you need a miniohm implementation for your host language of choice (JavaScript, Go, Python, etc.) This package provides a miniohm implementation for the Go Programming Language.

Overview

  • The miniohm module is in matcher.go and cst.go.
  • An example can be found in cmd/example/main.go.

Compiling grammars to Wasm

Use the ohm2wasm command from the @ohm-js/wasm NPM package to compile a .ohm file to a Wasm grammar blob. For example:

npx ohm2wasm myGrammar.ohm

See Makefile for an example.

Matching input

Create a new WasmMatcher and use the Match function:

matcher := NewWasmMatcher(ctx)
err := matcher.LoadModule("path/to/grammar.wasm")
matcher.SetInput("text to match")
success, err := matcher.Match()
cstRoot, err := matcher.GetCstRoot()

Walking the CST

A full implementation of semantics, operations, etc. is not part of the miniohm interface. Instead, you can walk the CST (concrete syntax tree) directly using the CstNode interface. See cmd/example/main.go for an example.

Developing

Useful commands:

make # Build
make test # Run tests

Documentation

Index

Constants

View Source
const (
	// Node type constants
	NodeTypeNonterminal = 0
	NodeTypeTerminal    = -1
	NodeTypeIter        = -2
)
View Source
const (
	InputBufferOffset = wasmPageSize
	InputBufferSize   = wasmPageSize
	MemoTableOffset   = InputBufferOffset + InputBufferSize
)

Constants for memory layout

Variables

This section is empty.

Functions

This section is empty.

Types

type CstNode

type CstNode struct {
	// contains filtered or unexported fields
}

CstNode represents a node in the Concrete Syntax Tree

func NewCstNode

func NewCstNode(ruleNames []string, memory api.Memory, offset uint32) *CstNode

NewCstNode creates a new CstNode with the given parameters

func (*CstNode) Children

func (n *CstNode) Children() ([]*CstNode, error)

Children returns a slice of child nodes

func (*CstNode) IsIter

func (n *CstNode) IsIter() bool

IsIter returns true if this node represents an iteration

func (*CstNode) IsNonterminal

func (n *CstNode) IsNonterminal() bool

IsNonterminal returns true if this node represents a nonterminal

func (*CstNode) IsTerminal

func (n *CstNode) IsTerminal() bool

IsTerminal returns true if this node represents a terminal

func (*CstNode) MatchLength

func (n *CstNode) MatchLength() (uint32, error)

MatchLength returns the length of the matched text

func (*CstNode) RuleName

func (n *CstNode) RuleName() (string, error)

RuleName returns the name of the rule that created this node

func (*CstNode) Type

func (n *CstNode) Type() int32

Type returns the type of this node (0 for nonterminal, -1 for terminal, -2 for iter)

type WasmMatcher

type WasmMatcher struct {
	// contains filtered or unexported fields
}

WasmMatcher is a Go implementation of the JavaScript WasmMatcher class for Ohm

func NewWasmMatcher

func NewWasmMatcher(ctx context.Context) *WasmMatcher

func (*WasmMatcher) Close

func (m *WasmMatcher) Close() error

Close releases all resources

func (*WasmMatcher) GetCstRoot

func (m *WasmMatcher) GetCstRoot() (*CstNode, error)

GetCstRoot returns a CstNode object for the current parse tree

func (*WasmMatcher) GetInput

func (m *WasmMatcher) GetInput() string

func (*WasmMatcher) GetModule

func (m *WasmMatcher) GetModule() api.Module

GetModule returns the WebAssembly module

func (*WasmMatcher) LoadModule

func (m *WasmMatcher) LoadModule(wasmPath string) error

func (*WasmMatcher) Match

func (m *WasmMatcher) Match(ruleName ...string) (bool, error)

func (*WasmMatcher) SetInput

func (m *WasmMatcher) SetInput(input string)

func (*WasmMatcher) SetInputFromFile

func (m *WasmMatcher) SetInputFromFile(filePath string) error

SetInputFromFile reads input from a file and sets it as the current input

Directories

Path Synopsis
cmd
example command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL