Skip to content

Commit

Permalink
Refactor export mechanism to have a common grammar element instead of…
Browse files Browse the repository at this point in the history
… distributing the publish keyword
  • Loading branch information
georg-schwarz committed May 24, 2024
1 parent b94c29c commit 4fef7c4
Show file tree
Hide file tree
Showing 38 changed files with 489 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
type BlockDefinition,
type InternalValueRepresentation,
type JayveeServices,
type TypedConstraintDefinition,
createJayveeServices,
isTypedConstraintDefinition,
} from '@jvalue/jayvee-language-server';
import {
type ParseHelperOptions,
Expand All @@ -18,6 +18,7 @@ import {
import {
type AstNode,
type AstNodeLocator,
AstUtils,
type LangiumDocument,
} from 'langium';
import { NodeFileSystem } from 'langium/node';
Expand Down Expand Up @@ -51,10 +52,15 @@ describe('Validation of AllowlistConstraintExecutor', () => {
document.parseResult.value,
'pipelines@0/blocks@2',
) as BlockDefinition;
const constraint = locator.getAstNode<TypedConstraintDefinition>(
document.parseResult.value,
'constraints@0',
) as TypedConstraintDefinition;

const allElements = AstUtils.streamAllContents(document.parseResult.value);
const allConstraints = [...allElements.filter(isTypedConstraintDefinition)];
expect(
allConstraints.length > 0,
'No constraint definition found in test file',
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const constraint = allConstraints[0]!;

return new AllowlistConstraintExecutor().isValid(
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
type BlockDefinition,
type InternalValueRepresentation,
type JayveeServices,
type TypedConstraintDefinition,
createJayveeServices,
isTypedConstraintDefinition,
} from '@jvalue/jayvee-language-server';
import {
type ParseHelperOptions,
Expand All @@ -18,6 +18,7 @@ import {
import {
type AstNode,
type AstNodeLocator,
AstUtils,
type LangiumDocument,
} from 'langium';
import { NodeFileSystem } from 'langium/node';
Expand Down Expand Up @@ -51,10 +52,15 @@ describe('Validation of DenylistConstraintExecutor', () => {
document.parseResult.value,
'pipelines@0/blocks@2',
) as BlockDefinition;
const constraint = locator.getAstNode<TypedConstraintDefinition>(
document.parseResult.value,
'constraints@0',
) as TypedConstraintDefinition;

const allElements = AstUtils.streamAllContents(document.parseResult.value);
const allConstraints = [...allElements.filter(isTypedConstraintDefinition)];
expect(
allConstraints.length > 0,
'No constraint definition found in test file',
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const constraint = allConstraints[0]!;

return new DenylistConstraintExecutor().isValid(
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import {
type BlockDefinition,
type ExpressionConstraintDefinition,
type InternalValueRepresentation,
type JayveeServices,
createJayveeServices,
isExpressionConstraintDefinition,
} from '@jvalue/jayvee-language-server';
import {
type ParseHelperOptions,
Expand All @@ -18,6 +18,7 @@ import {
import {
type AstNode,
type AstNodeLocator,
AstUtils,
type LangiumDocument,
} from 'langium';
import { NodeFileSystem } from 'langium/node';
Expand Down Expand Up @@ -51,10 +52,17 @@ describe('Validation of AllowlistConstraintExecutor', () => {
document.parseResult.value,
'pipelines@0/blocks@2',
) as BlockDefinition;
const constraint = locator.getAstNode<ExpressionConstraintDefinition>(
document.parseResult.value,
'constraints@0',
) as ExpressionConstraintDefinition;

const allElements = AstUtils.streamAllContents(document.parseResult.value);
const allConstraints = [
...allElements.filter(isExpressionConstraintDefinition),
];
expect(
allConstraints.length > 0,
'No constraint definition found in test file',
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const constraint = allConstraints[0]!;

return new ExpressionConstraintExecutor(constraint).isValid(
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
type BlockDefinition,
type InternalValueRepresentation,
type JayveeServices,
type TypedConstraintDefinition,
createJayveeServices,
isTypedConstraintDefinition,
} from '@jvalue/jayvee-language-server';
import {
type ParseHelperOptions,
Expand All @@ -18,6 +18,7 @@ import {
import {
type AstNode,
type AstNodeLocator,
AstUtils,
type LangiumDocument,
} from 'langium';
import { NodeFileSystem } from 'langium/node';
Expand Down Expand Up @@ -51,10 +52,15 @@ describe('Validation of LengthConstraintExecutor', () => {
document.parseResult.value,
'pipelines@0/blocks@2',
) as BlockDefinition;
const constraint = locator.getAstNode<TypedConstraintDefinition>(
document.parseResult.value,
'constraints@0',
) as TypedConstraintDefinition;

const allElements = AstUtils.streamAllContents(document.parseResult.value);
const allConstraints = [...allElements.filter(isTypedConstraintDefinition)];
expect(
allConstraints.length > 0,
'No constraint definition found in test file',
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const constraint = allConstraints[0]!;

return new LengthConstraintExecutor().isValid(
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
type BlockDefinition,
type InternalValueRepresentation,
type JayveeServices,
type TypedConstraintDefinition,
createJayveeServices,
initializeWorkspace,
isTypedConstraintDefinition,
} from '@jvalue/jayvee-language-server';
import {
type ParseHelperOptions,
Expand All @@ -19,6 +19,7 @@ import {
import {
type AstNode,
type AstNodeLocator,
AstUtils,
type LangiumDocument,
} from 'langium';
import { NodeFileSystem } from 'langium/node';
Expand Down Expand Up @@ -52,10 +53,15 @@ describe('Validation of RangeConstraintExecutor', () => {
document.parseResult.value,
'pipelines@0/blocks@2',
) as BlockDefinition;
const constraint = locator.getAstNode<TypedConstraintDefinition>(
document.parseResult.value,
'constraints@0',
) as TypedConstraintDefinition;

const allElements = AstUtils.streamAllContents(document.parseResult.value);
const allConstraints = [...allElements.filter(isTypedConstraintDefinition)];
expect(
allConstraints.length > 0,
'No constraint definition found in test file',
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const constraint = allConstraints[0]!;

return new RangeConstraintExecutor().isValid(
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
type BlockDefinition,
type InternalValueRepresentation,
type JayveeServices,
type TypedConstraintDefinition,
createJayveeServices,
isTypedConstraintDefinition,
} from '@jvalue/jayvee-language-server';
import {
type ParseHelperOptions,
Expand All @@ -18,6 +18,7 @@ import {
import {
type AstNode,
type AstNodeLocator,
AstUtils,
type LangiumDocument,
} from 'langium';
import { NodeFileSystem } from 'langium/node';
Expand Down Expand Up @@ -51,10 +52,15 @@ describe('Validation of RegexConstraintExecutor', () => {
document.parseResult.value,
'pipelines@0/blocks@2',
) as BlockDefinition;
const constraint = locator.getAstNode<TypedConstraintDefinition>(
document.parseResult.value,
'constraints@0',
) as TypedConstraintDefinition;

const allElements = AstUtils.streamAllContents(document.parseResult.value);
const allConstraints = [...allElements.filter(isTypedConstraintDefinition)];
expect(
allConstraints.length > 0,
'No constraint definition found in test file',
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const constraint = allConstraints[0]!;

return new RegexConstraintExecutor().isValid(
value,
Expand Down
12 changes: 7 additions & 5 deletions libs/execution/src/lib/transforms/transform-executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import path from 'node:path';
import {
type InternalValueRepresentation,
type JayveeServices,
type TransformDefinition,
createJayveeServices,
isTransformDefinition,
} from '@jvalue/jayvee-language-server';
import {
type ParseHelperOptions,
Expand All @@ -22,6 +22,7 @@ import {
import {
type AstNode,
type AstNodeLocator,
AstUtils,
type LangiumDocument,
} from 'langium';
import { NodeFileSystem } from 'langium/node';
Expand Down Expand Up @@ -77,10 +78,11 @@ describe('Validation of TransformExecutor', () => {
const document = await parse(input, { validation: true });
expectNoParserAndLexerErrors(document);

const transform = locator.getAstNode<TransformDefinition>(
document.parseResult.value,
'transforms@0',
) as TransformDefinition;
const allElements = AstUtils.streamAllContents(document.parseResult.value);
const allTransforms = [...allElements.filter(isTransformDefinition)];
expect(allTransforms.length > 0);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const transform = allTransforms[0]!;

const executionContext = getTestExecutionContext(
locator,
Expand Down
4 changes: 2 additions & 2 deletions libs/language-server/src/grammar/block-type.langium
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ ReferenceableBlockTypeDefinition:
BuiltinBlockTypeDefinition | CompositeBlockTypeDefinition;

BuiltinBlockTypeDefinition:
(isPublished?='publish')? 'builtin' 'blocktype' name=ID '{'
'builtin' 'blocktype' name=ID '{'
(inputs+=BlockTypeInput | outputs+=BlockTypeOutput | properties+=BlockTypeProperty)*
'}';

CompositeBlockTypeDefinition:
(isPublished?='publish')? 'composite' 'blocktype' name=ID '{'
'composite' 'blocktype' name=ID '{'
(
inputs+=BlockTypeInput
| outputs+=BlockTypeOutput
Expand Down
6 changes: 3 additions & 3 deletions libs/language-server/src/grammar/constraint.langium
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ ConstraintDefinition:
TypedConstraintDefinition | ExpressionConstraintDefinition;

TypedConstraintDefinition:
(isPublished?='publish')? 'constraint' name=ID 'oftype' type=[BuiltinConstrainttypeDefinition] body=PropertyBody;
'constraint' name=ID 'oftype' type=[BuiltinConstrainttypeDefinition] body=PropertyBody;

ExpressionConstraintDefinition:
(isPublished?='publish')? 'constraint' name=ID 'on' valueType=ValueTypeReference ':' expression=Expression ';';
'constraint' name=ID 'on' valueType=ValueTypeReference ':' expression=Expression ';';

BuiltinConstrainttypeDefinition:
(isPublished?='publish')? 'builtin' 'constrainttype' name=ID 'on' valueType=ValueTypeReference '{'
'builtin' 'constrainttype' name=ID 'on' valueType=ValueTypeReference '{'
(properties+=ConstrainttypeProperty)*
'}';

Expand Down
2 changes: 1 addition & 1 deletion libs/language-server/src/grammar/io-type.langium
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import './expression'
import './terminal'

IotypeDefinition:
(isPublished?='publish')? 'builtin' 'iotype' name=ID ';';
'builtin' 'iotype' name=ID ';';
27 changes: 10 additions & 17 deletions libs/language-server/src/grammar/main.langium
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,20 @@ entry JayveeModel:
(
imports+=ImportDefinition
| exports+=ExportDefinition
| exportableElements+=ExportableElementDefinition
| pipelines+=PipelineDefinition
| valueTypes+=(CustomValuetypeDefinition | BuiltinValuetypeDefinition)
| constraints+=ConstraintDefinition
| transforms+=TransformDefinition
| blockTypes+=ReferenceableBlockTypeDefinition
| constrainttypes+=BuiltinConstrainttypeDefinition
| iotypes+=IotypeDefinition
)*;

// When adding an element here, make sure the element has the following prependix in its rule
// (isPublished?='publish')?
// TypeScript lets us then infer that there is a field `isPublished` after using the type guard `isExportableElement`.
ExportableElement:
// Must ref rules where isPublished is defined
// So don't use aggregates like ReferenceableBlockTypeDefinition
// Otherwise it doesn't work to resolve the refenerces
ExportableElementDefinition:
(isPublished?='publish')? ExportableElement;

ExportableElement:
(CustomValuetypeDefinition | BuiltinValuetypeDefinition)
| (TypedConstraintDefinition | ExpressionConstraintDefinition | BuiltinConstrainttypeDefinition)
| TransformDefinition
| (BuiltinBlockTypeDefinition | CompositeBlockTypeDefinition)
| IotypeDefinition;
| ConstraintDefinition
| TransformDefinition
| ReferenceableBlockTypeDefinition
| BuiltinConstrainttypeDefinition
| IotypeDefinition;

ExportDefinition:
'publish' element=[ExportableElement] ';';
Expand Down
2 changes: 1 addition & 1 deletion libs/language-server/src/grammar/transform.langium
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'value-type'
import 'expression'

TransformDefinition:
(isPublished?='publish')? 'transform' name=ID body=TransformBody;
'transform' name=ID body=TransformBody;

TransformBody:
'{' (ports+=TransformPortDefinition)* (outputAssignments+=TransformOutputAssignment)* '}';
Expand Down
4 changes: 2 additions & 2 deletions libs/language-server/src/grammar/value-type.langium
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import './expression'
import './terminal'

BuiltinValuetypeDefinition infers ValuetypeDefinition:
(isPublished?='publish')? isBuiltin?='builtin' 'valuetype' name=ID
isBuiltin?='builtin' 'valuetype' name=ID
(genericDefinition=ValuetypeGenericsDefinition)?
';';

CustomValuetypeDefinition infers ValuetypeDefinition:
(isPublished?='publish')? 'valuetype' name=ID
'valuetype' name=ID
(genericDefinition=ValuetypeGenericsDefinition)?
'oftype' type=ValueTypeReference '{'
'constraints' ':' constraints=CollectionLiteral ';'
Expand Down
Loading

0 comments on commit 4fef7c4

Please sign in to comment.