Simple Ohm Grammar
2025-03-26
Here is a simple sample of a DSL, inspired by sketch.systems demo.
This simple example doesn’t parse everything in the demo, but, I hope that this is uncomplicated enough to give a flavour of the simplicity of OhmJS.
The test example is
Green*
tick -> Yellow
Yellow
tick -> Red
Red
tick -> Green
And the grammar is:
sketch {
Main = State+
State = name "*"? Transition+
Transition = name "->" name
name = letter+
}
Further
The sketch-systems demo goes on to show the use of indentation and multi-word state names. Those ideas are not included in the above grammar, for clarity. I’ve tried to address them below.
Note that the more elaborate grammar uses OhmJS’ “space-skipping” features
capitalized and uncapitalized grammar rule names
The use of the “#” operator
It turns out that I didn’t need to use OhmJS’ indentation parsing features, but, they are there if you need to parse something more complicated.
The ultimate example in the sketch-systems demo is:
Powered
power failed -> Red On
Green*
tick -> Yellow
Yellow
tick -> Red
Red
tick -> Green
Unpowered
power restored -> Green
Red On
tick -> Red Off
Red off
tick -> Red On
And, a grammar (untested) that appears to work is:
sketch {
Main = State+
State = StateName "*"? #nl Transition+
Transition = StateName "->" StateName #nl
StateName = name (#" " name)*
name = letter+
nl = "\n"
}
See Also
Email: ptcomputingsimplicity@gmail.com
References: https://guitarvydas.github.io/2024/01/06/References.html
Blog: guitarvydas.github.io
Videos: https://www.youtube.com/@programmingsimplicity2980
Discord: https://discord.gg/65YZUh6Jpq
Leanpub: [WIP] https://leanpub.com/u/paul-tarvydas
Gumroad: tarvydas.gumroad.com
Twitter: @paul_tarvydas
Substack: paultarvydas.substack.com



