Skip to content

Commit

Permalink
add rich-json.pegjs and an example
Browse files Browse the repository at this point in the history
  • Loading branch information
thzt committed May 25, 2017
1 parent 4d239e9 commit c2525fa
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
npm-debug.log
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# rich-json-parser
Support regular expression to be a JSON value.

Using [PEG.js](), the parser will not return an AST, but a javascript object.

For example:

```
{
"a": 1,
"b": [2, "3", /^\d+$/],
"c": /x(.+?)y/g
}
```

or

```
{ "a": 1, "b": [2, "3", /^\d+$/], "c": /a(.+?)y/g }
```

We will get the a javascript object as the result,

```
const result = {
"a": 1,
"b": [2, "3", /^\d+$/],
"c": /x(.+?)y/g
};
```
17 changes: 17 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs');
const path = require('path');
const peg = require('pegjs');

const grammarFile = path.resolve('../rich-json.pegjs');
const richJsonGrammar = fs.readFileSync(grammarFile, 'utf-8');

const richJsonParser = peg.generate(richJsonGrammar);

const json = `{
"a": 1,
"b": [2, "3", /^\d+$/],
"c": /x(.+?)y/g
}`;
const result = richJsonParser.parse(json);

console.log(result);
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "rich-json-parser",
"version": "1.0.0",
"description": "Support regular expression to be a JSON value.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/thzt/rich-json-parser.git"
},
"keywords": [
"json",
"regular expression",
"pegjs"
],
"author": "thzt",
"license": "ISC",
"bugs": {
"url": "https://github.com/thzt/rich-json-parser/issues"
},
"homepage": "https://github.com/thzt/rich-json-parser#readme",
"dependencies": {
"pegjs": "^0.10.0"
}
}
Loading

0 comments on commit c2525fa

Please sign in to comment.