@@ -2,69 +2,128 @@ import {
22 OriginalParserError ,
33 TParserInputData ,
44 isParserErrors ,
5+ isIntermediateASTValidationErrors ,
56 transpiler ,
7+ OriginalValidatorError ,
68} from '@bitloops/bl-transpiler' ;
79import { Diagnostic } from 'vscode-languageserver/node.js' ;
810import { TextDocument } from 'vscode-languageserver-textdocument' ;
911import { IAnalyzer } from '../analyzer.js' ;
1012import { DiagnosticFactory } from '../diagnostic.js' ;
13+ import { TParserCoreInputData , TParserSetupInputData } from '../types.js' ;
1114
1215export class BitloopsAnalyzer implements IAnalyzer {
13- static diagnostics : Diagnostic [ ] = [ ] ;
14- analyze ( document : TextDocument ) : Diagnostic [ ] {
16+ private diagnostics : Record < string , Diagnostic [ ] > = { } ;
17+ private res : Partial < TParserInputData > = { } ;
18+ private setup : Record < string , TParserSetupInputData > = { } ;
19+ private core : Record < string , TParserCoreInputData > = { } ;
20+ analyze ( document : TextDocument ) : Record < string , Diagnostic [ ] > {
1521 try {
1622 const transpileInputData = this . documentToParserInputData ( document ) ;
1723 const intermediateModel = transpiler . bitloopsCodeToIntermediateModel ( transpileInputData ) ;
1824 if ( isParserErrors ( intermediateModel ) ) {
19- const diagnostics = this . mapParserErrorsToLSPDiagnostics ( intermediateModel , document ) ;
20- return diagnostics ;
25+ this . mapParserErrorsToLSPDiagnostics ( intermediateModel , document ) ;
26+ return this . diagnostics ;
2127 }
22- return [ ] ;
28+ if ( isIntermediateASTValidationErrors ( intermediateModel ) ) {
29+ this . mapValidatorErrorsToLSPDiagnostics ( intermediateModel , document ) ;
30+ return this . diagnostics ;
31+ }
32+ return this . diagnostics ;
2333 } catch ( e ) {
2434 console . log ( 'error' , e ) ;
25- return [ ] ;
35+ return { } ;
2636 }
2737 }
2838
2939 private documentToParserInputData ( document : TextDocument ) : TParserInputData {
30- const res : Partial < TParserInputData > = { } ;
31-
32- if ( document . uri . endsWith ( 'setup.bl' ) ) {
33- res . setup = [
40+ //giving file id as uri for now
41+ if ( ! ( document . uri in this . diagnostics ) ) this . diagnostics [ document . uri ] = [ ] ;
42+ if ( document . uri . endsWith ( 'setup.bl' ) )
43+ this . setup [ document . uri ] = [
3444 {
35- fileId : document . uri . split ( '/' ) . slice ( - 1 ) [ 0 ] ,
45+ // fileId: document.uri.split('/').slice(-1)[0],
46+ fileId : document . uri ,
3647 fileContents : document . getText ( ) ,
3748 } ,
3849 ] ;
39- return res as TParserInputData ;
40- }
4150 // Handle possibly unknown bounded context and module
42- const boundedContext = document . uri . split ( '/' ) ?. slice ( - 3 ) ?. [ 0 ] ?? 'unknown' ;
43- const module = document . uri . split ( '/' ) ?. slice ( - 2 ) ?. [ 0 ] ?? 'unknown' ;
44- res . core = [
45- {
46- boundedContext,
47- module,
48- fileId : document . uri . split ( '/' ) . slice ( - 1 ) [ 0 ] ,
49- fileContents : document . getText ( ) ,
50- } ,
51- ] ;
52- return res as TParserInputData ;
53- }
54-
55- mapParserErrorsToLSPDiagnostics (
56- parserErrors : OriginalParserError ,
57- document : TextDocument ,
58- ) : Diagnostic [ ] {
59- return parserErrors . map ( ( e ) =>
60- DiagnosticFactory . create (
61- 1 ,
51+ else if ( document . uri . endsWith ( '.bl' ) ) {
52+ const boundedContext = document . uri . split ( '/' ) ?. slice ( - 3 ) ?. [ 0 ] ?? 'unknown' ;
53+ const module = document . uri . split ( '/' ) ?. slice ( - 2 ) ?. [ 0 ] ?? 'unknown' ;
54+ this . core [ document . uri ] = [
6255 {
63- start : document . positionAt ( e . start ) ,
64- end : document . positionAt ( e . stop ) ,
56+ boundedContext,
57+ module,
58+ // fileId: document.uri.split('/').slice(-1)[0],
59+ fileId : document . uri ,
60+ fileContents : document . getText ( ) ,
6561 } ,
66- `line: ${ e . line } :${ e . column } , offendingSymbol: ${ e . offendingToken . text } , msg: ${ e . message } ` ,
62+ ] ;
63+ }
64+ this . res . core = [ ] ;
65+ this . res . setup = [ ] ;
66+ for ( const core of Object . values ( this . core ) ) {
67+ this . res . core . push ( ...core ) ;
68+ }
69+ for ( const setup of Object . values ( this . setup ) ) {
70+ this . res . setup . push ( ...setup ) ;
71+ }
72+ for ( const key in this . diagnostics ) {
73+ this . diagnostics [ key ] = [ ] ;
74+ }
75+ return this . res as TParserInputData ;
76+ }
77+ mapParserErrorsToLSPDiagnostics ( parserErrors : OriginalParserError , document : TextDocument ) : void {
78+ parserErrors . forEach ( ( e ) => {
79+ this . diagnostics [ e . fileId ] = [ ] ;
80+ } ) ;
81+ for ( const key in this . diagnostics ) {
82+ this . diagnostics [ key ] = [ ] ;
83+ }
84+ parserErrors . map ( ( e ) =>
85+ this . diagnostics [ e . fileId ] . push (
86+ DiagnosticFactory . create (
87+ 1 ,
88+ {
89+ start : document . positionAt ( e . start ) ,
90+ end : document . positionAt ( e . stop ) ,
91+ } ,
92+ `line: ${ e . line } :${ e . column } , offendingSymbol: ${ e . offendingToken . text } , msg: ${ e . message } ` ,
93+ e . fileId ,
94+ ) ,
6795 ) ,
6896 ) ;
6997 }
98+
99+ mapValidatorErrorsToLSPDiagnostics (
100+ validatorErrors : OriginalValidatorError ,
101+ document : TextDocument ,
102+ ) : void {
103+ validatorErrors . forEach ( ( e ) => {
104+ this . diagnostics [ e . metadata . fileId ] = [ ] ;
105+ } ) ;
106+ for ( const key in this . diagnostics ) {
107+ this . diagnostics [ key ] = [ ] ;
108+ }
109+ validatorErrors . map ( ( e ) => {
110+ this . diagnostics [ e . metadata . fileId ] . push (
111+ DiagnosticFactory . create (
112+ 1 ,
113+ {
114+ start : {
115+ line : e . metadata . start . line - 1 ,
116+ character : e . metadata . start . column - 1 ,
117+ } ,
118+ end : {
119+ line : e . metadata . end . line - 1 ,
120+ character : e . metadata . end . column - 1 ,
121+ } ,
122+ } ,
123+ `line: ${ e . metadata . start . line } :${ e . metadata . start . column } , msg: ${ e . message } ` ,
124+ e . metadata . fileId ,
125+ ) ,
126+ ) ;
127+ } ) ;
128+ }
70129}
0 commit comments