Skip to content

Commit

Permalink
Renamed "property" to either "feature" or "attribute" according to Ko…
Browse files Browse the repository at this point in the history
…lasu terminology

Correctly record references in node metadata for ECore nodes
  • Loading branch information
alessiostalla committed Mar 21, 2024
1 parent 9715d9a commit 82c86da
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 31 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
All notable changes to this project from version 1.2.0 upwards are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [1.6.17] – 2024-03-21

### Changed
- TraceNode.get renamed to getDescendant

### Fixed
- TraceNode.get to access nodes by path

## [1.6.16] – 2024-03-21

### Changed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "AST building blocks for TypeScript/JavaScript, part of the *lasu family, with optional integrations with ANTLR4 and Ecore.",
"author": "Strumenta s.r.l.",
"publisher": "strumenta",
"version": "1.6.16",
"version": "1.6.17",
"license": "Apache-2.0",
"keywords": [
"antlr",
Expand Down
2 changes: 1 addition & 1 deletion src/interop/ecore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export class ${className} extends ${superClassName} {`;
defineProperty(classDef, name);
const prop = registerNodeAttribute(classDef as any, name);
prop.child = a.isTypeOf('EReference');
const annotation = prop.child ? (prop.multiple ? "@Children()" : "@Child()") : "@Property()"
const annotation = prop.child ? (prop.multiple ? "@Children()" : "@Child()") : "@Attribute()"
classDef[SYMBOL_CLASS_DEFINITION] += `\n\t${annotation}\n\t${name};`;
});
classDef[SYMBOL_CLASS_DEFINITION] += "\n}";
Expand Down
4 changes: 3 additions & 1 deletion src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,10 @@ export abstract class Node extends Origin implements Destination {
const raw = this.doGetChildOrChildren(name);
if (containment.multiple) {
return (raw as Node[]) || [];
} else if (raw) {
return [raw as Node];
} else {
throw new Error(name.toString() + " is not a collection");
return [];
}
}

Expand Down
25 changes: 12 additions & 13 deletions src/trace/trace-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class TraceNode extends Node implements PossiblyNamed {
refTarget = refTarget.parent;
}
if (tempParent) {
const newParent = this.getRoot().get(tempParent.getPathFromRoot());
const newParent = this.getRoot().getDescendant(tempParent.getPathFromRoot());
if (newParent instanceof Node) {
return newParent;
}
Expand Down Expand Up @@ -283,20 +283,19 @@ export class TraceNode extends Node implements PossiblyNamed {
return this.nodeAdapter.isStatement();
}

get(path: (string | number)[]) {
getDescendant(path: (string | number)[]) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let node: Node | Node[] | undefined = this;
for (const elem of path) {
if (typeof elem == "string") {
if (node instanceof Node) {
node = node.getChild(elem);
} else {
throw new Error("Invalid path at " + elem + ", expected node, got " + node);
}
} else if (Array.isArray(node)) {
node = node[elem];
let node: Node | undefined = this;
for (let i = 0; i < path.length; i++) {
const elem = path[i];
if (typeof elem !== "string") {
throw new Error("Invalid path at " + elem + ", expected node, got " + node);
}
if (i < path.length - 1 && typeof path[i + 1] === "number") {
node = node?.getChild(elem, path[i + 1] as number);
i++;
} else {
throw new Error("Invalid path at " + elem + ", expected children, got " + node);
node = node?.getChild(elem);
}
}
return node;
Expand Down
2 changes: 1 addition & 1 deletion tests/ecore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export class CompilationUnit extends Node {
expect(NODE_TYPES["SimpleMM"].nodes["StringLiteral"][SYMBOL_CLASS_DEFINITION]).to.equal(
`@ASTNode("SimpleMM", "StringLiteral")
export class StringLiteral extends Expression {
\t@Property()
\t@Attribute()
\tvalue;
}`);
node = new NODE_TYPES["SimpleMM"].nodes["StringLiteral"]() as any;
Expand Down
6 changes: 3 additions & 3 deletions tests/interop/lionweb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import FS_LANGUAGE_JSON from "./fs-language.json";
import FS_MODEL from "./fs-model.json";
import {expect} from "chai";
import {deserializeChunk, deserializeLanguages, SerializationChunk} from "@lionweb/core";
import {Children, Node, Property, TraceNode, walk} from "../../src";
import {Attribute, Children, Node, TraceNode, walk} from "../../src";
import {
findClassifier,
LanguageMapping, LionwebNode,
Expand All @@ -13,7 +13,7 @@ import {map, pipe, reduce} from "iter-ops";
import {STARLASU_LANGUAGE} from "../../src/interop/lionweb-starlasu-language";

abstract class File extends Node {
@Property()
@Attribute()
name: string;
}

Expand All @@ -23,7 +23,7 @@ class Directory extends File {
}

class TextFile extends File {
@Property()
@Attribute()
contents: string;
}

Expand Down
23 changes: 19 additions & 4 deletions tests/interop/workspace-transpilation-trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ describe('Workspace Transpilation traces', function() {
expect(sourceRoot.getSimpleType()).to.eql("CompilationUnit")
expect(sourceRoot.getPosition()).to.eql(new Position(new Point(1, 0), new Point(82, 18)))

const refExpr = pipe(sourceRoot.walkDescendants(),
let refExpr = pipe(sourceRoot.walkDescendants(),
filter((node: TraceNode) => node.getType() == "com.strumenta.rpgparser.model.ReferenceExpr"),
first()).first as TraceNode;
expect(refExpr).not.to.be.undefined;
expect(refExpr.getPathFromRoot()).to.eql(["mainStatements", 0, "expression", "target"]);
const refDef = refExpr.nodeDefinition.features["dataDefinition"];
let refDef = refExpr.nodeDefinition.features["dataDefinition"];
expect(refDef?.reference).to.be.true;
const reference = refExpr.getReference("dataDefinition");
let reference = refExpr.getReference("dataDefinition");
expect(reference).to.be.instanceof(ReferenceByName);
expect(reference?.name).to.equal("CNT");
const refTarget = reference?.referred;
let refTarget = reference?.referred;
expect(refTarget).to.be.instanceof(TraceNode);
expect(refTarget?.getType()).to.equal("com.strumenta.rpgparser.model.StandaloneField");
expect(refTarget?.name).to.equal("CNT");
Expand All @@ -130,6 +130,21 @@ describe('Workspace Transpilation traces', function() {
expect(refTarget?.getRoot()).to.equal(sourceRoot);
expect(refExpr.getAttributes()["dataDefinition"]).to.be.undefined;

refExpr = sourceRoot.getDescendant(["mainStatements", 4, "name"]) as TraceNode;
refDef = refExpr.nodeDefinition.features["dataDefinition"];
expect(refDef?.reference).to.be.true;
reference = refExpr.getReference("dataDefinition");
expect(reference).to.be.instanceof(ReferenceByName);
expect(reference?.name).to.equal("CUSTOMER");
refTarget = reference?.referred;
expect(refTarget).to.be.instanceof(TraceNode);
expect(refTarget?.getType()).to.equal("com.strumenta.rpgparser.model.ExternalFileDefinition");
expect(refTarget?.name).to.equal("CUSTOMER");
expect(refTarget?.getRole()).to.equal("file");
expect(refTarget?.getPathFromRoot()).to.eql(["externalDefinitions", 17, "record", "file"]);
expect(refTarget?.getRoot()).to.equal(sourceRoot);
expect(refExpr.getAttributes()["dataDefinition"]).to.be.undefined;

// TODO broken expect(cus300File.node.getChildren("dataDefinition").length).to.eql(4)
expect(sourceRoot.getChildren("mainStatements").length).to.eql(9)
const firstStatement = sourceRoot.getChildren("mainStatements")[0];
Expand Down
8 changes: 4 additions & 4 deletions tests/testing/testing.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from "chai";
import {assertASTsAreEqual} from "../../src/testing/testing";
import {ASTNode, Children, Node, Point, Position, PossiblyNamed, Property} from "../../src";
import {ASTNode, Attribute, Children, Node, Point, Position, PossiblyNamed} from "../../src";

describe('AssertASTsAreEqual', function() {
it("the very same node instance compared with itself must pass", function () {
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('AssertASTsAreEqual', function() {

@ASTNode("", "SimpleNode")
class SimpleNode extends Node implements PossiblyNamed {
@Property() public name?: string;
@Attribute() public name?: string;
@Children() public subTree: Node[];
constructor(name?: string, subTree: Node[] = []) {
super();
Expand All @@ -110,7 +110,7 @@ class SimpleNode extends Node implements PossiblyNamed {

@ASTNode("", "AnotherSimpleNode")
class AnotherSimpleNode extends Node implements PossiblyNamed {
@Property() public name?: string;
@Attribute() public name?: string;
@Children() public subTree: Node[];
constructor(name?: string, subTree: Node[] = []) {
super();
Expand All @@ -121,7 +121,7 @@ class AnotherSimpleNode extends Node implements PossiblyNamed {

@ASTNode("", "NodeWithStringSubTree")
class NodeWithStringSubTree extends Node implements PossiblyNamed {
@Property() public name?: string;
@Attribute() public name?: string;
@Children() public subTree?: string;
constructor(name?: string, subTree?: string) {
super();
Expand Down
5 changes: 2 additions & 3 deletions tests/transformation/transformation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import {expect} from "chai";

import {
ASTNode,
ASTTransformer,
ASTTransformer, Attribute,
Child,
ErrorNode, GenericErrorNode,
IssueSeverity,
Node,
pos,
Property,
} from "../../src";

@ASTNode("", "A")
Expand All @@ -31,7 +30,7 @@ class A extends Node {
class B extends Node {
@Child()
aChild: Node;
@Property()
@Attribute()
property: number;
other2: string;
notThere: any;
Expand Down

0 comments on commit 82c86da

Please sign in to comment.