Skip to content

Commit

Permalink
ParserNode instances usable without ECore
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiostalla committed Mar 7, 2024
1 parent 6dbf811 commit e733053
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
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.11] – 2024-03-07

### Changed
- ParserNode instances usable without ECore

## [1.6.10] – 2024-03-07

### 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.10",
"version": "1.6.11",
"license": "Apache-2.0",
"keywords": [
"antlr",
Expand Down
50 changes: 11 additions & 39 deletions src/interop/strumenta-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import {Node} from "../model/model";
import ECore from "ecore/dist/ecore";
import {Position} from "../model/position";
import {PARSER_EPACKAGE, PARSER_TRACE_ECLASS} from "./parser-package";
import {
THE_AST_EPACKAGE,
THE_NODE_ORIGIN_ECLASS,
THE_RESULT_ECLASS as THE_RESULT_ECLASS_V2
} from "./starlasu-v2-metamodel";
import {THE_RESULT_ECLASS as THE_RESULT_ECLASS_V1} from "./kolasu-v1-metamodel";
import {THE_AST_EPACKAGE, THE_NODE_ORIGIN_ECLASS,} from "./starlasu-v2-metamodel";
import {Issue} from "../validation";
import {
THE_TRANSPILATION_TRACE_ECLASS,
Expand All @@ -19,7 +14,7 @@ import {
} from "./transpilation-package";
import {TRANSPILATION_EPACKAGE_V1} from "./transpilation-package-v1";
import {ensureEcoreContainsAllDataTypes} from "./ecore-patching";
import {NodeAdapter, TraceNode} from "../trace/trace-node";
import {NodeAdapter, ParserNode, TraceNode} from "../trace/trace-node";

export function saveForStrumentaPlayground<R extends Node>(
result: ParsingResult<R>, name: string,
Expand Down Expand Up @@ -51,52 +46,29 @@ export function saveForStrumentaPlayground<R extends Node>(
}

export class ParserTrace {
constructor(private eo: ECore.EObject) {
if (!eo.eClass == PARSER_TRACE_ECLASS) {
throw new Error("Not a parser trace: " + eo.eClass);
constructor(private node: NodeAdapter) {
if (node.nodeDefinition?.package != "StrumentaLanguageSupportParsing" || node.nodeDefinition?.name != "ParserTrace") {
throw new Error("Not a parser trace: " + node.nodeDefinition?.package + "." + node.nodeDefinition?.name);
}
}

get rootNode(): ParserNode {
let root;
const ast = this.eo.get("ast");
if (ast.eClass == THE_RESULT_ECLASS_V2 || ast.eClass == THE_RESULT_ECLASS_V1) {
const ast = this.node.get("ast");
if (ast?.nodeDefinition?.name == "Result") {
root = ast.get("root");
} else {
root = ast;
}
return new ParserNode(new ECoreNode(root), undefined, this);
return new ParserNode(root);
}

get issues(): Issue[] {
const ast = this.eo.get("ast");
if (ast.eClass == THE_RESULT_ECLASS_V2 || ast.eClass == THE_RESULT_ECLASS_V1) {
return fromEObject(ast.get("issues")) as Issue[] || [];
} else {
return [];
}
return this.node.get("ast")?.getIssues() || [];
}

get name(): string | undefined {
return this.eo.get("name");
}
}

export class ParserNode extends TraceNode {

parent?: ParserNode;

constructor(inner: NodeAdapter, parent: ParserNode | undefined, protected trace: ParserTrace) {
super(inner);
this.parent = parent;
}

getChildren(role?: string): ParserNode[] {
return this.nodeAdapter.getChildren(role).map((c) => new ParserNode(c, this, this.trace));
}

get children(): Node[] {
return this.getChildren();
return this.node.getAttribute("name");
}
}

Expand Down Expand Up @@ -154,7 +126,7 @@ export class ParserTraceLoader {
const resource = this.resourceSet.create({uri: uri});
return withLanguageMetamodel(
this.languages, language, this.resourceSet, resource,
() => new ParserTrace(loadEObject(trace, resource, PARSER_TRACE_ECLASS)));
() => new ParserTrace(new ECoreNode(loadEObject(trace, resource, PARSER_TRACE_ECLASS))));
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/trace/trace-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,23 @@ export abstract class TraceNode extends Node {
return this.nodeAdapter.isStatement();
}
}

export class ParserNode extends TraceNode {

parent?: ParserNode;

constructor(inner: NodeAdapter) {
super(inner);
if (inner.parent) {
this.parent = new ParserNode(inner.parent);
}
}

getChildren(role?: string): ParserNode[] {
return this.nodeAdapter.getChildren(role).map((c) => new ParserNode(c));
}

get children(): ParserNode[] {
return this.getChildren();
}
}
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, walk} from "../../src";
import {Children, Node, ParserNode, Property, walk} from "../../src";
import {
findClassifier,
LanguageMapping, LionwebNode,
Expand All @@ -11,7 +11,7 @@ import {
} from "../../src/interop/lionweb";
import {map, pipe, reduce} from "iter-ops";
import {STARLASU_LANGUAGE} from "../../src/interop/lionweb-starlasu-language";
import {ParserNode, ParserTrace} from "../../src/interop/strumenta-playground";
import {ParserTrace} from "../../src/interop/strumenta-playground";
import {PARSER_TRACE_ECLASS} from "../../src/interop/parser-package";

abstract class File extends Node {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Lionweb integration', function() {
expect(nodes.length).to.equal(1);
const root = nodes[0];
expect(root.node).to.be.instanceof(LionwebNode);
let dir = new ParserNode(root.node as LionwebNode, undefined, new ParserTrace(PARSER_TRACE_ECLASS.create({}))); // TODO
let dir = new ParserNode(root.node as LionwebNode);
expect(dir.getRole()).to.be.undefined;
expect(dir.nodeDefinition.name).to.equal("Directory");
expect(dir.getAttribute("name")).to.equal("resources.zip");
Expand Down
4 changes: 2 additions & 2 deletions tests/interop/parser-trace.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from "chai";
import * as fs from "fs";
import {findByPosition, IssueSeverity, IssueType, Point, pos, Position} from "../../src";
import {ParserNode, ParserTraceLoader} from "../../src/interop/strumenta-playground";
import {findByPosition, IssueSeverity, IssueType, ParserNode, Point, pos, Position} from "../../src";
import {ParserTraceLoader} from "../../src/interop/strumenta-playground";
import {TraceNode} from "../../src/trace/trace-node";

describe('Parser traces – Kolasu metamodel V1', function() {
Expand Down

0 comments on commit e733053

Please sign in to comment.