diff --git a/CHANGELOG.md b/CHANGELOG.md index a9bdf1d..387dbdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index c52dd7b..fda8fcb 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/interop/strumenta-playground.ts b/src/interop/strumenta-playground.ts index 76976e3..78b44e8 100644 --- a/src/interop/strumenta-playground.ts +++ b/src/interop/strumenta-playground.ts @@ -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, @@ -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( result: ParsingResult, name: string, @@ -51,52 +46,29 @@ export function saveForStrumentaPlayground( } 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"); } } @@ -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)))); } } diff --git a/src/trace/trace-node.ts b/src/trace/trace-node.ts index 33cbea7..e297438 100644 --- a/src/trace/trace-node.ts +++ b/src/trace/trace-node.ts @@ -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(); + } +} diff --git a/tests/interop/lionweb.test.ts b/tests/interop/lionweb.test.ts index fb16b5d..8636316 100644 --- a/tests/interop/lionweb.test.ts +++ b/tests/interop/lionweb.test.ts @@ -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, @@ -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 { @@ -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"); diff --git a/tests/interop/parser-trace.test.ts b/tests/interop/parser-trace.test.ts index 4a996e5..b2a2db9 100644 --- a/tests/interop/parser-trace.test.ts +++ b/tests/interop/parser-trace.test.ts @@ -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() {