From 5907cce58e60e0260167bb9c8422017f7ea52cf9 Mon Sep 17 00:00:00 2001 From: Mark Sujew Date: Thu, 29 Aug 2024 11:57:01 +0000 Subject: [PATCH] Apply set and sorting --- .../src/language-server/generated/ast.ts | 15 +++++- .../src/language-server/generated/ast.ts | 11 +++- .../src/language-server/generated/ast.ts | 13 ++++- .../src/language-server/generated/ast.ts | 12 ++++- .../src/generator/ast-generator.ts | 13 ++--- .../langium/src/languages/generated/ast.ts | 52 ++++++++++++++++++- 6 files changed, 105 insertions(+), 11 deletions(-) diff --git a/examples/arithmetics/src/language-server/generated/ast.ts b/examples/arithmetics/src/language-server/generated/ast.ts index a3ce178ad..7b96eae0d 100644 --- a/examples/arithmetics/src/language-server/generated/ast.ts +++ b/examples/arithmetics/src/language-server/generated/ast.ts @@ -17,7 +17,20 @@ export const ArithmeticsTerminals = { export type ArithmeticsTerminalNames = keyof typeof ArithmeticsTerminals; -export type ArithmeticsKeywordNames = "%" | "(" | ")" | "*" | "+" | "," | "-" | "/" | ":" | ";" | "^" | "def" | "module"; +export type ArithmeticsKeywordNames = + | "%" + | "(" + | ")" + | "*" + | "+" + | "," + | "-" + | "/" + | ":" + | ";" + | "^" + | "def" + | "module"; export type ArithmeticsTokenNames = ArithmeticsTerminalNames | ArithmeticsKeywordNames; diff --git a/examples/domainmodel/src/language-server/generated/ast.ts b/examples/domainmodel/src/language-server/generated/ast.ts index 664058bb1..f777fde95 100644 --- a/examples/domainmodel/src/language-server/generated/ast.ts +++ b/examples/domainmodel/src/language-server/generated/ast.ts @@ -16,7 +16,16 @@ export const DomainModelTerminals = { export type DomainModelTerminalNames = keyof typeof DomainModelTerminals; -export type DomainModelKeywordNames = "." | ":" | "datatype" | "entity" | "extends" | "many" | "package" | "{" | "}"; +export type DomainModelKeywordNames = + | "." + | ":" + | "datatype" + | "entity" + | "extends" + | "many" + | "package" + | "{" + | "}"; export type DomainModelTokenNames = DomainModelTerminalNames | DomainModelKeywordNames; diff --git a/examples/requirements/src/language-server/generated/ast.ts b/examples/requirements/src/language-server/generated/ast.ts index 6ab1c8303..bca51919c 100644 --- a/examples/requirements/src/language-server/generated/ast.ts +++ b/examples/requirements/src/language-server/generated/ast.ts @@ -17,7 +17,18 @@ export const RequirementsAndTestsTerminals = { export type RequirementsAndTestsTerminalNames = keyof typeof RequirementsAndTestsTerminals; -export type RequirementsAndTestsKeywordNames = "," | ":" | "applicable" | "contact" | "environment" | "for" | "req" | "," | ":" | "=" | "applicable" | "contact" | "for" | "testFile" | "tests" | "tst"; +export type RequirementsAndTestsKeywordNames = + | "," + | ":" + | "=" + | "applicable" + | "contact" + | "environment" + | "for" + | "req" + | "testFile" + | "tests" + | "tst"; export type RequirementsAndTestsTokenNames = RequirementsAndTestsTerminalNames | RequirementsAndTestsKeywordNames; diff --git a/examples/statemachine/src/language-server/generated/ast.ts b/examples/statemachine/src/language-server/generated/ast.ts index a98f93aff..7c0df73e3 100644 --- a/examples/statemachine/src/language-server/generated/ast.ts +++ b/examples/statemachine/src/language-server/generated/ast.ts @@ -16,7 +16,17 @@ export const StatemachineTerminals = { export type StatemachineTerminalNames = keyof typeof StatemachineTerminals; -export type StatemachineKeywordNames = "=>" | "actions" | "commands" | "end" | "events" | "initialState" | "state" | "statemachine" | "{" | "}"; +export type StatemachineKeywordNames = + | "=>" + | "actions" + | "commands" + | "end" + | "events" + | "initialState" + | "state" + | "statemachine" + | "{" + | "}"; export type StatemachineTokenNames = StatemachineTerminalNames | StatemachineKeywordNames; diff --git a/packages/langium-cli/src/generator/ast-generator.ts b/packages/langium-cli/src/generator/ast-generator.ts index ba93abe93..4b55c325b 100644 --- a/packages/langium-cli/src/generator/ast-generator.ts +++ b/packages/langium-cli/src/generator/ast-generator.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ import type { Grammar, LangiumCoreServices } from 'langium'; -import { type Generated, expandToNode, joinToNode, toString } from 'langium/generate'; +import { EOL, type Generated, expandToNode, joinToNode, toString } from 'langium/generate'; import type { AstTypes, Property, PropertyDefaultValue } from 'langium/grammar'; import type { LangiumConfig } from '../package-types.js'; import { AstUtils, MultiMap, GrammarAST } from 'langium'; @@ -231,15 +231,16 @@ function groupBySupertypes(astTypes: AstTypes): MultiMap { function generateTerminalConstants(grammars: Grammar[], config: LangiumConfig): Generated { let collection: Record = {}; - const keywordTokens: string[] = []; + const keywordTokens = new Set(); grammars.forEach(grammar => { const terminalConstants = collectTerminalRegexps(grammar); collection = {...collection, ...terminalConstants}; - const keywords = collectKeywords(grammar); - keywordTokens.push(...keywords); + for (const keyword of collectKeywords(grammar)) { + keywordTokens.add(keyword); + } }); - const keywordStrings = keywordTokens.map((keyword) => JSON.stringify(keyword)); + const keywordStrings = Array.from(keywordTokens).sort().map((keyword) => JSON.stringify(keyword)); return expandToNode` export const ${config.projectName}Terminals = { @@ -248,7 +249,7 @@ function generateTerminalConstants(grammars: Grammar[], config: LangiumConfig): export type ${config.projectName}TerminalNames = keyof typeof ${config.projectName}Terminals; - export type ${config.projectName}KeywordNames = ${keywordStrings.length > 0 ? keywordStrings.join(' | ') : 'never'}; + export type ${config.projectName}KeywordNames = ${keywordStrings.length > 0 ? keywordStrings.map(keyword => `${EOL} | ${keyword}`).join('') : 'never'}; export type ${config.projectName}TokenNames = ${config.projectName}TerminalNames | ${config.projectName}KeywordNames; `.appendNewLine(); diff --git a/packages/langium/src/languages/generated/ast.ts b/packages/langium/src/languages/generated/ast.ts index f1246663b..41219f074 100644 --- a/packages/langium/src/languages/generated/ast.ts +++ b/packages/langium/src/languages/generated/ast.ts @@ -19,7 +19,57 @@ export const LangiumGrammarTerminals = { export type LangiumGrammarTerminalNames = keyof typeof LangiumGrammarTerminals; -export type LangiumGrammarKeywordNames = "!" | "&" | "(" | ")" | "*" | "+" | "+=" | "," | "->" | "." | ".." | ":" | ";" | "<" | "=" | "=>" | ">" | "?" | "?!" | "?" + | "." + | ".." + | ":" + | ";" + | "<" + | "=" + | "=>" + | ">" + | "?" + | "?!" + | "?