Skip to content

Commit c2525fa

Browse files
committed
add rich-json.pegjs and an example
1 parent 4d239e9 commit c2525fa

File tree

5 files changed

+319
-0
lines changed

5 files changed

+319
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
npm-debug.log

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
11
# rich-json-parser
22
Support regular expression to be a JSON value.
3+
4+
Using [PEG.js](), the parser will not return an AST, but a javascript object.
5+
6+
For example:
7+
8+
```
9+
{
10+
"a": 1,
11+
"b": [2, "3", /^\d+$/],
12+
"c": /x(.+?)y/g
13+
}
14+
```
15+
16+
or
17+
18+
```
19+
{ "a": 1, "b": [2, "3", /^\d+$/], "c": /a(.+?)y/g }
20+
```
21+
22+
We will get the a javascript object as the result,
23+
24+
```
25+
const result = {
26+
"a": 1,
27+
"b": [2, "3", /^\d+$/],
28+
"c": /x(.+?)y/g
29+
};
30+
```

examples/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const peg = require('pegjs');
4+
5+
const grammarFile = path.resolve('../rich-json.pegjs');
6+
const richJsonGrammar = fs.readFileSync(grammarFile, 'utf-8');
7+
8+
const richJsonParser = peg.generate(richJsonGrammar);
9+
10+
const json = `{
11+
"a": 1,
12+
"b": [2, "3", /^\d+$/],
13+
"c": /x(.+?)y/g
14+
}`;
15+
const result = richJsonParser.parse(json);
16+
17+
console.log(result);

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "rich-json-parser",
3+
"version": "1.0.0",
4+
"description": "Support regular expression to be a JSON value.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/thzt/rich-json-parser.git"
12+
},
13+
"keywords": [
14+
"json",
15+
"regular expression",
16+
"pegjs"
17+
],
18+
"author": "thzt",
19+
"license": "ISC",
20+
"bugs": {
21+
"url": "https://github.com/thzt/rich-json-parser/issues"
22+
},
23+
"homepage": "https://github.com/thzt/rich-json-parser#readme",
24+
"dependencies": {
25+
"pegjs": "^0.10.0"
26+
}
27+
}

0 commit comments

Comments
 (0)