/****************************************************************************** * Copyright 2025 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. * * @author Dennis Hübner ******************************************************************************/ import { type Grammar, GrammarAST, type LangiumDocument } from 'langium'; import type { LangiumServicesLike } from '../types.js'; import { type EvaluationContext } from '../evaluator/document-evaluator.js'; import { type EvaluatorResult } from '../evaluator/evaluator.js'; import { LangiumEvaluator, type LangiumEvaluatorResultData } from '../evaluator/langium-evaluator.js'; import { EvaluatorResultMsg, SyntaxStatistic } from '../gen/interface.js'; /** * Options for resolving grammar imports when collecting rules. * If provided, the analyzer can follow grammar imports to include * rules from transitively imported grammars. */ export interface GrammarImportResolver { /** * Resolve all transitively imported grammars and return their grammar objects. * This mirrors the behavior of langium's `resolveTransitiveImports`, which should be passed here */ resolveImports(grammar: Grammar): Grammar[]; } /** * Extends LangiumEvaluator and adds analysis capabilities. */ export declare class LangiumDocumentAnalyzer extends LangiumEvaluator { static readonly METADATA_KEY = "syntax_statistics"; private readonly analysisOptions; private readonly importResolver?; /** * Creates an instance of LangiumDocumentAnalyzer. * @param services Langium services * @param analysisOptions Analysis options * @param importResolver Optional resolver for grammar imports. If not provided, * imported grammar rules will not be included even if `includeImportedRules` is true. * @example * ```typescript * const analyzer = new LangiumDocumentAnalyzer(services, { * analysisMode: AnalysisMode.ALL, * excludeRules: ['DeprecatedRule'], * computeDiversity: false * }); * ``` */ constructor(services: T, analysisOptions?: Partial, importResolver?: GrammarImportResolver); /** * Evaluates a Langium document. * Here we return protocol compatible object EvaluatorResultMsg. * * @param doc Langium document to evaluate * @param ctx Evaluation context * @returns Evaluation result with syntax statistics in metadata */ evaluateDocument(doc: LangiumDocument, ctx: EvaluationContext): EvaluatorResult & EvaluatorResultMsg; collectSyntaxUsageStatistics(doc: LangiumDocument, grammar: Grammar): SyntaxStatistic; /** * Computes coverage as percentage of used rules over all available rules */ computeCoverage(ruleUsage: Record): number; /** * Computes Shannon entropy - measure of information diversity * Higher values indicate more diverse usage patterns */ computeEntropy(ruleUsage: Record): number; /** * Computes Gini coefficient - measure of inequality in rule usage * 0 = perfect equality, 1 = maximum inequality */ computeGiniCoefficient(ruleUsage: Record): number; /** * Computes Simpson's diversity index - probability that two randomly selected items are different * Higher values indicate more diversity */ computeSimpsonIndex(ruleUsage: Record): number; /** * Extracts syntax statistics from the evaluation result. * @param result The evaluation result. * @returns The extracted syntax statistics or undefined if not found. */ extractStatisticsFromResult(result: Partial | undefined): SyntaxStatistic | undefined; protected collectAllRules(grammar: Grammar): GrammarAST.AbstractRule[]; protected createEmptySyntaxStatistic(): SyntaxStatistic; } /** * Analysis mode for controlling what analysis operations to perform */ export declare enum AnalysisMode { ALL = "ALL", NO_STATISTIC = "NO_STATISTIC" } interface AnalysisOptions { analysisMode: AnalysisMode; /** * Filter for specific rules (e.g deprecated) to exclude in the analysis. * Rule WS (whitespace) is always excluded. */ excludeRules: string[]; /** * Whether to include rules from imported grammars. Default is true. */ includeImportedRules: boolean; /** * Whether to include hidden tokens (like comments, whitespace) in the analysis. Default is false. * Rule WS (whitespace) is always excluded. */ includeHiddenRules: boolean; /** * Whether to compute diversity metrics for rule usage. Default is true. */ computeDiversity: boolean; } export {}; //# sourceMappingURL=document-analyzer.d.ts.map