@@ -12,6 +12,9 @@ import { LangiumServices } from "langium/lsp";
1212import { EvaluationContext } from "../evaluator/document-evaluator.js" ;
1313import { EvaluatorResult } from "../evaluator/evaluator.js" ;
1414import { LangiumEvaluator , LangiumEvaluatorResultData } from "../evaluator/langium-evaluator.js" ;
15+ import { EvaluatorResultMsg , SyntaxStatistic } from "../gen/interface.js" ;
16+
17+
1518
1619/**
1720 * Extends LangiumEvaluator and adds analysis capabilities.
@@ -40,14 +43,40 @@ export class LangiumDocumentAnalyzer<T extends LangiumServices> extends LangiumE
4043 this . analysisOptions = { ...DEFAULT_OPTIONS , ...analysisOptions } ;
4144 }
4245
43- evaluateDocument ( doc : LangiumDocument , ctx : EvaluationContext ) : EvaluatorResult < LangiumEvaluatorResultData > {
46+ /**
47+ * Evaluates a Langium document.
48+ * Here we return protocol compatible object EvaluatorResultMsg.
49+ *
50+ * @param doc Langium document to evaluate
51+ * @param ctx Evaluation context
52+ * @returns Evaluation result with syntax statistics in metadata
53+ */
54+ evaluateDocument ( doc : LangiumDocument , ctx : EvaluationContext ) : EvaluatorResult < LangiumEvaluatorResultData > & EvaluatorResultMsg {
4455 const validationResult = super . evaluateDocument ( doc , ctx ) ;
45- if ( this . analysisOptions . analysisMode !== AnalysisMode . NO_STATISTIC && validationResult . data . failures === 0 ) {
56+ if ( this . analysisOptions . analysisMode !== AnalysisMode . NO_STATISTIC && validationResult . data && validationResult . data . failures === 0 ) {
4657 // Add syntax usage statistics only if build was successful
4758 const statistics = this . collectSyntaxUsageStatistics ( doc , this . services . Grammar ) ;
48- validationResult . metadata [ LangiumDocumentAnalyzer . METADATA_KEY ] = statistics ;
59+ validationResult . metadata [ LangiumDocumentAnalyzer . METADATA_KEY ] = {
60+ value : {
61+ oneofKind : 'syntaxStatisticValue' ,
62+ syntaxStatisticValue : statistics
63+ }
64+ } ;
4965 }
50- return validationResult ;
66+ // make sure we fulfill the EvaluatorResultMsg interface
67+ return {
68+ ...validationResult ,
69+ data : {
70+ ...validationResult . data ,
71+ diagnostics : validationResult . data . diagnostics . map ( diagnostic => {
72+ const code = typeof diagnostic . code === 'number' ? String ( diagnostic . code ) : diagnostic . code ;
73+ return {
74+ ...diagnostic ,
75+ code
76+ } ;
77+ } )
78+ }
79+ } ;
5180 }
5281
5382 collectSyntaxUsageStatistics ( doc : LangiumDocument , grammar : Grammar ) : SyntaxStatistic {
@@ -173,7 +202,11 @@ export class LangiumDocumentAnalyzer<T extends LangiumServices> extends LangiumE
173202 extractStatisticsFromResult ( result : Partial < EvaluatorResult > | undefined ) : SyntaxStatistic | undefined {
174203 const metadata = result ?. metadata ;
175204 if ( metadata && metadata [ LangiumDocumentAnalyzer . METADATA_KEY ] ) {
176- return metadata [ LangiumDocumentAnalyzer . METADATA_KEY ] as SyntaxStatistic ;
205+ const value = metadata [ LangiumDocumentAnalyzer . METADATA_KEY ] . value ;
206+ if ( value . oneofKind === 'syntaxStatisticValue' ) {
207+ return value . syntaxStatisticValue ;
208+ }
209+ return undefined ;
177210 }
178211 return undefined ;
179212 }
@@ -240,42 +273,3 @@ const DEFAULT_OPTIONS: AnalysisOptions = {
240273 computeDiversity : true
241274} ;
242275
243- /**
244- * Type representing syntax usage statistics.
245- */
246- export type SyntaxStatistic = {
247- /** Map of rule names to their usage counts */
248- ruleUsage : Record < string , number > ;
249-
250- /** Percentage of used rules compared to all available rules */
251- coverage : number ;
252-
253- /** Diversity metrics for rule usage patterns */
254- diversity : {
255-
256- /**
257- * Shannon entropy - information diversity measure.
258- * **Range:** 0 to log₂(n) where n = number of rules.
259- * - **Low (0-1):** dominated by few rules
260- * - **Medium (1-3):** moderate diversity
261- * - **High (>3):** high diversity
262- */
263- entropy : number ;
264-
265- /**
266- * Gini coefficient - inequality measure. Range: 0 to 1.
267- * - **Low (0-0.3):** equal distribution
268- * - **Medium (0.3-0.7):** moderate inequality
269- * - **High (0.7-1):** high inequality
270- */
271- giniCoefficient : number ;
272-
273- /**
274- * Simpson's diversity index - probability that two randomly selected items are different. **Range:** 0 to 1.
275- * - **Low (0-0.3):** low diversity
276- * - **Medium (0.3-0.7):** moderate diversity
277- * - **High (0.7-1):** high diversity
278- */
279- simpsonIndex : number ;
280- } ;
281- }
0 commit comments