diff --git a/examples/requirements/src/language-server/generated/grammar.ts b/examples/requirements/src/language-server/generated/grammar.ts index fdcf35491..10bce401b 100644 --- a/examples/requirements/src/language-server/generated/grammar.ts +++ b/examples/requirements/src/language-server/generated/grammar.ts @@ -333,7 +333,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@2" + "$ref": "#/rules@5" }, "arguments": [] }, @@ -377,7 +377,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@4" + "$ref": "#/rules@7" }, "arguments": [] } @@ -400,7 +400,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@6" + "$ref": "#/rules@9" }, "arguments": [] } @@ -419,12 +419,12 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "CrossReference", "type": { - "$ref": "#/rules@11" + "$ref": "#/rules@4" }, "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@4" + "$ref": "#/rules@7" }, "arguments": [] }, @@ -445,12 +445,12 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "CrossReference", "type": { - "$ref": "#/rules@11" + "$ref": "#/rules@4" }, "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@4" + "$ref": "#/rules@7" }, "arguments": [] }, @@ -478,7 +478,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "CrossReference", "type": { - "$ref": "#/rules@10" + "$ref": "#/rules@3" }, "deprecatedSyntax": false } @@ -497,7 +497,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "CrossReference", "type": { - "$ref": "#/rules@10" + "$ref": "#/rules@3" }, "deprecatedSyntax": false } @@ -517,105 +517,6 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "parameters": [], "wildcard": false }, - { - "$type": "ParserRule", - "name": "Contact", - "definition": { - "$type": "Group", - "elements": [ - { - "$type": "Keyword", - "value": "contact" - }, - { - "$type": "Keyword", - "value": ":" - }, - { - "$type": "Assignment", - "feature": "user_name", - "operator": "=", - "terminal": { - "$type": "RuleCall", - "rule": { - "$ref": "#/rules@6" - }, - "arguments": [] - } - } - ] - }, - "definesHiddenTokens": false, - "entry": false, - "fragment": false, - "hiddenTokens": [], - "parameters": [], - "wildcard": false - }, - { - "$type": "TerminalRule", - "hidden": true, - "name": "WS", - "definition": { - "$type": "RegexToken", - "regex": "/\\\\s+/" - }, - "fragment": false - }, - { - "$type": "TerminalRule", - "name": "ID", - "definition": { - "$type": "RegexToken", - "regex": "/[_a-zA-Z][\\\\w_]*/" - }, - "fragment": false, - "hidden": false - }, - { - "$type": "TerminalRule", - "name": "INT", - "type": { - "$type": "ReturnType", - "name": "number" - }, - "definition": { - "$type": "RegexToken", - "regex": "/[0-9]+/" - }, - "fragment": false, - "hidden": false - }, - { - "$type": "TerminalRule", - "name": "STRING", - "definition": { - "$type": "RegexToken", - "regex": "/\\"(\\\\\\\\.|[^\\"\\\\\\\\])*\\"|'(\\\\\\\\.|[^'\\\\\\\\])*'/" - }, - "fragment": false, - "hidden": false - }, - { - "$type": "TerminalRule", - "hidden": true, - "name": "ML_COMMENT", - "definition": { - "$type": "RegexToken", - "regex": "/\\\\/\\\\*[\\\\s\\\\S]*?\\\\*\\\\//" - }, - "fragment": false - }, - { - "$type": "TerminalRule", - "hidden": true, - "name": "SL_COMMENT", - "definition": { - "$type": "RegexToken", - "regex": "/\\\\/\\\\/[^\\\\n\\\\r]*/" - }, - "fragment": false - }, { "$type": "ParserRule", "name": "RequirementModel", @@ -630,7 +531,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@2" + "$ref": "#/rules@5" }, "arguments": [] }, @@ -643,7 +544,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@10" + "$ref": "#/rules@3" }, "arguments": [] }, @@ -656,7 +557,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@11" + "$ref": "#/rules@4" }, "arguments": [] }, @@ -687,7 +588,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@4" + "$ref": "#/rules@7" }, "arguments": [] } @@ -703,7 +604,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@6" + "$ref": "#/rules@9" }, "arguments": [] } @@ -734,7 +635,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@4" + "$ref": "#/rules@7" }, "arguments": [] } @@ -746,7 +647,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "RuleCall", "rule": { - "$ref": "#/rules@6" + "$ref": "#/rules@9" }, "arguments": [] } @@ -769,7 +670,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "CrossReference", "type": { - "$ref": "#/rules@10" + "$ref": "#/rules@3" }, "deprecatedSyntax": false } @@ -788,7 +689,7 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "terminal": { "$type": "CrossReference", "type": { - "$ref": "#/rules@10" + "$ref": "#/rules@3" }, "deprecatedSyntax": false } @@ -807,6 +708,105 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra "hiddenTokens": [], "parameters": [], "wildcard": false + }, + { + "$type": "ParserRule", + "name": "Contact", + "definition": { + "$type": "Group", + "elements": [ + { + "$type": "Keyword", + "value": "contact" + }, + { + "$type": "Keyword", + "value": ":" + }, + { + "$type": "Assignment", + "feature": "user_name", + "operator": "=", + "terminal": { + "$type": "RuleCall", + "rule": { + "$ref": "#/rules@9" + }, + "arguments": [] + } + } + ] + }, + "definesHiddenTokens": false, + "entry": false, + "fragment": false, + "hiddenTokens": [], + "parameters": [], + "wildcard": false + }, + { + "$type": "TerminalRule", + "hidden": true, + "name": "WS", + "definition": { + "$type": "RegexToken", + "regex": "/\\\\s+/" + }, + "fragment": false + }, + { + "$type": "TerminalRule", + "name": "ID", + "definition": { + "$type": "RegexToken", + "regex": "/[_a-zA-Z][\\\\w_]*/" + }, + "fragment": false, + "hidden": false + }, + { + "$type": "TerminalRule", + "name": "INT", + "type": { + "$type": "ReturnType", + "name": "number" + }, + "definition": { + "$type": "RegexToken", + "regex": "/[0-9]+/" + }, + "fragment": false, + "hidden": false + }, + { + "$type": "TerminalRule", + "name": "STRING", + "definition": { + "$type": "RegexToken", + "regex": "/\\"(\\\\\\\\.|[^\\"\\\\\\\\])*\\"|'(\\\\\\\\.|[^'\\\\\\\\])*'/" + }, + "fragment": false, + "hidden": false + }, + { + "$type": "TerminalRule", + "hidden": true, + "name": "ML_COMMENT", + "definition": { + "$type": "RegexToken", + "regex": "/\\\\/\\\\*[\\\\s\\\\S]*?\\\\*\\\\//" + }, + "fragment": false + }, + { + "$type": "TerminalRule", + "hidden": true, + "name": "SL_COMMENT", + "definition": { + "$type": "RegexToken", + "regex": "/\\\\/\\\\/[^\\\\n\\\\r]*/" + }, + "fragment": false } ], "definesHiddenTokens": false, diff --git a/examples/requirements/src/language-server/tests.langium b/examples/requirements/src/language-server/tests.langium index 81e631cec..24be70a93 100644 --- a/examples/requirements/src/language-server/tests.langium +++ b/examples/requirements/src/language-server/tests.langium @@ -1,5 +1,4 @@ grammar Tests -import "./common" import "./requirements" entry TestModel: diff --git a/packages/langium-cli/src/generate.ts b/packages/langium-cli/src/generate.ts index 25061e924..c73df435b 100644 --- a/packages/langium-cli/src/generate.ts +++ b/packages/langium-cli/src/generate.ts @@ -88,7 +88,7 @@ function mapGrammarElements(grammars: Grammar[], visited: Set = new Set( return map; } -function embedReferencedGrammar(grammar: Grammar, map: Map): void { +function embedReferencedGrammar(grammar: Grammar, map: Map): Grammar { const allGrammars = resolveTransitiveImports(documents, grammar); const linker = grammarServices.references.Linker; const buildReference = linker.buildReference.bind(linker); @@ -112,9 +112,13 @@ function embedReferencedGrammar(grammar: Grammar, map: Map { @@ -220,14 +224,17 @@ export async function generate(config: LangiumConfig, options: GenerateOptions): const grammarElements = mapGrammarElements(grammars); + const embeddedGrammars: Grammar[] = []; for (const grammar of grammars) { - embedReferencedGrammar(grammar, grammarElements); + const embeddedGrammar = embedReferencedGrammar(grammar, grammarElements); + embeddedGrammars.push(embeddedGrammar); + configMap.set(embeddedGrammar, configMap.get(grammar)!); } // We need to rescope the grammars again // They need to pick up on the embedded references - await relinkGrammars(grammars); + await relinkGrammars(embeddedGrammars); - for (const grammar of grammars) { + for (const grammar of embeddedGrammars) { // Create and validate the in-memory parser const parserAnalysis = await validateParser(grammar, config, configMap, grammarServices); if (parserAnalysis instanceof Error) { @@ -247,16 +254,16 @@ export async function generate(config: LangiumConfig, options: GenerateOptions): return buildResult(false); } - const genAst = generateAst(grammarServices, grammars, config); + const genAst = generateAst(grammarServices, embeddedGrammars, config); await writeWithFail(path.resolve(output, 'ast.ts'), genAst, options); - const serializedGrammar = serializeGrammar(grammarServices, grammars, config); + const serializedGrammar = serializeGrammar(grammarServices, embeddedGrammars, config); await writeWithFail(path.resolve(output, 'grammar.ts'), serializedGrammar, options); - const genModule = generateModule(grammars, config, configMap); + const genModule = generateModule(embeddedGrammars, config, configMap); await writeWithFail(path.resolve(output, 'module.ts'), genModule, options); - for (const grammar of grammars) { + for (const grammar of embeddedGrammars) { const languageConfig = configMap.get(grammar); if (languageConfig?.textMate) {