Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the global maxLookahead configuration #248

Merged
merged 6 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/arithmetics/src/language-server/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
* DO NOT EDIT MANUALLY!
******************************************************************************/

import { LangiumGeneratedServices, LangiumServices, Module } from 'langium';
import { LangiumGeneratedServices, LangiumServices, LanguageMetaData, Module } from 'langium';
import { ArithmeticsAstReflection } from './ast';
import { grammar } from './grammar';

export const languageMetaData = {
export const languageMetaData: LanguageMetaData = {
languageId: 'arithmetics',
fileExtensions: ['.calc']
};

export const ArithmeticsGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
Grammar: () => grammar(),
AstReflection: () => new ArithmeticsAstReflection(),
LanguageMetaData: () => languageMetaData
LanguageMetaData: () => languageMetaData,
parser: {}
};
3 changes: 0 additions & 3 deletions examples/arithmetics/syntaxes/arithmetics.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
".calc"
],
"patterns": [
{
"include": "#comments"
},
{
"name": "keyword.control.arithmetics",
"match": "\\b(def|module)\\b"
Expand Down
5 changes: 5 additions & 0 deletions examples/domainmodel/langium-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"out": "src/language-server/generated",
"textMate": {
"out": "syntaxes/domain-model.tmLanguage.json"
},
"chevrotainParserConfig": {
"recoveryEnabled": true,
"nodeLocationTracking": "onlyOffset",
"maxLookahead": 3
}
}
15 changes: 12 additions & 3 deletions examples/domainmodel/src/language-server/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
* DO NOT EDIT MANUALLY!
******************************************************************************/

import { LangiumGeneratedServices, LangiumServices, Module } from 'langium';
import { LangiumGeneratedServices, LangiumServices, LanguageMetaData, Module, IParserConfig } from 'langium';
import { DomainModelAstReflection } from './ast';
import { grammar } from './grammar';

export const languageMetaData = {
export const languageMetaData: LanguageMetaData = {
languageId: 'domain-model',
fileExtensions: ['.dmodel']
};

export const parserConfig: IParserConfig = {
recoveryEnabled: true,
nodeLocationTracking: 'onlyOffset',
maxLookahead: 3,
};

export const DomainModelGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
Grammar: () => grammar(),
AstReflection: () => new DomainModelAstReflection(),
LanguageMetaData: () => languageMetaData
LanguageMetaData: () => languageMetaData,
parser: {
ParserConfig: () => parserConfig
}
};
7 changes: 4 additions & 3 deletions examples/statemachine/src/language-server/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
* DO NOT EDIT MANUALLY!
******************************************************************************/

import { LangiumGeneratedServices, LangiumServices, Module } from 'langium';
import { LangiumGeneratedServices, LangiumServices, LanguageMetaData, Module } from 'langium';
import { StatemachineAstReflection } from './ast';
import { grammar } from './grammar';

export const languageMetaData = {
export const languageMetaData: LanguageMetaData = {
languageId: 'statemachine',
fileExtensions: ['.statemachine']
};

export const StatemachineGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
Grammar: () => grammar(),
AstReflection: () => new StatemachineAstReflection(),
LanguageMetaData: () => languageMetaData
LanguageMetaData: () => languageMetaData,
parser: {}
};
31 changes: 31 additions & 0 deletions packages/langium-cli/langium-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@
"out"
]
},
"chevrotainParserConfig": {
"description": "An object to describe the Chevrotain parser configuration",
"type": "object",
"properties": {
"recoveryEnabled": {
"description": "Is the error recovery / fault tolerance of the Chevrotain Parser enabled",
"type": "boolean"
},
"maxLookahead": {
"description": "Maximum number of tokens the parser will use to choose between alternatives",
"type": "number"
},
"dynamicTokensEnabled": {
"description": "A flag to support Dynamically defined Tokens",
"type": "boolean"
},
"nodeLocationTracking": {
"description": "Enable computation of CST nodes location",
"type": "string"
},
"traceInitPerf": {
"description": "A flag to print performance tracing logs during parser initialization",
"type": ["boolean", "number"]
},
"skipValidations": {
"description": "A flag to avoid running the grammar validations during Parser initialization",
"type": "boolean"
}
},
"required": []
},
"langiumInternal": {
"description": "A flag to determine whether langium uses itself to bootstrap",
"type": "boolean"
Expand Down
29 changes: 26 additions & 3 deletions packages/langium-cli/src/generator/module-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,56 @@ import { LangiumConfig } from '../package';
import { generatedHeader } from './util';

export function generateModule(grammar: langium.Grammar, config: LangiumConfig): string {
const parserConfig = config.chevrotainParserConfig;
const node = new CompositeGeneratorNode();

node.append(generatedHeader);
if (config.langiumInternal) {
node.append(`import { LanguageMetaData${parserConfig ? ', IParserConfig' : ''} } from '../..';`, NL);
node.append("import { Module } from '../../dependency-injection';", NL);
node.contents.push("import { LangiumGeneratedServices, LangiumServices } from '../../services';", NL);
} else {
node.append("import { LangiumGeneratedServices, LangiumServices, Module } from 'langium';", NL);
node.append(`import { LangiumGeneratedServices, LangiumServices, LanguageMetaData, Module${parserConfig ? ', IParserConfig' : ''} } from 'langium';`, NL);
}
node.append(
'import { ', grammar.name, "AstReflection } from './ast';", NL,
"import { grammar } from './grammar';", NL, NL
);

node.append('export const languageMetaData = {', NL);
node.append('export const languageMetaData: LanguageMetaData = {', NL);
node.indent(metaData => {
metaData.append(`languageId: '${config.languageId}',`, NL);
metaData.append(`fileExtensions: [${config.fileExtensions && config.fileExtensions.map(e => appendQuotesAndDot(e)).join(', ')}]`, NL);
});
node.append('};', NL, NL);

if (parserConfig) {
node.append('export const parserConfig: IParserConfig = {', NL);
node.indent(configNode => {
Object.keys(parserConfig).forEach(key => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const value = (parserConfig as any)[key];
configNode.append(`${key}: ${typeof value === 'string' ? `'${value}'` : value},`, NL);
});
});
node.append('};', NL, NL);
}

node.append('export const ', grammar.name, 'GeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {', NL);
node.indent(moduleNode => {
moduleNode.append(
'Grammar: () => grammar(),', NL,
'AstReflection: () => new ', grammar.name, 'AstReflection(),', NL,
'LanguageMetaData: () => languageMetaData', NL
'LanguageMetaData: () => languageMetaData,', NL,
'parser: {',
);
if (parserConfig) {
moduleNode.append(NL);
moduleNode.indent(parserGroupNode => {
parserGroupNode.append('ParserConfig: () => parserConfig', NL);
});
}
moduleNode.append('}', NL);
});
node.append('};', NL);

Expand Down
3 changes: 3 additions & 0 deletions packages/langium-cli/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
******************************************************************************/

import fs from 'fs-extra';
import { IParserConfig } from 'langium';
import path from 'path';
import { getTime } from './generator/util';

Expand Down Expand Up @@ -32,6 +33,8 @@ export interface LangiumConfig {
/** Output path to syntax highlighting file */
out: string
}
/** Configure the chevrotain parser */
chevrotainParserConfig?: IParserConfig,
/** The following option is meant to be used only by Langium itself */
langiumInternal?: boolean
}
Expand Down
6 changes: 4 additions & 2 deletions packages/langium/src/grammar/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
* DO NOT EDIT MANUALLY!
******************************************************************************/

