Skip to content

Commit d71d9a1

Browse files
committed
Release version 1.6.1
1 parent ba1ce6d commit d71d9a1

File tree

9 files changed

+43
-257
lines changed

9 files changed

+43
-257
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
All notable changes to this project from version 1.2.0 upwards are documented in this file.
33
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
44

5+
## [1.6.1] – 2024-02-28
6+
7+
### Added
8+
- Proper export of the Lionweb interop module
9+
- Module documentation for all optional interop modules
10+
11+
### Removed
12+
- Deprecated AST transformation functions and decorators, that had been superseded by StarLasu AST Transformers.
13+
514
## [1.6.0] – 2024-02-28
615

716
## Added

jest.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const config = {
1818
coverageThreshold: {
1919
// Note: we have several deprecated functions that reduce coverage
2020
global: {
21-
statements: 88,
21+
statements: 90,
2222
branches: 84,
23-
functions: 76, // Note: our CI on Node 16 computes a lower number for functions, for some reason
24-
lines: 88,
23+
functions: 80,
24+
lines: 90,
2525
},
2626
},
2727
moduleDirectories: ["node_modules"],

package.json

+13-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "AST building blocks for TypeScript/JavaScript, part of the *lasu family, with optional integrations with ANTLR4 and Ecore.",
44
"author": "Strumenta s.r.l.",
55
"publisher": "strumenta",
6-
"version": "1.6.0",
6+
"version": "1.6.1",
77
"license": "Apache-2.0",
88
"keywords": [
99
"antlr",
@@ -49,6 +49,11 @@
4949
"require": "./dist/cjs/interop/ecore-enabled-parser.js",
5050
"types": "./dist/types/interop/ecore-enabled-parser.d.ts"
5151
},
52+
"./interop/lionweb": {
53+
"import": "./dist/esm/interop/lionweb.js",
54+
"require": "./dist/cjs/interop/lionweb.js",
55+
"types": "./dist/types/interop/lionweb.d.ts"
56+
},
5257
"./interop/strumenta-playground": {
5358
"import": "./dist/esm/interop/strumenta-playground.js",
5459
"require": "./dist/cjs/interop/strumenta-playground.js",
@@ -57,21 +62,12 @@
5762
},
5863
"typesVersions": {
5964
"*": {
60-
"parsing": [
61-
"dist/types/parsing/index.d.ts"
62-
],
63-
"mapping": [
64-
"dist/types/mapping.d.ts"
65-
],
66-
"interop/ecore": [
67-
"dist/types/interop/ecore.d.ts"
68-
],
69-
"interop/ecore-enabled-parser": [
70-
"dist/types/interop/ecore-enabled-parser.d.ts"
71-
],
72-
"interop/strumenta-playground": [
73-
"dist/types/interop/strumenta-playground.d.ts"
74-
]
65+
"parsing": ["dist/types/parsing/index.d.ts"],
66+
"mapping": ["dist/types/mapping.d.ts"],
67+
"interop/ecore": ["dist/types/interop/ecore.d.ts"],
68+
"interop/lionweb": ["dist/types/interop/lionweb.d.ts"],
69+
"interop/ecore-enabled-parser": ["dist/types/interop/ecore-enabled-parser.d.ts"],
70+
"interop/strumenta-playground": ["dist/types/interop/strumenta-playground.d.ts"]
7571
}
7672
},
7773
"browser": "dist/esm/index.js",
@@ -107,7 +103,7 @@
107103
"build": "yarn run build:esm && yarn run build:cjs",
108104
"build:esm": "tsc --module es6 --outDir dist/esm",
109105
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
110-
"extract-docs": "typedoc --out dist/docs src/index.ts src/parsing/index.ts src/mapping.ts src/interop/ecore.ts src/interop/ecore-enabled-parser.ts",
106+
"extract-docs": "typedoc --out dist/docs src/index.ts src/parsing/index.ts src/mapping.ts src/interop/ecore.ts src/interop/ecore-enabled-parser.ts src/interop/lionweb.ts",
111107
"lint": "eslint src tests",
112108
"build-test-parser": "antlr4ng -Dlanguage=TypeScript -o tests/parser -Xexact-output-dir -visitor tests/grammar/SimpleLangLexer.g4 tests/grammar/SimpleLangParser.g4",
113109
"test": "yarn run build-test-parser && node --experimental-vm-modules node_modules/jest/bin/jest.js",

src/interop/antlr4.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* ANTLR4 interoperability module. Requires the antlr4ng implementation.
3+
* @module interop/antlr4
4+
*/
15
export * from '../parsing/parse-tree';
26
export * from '../parsing/parsing';
37
export * from "../mapping";

src/interop/ecore.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* ECore interoperability module with ecore.js.
3+
* @module interop/ecore
4+
*/
15
import {
26
ensureNodeDefinition,
37
ensurePackage,

src/interop/lionweb.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Lionweb interoperability module.
3+
* @module interop/lionweb
4+
*/
15
import {
26
Classifier,
37
Containment, deserializeLanguages,

src/mapping.ts

+3-44
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,8 @@
1-
import {ParserRuleContext, ParseTree, TerminalNode} from "antlr4ng";
2-
import {Child, Node, NODE_DEFINITION_SYMBOL, Origin, registerNodeDefinition} from "./model/model";
3-
import {ASTTransformer, GenericNode, Mapped, registerNodeFactory, transform} from "./transformation/transformation";
1+
import {ParserRuleContext, TerminalNode} from "antlr4ng";
2+
import {Node, Origin} from "./model/model";
3+
import {ASTTransformer} from "./transformation/transformation";
44
import {ParseTreeOrigin} from "./parsing";
55

6-
/**
7-
* Registers the decorated node as a target for transformation from the given `type`.
8-
*
9-
* Note: this will eventually be integrated with Kolasu-style transformers.
10-
* @param type the type of the source node to map to this node.
11-
* @deprecated please use StarLasu AST transformers.
12-
*/
13-
export function ASTNodeFor<T extends ParseTree>(type: new (...args: any[]) => T) {
14-
return function (target: new () => Node): void {
15-
if(!target[NODE_DEFINITION_SYMBOL]) {
16-
registerNodeDefinition(target);
17-
}
18-
registerNodeFactory(type, () => new target());
19-
};
20-
}
21-
22-
//-------//
23-
// toAST //
24-
//-------//
25-
26-
/**
27-
* @deprecated please use StarLasu AST transformers.
28-
*/
29-
export function toAST(tree?: ParseTree | null, parent?: Node): Node | undefined {
30-
if (tree == null)
31-
tree = undefined;
32-
33-
const node = transform(tree, parent, toAST);
34-
if(node && !node.origin) { //Give a chance to custom factories to set a different node
35-
node.origin = new ParseTreeOrigin(tree);
36-
}
37-
return node;
38-
}
39-
40-
export class GenericParseTreeNode extends GenericNode {
41-
@Child()
42-
@Mapped("children")
43-
childNodes: GenericParseTreeNode[] = [];
44-
}
45-
46-
registerNodeFactory(ParserRuleContext, () => new GenericParseTreeNode());
476

487
/**
498
* Implements a transformation from an ANTLR parse tree (the output of the parser) to an AST (a higher-level

src/transformation/transformation.ts

+2-192
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import {
22
ASTNode,
3-
ensureNodeDefinition,
43
getNodeDefinition,
54
Node,
6-
NODE_DEFINITION_SYMBOL, NodeDefinition,
5+
NodeDefinition,
76
Origin,
8-
registerNodeDefinition,
9-
registerNodeProperty
107
} from "../model/model";
118
import {Issue, IssueSeverity} from "../validation";
129
import {Position} from "../model/position";
13-
import {ErrorNode, GenericErrorNode} from "../model/errors";
10+
import {GenericErrorNode} from "../model/errors";
1411

1512
export class PropertyRef<Obj, Value> {
1613
constructor(
@@ -393,197 +390,10 @@ export class ASTTransformer {
393390
}
394391
}
395392

396-
//-----------------------------------//
397-
// Factory and metadata registration //
398-
//-----------------------------------//
399-
400-
/**
401-
* @deprecated please use StarLasu AST transformers.
402-
*/
403-
export const NODE_FACTORY_SYMBOL = Symbol("nodeFactory");
404-
/**
405-
* @deprecated please use StarLasu AST transformers.
406-
*/
407-
export const INIT_SYMBOL = Symbol("init");
408-
409-
/**
410-
* @deprecated please use StarLasu AST transformers.
411-
*/
412-
export function registerNodeFactory<T>(type: new (...args: any[]) => T, factory: (tree: T) => Node): void {
413-
type.prototype[NODE_FACTORY_SYMBOL] = factory;
414-
}
415-
416-
/**
417-
* Marks a property of a node as mapped from a property of another node of a different name.
418-
* @deprecated to be removed, use ParseTreeToASTTranformer
419-
* @param type the source node's type.
420-
* @param propertyName the name of the target property.
421-
* @param path the path in the source node that will be mapped to the target property.
422-
*/
423-
export function registerPropertyMapping<T extends Node>(
424-
type: new (...args: any[]) => T, propertyName: string, path: string = propertyName): any {
425-
const propInfo: any = registerNodeProperty(type, propertyName);
426-
propInfo.path = path || propertyName;
427-
return propInfo;
428-
}
429-
430-
/**
431-
* @deprecated please use StarLasu AST transformers.
432-
*/
433-
export function registerInitializer<T extends Node>(type: new (...args: any[]) => T, methodName: string): void {
434-
type[INIT_SYMBOL] = methodName;
435-
}
436-
437-
//------------//
438-
// Decorators //
439-
//------------//
440-
441-
/**
442-
* @deprecated please use StarLasu AST transformers.
443-
*/
444-
export function NodeTransform<T extends Node>(type: new (...args: any[]) => T) {
445-
return function (target: new () => Node): void {
446-
if(!target[NODE_DEFINITION_SYMBOL]) {
447-
registerNodeDefinition(target);
448-
}
449-
registerNodeFactory(type, () => new target());
450-
};
451-
}
452-
453-
/**
454-
* Marks a property of a node as mapped from a property of another node of a different name.
455-
* @deprecated to be removed, use ASTTranformer.withChild
456-
* @param path the path in the source node that will be mapped to the target property.
457-
*/
458-
export function Mapped(path?: string): (target, methodName: string) => void {
459-
return function (target, methodName: string) {
460-
registerPropertyMapping(target, methodName, path);
461-
};
462-
}
463-
464-
/**
465-
* Decorator to register an initializer method on a Node. When a node is instantiated as the target of a
466-
* transformation, after its properties have been set, the transformer calls the init method, if any.
467-
* @param target the target type.
468-
* @param methodName the name of the init method.
469-
* @deprecated please use StarLasu AST transformers.
470-
*/
471-
// Since target is any-typed (see https://www.typescriptlang.org/docs/handbook/decorators.html),
472-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
473-
export function Init(target, methodName: string): void {
474-
registerInitializer(target, methodName);
475-
}
476-
477-
//-----------------//
478-
// Transformations //
479-
//-----------------//
480-
481-
/**
482-
* @deprecated please use StarLasu AST transformers.
483-
*/
484-
export function fillChildAST<FROM, TO extends Node>(
485-
node: TO, property: string, tree: FROM | undefined, transformer: (node: FROM) => TO | undefined): TO[] {
486-
const propDef: any = ensureNodeDefinition(node).properties[property];
487-
const propertyPath = propDef.path || property;
488-
if (propertyPath && propertyPath.length > 0) {
489-
const path = propertyPath.split(".");
490-
let error;
491-
for (const segment in path) {
492-
if (tree && (typeof(tree[path[segment]]) === "function")) {
493-
try {
494-
tree = tree[path[segment]]();
495-
} catch (e) {
496-
error = e;
497-
break;
498-
}
499-
} else if (tree && tree[path[segment]]) {
500-
tree = tree[path[segment]];
501-
} else {
502-
tree = undefined;
503-
break;
504-
}
505-
}
506-
if(error) {
507-
node[property] = new GenericErrorNode(error);
508-
} else if (tree) {
509-
if(propDef.child) {
510-
if (Array.isArray(tree)) {
511-
node[property] = [];
512-
for (const i in tree) {
513-
node[property].push(transformer(tree[i])?.withParent(node));
514-
}
515-
return node[property];
516-
} else {
517-
node[property] = transformer(tree)?.withParent(node);
518-
return [node[property]];
519-
}
520-
} else {
521-
node[property] = tree;
522-
}
523-
}
524-
}
525-
return [];
526-
}
527-
528-
function makeNode(factory, tree: unknown) {
529-
try {
530-
return factory(tree) as Node;
531-
} catch (e) {
532-
return new GenericErrorNode(e);
533-
}
534-
}
535-
536-
/**
537-
* @deprecated please use StarLasu AST transformers.
538-
*/
539-
export function transform(tree: unknown, parent?: Node, transformer: typeof transform = transform): Node | undefined {
540-
if (typeof tree !== "object" || !tree) {
541-
return undefined;
542-
}
543-
const factory = tree[NODE_FACTORY_SYMBOL];
544-
let node: Node;
545-
if (factory) {
546-
node = makeNode(factory, tree);
547-
const def = getNodeDefinition(node);
548-
if (def) {
549-
for (const p in def.properties) {
550-
fillChildAST(node, p, tree, transformer);
551-
}
552-
}
553-
const initFunction = node[INIT_SYMBOL];
554-
if (initFunction) {
555-
try {
556-
node[initFunction].call(node, tree);
557-
} catch (e) {
558-
node = new PartiallyInitializedNode(node, e);
559-
}
560-
}
561-
} else {
562-
node = new GenericNode();
563-
}
564-
return node.withParent(parent);
565-
}
566-
567393
@ASTNode("com.strumenta.tylasu.transformation", "GenericNode")
568394
export class GenericNode extends Node {
569395
constructor(parent?: Node) {
570396
super();
571397
this.parent = parent;
572398
}
573399
}
574-
575-
/**
576-
* @deprecated please use StarLasu AST transformers.
577-
*/
578-
export class PartiallyInitializedNode extends Node implements ErrorNode {
579-
message: string;
580-
581-
get position(): Position | undefined {
582-
return this.node.position;
583-
}
584-
585-
constructor(readonly node: Node, error: Error) {
586-
super();
587-
this.message = `Could not initialize node: ${error}`;
588-
}
589-
}

tests/mapping.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {expect} from "chai";
22

3-
import {ASTTransformer, Child, GenericErrorNode, GenericNode, Mapped, Node, Position} from "../src";
3+
import {ASTTransformer, Child, GenericErrorNode, GenericNode, Node, Position} from "../src";
44
import {SimpleLangLexer} from "./parser/SimpleLangLexer";
55
import {CharStreams, CommonTokenStream, ParserRuleContext} from "antlr4ng";
66
import {CompilationUnitContext, DisplayStmtContext, SetStmtContext, SimpleLangParser} from "./parser/SimpleLangParser";

0 commit comments

Comments
 (0)