Registry / serialization / canopy

canopy

JSON →
library0.4.1jsnpmunverified

Canopy is a parser compiler for parsing expression grammars (PEG) targeting JavaScript, Java, Python, and Ruby. Version 0.4.1 is the latest stable release; the project appears to be in maintenance mode with no recent activity. It generates standalone parsers with no runtime dependencies, making it lightweight. Unlike PEG.js or Nearley, Canopy offers multi-language code generation from a single grammar file.

npm install canopy
INSTALL
IMPORT
SIG · CANOPY
C
canopy
serializationjavascriptv0.4.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import Parser from './path/to/generated-parser'
const Parser = require('./path/to/generated-parser')
Generated parsers are ESM modules; use ES2015 import syntax.
parse
import Parser from './parser'; const result = Parser.parse('input');
The generated parser exports a parse function as the default export.
SyntaxTree
import { SyntaxTree } from 'canopy'
import { SyntaxTree } from './parser'
SyntaxTree is a helper type from the main 'canopy' package, not from generated parsers.

Shows how to create a simple PEG grammar, compile it with Canopy CLI, and use the generated parser in JavaScript.

// Install canopy globally // npm install -g canopy // grammar.peg /* program <- expr+ expr <- number / '(' expr ')' number <- [0-9]+ */ // Compile to JavaScript // canopy grammar.peg --lang javascript --output parser.js // In app.js import Parser from './parser.js' const result = Parser.parse('(42)') console.log(result)
canopy --version
Debug
Known issues
gotchaGenerated parsers use strict mode and may fail with 'use strict' conflicts.
fix
Ensure your code is compatible with strict mode. Avoid using reserved words as node labels.
affects: >=0.1
breakingOutput format changes between versions; generated parsers from different Canopy versions may not be compatible.
fix
Regenerate parsers after upgrading Canopy to avoid silent failures.
affects: >=0.3
gotchaLeft recursion in grammars causes infinite loops or stack overflow.
fix
Rewrite left-recursive rules using iteration or right recursion. Canopy does not support left recursion.
affects: >=0.1
deprecatedCanopy does not generate ESM modules by default; output uses CommonJS in older versions.
fix
Use version 0.4 or later, or manually convert to ESM.
affects: <0.4
Errors
Common errors & fixes
Cannot find module './parser.js'
Output file not generated or wrong path.
fix
Run canopy grammar.peg --lang javascript --output parser.js to generate the parser.
SyntaxError: Unexpected token ILLEGAL
Grammar contains invalid PEG syntax or characters.
fix
Check grammar for non-ASCII characters or misplaced operators. Refer to PEG specification.
ReferenceError: process is not defined
Generated parser used in non-Node environment without browser polyfill.
fix
Use a bundler like Webpack or include a shim for process.env if needed. Alternatively, avoid using environment variable features.
Upgrade
Version history
0.4.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
canopy — npm install canopy · libregistry