Skip to content

Commit 28ea2e9

Browse files
committed
feat: added basic checking
1 parent b6b5a72 commit 28ea2e9

8 files changed

Lines changed: 100 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/diagnostics/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ pub fn has_diagnostics() -> bool {
7474
DIAGNOSTIC_CONTAINER.with_borrow(|f| return !f.diagnostics.is_empty())
7575
}
7676

77+
pub fn get_diagnostics() -> Vec<Diagnostic> {
78+
DIAGNOSTIC_CONTAINER.with_borrow(|f| return f.diagnostics.clone())
79+
}
80+
7781
pub fn move_current_diagnostic_pos(pos: SpanPosition) {
7882
CURR_DIAGNOSTIC_POS.with_borrow_mut(|f| {
7983
*f = Some(pos);

compiler/lexer/src/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const USE_KEYWORD_HASH: u64 = hash!("use");
4242
pub fn lexer_parse_file(file_path: &String) -> DiagnosticResult<Vec<LexerToken>> {
4343
let contents: String = match fs::read_to_string(file_path) {
4444
Ok(v) => v,
45-
Err(_) => panic!("Couldn't read the file"),
45+
Err(_) => panic!("Couldn't read the file {}", file_path),
4646
};
4747

4848
let mut tokens: Vec<LexerToken> = Vec::new();

lsp/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ edition = "2024"
77
tower-lsp = "0.20"
88
tokio = { version = "1", features = ["full"] }
99
diagnostics = { path = "../compiler/diagnostics" }
10+
astoir = { path = "../compiler/astoir" }
11+
ast = { path = "../compiler/ast" }
12+
ast_parser = { path = "../compiler/ast_parser" }
13+
lexer = { path = "../compiler/lexer" }

lsp/src/bridge.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use ast_parser::parse_ast_ctx;
2+
use astoir::run_astoir_mir;
3+
use diagnostics::{diagnostic::Diagnostic, get_diagnostics, has_diagnostics};
4+
use lexer::lexer::lexer_parse_file;
5+
6+
pub fn check_for_file(url: String) -> Vec<Diagnostic> {
7+
let lexer = lexer_parse_file(&url);
8+
if has_diagnostics() {
9+
return get_diagnostics();
10+
}
11+
12+
let ast = parse_ast_ctx(&lexer.unwrap());
13+
if has_diagnostics() {
14+
return get_diagnostics();
15+
}
16+
17+
run_astoir_mir(ast.unwrap());
18+
19+
return get_diagnostics();
20+
}

lsp/src/diags.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use tower_lsp::lsp_types::{
33
Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, Location, Position, Range, Url,
44
};
55

6+
use crate::main;
7+
68
pub fn to_tower_diag(diagnostic: diagnostics::diagnostic::Diagnostic) -> Diagnostic {
79
let severity = match diagnostic.level {
810
Level::Error => DiagnosticSeverity::ERROR,
@@ -15,7 +17,7 @@ pub fn to_tower_diag(diagnostic: diagnostics::diagnostic::Diagnostic) -> Diagnos
1517
for span in diagnostic.spans {
1618
related_info.push(DiagnosticRelatedInformation {
1719
location: Location {
18-
uri: Url::parse(&span.start.file_path).unwrap(),
20+
uri: Url::parse(&format!("file:///{}", span.start.file_path)).unwrap(),
1921
range: Range {
2022
start: Position::new(span.start.line as u32, span.start.col as u32),
2123
end: Position::new(span.start.line as u32, span.start.end_col as u32),
@@ -36,9 +38,23 @@ pub fn to_tower_diag(diagnostic: diagnostics::diagnostic::Diagnostic) -> Diagnos
3638
),
3739
};
3840

41+
if diagnostic.primary_span.label.is_some() {
42+
related_info.push(DiagnosticRelatedInformation {
43+
location: Location {
44+
uri: Url::parse(&format!(
45+
"file:///{}",
46+
diagnostic.primary_span.start.file_path
47+
))
48+
.unwrap(),
49+
range: main_range.clone(),
50+
},
51+
message: diagnostic.primary_span.label.unwrap(),
52+
})
53+
}
54+
3955
Diagnostic {
4056
range: main_range,
41-
message: diagnostic.primary_span.label.unwrap(),
57+
message: diagnostic.message,
4258
severity: Some(severity),
4359
related_information: Some(related_info),
4460

lsp/src/lsp.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ use std::{
66
use tower_lsp::{
77
Client, LanguageServer,
88
lsp_types::{
9-
CompletionOptions, HoverProviderCapability, InitializeParams, InitializeResult,
10-
InitializedParams, ServerCapabilities, ServerInfo, TextDocumentSyncCapability,
11-
TextDocumentSyncKind,
9+
CompletionOptions, DidOpenTextDocumentParams, HoverProviderCapability, InitializeParams,
10+
InitializeResult, InitializedParams, MessageType, ServerCapabilities, ServerInfo,
11+
TextDocumentSyncCapability, TextDocumentSyncKind,
1212
},
1313
};
1414

15+
use crate::{bridge::check_for_file, diags::to_tower_diag};
16+
1517
#[derive(Debug)]
1618
pub struct LSPBackend {
17-
lsp_client: Client,
18-
documents: Arc<Mutex<HashSet<String>>>,
19+
pub lsp_client: Client,
20+
pub documents: Arc<Mutex<HashSet<String>>>,
1921
}
2022

2123
#[tower_lsp::async_trait]
@@ -40,6 +42,27 @@ impl LanguageServer for LSPBackend {
4042
})
4143
}
4244

45+
async fn did_open(&self, param: DidOpenTextDocumentParams) {
46+
let uri = param.text_document.uri.to_string();
47+
let text = param.text_document.text;
48+
49+
self.documents.lock().unwrap().insert(uri.clone());
50+
51+
let mut diags = vec![];
52+
53+
self.lsp_client
54+
.log_message(MessageType::ERROR, format!("Path: {}", uri))
55+
.await;
56+
57+
for diag in check_for_file(uri.clone().replace("file://", "")) {
58+
diags.push(to_tower_diag(diag))
59+
}
60+
61+
self.lsp_client
62+
.publish_diagnostics(uri.parse().unwrap(), diags, None)
63+
.await;
64+
}
65+
4366
async fn shutdown(&self) -> tower_lsp::jsonrpc::Result<()> {
4467
Ok(())
4568
}

lsp/src/main.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1+
use std::{
2+
collections::HashSet,
3+
sync::{Arc, Mutex},
4+
};
5+
6+
use tower_lsp::{LspService, Server};
7+
8+
use crate::lsp::LSPBackend;
9+
10+
pub mod bridge;
111
pub mod diags;
212
pub mod lsp;
313

4-
fn main() {}
14+
#[tokio::main]
15+
async fn main() {
16+
let stdin = tokio::io::stdin();
17+
let stdout = tokio::io::stdout();
18+
let (service, socket) = LspService::build(|client| LSPBackend {
19+
lsp_client: client,
20+
documents: Arc::new(Mutex::new(HashSet::new())),
21+
})
22+
.finish();
23+
Server::new(stdin, stdout, socket).serve(service).await;
24+
}

0 commit comments

Comments
 (0)