Skip to content

Commit

Permalink
Remove deletion option and outdated instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lotes committed Aug 15, 2024
1 parent d4476b0 commit 9b0316b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 131 deletions.
2 changes: 1 addition & 1 deletion packages/langium/src/default-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function createDefaultCoreModule(context: DefaultCoreModuleContext): Modu
References: (services) => new DefaultReferences(services)
},
serializer: {
AstDisassembler: (services) => new DefaultAstDisassembler(services, false),
AstDisassembler: (services) => new DefaultAstDisassembler(services),
AstReassembler: (services) => new DefaultAstReassembler(services),
Hydrator: (services) => new DefaultHydrator(services),
JsonSerializer: (services) => new DefaultJsonSerializer(services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@ import { streamAst } from '../../utils/ast-utils.js';
import { BiMap } from '../../utils/collections.js';

export enum InstructionType {
//setup
Allocate,
Element,
TokenType,
Property,
PropertyArray,
LinkNode,
LinkNodeArray,
Reference,
ReferenceArray,
Empty,
Error,
Return,

//CST
RootCstNode,
CompositeCstNode,
LeafCstNode,
PopCstNode
PopCstNode,

//AST
Property,
PropertyArray,
LinkNode,
LinkNodeArray,
Reference,
ReferenceArray,
Empty
}
export enum NodeType {
Cst,
Expand All @@ -50,18 +52,6 @@ export namespace Instructions {
cstNodeCount: number;
astNodeCount: number;
}
export interface TokenType extends AstAssemblerInstructionBase {
$type: InstructionType.TokenType;
sourceId: number;
property: string;
tokenName: string;
}
export interface Element extends AstAssemblerInstructionBase {
$type: InstructionType.Element;
sourceId: number;
property: string;
value: number;
}
export interface Property extends AstAssemblerInstructionBase {
$type: InstructionType.Property;
sourceId: number;
Expand Down Expand Up @@ -141,10 +131,9 @@ export namespace Instructions {
}
}

export type AstAssemblerInstruction = Instructions.Allocate
| Instructions.TokenType
export type AstAssemblerInstruction =
| Instructions.Allocate
| Instructions.Property
| Instructions.Element
| Instructions.PropertyArray
| Instructions.Reference
| Instructions.ReferenceArray
Expand All @@ -153,7 +142,6 @@ export type AstAssemblerInstruction = Instructions.Allocate
| Instructions.Empty
| Instructions.Error
| Instructions.Return

| Instructions.RootCstNode
| Instructions.CompositeCstNode
| Instructions.LeafCstNode
Expand Down
152 changes: 56 additions & 96 deletions packages/langium/src/serializer/reassembler/AstDisassembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ export class DefaultAstDisassembler implements AstDisassembler {
private readonly cstNodeToId = new Map<CstNode, number>();
private readonly astNodeToId = new Map<AstNode, number>();
private readonly grammarElementIdMap = new BiMap<AbstractElement, number>();
private readonly deleteOriginal: boolean;

constructor(services: LangiumCoreServices, deleteOriginal: boolean) {
this.deleteOriginal = deleteOriginal;
constructor(services: LangiumCoreServices) {
this.grammarElementIdMap = createGrammarElementIdMap(services.Grammar);
}

Expand Down Expand Up @@ -89,96 +87,55 @@ export class DefaultAstDisassembler implements AstDisassembler {
assertType<Mutable<AstNode>>(node);
const sourceId = this.astNodeToId.get(node)!;

const cleanUp = (property: string) => {
if (this.deleteOriginal) {
assertType<Record<string, undefined>>(node);
node[property] = undefined!;
}
};

const setProperty = (property: string, value: number | boolean | string | bigint) => {
const instr = <Instructions.Property>{
$type: InstructionType.Property,
sourceId,
property,
value
};
if (this.deleteOriginal) {
assertType<Record<string, undefined>>(node);
node[property] = undefined!;
}
return instr;
};

const setProperties = (property: string, values: Array<number | boolean | string | bigint>) => {
const instr = <Instructions.PropertyArray>{
$type: InstructionType.PropertyArray,
sourceId,
property,
values
};
cleanUp(property);
return instr;
};

const setLink = (property: string, type: NodeType, index: number) => {
const instr = <Instructions.LinkNode>{
$type: InstructionType.LinkNode,
sourceId,
targetKind: type,
targetId: index,
property,
};
if (this.deleteOriginal) {
assertType<Record<string, undefined>>(node);
node[property] = undefined!;
}
return instr;
};

const setLinks = (property: string, type: NodeType, indices: number[]) => {
const instr = <Instructions.LinkNodeArray>{
$type: InstructionType.LinkNodeArray,
sourceId,
targetKind: type,
targetIds: indices,
property,
};
cleanUp(property);
return instr;
};

const setReferences = (property: string, references: ReferenceData[]) => {
const instr = <Instructions.ReferenceArray>{
$type: InstructionType.ReferenceArray,
sourceId,
property,
references
};
cleanUp(property);
return instr;
};

const setReference = (property: string, reference: ReferenceData) => {
const instr = <Instructions.Reference>{
$type: InstructionType.Reference,
sourceId,
property,
...reference
};
cleanUp(property);
return instr;
};

const setEmpty = (property: string) => {
const instr = <Instructions.Empty>{
$type: InstructionType.Empty,
sourceId,
property,
};
cleanUp(property);
return instr;
};
const setProperty = (property: string, value: number | boolean | string | bigint) => (<Instructions.Property>{
$type: InstructionType.Property,
sourceId,
property,
value
});

const setPropertyArray = (property: string, values: Array<number | boolean | string | bigint>) => (<Instructions.PropertyArray>{
$type: InstructionType.PropertyArray,
sourceId,
property,
values
});

const setLink = (property: string, type: NodeType, index: number) => (<Instructions.LinkNode>{
$type: InstructionType.LinkNode,
sourceId,
targetKind: type,
targetId: index,
property,
});

const setLinkArray = (property: string, type: NodeType, indices: number[]) => (<Instructions.LinkNodeArray>{
$type: InstructionType.LinkNodeArray,
sourceId,
targetKind: type,
targetIds: indices,
property,
});

const setReferenceArray = (property: string, references: ReferenceData[]) => (<Instructions.ReferenceArray>{
$type: InstructionType.ReferenceArray,
sourceId,
property,
references
});

const setReference = (property: string, reference: ReferenceData) => (<Instructions.Reference>{
$type: InstructionType.Reference,
sourceId,
property,
...reference
});

const setEmpty = (property: string) => (<Instructions.Empty>{
$type: InstructionType.Empty,
sourceId,
property,
});

yield setProperty('$type', node.$type);
if (node.$containerIndex) {
Expand All @@ -187,6 +144,9 @@ export class DefaultAstDisassembler implements AstDisassembler {
if (node.$containerProperty) {
yield setProperty('$containerProperty', node.$containerProperty);
}
if (node.$container) {
yield setLink('$container', NodeType.Ast, this.astNodeToId.get(node.$container)!);
}
if (node.$cstNode !== undefined) {
yield setLink('$cstNode', NodeType.Cst, this.cstNodeToId.get(node.$cstNode)!);
}
Expand All @@ -199,16 +159,16 @@ export class DefaultAstDisassembler implements AstDisassembler {
const item = value[0];
if (isAstNode(item)) {
assertType<AstNode[]>(value);
yield setLinks(name, NodeType.Ast, value.map(v => this.astNodeToId.get(v)!));
yield setLinkArray(name, NodeType.Ast, value.map(v => this.astNodeToId.get(v)!));
} else if (isReference(item)) {
assertType<Reference[]>(value);
yield setReferences(name, value.map(v => (<ReferenceData>{
yield setReferenceArray(name, value.map(v => (<ReferenceData>{
refText: v.$refText,
refNode: v.$refNode ? this.cstNodeToId.get(v.$refNode) : undefined
})));
} else {
//type string[]: just to keep Typescript calm
yield setProperties(name, value as string[]);
yield setPropertyArray(name, value as string[]);
}
} else {
yield setEmpty(name);
Expand Down
8 changes: 0 additions & 8 deletions packages/langium/src/serializer/reassembler/AstReassembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ export class DefaultAstReassembler implements AstReassembler {
case InstructionType.Empty:
ctx.idToAstNode[instr.sourceId][instr.property] = [];
break;
case InstructionType.Element:
ctx.idToAstNode[instr.sourceId][instr.property] = ctx.elementToId.getKey(instr.value);
break;
case InstructionType.Property:
ctx.idToAstNode[instr.sourceId][instr.property] = instr.value;
break;
Expand Down Expand Up @@ -112,11 +109,6 @@ export class DefaultAstReassembler implements AstReassembler {
ctx.parserErrors.push({ ...instr.items } as unknown as IRecognitionException);
}
break;
case InstructionType.TokenType: {
const tokenType = this.grammarTokenTypeIdMap.getKey(instr.tokenName)!;
ctx.idToAstNode[instr.sourceId][instr.property] = tokenType;
break;
}
case InstructionType.RootCstNode: {
const index = ctx.nextFreeCstNode++;
const rootNode = ctx.idToCstNode[index] = new RootCstNodeImpl(instr.input);
Expand Down

0 comments on commit 9b0316b

Please sign in to comment.