Skip to content

Commit

Permalink
Apply set and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Aug 29, 2024
1 parent bf554ed commit 5907cce
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
15 changes: 14 additions & 1 deletion examples/arithmetics/src/language-server/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 10 additions & 1 deletion examples/domainmodel/src/language-server/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 12 additions & 1 deletion examples/requirements/src/language-server/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 11 additions & 1 deletion examples/statemachine/src/language-server/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 7 additions & 6 deletions packages/langium-cli/src/generator/ast-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -231,15 +231,16 @@ function groupBySupertypes(astTypes: AstTypes): MultiMap<string, string> {

function generateTerminalConstants(grammars: Grammar[], config: LangiumConfig): Generated {
let collection: Record<string, RegExp> = {};
const keywordTokens: string[] = [];
const keywordTokens = new Set<string>();
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 = {
Expand All @@ -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();
Expand Down
52 changes: 51 additions & 1 deletion packages/langium/src/languages/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,57 @@ export const LangiumGrammarTerminals = {

export type LangiumGrammarTerminalNames = keyof typeof LangiumGrammarTerminals;

export type LangiumGrammarKeywordNames = "!" | "&" | "(" | ")" | "*" | "+" | "+=" | "," | "->" | "." | ".." | ":" | ";" | "<" | "=" | "=>" | ">" | "?" | "?!" | "?<!" | "?<=" | "?=" | "@" | "Date" | "EOF" | "[" | "]" | "bigint" | "boolean" | "current" | "entry" | "extends" | "false" | "fragment" | "grammar" | "hidden" | "import" | "infer" | "infers" | "interface" | "number" | "returns" | "string" | "terminal" | "true" | "type" | "with" | "{" | "|" | "}";
export type LangiumGrammarKeywordNames =
| "!"
| "&"
| "("
| ")"
| "*"
| "+"
| "+="
| ","
| "->"
| "."
| ".."
| ":"
| ";"
| "<"
| "="
| "=>"
| ">"
| "?"
| "?!"
| "?<!"
| "?<="
| "?="
| "@"
| "Date"
| "EOF"
| "["
| "]"
| "bigint"
| "boolean"
| "current"
| "entry"
| "extends"
| "false"
| "fragment"
| "grammar"
| "hidden"
| "import"
| "infer"
| "infers"
| "interface"
| "number"
| "returns"
| "string"
| "terminal"
| "true"
| "type"
| "with"
| "{"
| "|"
| "}";

export type LangiumGrammarTokenNames = LangiumGrammarTerminalNames | LangiumGrammarKeywordNames;

Expand Down

0 comments on commit 5907cce

Please sign in to comment.