Skip to content

Commit 3b8ba68

Browse files
committed
Simplify: remove ParserNode
1 parent 4e29e15 commit 3b8ba68

File tree

4 files changed

+48
-54
lines changed

4 files changed

+48
-54
lines changed

src/interop/strumenta-playground.ts

+27-22
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from "./transpilation-package";
1515
import {TRANSPILATION_EPACKAGE_V1} from "./transpilation-package-v1";
1616
import {ensureEcoreContainsAllDataTypes} from "./ecore-patching";
17-
import {NodeAdapter, ParserNode, TraceNode} from "../trace/trace-node";
17+
import {NodeAdapter, TraceNode} from "../trace/trace-node";
1818

1919
export function saveForStrumentaPlayground<R extends Node>(
2020
result: ParsingResult<R>, name: string,
@@ -52,15 +52,15 @@ export class ParserTrace {
5252
}
5353
}
5454

55-
get rootNode(): ParserNode {
55+
get rootNode(): TraceNode {
5656
let root;
5757
const ast = this.node.get("ast");
5858
if (ast?.nodeDefinition?.name == "Result") {
5959
root = ast.get("root");
6060
} else {
6161
root = ast;
6262
}
63-
return new ParserNode(root);
63+
return new TraceNode(root);
6464
}
6565

6666
get issues(): Issue[] {
@@ -247,15 +247,17 @@ export class TargetWorkspaceFile extends AbstractWorkspaceFile<TargetNode> {
247247
}
248248

249249
export class SourceNode extends TraceNode {
250-
parent?: SourceNode;
250+
251+
parent?: SourceNode = undefined;
251252
protected _destinations?: TargetNode[];
252253

253254
constructor(wrappedNode: NodeAdapter, protected trace: AbstractTranspilationTrace,
254255
public readonly file?: SourceWorkspaceFile) {
255256
super(wrappedNode);
256-
if (wrappedNode.parent) {
257-
this.parent = new SourceNode(wrappedNode.parent, this.trace, file);
258-
}
257+
}
258+
259+
protected make(node: NodeAdapter): SourceNode {
260+
return new SourceNode(node, this.trace, this.file);
259261
}
260262

261263
getDestinationNodes(): TargetNode[] {
@@ -265,23 +267,21 @@ export class SourceNode extends TraceNode {
265267
return this._destinations;
266268
}
267269

268-
getChildren(role?: string): SourceNode[] {
269-
return this.nodeAdapter.getChildren(role).map((c) => new SourceNode(c, this.trace, this.file).withParent(this));
270-
}
271-
272-
get children(): Node[] {
273-
return this.getChildren();
270+
getChildren(role?: string | symbol): SourceNode[] {
271+
return super.getChildren(role) as SourceNode[];
274272
}
275273
}
276274

277275
export class TargetNode extends TraceNode {
278-
parent?: TargetNode;
276+
277+
parent?: TargetNode = undefined;
279278

280279
constructor(wrapped: NodeAdapter, protected trace: AbstractTranspilationTrace, public file?: TargetWorkspaceFile) {
281280
super(wrapped);
282-
if (wrapped.parent) {
283-
this.parent = new TargetNode(wrapped.parent, this.trace, this.file);
284-
}
281+
}
282+
283+
protected make(node: NodeAdapter): TargetNode {
284+
return new TargetNode(node, this.trace, this.file);
285285
}
286286

287287
getPosition(): Position | undefined {
@@ -317,18 +317,23 @@ export class TargetNode extends TraceNode {
317317
parent = parent.eContainer;
318318
}
319319
}
320-
this.origin = new SourceNode(new ECoreNode(rawOrigin), this.trace, file);
320+
const eCoreNode = new ECoreNode(rawOrigin);
321+
this.origin = this.makeSourceNode(eCoreNode, file);
321322
}
322323
}
323324
return this.origin as SourceNode;
324325
}
325326

326-
getChildren(role?: string): TargetNode[] {
327-
return this.nodeAdapter.getChildren(role).map((c) => new TargetNode(c, this.trace, this.file));
327+
protected makeSourceNode(eCoreNode: ECoreNode, file: SourceWorkspaceFile | undefined) {
328+
const sourceNode = new SourceNode(eCoreNode, this.trace, file);
329+
if (eCoreNode.parent) {
330+
sourceNode.withParent(this.makeSourceNode(eCoreNode.parent, file));
331+
}
332+
return sourceNode;
328333
}
329334

330-
get children(): Node[] {
331-
return this.getChildren();
335+
getChildren(role?: string | symbol): TargetNode[] {
336+
return super.getChildren(role) as TargetNode[];
332337
}
333338
}
334339

src/trace/trace-node.ts

+15-23
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ export class AugmentedNode extends NodeAdapter {
119119
}
120120
}
121121

122-
export abstract class TraceNode extends Node {
122+
export class TraceNode extends Node {
123123

124-
abstract parent?: TraceNode;
124+
parent?: TraceNode = undefined;
125125

126-
protected constructor(public nodeAdapter: NodeAdapter) {
126+
constructor(public nodeAdapter: NodeAdapter) {
127127
super();
128128
}
129129

@@ -184,6 +184,18 @@ export abstract class TraceNode extends Node {
184184
}
185185
}
186186

187+
protected make(node: NodeAdapter): TraceNode {
188+
return new TraceNode(node);
189+
}
190+
191+
getChildren(role?: string | symbol): TraceNode[] {
192+
return this.nodeAdapter.getChildren(role).map((c) => this.make(c).withParent(this));
193+
}
194+
195+
get children(): TraceNode[] {
196+
return this.getChildren();
197+
}
198+
187199
equals(node: TraceNode) {
188200
return node === this || node.nodeAdapter.equals(this.nodeAdapter);
189201
}
@@ -200,23 +212,3 @@ export abstract class TraceNode extends Node {
200212
return this.nodeAdapter.isStatement();
201213
}
202214
}
203-
204-
export class ParserNode extends TraceNode {
205-
206-
parent?: ParserNode;
207-
208-
constructor(inner: NodeAdapter) {
209-
super(inner);
210-
if (inner.parent) {
211-
this.parent = new ParserNode(inner.parent);
212-
}
213-
}
214-
215-
getChildren(role?: string): ParserNode[] {
216-
return this.nodeAdapter.getChildren(role).map((c) => new ParserNode(c));
217-
}
218-
219-
get children(): ParserNode[] {
220-
return this.getChildren();
221-
}
222-
}

tests/interop/lionweb.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import FS_LANGUAGE_JSON from "./fs-language.json";
22
import FS_MODEL from "./fs-model.json";
33
import {expect} from "chai";
44
import {deserializeChunk, deserializeLanguages, SerializationChunk} from "@lionweb/core";
5-
import {Children, Node, ParserNode, Property, walk} from "../../src";
5+
import {Children, Node, Property, TraceNode, walk} from "../../src";
66
import {
77
findClassifier,
88
LanguageMapping, LionwebNode,
@@ -11,8 +11,6 @@ import {
1111
} from "../../src/interop/lionweb";
1212
import {map, pipe, reduce} from "iter-ops";
1313
import {STARLASU_LANGUAGE} from "../../src/interop/lionweb-starlasu-language";
14-
import {ParserTrace} from "../../src/interop/strumenta-playground";
15-
import {PARSER_TRACE_ECLASS} from "../../src/interop/parser-package";
1614

1715
abstract class File extends Node {
1816
@Property()
@@ -104,7 +102,7 @@ describe('Lionweb integration', function() {
104102
expect(nodes.length).to.equal(1);
105103
const root = nodes[0];
106104
expect(root.node).to.be.instanceof(LionwebNode);
107-
let dir = new ParserNode(root.node as LionwebNode);
105+
let dir = new TraceNode(root.node as LionwebNode);
108106
expect(dir.getRole()).to.be.undefined;
109107
expect(dir.nodeDefinition.name).to.equal("Directory");
110108
expect(dir.getAttribute("name")).to.equal("resources.zip");

tests/interop/parser-trace.test.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {expect} from "chai";
22
import * as fs from "fs";
3-
import {findByPosition, IssueSeverity, IssueType, ParserNode, Point, pos, Position} from "../../src";
3+
import {findByPosition, IssueSeverity, IssueType, TraceNode, Point, pos, Position} from "../../src";
44
import {ParserTraceLoader} from "../../src/interop/strumenta-playground";
5-
import {TraceNode} from "../../src/trace/trace-node";
65

76
describe('Parser traces – Kolasu metamodel V1', function() {
87

@@ -81,10 +80,10 @@ describe('Parser traces – Starlasu metamodel V2', function() {
8180
expect(rootNode.getType()).to.eql("com.strumenta.sas.ast.SourceFile");
8281
expect(rootNode.getSimpleType()).to.eql("SourceFile");
8382
expect(rootNode.getPosition()).to.eql(pos(12, 0,369, 0));
84-
let foundNode = findByPosition(rootNode, pos(12, 0,369, 0)) as ParserNode;
83+
let foundNode = findByPosition(rootNode, pos(12, 0,369, 0)) as TraceNode;
8584
expect(foundNode.equals(rootNode)).to.be.true;
86-
const descNode = rootNode.children[10].children[7] as ParserNode;
87-
foundNode = findByPosition(descNode, descNode.position!) as ParserNode;
85+
const descNode = rootNode.children[10].children[7] as TraceNode;
86+
foundNode = findByPosition(descNode, descNode.position!) as TraceNode;
8887
expect(foundNode.equals(descNode)).to.be.true;
8988
expect(rootNode.getChildren().length).to.equal(18);
9089
expect(rootNode.getChildren("statementsAndDeclarations").length).to.equal(18);

0 commit comments

Comments
 (0)