import { LanguageMetaData } from '../..';
import { Module } from '../../dependency-injection';
import { LangiumGeneratedServices, LangiumServices } from '../../services';
import { LangiumGrammarAstReflection } from './ast';
import { grammar } from './grammar';

export const languageMetaData = {
export const languageMetaData: LanguageMetaData = {
languageId: 'langium',
fileExtensions: ['.langium']
};

export const LangiumGrammarGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
Grammar: () => grammar(),
AstReflection: () => new LangiumGrammarAstReflection(),
LanguageMetaData: () => languageMetaData
LanguageMetaData: () => languageMetaData,
parser: {}
};
1 change: 1 addition & 0 deletions packages/langium/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from './lsp/language-server';
export * from './validation/document-validator';
export * from './validation/validation-registry';
export * from './parser/langium-parser';
export * from './parser/parser-config';
export * from './references/linker';
export * from './references/naming';
export * from './references/scope';
Expand Down
11 changes: 8 additions & 3 deletions packages/langium/src/parser/langium-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Linker } from '../references/linker';
import { LangiumServices } from '../services';
import { getContainerOfType } from '../utils/ast-util';
import { ValueConverter } from './value-converter';
import { IParserConfig } from './parser-config';

export type ParseResult<T = AstNode> = {
value: T,
Expand All @@ -39,7 +40,7 @@ export class LangiumParser {
}

constructor(services: LangiumServices, tokens: TokenType[]) {
this.wrapper = new ChevrotainWrapper(tokens);
this.wrapper = new ChevrotainWrapper(tokens, services.parser.ParserConfig);
this.linker = services.references.Linker;
this.converter = services.parser.ValueConverter;
this.lexer = new Lexer(tokens);
Expand Down Expand Up @@ -302,8 +303,12 @@ class ChevrotainWrapper extends EmbeddedActionsParser {

private analysed = false;

constructor(tokens: TokenType[]) {
super(tokens, { recoveryEnabled: true, nodeLocationTracking: 'onlyOffset' });
constructor(tokens: TokenType[], config?: IParserConfig) {
super(tokens, {
recoveryEnabled: true,
nodeLocationTracking: 'onlyOffset',
...config
pluralia marked this conversation as resolved.
Show resolved Hide resolved
});
}

get IS_RECORDING(): boolean {
Expand Down
7 changes: 7 additions & 0 deletions packages/langium/src/parser/parser-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/******************************************************************************
* Copyright 2021 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

export { IParserConfig } from 'chevrotain';
5 changes: 4 additions & 1 deletion packages/langium/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Connection, TextDocuments } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { AstReflection, CompletionProvider, DocumentBuilder, LangiumDocumentFactory, LangiumDocuments, DocumentValidator, Grammar, JsonSerializer, LangiumParser, LanguageMetaData, Linker, NameProvider, RuleInterpreter, ScopeComputation, ScopeProvider, TextDocumentFactory, ValidationRegistry } from '.';
import { AstReflection, CompletionProvider, DocumentBuilder, LangiumDocumentFactory, LangiumDocuments, DocumentValidator, Grammar, JsonSerializer, LangiumParser, LanguageMetaData, Linker, NameProvider, RuleInterpreter, ScopeComputation, ScopeProvider, TextDocumentFactory, ValidationRegistry, IParserConfig } from '.';
import { AstNodeDescriptionProvider, ReferenceDescriptionProvider } from './index/ast-descriptions';
import { AstNodeLocator } from './index/ast-node-locator';
import { IndexManager } from './index/index-manager';
Expand All @@ -26,6 +26,9 @@ export type LangiumGeneratedServices = {
Grammar: Grammar
AstReflection: AstReflection
LanguageMetaData: LanguageMetaData
parser: {
ParserConfig?: IParserConfig
}
}

export type LangiumLspServices = {
Expand Down