-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(perser-adaper-json): fold syntactic analysis phases
Code of the parser have been consolidate into idiomatic parsing phases: lexical and syntactic analysis. There are two types of syntactic analysis: direct and indirect. Indirect syntactic analysis is refactored and simplified original code which turncs CST into JSON AST and then into ApiDOM. Direct one is a new one which turns CST directly into ApiDOM. Switching syntactic analysis is possible via configuration option. Performance of direct syntactic analysis has been increased by 300% comparsing to original code. Performance of indirect syntactic analysis has been increased by 10% comparing to original code and has been significantly simplified. Closes #406
- Loading branch information
Showing
36 changed files
with
1,725 additions
and
995 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 33 additions & 2 deletions
35
apidom/packages/apidom-parser-adapter-json/src/adapter-browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
export { default as parse, namespace } from './parser/index-browser'; | ||
export { detect, mediaTypes } from './adapter'; | ||
import { ParseResultElement } from 'apidom'; | ||
|
||
import lexicallyAnalyze from './lexical-analysis/browser'; | ||
import syntacticallyAnalyzeDirectly from './syntactic-analysis/direct'; | ||
import syntacticallyAnalyzeIndirectly from './syntactic-analysis/indirect'; | ||
|
||
export { detect, mediaTypes, namespace } from './adapter'; | ||
|
||
interface ParseFunctionOptions { | ||
sourceMap?: boolean; | ||
syntacticAnalysis?: 'direct' | 'indirect'; | ||
} | ||
|
||
type ParseFunction = ( | ||
source: string, | ||
options?: ParseFunctionOptions, | ||
) => Promise<ParseResultElement>; | ||
|
||
export const parse: ParseFunction = async ( | ||
source, | ||
{ sourceMap = false, syntacticAnalysis = 'direct' } = {}, | ||
) => { | ||
const cst = await lexicallyAnalyze(source); | ||
let apiDOM; | ||
|
||
if (syntacticAnalysis === 'indirect') { | ||
apiDOM = syntacticallyAnalyzeIndirectly(cst, { sourceMap }); | ||
} else { | ||
apiDOM = syntacticallyAnalyzeDirectly(cst, { sourceMap }); | ||
} | ||
|
||
return apiDOM; | ||
}; |
35 changes: 33 additions & 2 deletions
35
apidom/packages/apidom-parser-adapter-json/src/adapter-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
export { default as parse, namespace } from './parser/index-node'; | ||
export { mediaTypes, detect } from './adapter'; | ||
import { ParseResultElement } from 'apidom'; | ||
|
||
import lexicallyAnalyze from './lexical-analysis/node'; | ||
import syntacticallyAnalyzeDirectly from './syntactic-analysis/direct'; | ||
import syntacticallyAnalyzeIndirectly from './syntactic-analysis/indirect'; | ||
|
||
export { detect, mediaTypes, namespace } from './adapter'; | ||
|
||
interface ParseFunctionOptions { | ||
sourceMap?: boolean; | ||
syntacticAnalysis?: 'direct' | 'indirect'; | ||
} | ||
|
||
type ParseFunction = ( | ||
source: string, | ||
options?: ParseFunctionOptions, | ||
) => Promise<ParseResultElement>; | ||
|
||
export const parse: ParseFunction = async ( | ||
source, | ||
{ sourceMap = false, syntacticAnalysis = 'direct' } = {}, | ||
) => { | ||
const cst = await lexicallyAnalyze(source); | ||
let apiDOM; | ||
|
||
if (syntacticAnalysis === 'indirect') { | ||
apiDOM = syntacticallyAnalyzeIndirectly(cst, { sourceMap }); | ||
} else { | ||
apiDOM = syntacticallyAnalyzeDirectly(cst, { sourceMap }); | ||
} | ||
|
||
return apiDOM; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
apidom/packages/apidom-parser-adapter-json/src/lexical-analysis/browser-patch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { tail } from 'ramda'; | ||
import { isString, isFunction } from 'ramda-adjunct'; | ||
// @ts-ignore | ||
import treeSitterWasm from 'web-tree-sitter/tree-sitter.wasm'; | ||
|
||
// patch fetch() to let emscripten load the WASM file | ||
const realFetch = globalThis.fetch; | ||
|
||
if (isFunction(realFetch)) { | ||
globalThis.fetch = (...args) => { | ||
// @ts-ignore | ||
if (isString(args[0]) && args[0].endsWith('/tree-sitter.wasm')) { | ||
// @ts-ignore | ||
return realFetch.apply(globalThis, [treeSitterWasm, tail(args)]); | ||
} | ||
return realFetch.apply(globalThis, args); | ||
}; | ||
} |
26 changes: 26 additions & 0 deletions
26
apidom/packages/apidom-parser-adapter-json/src/lexical-analysis/browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import './browser-patch'; | ||
|
||
import Parser, { Tree } from 'web-tree-sitter'; | ||
// @ts-ignore | ||
import treeSitterJson from 'tree-sitter-json/tree-sitter-json.wasm'; | ||
|
||
/** | ||
* We initialize the WebTreeSitter as soon as we can. | ||
*/ | ||
const parserP = (async () => { | ||
await Parser.init(); | ||
await Parser.Language.load(treeSitterJson); | ||
|
||
return new Parser(); | ||
})(); | ||
|
||
/** | ||
* Lexical Analysis of source string using WebTreeSitter. | ||
* This is WebAssembly version of TreeSitters Lexical Analysis. | ||
*/ | ||
const analyze = async (source: string): Promise<Tree> => { | ||
const parser = await parserP; | ||
return parser.parse(source); | ||
}; | ||
|
||
export default analyze; |
16 changes: 16 additions & 0 deletions
16
apidom/packages/apidom-parser-adapter-json/src/lexical-analysis/node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Parser, { Tree } from 'tree-sitter'; | ||
// @ts-ignore | ||
import JSONLanguage from 'tree-sitter-json'; | ||
|
||
const parser = new Parser(); | ||
parser.setLanguage(JSONLanguage); | ||
|
||
/** | ||
* Lexical Analysis of source string using TreeSitter. | ||
* This is Node.js version of TreeSitters Lexical Analysis. | ||
*/ | ||
const analyze = async (source: string): Promise<Tree> => { | ||
return parser.parse(source); | ||
}; | ||
|
||
export default analyze; |
15 changes: 0 additions & 15 deletions
15
apidom/packages/apidom-parser-adapter-json/src/parser/index-browser-patch.ts
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
apidom/packages/apidom-parser-adapter-json/src/parser/index-browser.ts
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
apidom/packages/apidom-parser-adapter-json/src/parser/index-node.ts
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
apidom/packages/apidom-parser-adapter-json/src/parser/index.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.