Compare commits
4 Commits
3e0367fb1e
...
dcb4e76a40
Author | SHA1 | Date | |
---|---|---|---|
dcb4e76a40 | |||
6dccab8b12 | |||
7b4f38406d | |||
9e37ae7aac |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -667,6 +667,8 @@ version = "1.0.0-beta.1"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"dashmap 6.1.0",
|
"dashmap 6.1.0",
|
||||||
"reid",
|
"reid",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"socket",
|
"socket",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower-lsp",
|
"tower-lsp",
|
||||||
|
7
examples/a3.reid
Normal file
7
examples/a3.reid
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fn main() -> i32 {
|
||||||
|
let a = 4f32;
|
||||||
|
if (a % 2) == 0 {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
@ -2,11 +2,14 @@ import std::print;
|
|||||||
import std::from_str;
|
import std::from_str;
|
||||||
import std::String;
|
import std::String;
|
||||||
|
|
||||||
|
/// Asd
|
||||||
struct Otus {
|
struct Otus {
|
||||||
field: u32,
|
field: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Otus {
|
impl Otus {
|
||||||
|
/// Some test documentation here.
|
||||||
|
/// On a second line
|
||||||
fn test(&self) -> u32 {
|
fn test(&self) -> u32 {
|
||||||
*self.field
|
*self.field
|
||||||
}
|
}
|
||||||
@ -28,7 +31,6 @@ fn main() -> u32 {
|
|||||||
let mut list = u64::malloc(15);
|
let mut list = u64::malloc(15);
|
||||||
list[4] = 17;
|
list[4] = 17;
|
||||||
|
|
||||||
|
|
||||||
print(from_str("value: ") + list[4]);
|
print(from_str("value: ") + list[4]);
|
||||||
|
|
||||||
return i32::sizeof() as u32;
|
return i32::sizeof() as u32;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// Arithmetic, function calls and imports!
|
// Arithmetic, function calls and imports!
|
||||||
|
|
||||||
|
/// Test stuff
|
||||||
fn changer(param: &mut u32) {
|
fn changer(param: &mut u32) {
|
||||||
*param = 17;
|
*param = 17;
|
||||||
}
|
}
|
||||||
|
@ -9,3 +9,5 @@ tokio = { version = "1.47.0", features = ["full"] }
|
|||||||
tower-lsp = "0.20.0"
|
tower-lsp = "0.20.0"
|
||||||
reid = { path = "../reid", version = "1.0.0-beta.4", registry="gitea-teascade", features=[] }
|
reid = { path = "../reid", version = "1.0.0-beta.4", registry="gitea-teascade", features=[] }
|
||||||
dashmap = "6.1.0"
|
dashmap = "6.1.0"
|
||||||
|
serde = "*"
|
||||||
|
serde_json = "*"
|
||||||
|
@ -2,6 +2,7 @@ use std::{collections::HashMap, hash::Hash, path::PathBuf};
|
|||||||
|
|
||||||
use reid::{
|
use reid::{
|
||||||
ast::{
|
ast::{
|
||||||
|
ReturnType,
|
||||||
lexer::{FullToken, Token},
|
lexer::{FullToken, Token},
|
||||||
token_stream::TokenRange,
|
token_stream::TokenRange,
|
||||||
},
|
},
|
||||||
@ -84,7 +85,7 @@ impl StaticAnalysis {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SemanticToken {
|
pub struct SemanticToken {
|
||||||
pub ty: Option<TypeKind>,
|
pub hover: Option<Hover>,
|
||||||
pub autocomplete: Vec<Autocomplete>,
|
pub autocomplete: Vec<Autocomplete>,
|
||||||
pub symbol: Option<SymbolId>,
|
pub symbol: Option<SymbolId>,
|
||||||
}
|
}
|
||||||
@ -92,6 +93,7 @@ pub struct SemanticToken {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Autocomplete {
|
pub struct Autocomplete {
|
||||||
pub text: String,
|
pub text: String,
|
||||||
|
pub documentation: Option<String>,
|
||||||
pub kind: AutocompleteKind,
|
pub kind: AutocompleteKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,6 +104,18 @@ pub enum AutocompleteKind {
|
|||||||
Function(Vec<FunctionParam>, TypeKind),
|
Function(Vec<FunctionParam>, TypeKind),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Hover {
|
||||||
|
pub documentation: Option<String>,
|
||||||
|
pub kind: Option<HoverKind>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum HoverKind {
|
||||||
|
Type(TypeKind),
|
||||||
|
Function(String, Vec<FunctionParam>, TypeKind),
|
||||||
|
}
|
||||||
|
|
||||||
impl ToString for AutocompleteKind {
|
impl ToString for AutocompleteKind {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
@ -148,12 +162,15 @@ impl AnalysisState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AnalysisState {
|
impl AnalysisState {
|
||||||
pub fn init_types(&mut self, meta: &mir::Metadata, ty: Option<TypeKind>) {
|
pub fn init_hover(&mut self, meta: &mir::Metadata, kind: Option<HoverKind>, documentation: Option<String>) {
|
||||||
for token in meta.range.start..=meta.range.end {
|
for token in meta.range.start..=meta.range.end {
|
||||||
self.map.insert(
|
self.map.insert(
|
||||||
token,
|
token,
|
||||||
SemanticToken {
|
SemanticToken {
|
||||||
ty: ty.clone(),
|
hover: Some(Hover {
|
||||||
|
documentation: documentation.clone(),
|
||||||
|
kind: kind.clone(),
|
||||||
|
}),
|
||||||
autocomplete: Vec::new(),
|
autocomplete: Vec::new(),
|
||||||
symbol: Default::default(),
|
symbol: Default::default(),
|
||||||
},
|
},
|
||||||
@ -168,7 +185,7 @@ impl AnalysisState {
|
|||||||
self.map.insert(
|
self.map.insert(
|
||||||
token_idx,
|
token_idx,
|
||||||
SemanticToken {
|
SemanticToken {
|
||||||
ty: None,
|
hover: None,
|
||||||
autocomplete: autocomplete.clone(),
|
autocomplete: autocomplete.clone(),
|
||||||
symbol: Default::default(),
|
symbol: Default::default(),
|
||||||
},
|
},
|
||||||
@ -176,6 +193,33 @@ impl AnalysisState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_documentation(&mut self, token_idx: usize, documentation: Option<String>) {
|
||||||
|
if let Some(documentation) = documentation {
|
||||||
|
if let Some(token) = self.map.get_mut(&token_idx) {
|
||||||
|
if let Some(hover) = &mut token.hover {
|
||||||
|
hover.documentation = Some(documentation);
|
||||||
|
} else {
|
||||||
|
token.hover = Some(Hover {
|
||||||
|
documentation: Some(documentation),
|
||||||
|
kind: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.map.insert(
|
||||||
|
token_idx,
|
||||||
|
SemanticToken {
|
||||||
|
hover: Some(Hover {
|
||||||
|
documentation: Some(documentation),
|
||||||
|
kind: None,
|
||||||
|
}),
|
||||||
|
autocomplete: Vec::new(),
|
||||||
|
symbol: Default::default(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_symbol(&mut self, idx: usize, symbol: SymbolId) {
|
pub fn set_symbol(&mut self, idx: usize, symbol: SymbolId) {
|
||||||
self.symbol_to_token.insert(symbol, idx);
|
self.symbol_to_token.insert(symbol, idx);
|
||||||
if let Some(token) = self.map.get_mut(&idx) {
|
if let Some(token) = self.map.get_mut(&idx) {
|
||||||
@ -184,7 +228,7 @@ impl AnalysisState {
|
|||||||
self.map.insert(
|
self.map.insert(
|
||||||
idx,
|
idx,
|
||||||
SemanticToken {
|
SemanticToken {
|
||||||
ty: None,
|
hover: None,
|
||||||
autocomplete: Vec::new(),
|
autocomplete: Vec::new(),
|
||||||
symbol: Some(symbol),
|
symbol: Some(symbol),
|
||||||
},
|
},
|
||||||
@ -229,8 +273,8 @@ pub struct AnalysisScope<'a> {
|
|||||||
tokens: &'a Vec<FullToken>,
|
tokens: &'a Vec<FullToken>,
|
||||||
variables: HashMap<String, SymbolId>,
|
variables: HashMap<String, SymbolId>,
|
||||||
types: HashMap<TypeKind, (SourceModuleId, SymbolId)>,
|
types: HashMap<TypeKind, (SourceModuleId, SymbolId)>,
|
||||||
functions: HashMap<String, (SourceModuleId, SymbolId)>,
|
functions: HashMap<String, (SourceModuleId, SymbolId, Option<String>)>,
|
||||||
associated_functions: HashMap<(TypeKind, String), (SourceModuleId, SymbolId)>,
|
associated_functions: HashMap<(TypeKind, String), (SourceModuleId, SymbolId, Option<String>)>,
|
||||||
map: &'a StateMap,
|
map: &'a StateMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,6 +445,33 @@ pub fn analyze(
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pub fn find_documentation(meta: &Metadata, tokens: &Vec<FullToken>) -> Option<String> {
|
||||||
|
// let mut documentation = None;
|
||||||
|
// for idx in meta.range.start..=meta.range.end {
|
||||||
|
// if let Some(token) = tokens.get(idx) {
|
||||||
|
// dbg!(&token);
|
||||||
|
// if matches!(token.token, Token::Whitespace(_) | Token::Doc(_) | Token::Comment(_)) {
|
||||||
|
// if let Token::Doc(doctext) = &token.token {
|
||||||
|
// documentation = Some(
|
||||||
|
// match documentation {
|
||||||
|
// Some(t) => t + " ",
|
||||||
|
// None => String::new(),
|
||||||
|
// } + doctext.trim(),
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// dbg!(&token);
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// dbg!(&idx);
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// dbg!(&documentation);
|
||||||
|
// documentation
|
||||||
|
// }
|
||||||
|
|
||||||
pub fn analyze_context(
|
pub fn analyze_context(
|
||||||
context: &mir::Context,
|
context: &mir::Context,
|
||||||
module: &mir::Module,
|
module: &mir::Module,
|
||||||
@ -505,7 +576,7 @@ pub fn analyze_context(
|
|||||||
.token_idx(&field.2, |t| matches!(t, Token::Identifier(_)))
|
.token_idx(&field.2, |t| matches!(t, Token::Identifier(_)))
|
||||||
.unwrap_or(field.2.range.end);
|
.unwrap_or(field.2.range.end);
|
||||||
|
|
||||||
scope.state.init_types(
|
scope.state.init_hover(
|
||||||
&Metadata {
|
&Metadata {
|
||||||
source_module_id: field.2.source_module_id,
|
source_module_id: field.2.source_module_id,
|
||||||
range: TokenRange {
|
range: TokenRange {
|
||||||
@ -514,7 +585,8 @@ pub fn analyze_context(
|
|||||||
},
|
},
|
||||||
position: None,
|
position: None,
|
||||||
},
|
},
|
||||||
Some(field.1.clone()),
|
Some(HoverKind::Type(field.1.clone())),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
let field_symbol = scope.state.new_symbol(field_idx, SemanticKind::Property);
|
let field_symbol = scope.state.new_symbol(field_idx, SemanticKind::Property);
|
||||||
@ -556,7 +628,7 @@ pub fn analyze_context(
|
|||||||
scope.state.new_symbol(field_ty_idx, SemanticKind::Type)
|
scope.state.new_symbol(field_ty_idx, SemanticKind::Type)
|
||||||
};
|
};
|
||||||
|
|
||||||
scope.state.init_types(
|
scope.state.init_hover(
|
||||||
&Metadata {
|
&Metadata {
|
||||||
source_module_id: field.2.source_module_id,
|
source_module_id: field.2.source_module_id,
|
||||||
range: TokenRange {
|
range: TokenRange {
|
||||||
@ -565,7 +637,8 @@ pub fn analyze_context(
|
|||||||
},
|
},
|
||||||
position: None,
|
position: None,
|
||||||
},
|
},
|
||||||
Some(field.1.clone()),
|
Some(HoverKind::Type(field.1.clone())),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
scope.state.set_symbol(field_ty_idx, field_ty_symbol);
|
scope.state.set_symbol(field_ty_idx, field_ty_symbol);
|
||||||
}
|
}
|
||||||
@ -576,7 +649,9 @@ pub fn analyze_context(
|
|||||||
for binop in &module.binop_defs {
|
for binop in &module.binop_defs {
|
||||||
if binop.meta.source_module_id == module.module_id {
|
if binop.meta.source_module_id == module.module_id {
|
||||||
for param in [&binop.lhs, &binop.rhs] {
|
for param in [&binop.lhs, &binop.rhs] {
|
||||||
scope.state.init_types(¶m.meta, Some(param.ty.clone()));
|
scope
|
||||||
|
.state
|
||||||
|
.init_hover(¶m.meta, Some(HoverKind::Type(param.ty.clone())), None);
|
||||||
let idx = scope
|
let idx = scope
|
||||||
.token_idx(¶m.meta, |t| matches!(t, Token::Identifier(_)))
|
.token_idx(¶m.meta, |t| matches!(t, Token::Identifier(_)))
|
||||||
.unwrap_or(param.meta.range.end);
|
.unwrap_or(param.meta.range.end);
|
||||||
@ -598,9 +673,10 @@ pub fn analyze_context(
|
|||||||
if source_id != module.module_id {
|
if source_id != module.module_id {
|
||||||
if let Some(state) = map.get(&source_id) {
|
if let Some(state) = map.get(&source_id) {
|
||||||
if let Some(symbol) = state.associated_functions.get(&(ty.clone(), function.name.clone())) {
|
if let Some(symbol) = state.associated_functions.get(&(ty.clone(), function.name.clone())) {
|
||||||
scope
|
scope.associated_functions.insert(
|
||||||
.associated_functions
|
(ty.clone(), function.name.clone()),
|
||||||
.insert((ty.clone(), function.name.clone()), (source_id, *symbol));
|
(source_id, *symbol, function.documentation.clone()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@ -616,9 +692,10 @@ pub fn analyze_context(
|
|||||||
.state
|
.state
|
||||||
.associated_functions
|
.associated_functions
|
||||||
.insert((ty.clone(), function.name.clone()), symbol);
|
.insert((ty.clone(), function.name.clone()), symbol);
|
||||||
scope
|
scope.associated_functions.insert(
|
||||||
.associated_functions
|
(ty.clone(), function.name.clone()),
|
||||||
.insert((ty.clone(), function.name.clone()), (module.module_id, symbol));
|
(module.module_id, symbol, function.documentation.clone()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (_, function) in &module.associated_functions {
|
for (_, function) in &module.associated_functions {
|
||||||
@ -644,16 +721,21 @@ pub fn analyze_context(
|
|||||||
if source_id != module.module_id {
|
if source_id != module.module_id {
|
||||||
if let Some(state) = map.get(&source_id) {
|
if let Some(state) = map.get(&source_id) {
|
||||||
if let Some(symbol) = state.functions.get(&function.name) {
|
if let Some(symbol) = state.functions.get(&function.name) {
|
||||||
scope.functions.insert(function.name.clone(), (source_id, *symbol));
|
scope.functions.insert(
|
||||||
|
function.name.clone(),
|
||||||
|
(source_id, *symbol, function.documentation.clone()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scope
|
scope.state.init_hover(
|
||||||
.state
|
&function.signature(),
|
||||||
.init_types(&function.signature(), Some(function.return_type.clone()));
|
Some(HoverKind::Type(function.return_type.clone())),
|
||||||
|
function.documentation.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
let idx = scope
|
let idx = scope
|
||||||
.token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_)))
|
.token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_)))
|
||||||
@ -661,9 +743,10 @@ pub fn analyze_context(
|
|||||||
let function_symbol = scope.state.new_symbol(idx, SemanticKind::Function);
|
let function_symbol = scope.state.new_symbol(idx, SemanticKind::Function);
|
||||||
scope.state.set_symbol(idx, function_symbol);
|
scope.state.set_symbol(idx, function_symbol);
|
||||||
scope.state.functions.insert(function.name.clone(), function_symbol);
|
scope.state.functions.insert(function.name.clone(), function_symbol);
|
||||||
scope
|
scope.functions.insert(
|
||||||
.functions
|
function.name.clone(),
|
||||||
.insert(function.name.clone(), (module.module_id, function_symbol));
|
(module.module_id, function_symbol, function.documentation.clone()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for function in &module.functions {
|
for function in &module.functions {
|
||||||
@ -685,7 +768,7 @@ pub fn analyze_context(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for import in &module.imports {
|
for import in &module.imports {
|
||||||
scope.state.init_types(&import.1, None);
|
scope.state.init_hover(&import.1, None, None);
|
||||||
if let Some((module_name, _)) = import.0.get(0) {
|
if let Some((module_name, _)) = import.0.get(0) {
|
||||||
let module_idx = scope
|
let module_idx = scope
|
||||||
.token_idx(&import.1, |t| matches!(t, Token::Identifier(_)))
|
.token_idx(&import.1, |t| matches!(t, Token::Identifier(_)))
|
||||||
@ -712,27 +795,27 @@ pub fn analyze_context(
|
|||||||
if !function.is_pub {
|
if !function.is_pub {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if function.name.starts_with(&import_name) {
|
|
||||||
autocompletes.push(Autocomplete {
|
autocompletes.push(Autocomplete {
|
||||||
text: function.name.clone(),
|
text: function.name.clone(),
|
||||||
|
documentation: function.documentation.clone(),
|
||||||
kind: AutocompleteKind::Function(function.parameters.clone(), function.return_type.clone()),
|
kind: AutocompleteKind::Function(function.parameters.clone(), function.return_type.clone()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
for typedef in &module.typedefs {
|
for typedef in &module.typedefs {
|
||||||
if typedef.name.starts_with(&import_name) {
|
|
||||||
autocompletes.push(Autocomplete {
|
autocompletes.push(Autocomplete {
|
||||||
text: typedef.name.clone(),
|
text: typedef.name.clone(),
|
||||||
|
documentation: None,
|
||||||
kind: AutocompleteKind::Type,
|
kind: AutocompleteKind::Type,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let symbol = if let Some((source_id, symbol_id)) = scope.functions.get(&import_name) {
|
let symbol = if let Some((source_id, symbol_id, doc)) = scope.functions.get(&import_name) {
|
||||||
scope
|
let symbol_id = scope
|
||||||
.state
|
.state
|
||||||
.new_symbol(import_idx, SemanticKind::Reference(*source_id, *symbol_id))
|
.new_symbol(import_idx, SemanticKind::Reference(*source_id, *symbol_id));
|
||||||
|
scope.state.set_documentation(import_idx, doc.clone());
|
||||||
|
symbol_id
|
||||||
} else if let Some(module_source) = scope.map.values().find(|s| s.module_name == *module_name) {
|
} else if let Some(module_source) = scope.map.values().find(|s| s.module_name == *module_name) {
|
||||||
if let Some((source_id, symbol_id)) = scope.types.get(&TypeKind::CustomType(CustomTypeKey(
|
if let Some((source_id, symbol_id)) = scope.types.get(&TypeKind::CustomType(CustomTypeKey(
|
||||||
import_name.clone(),
|
import_name.clone(),
|
||||||
@ -767,7 +850,9 @@ pub fn analyze_function_parameters(
|
|||||||
scope: &mut AnalysisScope,
|
scope: &mut AnalysisScope,
|
||||||
) {
|
) {
|
||||||
for param in &function.parameters {
|
for param in &function.parameters {
|
||||||
scope.state.init_types(¶m.meta, Some(param.ty.clone()));
|
scope
|
||||||
|
.state
|
||||||
|
.init_hover(¶m.meta, Some(HoverKind::Type(param.ty.clone())), None);
|
||||||
|
|
||||||
if param.meta.source_module_id == module.module_id {
|
if param.meta.source_module_id == module.module_id {
|
||||||
let param_var_idx = scope
|
let param_var_idx = scope
|
||||||
@ -806,12 +891,14 @@ pub fn analyze_block(
|
|||||||
for statement in &block.statements {
|
for statement in &block.statements {
|
||||||
match &statement.0 {
|
match &statement.0 {
|
||||||
mir::StmtKind::Let(named_variable_ref, _, expression) => {
|
mir::StmtKind::Let(named_variable_ref, _, expression) => {
|
||||||
scope.state.init_types(
|
scope.state.init_hover(
|
||||||
&named_variable_ref.2,
|
&named_variable_ref.2,
|
||||||
expression
|
expression
|
||||||
.return_type(&TypeRefs::unknown(), source_module.module_id)
|
.return_type(&TypeRefs::unknown(), source_module.module_id)
|
||||||
.ok()
|
.ok()
|
||||||
.map(|(_, ty)| ty),
|
.map(|(_, ty)| ty)
|
||||||
|
.map(|t| HoverKind::Type(t)),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
let idx = scope
|
let idx = scope
|
||||||
.token_idx(&named_variable_ref.2, |t| matches!(t, Token::Identifier(_)))
|
.token_idx(&named_variable_ref.2, |t| matches!(t, Token::Identifier(_)))
|
||||||
@ -823,7 +910,6 @@ pub fn analyze_block(
|
|||||||
let ty_idx = scope.token_idx(&named_variable_ref.2.after(idx + 1), |t| {
|
let ty_idx = scope.token_idx(&named_variable_ref.2.after(idx + 1), |t| {
|
||||||
matches!(t, Token::Identifier(_))
|
matches!(t, Token::Identifier(_))
|
||||||
});
|
});
|
||||||
dbg!(ty_idx);
|
|
||||||
if let Some(ty_idx) = ty_idx {
|
if let Some(ty_idx) = ty_idx {
|
||||||
let ty_symbol = if let Some((source_id, symbol_id)) = scope.types.get(&named_variable_ref.0) {
|
let ty_symbol = if let Some((source_id, symbol_id)) = scope.types.get(&named_variable_ref.0) {
|
||||||
scope
|
scope
|
||||||
@ -863,16 +949,19 @@ pub fn analyze_expr(
|
|||||||
expr: &mir::Expression,
|
expr: &mir::Expression,
|
||||||
scope: &mut AnalysisScope,
|
scope: &mut AnalysisScope,
|
||||||
) {
|
) {
|
||||||
scope.state.init_types(
|
scope.state.init_hover(
|
||||||
&expr.1,
|
&expr.1,
|
||||||
expr.return_type(&TypeRefs::unknown(), source_module.module_id)
|
expr.return_type(&TypeRefs::unknown(), source_module.module_id)
|
||||||
.ok()
|
.ok()
|
||||||
.map(|(_, t)| t),
|
.map(|(_, t)| HoverKind::Type(t)),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
match &expr.0 {
|
match &expr.0 {
|
||||||
mir::ExprKind::Variable(var_ref) => {
|
mir::ExprKind::Variable(var_ref) => {
|
||||||
scope.state.init_types(&var_ref.2, Some(var_ref.0.clone()));
|
scope
|
||||||
|
.state
|
||||||
|
.init_hover(&var_ref.2, Some(HoverKind::Type(var_ref.0.clone())), None);
|
||||||
|
|
||||||
let idx = scope
|
let idx = scope
|
||||||
.token_idx(&var_ref.2, |t| matches!(t, Token::Identifier(_)))
|
.token_idx(&var_ref.2, |t| matches!(t, Token::Identifier(_)))
|
||||||
@ -902,9 +991,10 @@ pub fn analyze_expr(
|
|||||||
source_module
|
source_module
|
||||||
.associated_functions
|
.associated_functions
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(t, fun)| *t == accessed_type && fun.name.starts_with(name))
|
.filter(|(t, _)| *t == accessed_type)
|
||||||
.map(|(_, fun)| Autocomplete {
|
.map(|(_, fun)| Autocomplete {
|
||||||
text: fun.name.clone(),
|
text: fun.name.clone(),
|
||||||
|
documentation: fun.documentation.clone(),
|
||||||
kind: AutocompleteKind::Function(fun.parameters.clone(), fun.return_type.clone()),
|
kind: AutocompleteKind::Function(fun.parameters.clone(), fun.return_type.clone()),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -933,8 +1023,9 @@ pub fn analyze_expr(
|
|||||||
if let Some(typedef) = typedef {
|
if let Some(typedef) = typedef {
|
||||||
autocompletes.extend(match &typedef.kind {
|
autocompletes.extend(match &typedef.kind {
|
||||||
mir::TypeDefinitionKind::Struct(StructType(fields)) => {
|
mir::TypeDefinitionKind::Struct(StructType(fields)) => {
|
||||||
fields.iter().filter(|f| f.0.starts_with(name)).map(|f| Autocomplete {
|
fields.iter().map(|f| Autocomplete {
|
||||||
text: f.0.clone(),
|
text: f.0.clone(),
|
||||||
|
documentation: None,
|
||||||
kind: AutocompleteKind::Field(f.1.clone()),
|
kind: AutocompleteKind::Field(f.1.clone()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -1014,10 +1105,12 @@ pub fn analyze_expr(
|
|||||||
let idx = scope
|
let idx = scope
|
||||||
.token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
|
.token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
|
||||||
.unwrap_or(meta.range.end);
|
.unwrap_or(meta.range.end);
|
||||||
let symbol = if let Some((module_id, symbol_id)) = scope.functions.get(name) {
|
let symbol = if let Some((module_id, symbol_id, doc)) = scope.functions.get(name) {
|
||||||
scope
|
let symbol = scope
|
||||||
.state
|
.state
|
||||||
.new_symbol(idx, SemanticKind::Reference(*module_id, *symbol_id))
|
.new_symbol(idx, SemanticKind::Reference(*module_id, *symbol_id));
|
||||||
|
scope.state.set_documentation(idx, doc.clone());
|
||||||
|
symbol
|
||||||
} else {
|
} else {
|
||||||
scope.state.new_symbol(idx, SemanticKind::Function)
|
scope.state.new_symbol(idx, SemanticKind::Function)
|
||||||
};
|
};
|
||||||
@ -1050,12 +1143,14 @@ pub fn analyze_expr(
|
|||||||
let fn_idx = scope
|
let fn_idx = scope
|
||||||
.token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
|
.token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
|
||||||
.unwrap_or(meta.range.end);
|
.unwrap_or(meta.range.end);
|
||||||
let fn_symbol = if let Some((module_id, symbol_id)) =
|
let fn_symbol = if let Some((module_id, symbol_id, doc)) =
|
||||||
scope.associated_functions.get(&(invoked_ty.clone(), name.clone()))
|
scope.associated_functions.get(&(invoked_ty.clone(), name.clone()))
|
||||||
{
|
{
|
||||||
scope
|
let symbol = scope
|
||||||
.state
|
.state
|
||||||
.new_symbol(fn_idx, SemanticKind::Reference(*module_id, *symbol_id))
|
.new_symbol(fn_idx, SemanticKind::Reference(*module_id, *symbol_id));
|
||||||
|
scope.state.set_documentation(fn_idx, doc.clone());
|
||||||
|
symbol
|
||||||
} else {
|
} else {
|
||||||
scope.state.new_symbol(fn_idx, SemanticKind::Function)
|
scope.state.new_symbol(fn_idx, SemanticKind::Function)
|
||||||
};
|
};
|
||||||
@ -1067,18 +1162,19 @@ pub fn analyze_expr(
|
|||||||
let mut function_autocomplete = source_module
|
let mut function_autocomplete = source_module
|
||||||
.associated_functions
|
.associated_functions
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(t, fun)| *t == invoked_ty && fun.name.starts_with(name))
|
.filter(|(t, _)| *t == invoked_ty)
|
||||||
.map(|(_, fun)| Autocomplete {
|
.map(|(_, fun)| Autocomplete {
|
||||||
text: fun.name.clone(),
|
text: fun.name.clone(),
|
||||||
|
documentation: fun.documentation.clone(),
|
||||||
kind: AutocompleteKind::Function(fun.parameters.clone(), fun.return_type.clone()),
|
kind: AutocompleteKind::Function(fun.parameters.clone(), fun.return_type.clone()),
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
function_autocomplete.extend(
|
function_autocomplete.extend(
|
||||||
get_intrinsic_assoc_functions(&invoked_ty)
|
get_intrinsic_assoc_functions(&invoked_ty)
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|fun| fun.name.starts_with(name))
|
|
||||||
.map(|fun| Autocomplete {
|
.map(|fun| Autocomplete {
|
||||||
text: fun.name.clone(),
|
text: fun.name.clone(),
|
||||||
|
documentation: Some("intrinsic function documentation".to_string()),
|
||||||
kind: AutocompleteKind::Function(fun.parameters.clone(), fun.return_type.clone()),
|
kind: AutocompleteKind::Function(fun.parameters.clone(), fun.return_type.clone()),
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
|
@ -6,17 +6,18 @@ use reid::ast::lexer::{FullToken, Position};
|
|||||||
use reid::error_raporting::{self, ErrorModules, ReidError};
|
use reid::error_raporting::{self, ErrorModules, ReidError};
|
||||||
use reid::mir::SourceModuleId;
|
use reid::mir::SourceModuleId;
|
||||||
use reid::parse_module;
|
use reid::parse_module;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tower_lsp::lsp_types::{
|
use tower_lsp::lsp_types::{
|
||||||
self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity,
|
self, CompletionItem, CompletionItemKind, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic,
|
||||||
DidChangeTextDocumentParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams, DocumentFilter,
|
DiagnosticSeverity, DidChangeTextDocumentParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams,
|
||||||
GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverContents, HoverParams, HoverProviderCapability,
|
DocumentFilter, GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverContents, HoverParams,
|
||||||
InitializeParams, InitializeResult, InitializedParams, Location, MarkupContent, MarkupKind, MessageType, OneOf,
|
HoverProviderCapability, InitializeParams, InitializeResult, InitializedParams, Location, MarkedString,
|
||||||
Range, ReferenceParams, RenameParams, SemanticToken, SemanticTokensLegend, SemanticTokensOptions,
|
MarkupContent, MarkupKind, MessageType, OneOf, Range, ReferenceParams, RenameParams, SemanticToken,
|
||||||
SemanticTokensParams, SemanticTokensResult, SemanticTokensServerCapabilities, ServerCapabilities, TextDocumentItem,
|
SemanticTokensLegend, SemanticTokensOptions, SemanticTokensParams, SemanticTokensResult,
|
||||||
TextDocumentRegistrationOptions, TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions,
|
SemanticTokensServerCapabilities, ServerCapabilities, TextDocumentItem, TextDocumentRegistrationOptions,
|
||||||
TextDocumentSyncSaveOptions, TextEdit, Url, WorkspaceEdit, WorkspaceFoldersServerCapabilities,
|
TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions, TextDocumentSyncSaveOptions, TextEdit,
|
||||||
WorkspaceServerCapabilities,
|
Url, WorkspaceEdit, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
|
||||||
};
|
};
|
||||||
use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc};
|
use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc};
|
||||||
|
|
||||||
@ -33,6 +34,12 @@ struct Backend {
|
|||||||
module_id_counter: Mutex<SourceModuleId>,
|
module_id_counter: Mutex<SourceModuleId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct CompletionData {
|
||||||
|
token_idx: usize,
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
#[tower_lsp::async_trait]
|
#[tower_lsp::async_trait]
|
||||||
impl LanguageServer for Backend {
|
impl LanguageServer for Backend {
|
||||||
async fn initialize(&self, _: InitializeParams) -> jsonrpc::Result<InitializeResult> {
|
async fn initialize(&self, _: InitializeParams) -> jsonrpc::Result<InitializeResult> {
|
||||||
@ -52,7 +59,17 @@ impl LanguageServer for Backend {
|
|||||||
|
|
||||||
let capabilities = ServerCapabilities {
|
let capabilities = ServerCapabilities {
|
||||||
hover_provider: Some(HoverProviderCapability::Simple(true)),
|
hover_provider: Some(HoverProviderCapability::Simple(true)),
|
||||||
completion_provider: Some(CompletionOptions { ..Default::default() }),
|
completion_provider: Some(CompletionOptions {
|
||||||
|
trigger_characters: Some(vec![".".to_string(), "::".to_string()]),
|
||||||
|
all_commit_characters: None,
|
||||||
|
completion_item: Some(lsp_types::CompletionOptionsCompletionItem {
|
||||||
|
label_details_support: Some(true),
|
||||||
|
}),
|
||||||
|
resolve_provider: Some(true),
|
||||||
|
work_done_progress_options: lsp_types::WorkDoneProgressOptions {
|
||||||
|
work_done_progress: Some(true),
|
||||||
|
},
|
||||||
|
}),
|
||||||
text_document_sync: Some(TextDocumentSyncCapability::Options(sync)),
|
text_document_sync: Some(TextDocumentSyncCapability::Options(sync)),
|
||||||
workspace: Some(WorkspaceServerCapabilities {
|
workspace: Some(WorkspaceServerCapabilities {
|
||||||
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
|
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
|
||||||
@ -121,11 +138,26 @@ impl LanguageServer for Backend {
|
|||||||
// dbg!(position, token);
|
// dbg!(position, token);
|
||||||
|
|
||||||
let list = if let Some((idx, _)) = token {
|
let list = if let Some((idx, _)) = token {
|
||||||
if let Some(analysis) = self.analysis.get(&path).unwrap().state.map.get(&idx) {
|
if let Some(token_analysis) = self.analysis.get(&path).unwrap().state.map.get(&idx) {
|
||||||
analysis
|
token_analysis
|
||||||
.autocomplete
|
.autocomplete
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| CompletionItem::new_simple(s.text.to_string(), s.kind.to_string()))
|
.map(|autocomplete| {
|
||||||
|
let mut item =
|
||||||
|
CompletionItem::new_simple(autocomplete.text.to_string(), autocomplete.kind.to_string());
|
||||||
|
item.data = Some(
|
||||||
|
serde_json::to_value(CompletionData {
|
||||||
|
token_idx: idx,
|
||||||
|
path: path.clone(),
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
item.documentation = autocomplete
|
||||||
|
.documentation
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|d| Some(lsp_types::Documentation::String(d.clone())));
|
||||||
|
item
|
||||||
|
})
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
@ -138,6 +170,20 @@ impl LanguageServer for Backend {
|
|||||||
Ok(Some(CompletionResponse::Array(list)))
|
Ok(Some(CompletionResponse::Array(list)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn completion_resolve(&self, params: CompletionItem) -> jsonrpc::Result<CompletionItem> {
|
||||||
|
let data: Option<CompletionData> = if let Some(data) = ¶ms.data {
|
||||||
|
serde_json::from_value(data.clone()).ok()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
if let Some(data) = data {
|
||||||
|
let analysis = self.analysis.get(&data.path).unwrap();
|
||||||
|
let token = analysis.tokens.get(data.token_idx).unwrap();
|
||||||
|
if let Some(token_analysis) = analysis.state.map.get(&data.token_idx) {}
|
||||||
|
}
|
||||||
|
Ok(params)
|
||||||
|
}
|
||||||
|
|
||||||
async fn hover(&self, params: HoverParams) -> jsonrpc::Result<Option<Hover>> {
|
async fn hover(&self, params: HoverParams) -> jsonrpc::Result<Option<Hover>> {
|
||||||
let path = PathBuf::from(params.text_document_position_params.text_document.uri.path());
|
let path = PathBuf::from(params.text_document_position_params.text_document.uri.path());
|
||||||
let analysis = self.analysis.get(&path);
|
let analysis = self.analysis.get(&path);
|
||||||
@ -153,7 +199,7 @@ impl LanguageServer for Backend {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let (range, ty) = if let Some((idx, token)) = token {
|
let (range, ty, documentation) = if let Some((idx, token)) = token {
|
||||||
if let Some(analysis) = self.analysis.get(&path).unwrap().state.map.get(&idx) {
|
if let Some(analysis) = self.analysis.get(&path).unwrap().state.map.get(&idx) {
|
||||||
let start = token.position;
|
let start = token.position;
|
||||||
let end = token.position.add(token.token.len() as u32);
|
let end = token.position.add(token.token.len() as u32);
|
||||||
@ -167,22 +213,48 @@ impl LanguageServer for Backend {
|
|||||||
character: (end.0 as i32 - 1).max(0) as u32,
|
character: (end.0 as i32 - 1).max(0) as u32,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
if let Some(ty) = analysis.ty.clone() {
|
if let Some(hover) = analysis.hover.clone() {
|
||||||
(Some(range), format!("{}", ty))
|
if let Some(kind) = hover.kind {
|
||||||
} else {
|
match kind {
|
||||||
(Some(range), String::from("None type"))
|
analysis::HoverKind::Type(type_kind) => {
|
||||||
|
(Some(range), format!("{}", type_kind), hover.documentation)
|
||||||
|
}
|
||||||
|
analysis::HoverKind::Function(name, function_params, return_type) => (
|
||||||
|
Some(range),
|
||||||
|
format!(
|
||||||
|
"{}({}) -> {}",
|
||||||
|
name,
|
||||||
|
function_params
|
||||||
|
.iter()
|
||||||
|
.map(|p| format!("{}: {}", p.name, p.ty))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", "),
|
||||||
|
return_type
|
||||||
|
),
|
||||||
|
hover.documentation,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(None, String::from("no type"))
|
(Some(range), String::from("No type"), hover.documentation)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(None, String::from("no token"))
|
(Some(range), String::from("No hover"), None)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(None, String::from("no type"), None)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(None, String::from("no token"), None)
|
||||||
};
|
};
|
||||||
|
|
||||||
let contents = HoverContents::Markup(MarkupContent {
|
let contents = if let Some(doc) = documentation {
|
||||||
|
HoverContents::Array(vec![MarkedString::String(doc), MarkedString::String(format!("`{ty}`"))])
|
||||||
|
} else {
|
||||||
|
HoverContents::Markup(MarkupContent {
|
||||||
kind: MarkupKind::Markdown,
|
kind: MarkupKind::Markdown,
|
||||||
value: format!("`{ty}`"),
|
value: format!("`{ty}`"),
|
||||||
});
|
})
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Some(Hover { contents, range }))
|
Ok(Some(Hover { contents, range }))
|
||||||
}
|
}
|
||||||
|
@ -116,6 +116,7 @@ pub enum Token {
|
|||||||
|
|
||||||
Whitespace(String),
|
Whitespace(String),
|
||||||
Comment(String),
|
Comment(String),
|
||||||
|
Doc(String),
|
||||||
Eof,
|
Eof,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,6 +197,7 @@ impl ToString for Token {
|
|||||||
Token::Percent => String::from('%'),
|
Token::Percent => String::from('%'),
|
||||||
Token::Whitespace(val) => val.clone(),
|
Token::Whitespace(val) => val.clone(),
|
||||||
Token::Comment(val) => format!("//{}", val.clone()),
|
Token::Comment(val) => format!("//{}", val.clone()),
|
||||||
|
Token::Doc(val) => format!("///{}", val.clone()),
|
||||||
Token::Unknown(val) => val.to_string(),
|
Token::Unknown(val) => val.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -309,14 +311,26 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
|
|||||||
}
|
}
|
||||||
// Comments
|
// Comments
|
||||||
'/' if cursor.first() == Some('/') => {
|
'/' if cursor.first() == Some('/') => {
|
||||||
|
cursor.next();
|
||||||
|
let doc = if cursor.first() == Some('/') {
|
||||||
|
cursor.next();
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
let mut comment = String::new();
|
let mut comment = String::new();
|
||||||
while !matches!(cursor.first(), Some('\n') | None) {
|
while !matches!(cursor.first(), Some('\n') | None) {
|
||||||
if let Some(c) = cursor.next() {
|
if let Some(c) = cursor.next() {
|
||||||
comment.push(c);
|
comment.push(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if doc {
|
||||||
|
Token::Doc(comment)
|
||||||
|
} else {
|
||||||
Token::Comment(comment)
|
Token::Comment(comment)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
'\"' | '\'' => {
|
'\"' | '\'' => {
|
||||||
let mut value = String::new();
|
let mut value = String::new();
|
||||||
let mut escape_next = false;
|
let mut escape_next = false;
|
||||||
|
@ -192,6 +192,7 @@ pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub To
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FunctionSignature {
|
pub struct FunctionSignature {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub documentation: Option<String>,
|
||||||
pub self_kind: SelfKind,
|
pub self_kind: SelfKind,
|
||||||
pub params: Vec<(String, Type, TokenRange)>,
|
pub params: Vec<(String, Type, TokenRange)>,
|
||||||
pub return_type: Option<Type>,
|
pub return_type: Option<Type>,
|
||||||
|
@ -696,6 +696,10 @@ impl Parse for ImportStatement {
|
|||||||
|
|
||||||
impl Parse for FunctionDefinition {
|
impl Parse for FunctionDefinition {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
|
let documentation = stream.find_documentation();
|
||||||
|
dbg!(&stream.get_range());
|
||||||
|
dbg!(&documentation);
|
||||||
|
|
||||||
let is_pub = if let Some(Token::PubKeyword) = stream.peek() {
|
let is_pub = if let Some(Token::PubKeyword) = stream.peek() {
|
||||||
stream.next(); // Consume pub
|
stream.next(); // Consume pub
|
||||||
true
|
true
|
||||||
@ -704,8 +708,10 @@ impl Parse for FunctionDefinition {
|
|||||||
};
|
};
|
||||||
|
|
||||||
stream.expect(Token::FnKeyword)?;
|
stream.expect(Token::FnKeyword)?;
|
||||||
|
let mut signature: FunctionSignature = stream.parse()?;
|
||||||
|
signature.documentation = documentation;
|
||||||
Ok(FunctionDefinition(
|
Ok(FunctionDefinition(
|
||||||
stream.parse()?,
|
signature,
|
||||||
is_pub,
|
is_pub,
|
||||||
stream.parse()?,
|
stream.parse()?,
|
||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
@ -810,6 +816,7 @@ impl Parse for FunctionSignature {
|
|||||||
|
|
||||||
Ok(FunctionSignature {
|
Ok(FunctionSignature {
|
||||||
name,
|
name,
|
||||||
|
documentation: None,
|
||||||
params,
|
params,
|
||||||
self_kind,
|
self_kind,
|
||||||
return_type,
|
return_type,
|
||||||
@ -1089,9 +1096,12 @@ impl Parse for TopLevelStatement {
|
|||||||
Ok(match stream.peek() {
|
Ok(match stream.peek() {
|
||||||
Some(Token::ImportKeyword) => Stmt::Import(stream.parse()?),
|
Some(Token::ImportKeyword) => Stmt::Import(stream.parse()?),
|
||||||
Some(Token::Extern) => {
|
Some(Token::Extern) => {
|
||||||
|
let documentation = stream.find_documentation();
|
||||||
stream.next(); // Consume Extern
|
stream.next(); // Consume Extern
|
||||||
stream.expect(Token::FnKeyword)?;
|
stream.expect(Token::FnKeyword)?;
|
||||||
let extern_fn = Stmt::ExternFunction(stream.parse()?);
|
let mut signature: FunctionSignature = stream.parse()?;
|
||||||
|
signature.documentation = documentation;
|
||||||
|
let extern_fn = Stmt::ExternFunction(signature);
|
||||||
stream.expect_nonfatal(Token::Semi).ok();
|
stream.expect_nonfatal(Token::Semi).ok();
|
||||||
extern_fn
|
extern_fn
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,7 @@ impl ast::Module {
|
|||||||
ExternFunction(signature) => {
|
ExternFunction(signature) => {
|
||||||
let def = mir::FunctionDefinition {
|
let def = mir::FunctionDefinition {
|
||||||
name: signature.name.clone(),
|
name: signature.name.clone(),
|
||||||
|
documentation: signature.documentation.clone(),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: false,
|
is_pub: false,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -176,6 +177,7 @@ impl ast::FunctionDefinition {
|
|||||||
}));
|
}));
|
||||||
mir::FunctionDefinition {
|
mir::FunctionDefinition {
|
||||||
name: signature.name.clone(),
|
name: signature.name.clone(),
|
||||||
|
documentation: signature.documentation.clone(),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: *is_pub,
|
is_pub: *is_pub,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
|
@ -87,6 +87,28 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_documentation(&mut self) -> Option<String> {
|
||||||
|
let mut from = self.position;
|
||||||
|
let mut documentation = None;
|
||||||
|
while let Some(token) = self.tokens.get(from) {
|
||||||
|
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_) | Token::Doc(_)) {
|
||||||
|
from += 1;
|
||||||
|
if let Token::Doc(doctext) = &token.token {
|
||||||
|
documentation = Some(
|
||||||
|
match documentation {
|
||||||
|
Some(t) => t + " ",
|
||||||
|
None => String::new(),
|
||||||
|
} + doctext.trim(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dbg!(self.position, from, &documentation);
|
||||||
|
documentation
|
||||||
|
}
|
||||||
|
|
||||||
pub fn expect_nonfatal(&mut self, token: Token) -> Result<(), ()> {
|
pub fn expect_nonfatal(&mut self, token: Token) -> Result<(), ()> {
|
||||||
if let (pos, Some(peeked)) = self.next_token(self.position) {
|
if let (pos, Some(peeked)) = self.next_token(self.position) {
|
||||||
if token == peeked.token {
|
if token == peeked.token {
|
||||||
@ -249,7 +271,7 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
fn previous_token(&self, mut from: usize) -> (usize, Option<&'a FullToken>) {
|
fn previous_token(&self, mut from: usize) -> (usize, Option<&'a FullToken>) {
|
||||||
from -= 1;
|
from -= 1;
|
||||||
while let Some(token) = self.tokens.get(from) {
|
while let Some(token) = self.tokens.get(from) {
|
||||||
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_)) {
|
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_) | Token::Doc(_)) {
|
||||||
from -= 1;
|
from -= 1;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -260,7 +282,7 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
|
|
||||||
fn next_token(&self, mut from: usize) -> (usize, Option<&'a FullToken>) {
|
fn next_token(&self, mut from: usize) -> (usize, Option<&'a FullToken>) {
|
||||||
while let Some(token) = self.tokens.get(from) {
|
while let Some(token) = self.tokens.get(from) {
|
||||||
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_)) {
|
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_) | Token::Doc(_)) {
|
||||||
from += 1;
|
from += 1;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
@ -71,6 +71,7 @@ pub fn form_intrinsics() -> Vec<FunctionDefinition> {
|
|||||||
|
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: MALLOC_IDENT.to_owned(),
|
name: MALLOC_IDENT.to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: Some("malloc".to_owned()),
|
linkage_name: Some("malloc".to_owned()),
|
||||||
is_pub: false,
|
is_pub: false,
|
||||||
is_imported: true,
|
is_imported: true,
|
||||||
@ -96,6 +97,7 @@ pub fn simple_intrinsic<T: Into<String> + Clone>(
|
|||||||
) -> FunctionDefinition {
|
) -> FunctionDefinition {
|
||||||
FunctionDefinition {
|
FunctionDefinition {
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -115,6 +117,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
|||||||
if let TypeKind::Array(_, len) = ty {
|
if let TypeKind::Array(_, len) = ty {
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: "length".to_owned(),
|
name: "length".to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -252,6 +255,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
|||||||
));
|
));
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: "powi".to_owned(),
|
name: "powi".to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -293,6 +297,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
|||||||
if ty.signed() {
|
if ty.signed() {
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: "abs".to_owned(),
|
name: "abs".to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -323,6 +328,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
|||||||
}
|
}
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: "sizeof".to_owned(),
|
name: "sizeof".to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -334,6 +340,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
|||||||
});
|
});
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: "malloc".to_owned(),
|
name: "malloc".to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -350,6 +357,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
|||||||
|
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: "memcpy".to_owned(),
|
name: "memcpy".to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -378,6 +386,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
|||||||
|
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: "null".to_owned(),
|
name: "null".to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -390,6 +399,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
|||||||
|
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: "is_null".to_owned(),
|
name: "is_null".to_owned(),
|
||||||
|
documentation: Some("temp".to_string()),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: true,
|
is_pub: true,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -585,6 +595,9 @@ pub fn form_intrinsic_binops() -> Vec<BinopDefinition> {
|
|||||||
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::NE), &ty, |scope, lhs, rhs| {
|
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::NE), &ty, |scope, lhs, rhs| {
|
||||||
scope.block.build(Instr::FCmp(CmpPredicate::NE, lhs, rhs)).unwrap()
|
scope.block.build(Instr::FCmp(CmpPredicate::NE, lhs, rhs)).unwrap()
|
||||||
}));
|
}));
|
||||||
|
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::EQ), &ty, |scope, lhs, rhs| {
|
||||||
|
scope.block.build(Instr::FCmp(CmpPredicate::EQ, lhs, rhs)).unwrap()
|
||||||
|
}));
|
||||||
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::GT), &ty, |scope, lhs, rhs| {
|
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::GT), &ty, |scope, lhs, rhs| {
|
||||||
scope.block.build(Instr::FCmp(CmpPredicate::GT, lhs, rhs)).unwrap()
|
scope.block.build(Instr::FCmp(CmpPredicate::GT, lhs, rhs)).unwrap()
|
||||||
}));
|
}));
|
||||||
|
@ -226,8 +226,6 @@ impl mir::Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dbg!(&typedefs_sorted);
|
|
||||||
|
|
||||||
for typedef in typedefs_sorted {
|
for typedef in typedefs_sorted {
|
||||||
let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module);
|
let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module);
|
||||||
type_map.insert(type_key.clone(), typedef.clone());
|
type_map.insert(type_key.clone(), typedef.clone());
|
||||||
@ -336,7 +334,6 @@ impl mir::Module {
|
|||||||
if module_id == self.module_id {
|
if module_id == self.module_id {
|
||||||
format!("reid.{}.", self.name)
|
format!("reid.{}.", self.name)
|
||||||
} else {
|
} else {
|
||||||
dbg!(self.module_id, module_id);
|
|
||||||
format!("reid.{}.", modules.get(&module_id).unwrap().name)
|
format!("reid.{}.", modules.get(&module_id).unwrap().name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -152,6 +152,9 @@ impl Display for StructField {
|
|||||||
|
|
||||||
impl Display for FunctionDefinition {
|
impl Display for FunctionDefinition {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
if let Some(documentation) = &self.documentation {
|
||||||
|
writeln!(f, "/// {}", documentation)?;
|
||||||
|
}
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{}fn {}({}) -> {:#} ",
|
"{}fn {}({}) -> {:#} ",
|
||||||
|
@ -311,6 +311,7 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
|
|
||||||
importer_module.functions.push(FunctionDefinition {
|
importer_module.functions.push(FunctionDefinition {
|
||||||
name: function.name.clone(),
|
name: function.name.clone(),
|
||||||
|
documentation: function.documentation.clone(),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: false,
|
is_pub: false,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -458,6 +459,7 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
ty.clone(),
|
ty.clone(),
|
||||||
FunctionDefinition {
|
FunctionDefinition {
|
||||||
name: func_name.clone(),
|
name: func_name.clone(),
|
||||||
|
documentation: func.documentation.clone(),
|
||||||
linkage_name: Some(format!("{}::{}", ty, func_name)),
|
linkage_name: Some(format!("{}::{}", ty, func_name)),
|
||||||
is_pub: false,
|
is_pub: false,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
|
@ -318,6 +318,7 @@ pub struct FunctionCall {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FunctionDefinition {
|
pub struct FunctionDefinition {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub documentation: Option<String>,
|
||||||
pub linkage_name: Option<String>,
|
pub linkage_name: Option<String>,
|
||||||
/// Whether this function is visible to outside modules
|
/// Whether this function is visible to outside modules
|
||||||
pub is_pub: bool,
|
pub is_pub: bool,
|
||||||
|
Loading…
Reference in New Issue
Block a user