Skip to content

Commit

Permalink
Perform parser optimizations in production mode
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Sep 19, 2024
1 parent 335f9a9 commit 781d365
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/langium-cli/src/generator/module-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
******************************************************************************/

import type { Grammar, IParserConfig } from 'langium';
import { type Generated, expandToNode, joinToNode, toString } from 'langium/generate';
import { EOL, type Generated, expandToNode, joinToNode, toString } from 'langium/generate';
import type { LangiumConfig, LangiumLanguageConfig } from '../package-types.js';
import { generatedHeader } from './node-util.js';

export function generateModule(grammars: Grammar[], config: LangiumConfig, grammarConfigMap: Map<Grammar, LangiumLanguageConfig>): string {
const grammarsWithName = grammars.filter(grammar => !!grammar.name);
const parserConfig = config.chevrotainParserConfig;
const mode = config.mode;
const hasIParserConfigImport = Boolean(parserConfig) || grammars.some(grammar => grammarConfigMap.get(grammar)?.chevrotainParserConfig !== undefined);
let needsGeneralParserConfig = undefined;

Expand Down Expand Up @@ -41,12 +42,13 @@ export function generateModule(grammars: Grammar[], config: LangiumConfig, gramm
grammarsWithName,
grammar => {
const config = grammarConfigMap.get(grammar)!;
const modeValue = mode ? `,${EOL} mode: '${mode}'` : '';
return expandToNode`
export const ${ grammar.name }LanguageMetaData = {
languageId: '${config.id}',
fileExtensions: [${config.fileExtensions && joinToNode(config.fileExtensions, e => appendQuotesAndDot(e), { separator: ', ' })}],
caseInsensitive: ${Boolean(config.caseInsensitive)}
caseInsensitive: ${Boolean(config.caseInsensitive)}${modeValue}
} as const satisfies LanguageMetaData;
`;
},
Expand Down
9 changes: 9 additions & 0 deletions packages/langium/src/languages/language-meta-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

/**
* Metadata of a language.
*/
export interface LanguageMetaData {
languageId: string;
fileExtensions: readonly string[];
caseInsensitive: boolean;
/**
* Mode used to optimize code for development or production environments.
*

Check failure on line 16 in packages/langium/src/languages/language-meta-data.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Trailing spaces not allowed
* In production mode, all lexer/parser validations are disabled.
*/
mode?: 'development' | 'production';
}
9 changes: 7 additions & 2 deletions packages/langium/src/parser/langium-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ export abstract class AbstractLangiumParser implements BaseParser {
constructor(services: LangiumCoreServices) {
this.lexer = services.parser.Lexer;
const tokens = this.lexer.definition;
const production = services.LanguageMetaData.mode === 'production';
this.wrapper = new ChevrotainWrapper(tokens, {
...services.parser.ParserConfig,
skipValidations: production,
errorMessageProvider: services.parser.ParserErrorMessageProvider
});
}
Expand Down Expand Up @@ -631,13 +633,16 @@ class ChevrotainWrapper extends EmbeddedActionsParser {
// This array is set in the base implementation of Chevrotain.
definitionErrors: IParserDefinitionError[];

constructor(tokens: TokenVocabulary, config?: IParserConfig) {
constructor(tokens: TokenVocabulary, config: IParserConfig) {
const useDefaultLookahead = config && 'maxLookahead' in config;
super(tokens, {
...defaultConfig,
lookaheadStrategy: useDefaultLookahead
? new LLkLookaheadStrategy({ maxLookahead: config.maxLookahead })
: new LLStarLookaheadStrategy(),
: new LLStarLookaheadStrategy({
// If validations are skipped, don't log the lookahead warnings
logging: config.skipValidations ? () => { } : undefined
}),
...config,
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/langium/src/parser/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ export class DefaultLexer implements Lexer {
});
this.tokenTypes = this.toTokenTypeDictionary(tokens);
const lexerTokens = isTokenTypeDictionary(tokens) ? Object.values(tokens) : tokens;
const production = services.LanguageMetaData.mode === 'production';
this.chevrotainLexer = new ChevrotainLexer(lexerTokens, {
positionTracking: 'full'
positionTracking: 'full',
skipValidations: production
});
}

Expand Down

0 comments on commit 781d365

Please sign in to comment.