https://github.com/harc/ohm Skip to content Sign up Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Project management - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this organization All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up Sign up {{ message }} harc / ohm * Notifications * Star 3.3k * Fork 166 A library and language for building parsers, interpreters, compilers, etc. MIT License 3.3k stars 166 forks Star Notifications * Code * Issues 38 * Pull requests 6 * Discussions * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Discussions * Actions * Projects * Wiki * Security * Insights master Switch branches/tags [ ] Branches Tags Nothing to show {{ refName }} default View all branches Nothing to show {{ refName }} default View all tags 26 branches 28 tags Go to file Code Clone HTTPS GitHub CLI [https://github.com/h] Use Git or checkout with SVN using the web URL. [gh repo clone harc/o] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio If nothing happens, download the GitHub extension for Visual Studio and try again. Go back Latest commit @bookcasey bookcasey Fix broken link (#291) ... 1d5b94c Mar 18, 2021 Fix broken link (#291) 1d5b94c Git stats * 1,450 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows chore: Disable Travis, test Node 15.x and not 10.x Dec 28, 2020 bin docs: Update contributor guide and release instructions Jan 15, 2021 doc Fix broken link (#291) Mar 18, 2021 examples test: Move ES5 test into the same directory as source Feb 26, 2021 ohm-js test: Move ES5 test into the same directory as source Feb 26, 2021 third_party fix error when publishing by refactoring into a multi-package repo Aug 22, 2020 visualizer Replace visualizer/ with latest from ohm-editor repo. Nov 18, 2016 .eslintignore test: Move ES5 test into the same directory as source Feb 26, 2021 .eslintrc Switch from tape to ava for tests. (#290) Feb 25, 2021 .gitattributes Don't ignore diffs for ohm-grammar.js. Apr 9, 2015 .gitignore doc: add 'Comment' secion in syntax-reference.md (#277) Dec 30, 2020 .jscsrc Convert CSV example to be a Node script, and add it to the tests. May 22, 2015 .mailmap Update contributors. Feb 24, 2017 .npmignore Add a dev-setup script that is run on postinstall. Mar 19, 2015 CHANGELOG.md Update changelog for 15.1.0. Sep 24, 2020 CONTRIBUTING.md docs: Update contributor guide and release instructions Jan 15, 2021 LICENSE Update LICENSE year, and remove my name. May 17, 2016 README.md doc: minor tweak to README Jan 15, 2021 TODO.md Rename Grammar.prototype.semantics to `createSemantics`. Jul 25, 2016 npm-refs More fixups for multi-package repo. Aug 22, 2020 package.json Make simple-lisp into a package and move tests into a separate file. (#... Feb 24, 2021 tsconfig.json Actually execute the test for the Typescript type declarations :-) Feb 25, 2017 yarn.lock Switch from tape to ava for tests. (#290) Feb 25, 2021 View code Ohm * Getting Started Resources Installation Basics Defining Grammars Using Grammars Debugging Publishing Grammars Contributing to Ohm Some useful scripts README.md Ohm * NPM Node.js CI Chat on Discord Ohm is a parsing toolkit consisting of a library and a domain-specific language. You can use it to parse custom file formats or quickly build parsers, interpreters, and compilers for programming languages. The Ohm language is based on parsing expression grammars (PEGs), which are a formal way of describing syntax, similar to regular expressions and context-free grammars. The Ohm library provides a JavaScript interface for creating parsers, interpreters, and more from the grammars you write. * Full support for left-recursive rules means that you can define left-associative operators in a natural way. * Object-oriented grammar extension makes it easy to extend an existing language with new syntax. * Modular semantic actions. Unlike many similar tools, Ohm completely separates grammars from semantic actions. This separation improves modularity and extensibility, and makes both grammars and semantic actions easier to read and understand. * Online editor and visualizer. The Ohm Editor provides instant feedback and an interactive visualization that makes the entire execution of the parser visible and tangible. It'll make you feel like you have superpowers. Some awesome things people have built using Ohm: * Seymour, a live programming environment for the classroom. * Chorus, a project exploring the middle ground between spreadsheets and programming. * Shadama, a particle simulation language designed for high-school science. * turtle.audio, an audio environment where simple text commands generate lines that can play music. * A browser-based tool that turns written Konnakkol (a South Indian vocal percussion art) into audio. * Wildcard, a browser extension that empowers anyone to modify websites to meet their own specific needs, uses Ohm for its spreadsheet formulas. Getting Started The easiest way to get started with Ohm is to use the interactive editor. Alternatively, you can play with one of the following examples on JSFiddle: * Basic parsing example * Arithmetic example with semantics Resources * Tutorial: Ohm: Parsing Made Easy * The math example is extensively commented and is a good way to dive deeper. * Examples * Documentation Installation For use in the browser: * Download ohm.js (development version, with full source and comments) or ohm.min.js (a minified version for faster page loads). * Add a new script tag to your page, and set the src attribute to the path of the file you just downloaded. E.g.: This creates a global variable named ohm. If you are using Node.js, you can just install the ohm-js package using npm: npm install ohm-js This will install Ohm in the local node_modules folder. Use require to access it from a Node script: const ohm = require('ohm-js'); Basics Defining Grammars Instantiating a grammar To use Ohm, you need a grammar that is written in the Ohm language. The grammar provides a formal definition of the language or data format that you want to parse. There are a few different ways you can define an Ohm grammar: * Define the grammar directly in a JavaScript string and instantiate it using ohm.grammar(): const myGrammar = ohm.grammar('MyGrammar { greeting = "Hello" | "Hola" }'); This is the simplest option, but it can be awkward to define larger grammars this way. * Recommended when running in the browser: Embed the grammar source inside its own * Recommended with Node.js: Define the grammar in a separate file, read the file's contents and instantiate it using ohm.grammar (contents): In myGrammar.ohm: MyGrammar { greeting = "Hello" | "Hola" } In JavaScript: const fs = require('fs'); const ohm = require('ohm-js'); const contents = fs.readFileSync('myGrammar.ohm'); const myGrammar = ohm.grammar(contents); For more information, see Instantiating Grammars in the API reference. Using Grammars Matching input Once you've instantiated a grammar object, use the grammar's match() method to recognize input: const userInput = 'Hello'; const m = myGrammar.match(userInput); if (m.succeeded()) { console.log('Greetings, human.'); } else { console.log("That's not a greeting!"); } The result is a MatchResult object. You can use the succeeded() and failed() methods to see whether the input was recognized or not. For more information, see the main documentation. Debugging Ohm has two tools to help you debug grammars: a text trace, and a graphical visualizer. Ohm Visualizer You can try the visualizer online, or if you have an Ohm checkout, open visualizer/index.html in your web browser. To see the text trace for a grammar g, just use the g.trace() method instead of g.match. It takes the same arguments, but instead of returning a MatchResult object, it returns a Trace object -- calling its toString method returns a string describing all of the decisions the parser made when trying to match the input. For example, here is the result of g.trace('ab').toString() for the grammar G { start = letter+ }: ab start = "ab" ab letter+ = "ab" ab letter = "a" ab lower = "a" ab Unicode [Ll] character = "a" b letter = "b" b lower = "b" b Unicode [Ll] character = "b" letter lower Unicode [Ll] character upper Unicode [Lu] character unicodeLtmo Unicode [Ltmo] character end = "" Publishing Grammars If you've written an Ohm grammar that you'd like to share with others, see our suggestions for publishing grammars. Contributing to Ohm All you need to get started: git clone https://github.com/harc/ohm.git cd ohm npm install NOTE: We recommend using the latest Node.js stable release. Some useful scripts * npm test runs the unit tests. * npm run test-watch re-runs the unit tests every time a file changes. * npm run build builds dist/ohm.js and dist/ohm.min.js, which are stand-alone bundles that can be included in a webpage. * When editing Ohm's own grammar (in src/ohm-grammar.ohm), run npm run bootstrap to re-build Ohm and test your changes. Before submitting a pull request, be sure to add tests, and ensure that npm run prepublish runs without errors. About A library and language for building parsers, interpreters, compilers, etc. Topics grammars parsing peg Resources Readme License MIT License Releases 28 tags Packages 0 No packages published Used by 2 * @harc @harc / ohm * @kalhauge @kalhauge / like Contributors 32 * @pdubroy * @alexwarth * @lilyx * @mroeder * @tonyg * @sakekasi * @djdeath * @rtoal * @jwmerrill * @yoshikiohshima * @njjewers + 21 contributors Languages * JavaScript 91.2% * CSS 3.5% * HTML 3.3% * Shell 1.8% * TypeScript 0.2% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.