Skip to content

Commit

Permalink
feat (framework): new nearley framework
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Aug 18, 2023
1 parent ba30422 commit a6e9464
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
14 changes: 14 additions & 0 deletions frameworks/nearley/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
permalink: /frameworks/nearley/
layout: single
---

Nearley
=======

[Nearley](https://nearley.js.org/) .

~~~
npm install nearley
npx nearleyc grammar/01-basic.ne -o js/01-basic.js
~~~
2 changes: 2 additions & 0 deletions frameworks/nearley/grammar/01-basic.ne
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main -> (statement "\n"):+
statement -> "foo" | "bar"
8 changes: 8 additions & 0 deletions frameworks/nearley/js/01-basic-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Create a Parser object from our grammar.
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));

// Parse something!
parser.feed("foo\n");

// parser.results is an array of possible parsings.
console.log(JSON.stringify(parser.results)); // [[[[["foo"],"\n"]]]]
25 changes: 25 additions & 0 deletions frameworks/nearley/js/01-basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Generated automatically by nearley, version 2.20.1
// http://github.com/Hardmath123/nearley
(function () {
function id(x) { return x[0]; }
var grammar = {
Lexer: undefined,
ParserRules: [
{"name": "main$ebnf$1$subexpression$1", "symbols": ["statement", {"literal":"\n"}]},
{"name": "main$ebnf$1", "symbols": ["main$ebnf$1$subexpression$1"]},
{"name": "main$ebnf$1$subexpression$2", "symbols": ["statement", {"literal":"\n"}]},
{"name": "main$ebnf$1", "symbols": ["main$ebnf$1", "main$ebnf$1$subexpression$2"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "main", "symbols": ["main$ebnf$1"]},
{"name": "statement$string$1", "symbols": [{"literal":"f"}, {"literal":"o"}, {"literal":"o"}], "postprocess": function joiner(d) {return d.join('');}},
{"name": "statement", "symbols": ["statement$string$1"]},
{"name": "statement$string$2", "symbols": [{"literal":"b"}, {"literal":"a"}, {"literal":"r"}], "postprocess": function joiner(d) {return d.join('');}},
{"name": "statement", "symbols": ["statement$string$2"]}
]
, ParserStart: "main"
}
if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
module.exports = grammar;
} else {
window.grammar = grammar;
}
})();
11 changes: 11 additions & 0 deletions frameworks/nearley/js/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<title>Basic Example</title>
<script src="https://cdn.jsdelivr.net/npm/nearley@2.20.1/lib/nearley.min.js"></script>
<script src="01-basic.js"></script>
</head>
<body>
<script src="01-basic-app.js"></script>
</body>
</html>
120 changes: 120 additions & 0 deletions frameworks/nearley/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions frameworks/nearley/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"nearley": "^2.20.1"
}
}

0 comments on commit a6e9464

Please sign in to comment.