Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JohannesOehm/json-parse-ast
JSON AST parser written in JavaScript. For usage in conjunction with the monaco editor to enhance JSON autocomplete experience.
https://github.com/JohannesOehm/json-parse-ast
Last synced: about 1 month ago
JSON representation
JSON AST parser written in JavaScript. For usage in conjunction with the monaco editor to enhance JSON autocomplete experience.
- Host: GitHub
- URL: https://github.com/JohannesOehm/json-parse-ast
- Owner: JohannesOehm
- Created: 2021-02-21T12:39:37.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-02-25T13:12:36.000Z (almost 4 years ago)
- Last Synced: 2024-09-20T13:18:10.516Z (3 months ago)
- Language: JavaScript
- Size: 118 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
- awesome-github-star - json-parse-ast
README
# json-parse-ast
JSON Tokenizer & AST (Abstract Syntax Tree) parser## Background
I wrote my custom parser since [monaco's](https://microsoft.github.io/monaco-editor/) built-in JSON
library does not expose any syntax tree which can be used to check position in file and enhance
autocomplete experience.## Usage
Currently, only tokenizer is stable:
```javascript
var tokens = tokenize('{"json": "string"}'); //returns list of tokens
//tokens have the following attributes
// type: "inlinecomment"|"multilinecomment"|"whitespace"|"string"|"literal"|"number"|"punctuaction"
// position: IRange compatible with monaco's IRange-interface
// raw: string
// value: string Unescaped JSON string, parsed number, parsed literal (null, true, false)
```
## Tokenizer additional features
```javascript
var [path, willBeValue] = getPathInObject(tokenize('{"foo": {"bar": "'));
//returns path == ["foo", "bar"] and willBeValue == true
```## Parser
```javascript
var ast = parseTokens(tokenize(testString));
//AST elements have
// type
// position: IRange
// raw: string
// value?: string
// parent?: AST
// children?: AST[]//Find node at specified position
var node = findAtPosition(ast, {lineNumber: 4, column: 16})
```## Usage with monaco
This libraries intended use is for enhancing experience with custom JSON formats. See example use-cases below:### Enhancing autocomplete/IntelliSense
```javascript
monaco.languages.registerCompletionItemProvider('json', {
provideCompletionItems: function(model, position) {
let textUntilPosition = model.getValueInRange({startLineNumber: 1, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column});
let [jsonPath, willBeValue] = getPathInObject(tokenize(textUntilPosition));
if (willBeValue && jsonPath[jsonPath.length-1] === "dependencies") {
return { suggestions: listDependencies() };
} else {
return { suggestions: [] };
}
}
});
```