|
| 1 | +# Ohm-WASM Go Integration |
| 2 | + |
| 3 | +This directory contains a Go implementation for running Ohm grammars compiled to WebAssembly. It provides a simple API to load grammar modules, match input against them, and access the resulting concrete syntax trees. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The implementation consists of two main components: |
| 8 | + |
| 9 | +1. **matcher.go**: A Go implementation of the JavaScript `WasmMatcher` class from the Ohm `wasm` package |
| 10 | +2. **testmain.go**: A command-line program that demonstrates how to use the WasmMatcher |
| 11 | + |
| 12 | +## WasmMatcher |
| 13 | + |
| 14 | +The `WasmMatcher` class provides a high-level API for working with Ohm grammars compiled to WebAssembly: |
| 15 | + |
| 16 | +```go |
| 17 | +// Create a new matcher |
| 18 | +matcher := NewWasmMatcher(ctx) |
| 19 | + |
| 20 | +// Load a grammar module |
| 21 | +err := matcher.LoadModule("path/to/grammar.wasm") |
| 22 | + |
| 23 | +// Set input text |
| 24 | +matcher.SetInput("text to match") |
| 25 | + |
| 26 | +// Match against the grammar |
| 27 | +success, err := matcher.Match() |
| 28 | + |
| 29 | +// Match against a specific rule |
| 30 | +success, err := matcher.MatchRule("specificRule") |
| 31 | + |
| 32 | +// Access the concrete syntax tree |
| 33 | +cstRoot, err := matcher.GetCstRoot() |
| 34 | +``` |
| 35 | + |
| 36 | +## Command-Line Usage |
| 37 | + |
| 38 | +You can use the command-line program to test Ohm grammars: |
| 39 | + |
| 40 | +``` |
| 41 | +# Test the simple add function (automatically detected) |
| 42 | +./testmain -wasm ../data/_add.wasm |
| 43 | +
|
| 44 | +# Match input against a grammar |
| 45 | +./testmain -wasm path/to/grammar.wasm -input "text to match" |
| 46 | +
|
| 47 | +# Specify a start rule |
| 48 | +./testmain -wasm path/to/grammar.wasm -input "text to match" -rule "StartRule" |
| 49 | +``` |
| 50 | + |
| 51 | +### Example with ES5 Grammar |
| 52 | + |
| 53 | +``` |
| 54 | +./testmain -wasm ../data/_es5.wasm -input "var x = 3;" |
| 55 | +``` |
| 56 | + |
| 57 | +## Build Instructions |
| 58 | + |
| 59 | +Use the Makefile in the parent directory to build and test: |
| 60 | + |
| 61 | +``` |
| 62 | +# Build the testmain program |
| 63 | +make test/go/testmain |
| 64 | +
|
| 65 | +# Run the test with the ES5 grammar |
| 66 | +make go-test-es5 |
| 67 | +``` |
0 commit comments