Skip to content

Commit c0ee825

Browse files
shobhitgbuehler
authored andcommitted
feat(parser): Parse iterative instead of recursive
Closes #49
1 parent f8f72f4 commit c0ee825

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/TypescriptParser.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,46 +151,54 @@ export class TypescriptParser {
151151
*
152152
* @memberof TsResourceParser
153153
*/
154-
private parse(resource: Resource, node: Node): void {
155-
for (const child of node.getChildren()) {
156-
switch (child.kind) {
154+
private parse(rootResource: Resource, rootNode: Node): void {
155+
let [resource, ...resourceQueue]: Resource[] = Array(rootNode.getChildren().length).fill(rootResource);
156+
let [node, ...nodeQueue]: Node[] = [...rootNode.getChildren()];
157+
while (node) {
158+
switch (node.kind) {
157159
case SyntaxKind.ImportDeclaration:
158160
case SyntaxKind.ImportEqualsDeclaration:
159-
parseImport(resource, <ImportDeclaration | ImportEqualsDeclaration>child);
161+
parseImport(resource, <ImportDeclaration | ImportEqualsDeclaration>node);
160162
break;
161163
case SyntaxKind.ExportDeclaration:
162164
case SyntaxKind.ExportAssignment:
163-
parseExport(resource, <ExportAssignment | ExportDeclaration>child);
165+
parseExport(resource, <ExportAssignment | ExportDeclaration>node);
164166
break;
165167
case SyntaxKind.EnumDeclaration:
166-
parseEnum(resource, <EnumDeclaration>child);
168+
parseEnum(resource, <EnumDeclaration>node);
167169
break;
168170
case SyntaxKind.TypeAliasDeclaration:
169-
parseTypeAlias(resource, <TypeAliasDeclaration>child);
171+
parseTypeAlias(resource, <TypeAliasDeclaration>node);
170172
break;
171173
case SyntaxKind.FunctionDeclaration:
172-
parseFunction(resource, <FunctionDeclaration>child);
174+
parseFunction(resource, <FunctionDeclaration>node);
175+
[resource, ...resourceQueue] = resourceQueue;
176+
[node, ...nodeQueue] = nodeQueue;
173177
continue;
174178
case SyntaxKind.VariableStatement:
175-
parseVariable(resource, <VariableStatement>child);
179+
parseVariable(resource, <VariableStatement>node);
176180
break;
177181
case SyntaxKind.InterfaceDeclaration:
178-
parseInterface(resource, <InterfaceDeclaration>child);
182+
parseInterface(resource, <InterfaceDeclaration>node);
179183
break;
180184
case SyntaxKind.ClassDeclaration:
181-
parseClass(resource, <ClassDeclaration>child);
185+
parseClass(resource, <ClassDeclaration>node);
186+
[resource, ...resourceQueue] = resourceQueue;
187+
[node, ...nodeQueue] = nodeQueue;
182188
continue;
183189
case SyntaxKind.Identifier:
184-
parseIdentifier(resource, <Identifier>child);
190+
parseIdentifier(resource, <Identifier>node);
185191
break;
186192
case SyntaxKind.ModuleDeclaration:
187-
const newResource = parseModule(resource, <ModuleDeclaration>child);
188-
this.parse(newResource, child);
193+
const newResource = parseModule(resource, <ModuleDeclaration>node);
194+
[resource, ...resourceQueue] = [...Array(node.getChildren().length).fill(newResource), ...resourceQueue];
195+
[node, ...nodeQueue] = [...node.getChildren(), ...nodeQueue];
189196
continue;
190197
default:
191198
break;
192199
}
193-
this.parse(resource, child);
200+
[resource, ...resourceQueue] = [...Array(node.getChildren().length).fill(resource), ...resourceQueue];
201+
[node, ...nodeQueue] = [...node.getChildren(), ...nodeQueue];
194202
}
195203
}
196204
}

0 commit comments

Comments
 (0)