get_completion works but lsp doesn't

This commit is contained in:
2024-06-04 22:57:57 +02:00
parent 35653cff1f
commit a6a5b46a01
5 changed files with 181 additions and 56 deletions
+52 -4
View File
@@ -1,7 +1,7 @@
use crate::lexer::{Token, TokenType};
use crate::lexer::{LexerState, Token, TokenType};
use once_cell::sync::Lazy;
use regex::Regex;
use std::fmt;
use std::{collections::HashMap, fmt};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstType {
@@ -63,17 +63,37 @@ pub struct Parser {
pub index: u32,
pub include_regex: Lazy<Regex>,
pub include_regex_local: Lazy<Regex>,
pub variables: HashMap<String, Variable>,
pub json: bool,
}
#[derive(Clone, PartialEq, Debug)]
pub enum VariableType {
Var,
Func,
Keyword,
}
#[derive(Clone, Debug)]
pub struct Variable {
pub vtype: VariableType,
pub state: LexerState,
}
pub fn new_vars() -> HashMap<String, Variable> {
HashMap::from([
("void".to_string(), Variable { vtype: VariableType::Keyword, state: LexerState {line:0,column:0}})
])
}
impl Parser {
pub fn new(tokens: Vec<Token>) -> Parser {
pub fn new(tokens: Vec<Token>, variables: HashMap<String, Variable>) -> Parser {
Parser {
tokens: tokens,
index: 0,
include_regex: Lazy::new(|| Regex::new(r"^(#include *)<(.*?)>").unwrap()),
include_regex_local: Lazy::new(|| Regex::new(r#"^(#include *)"(.*?)""#).unwrap()),
// variables: variables,
variables: variables,
json: false,
}
}
@@ -166,6 +186,13 @@ impl Parser {
} else {
ast_res.ast_type = AstType::FunctionDeceleration;
}
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
vtype: VariableType::Func,
state: LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column
}
});
self.index += 3;
} else if self.tokens.len() - index > 1
&& self.tokens[index + 1].token_type == TokenType::Curly
@@ -186,6 +213,13 @@ impl Parser {
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::VariableDeceleration;
self.index += 1;
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
vtype: VariableType::Var,
state: LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column
}
});
} else if self.tokens.len() - index > 2
&& self.tokens[index + 2].token_type == TokenType::Identifier
&& self.tokens[index + 1].token_type == TokenType::Angle
@@ -196,6 +230,13 @@ impl Parser {
ast_res.tokens[0].value += ">";
ast_res.ast_type = AstType::VariableDeceleration;
self.index += 2;
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
vtype: VariableType::Var,
state: LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column
}
});
} else if self.tokens.len() - index > 2
&& self.tokens[index + 1].value == "*"
&& self.tokens[index + 2].token_type == TokenType::Identifier
@@ -203,6 +244,13 @@ impl Parser {
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::PointerDeceleration;
self.index += 2;
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
vtype: VariableType::Var,
state: LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column
}
});
}
}
}