danielxmoore / hera Goto Github PK
View Code? Open in Web Editor NEWElegant parsing expression grammars
Elegant parsing expression grammars
( Foo:foo )?
never binds foo
. Similar to pattern matching in Civet.
The example code in README.md does not work:
Hera = require("hera")
fs = require("fs")
source = fs.readFileSync("cool-grammar.hera")
rules = Hera.parse(source, {
filename: path
})
// Generate parser.js source code
parserSource = Hera.generate(rules)
fs.writeFile("cool-parser.js", parserSource)
parser = require("./cool-parser")
// or generate parser object
parser = Hera.generate(rules, true)
parser.parse("text that my cool parser should parse")
The first problem is the first require. It should be Hera = require("@danielx/hera")
.
The second problem is that the hera API are outdated. The latest API are tested in https://github.com/DanielXMoore/Hera/blob/main/test/main.coffee.
Here's a small idea for Hera optimization: skip matching the empty string literal ""
. We use this ~70 times in the Civet grammar, mostly for declarative rules, which I imagine get hit a lot (you probably know better).
Given that it always matches, we shouldn't need to build/call $L
at all. I think this optimization could be in compileOp
but I haven't understood this code yet to know how to try it.
I recently wanted to do something like:
HashThis
LengthShorthand:id &( ( _+ ( ( Not __ In ) / In ) ) )?:beforeIn ->
if (beforeIn) ...
but ended up needing to do:
LengthShorthand:id ( &( _+ ( ( Not __ In ) / In ) ) "" )?:beforeIn ->
if (beforeIn != null) ...
This is because assertions always return undefined
since they fail the parse if not met. This causes issue with ?
because it returns undefined
if the parse wasn't matched so there's not a simple way find out whether the optional assertion matched or not.
It would be nice if optional assertions translated to true/false
for whether the assertion passes or fails.
Rule
A &B?:b ->
// b is `true` or `false`
-Foo
omits Foo
from $0
, and doesn't assign it $i
index.&
/!
do this automatically (this would be a breaking change, potentially requiring all grammars to be rewritten, as it shifts numeric indexing, unless we still assign them $i
indices)^Foo
selects Foo
for $0
(omitting others not marked ^
)A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.