This commit is contained in:
2024-06-05 15:58:17 +02:00
parent a6a5b46a01
commit c5331279da
3 changed files with 120 additions and 82 deletions
+1
View File
@@ -13,3 +13,4 @@ lspower = "1.5.0"
tokio = { version = "1.38.0", features = ["full"] }
tokio-macros = "2.3.0"
rand = "0.8.5"
serde_json = "1.0"
+16 -32
View File
@@ -1,42 +1,23 @@
// use std::collections::HashMap;
// use std::fs;
use std::collections::HashMap;
use std::vec;
use lspower::{self, LspService, Server};
use lspower::jsonrpc::Result;
use lspower::lsp::{self, *};
use lspower::{Client, LanguageServer};
use rand::{thread_rng, Rng};
use crate::parser::{new_vars, Variable};
use crate::transpiler::Transpiler;
use lspower::jsonrpc::Result;
use lspower::lsp::{self, *};
use lspower::{self, LspService, Server};
use lspower::{Client, LanguageServer};
use once_cell::sync::Lazy;
use rand::{thread_rng, Rng};
use std::collections::HashMap;
use std::vec;
#[derive(Debug)]
struct Backend {
client: Client
client: Client,
}
#[derive(Debug)]
struct DocumentManager {
documents: HashMap<String, String>, // Key: Document URI, Value: Document content
}
impl DocumentManager {
fn new() -> DocumentManager {
DocumentManager {
documents: HashMap::new()
}
}
fn update_document(&mut self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri.to_string(); // Convert URI to string
let content = params.content_changes.last().unwrap().text.clone(); // Access latest content
self.documents.insert(uri, content);
}
fn get_document(&self, uri: &str) -> Option<&str> {
self.documents.get(uri).map(|x| x.as_str())
}
}
static DOCUMENTS: Lazy<HashMap<Url, String>> = Lazy::new(|| HashMap::default());
#[lspower::async_trait]
impl LanguageServer for Backend {
@@ -60,6 +41,11 @@ impl LanguageServer for Backend {
.await;
}
async fn did_change(&self, params: lsp::DidChangeTextDocumentParams) {
// DOCUMENTS.insert(params.text_document.uri, d);
}
async fn completion(
&self,
params: lsp::CompletionParams,
@@ -124,8 +110,6 @@ pub fn get_completion(input: String, line: usize, column: usize) -> HashMap<Stri
peek: rand_id.clone(),
..Default::default()
};
transpiler.transpile(
place_at(input, rand_id, line, column)
, 0, new_vars());
transpiler.transpile(place_at(input, rand_id, line, column), 0, new_vars());
transpiler.matched_vars
}
+81 -28
View File
@@ -31,7 +31,12 @@ impl Default for Transpiler {
}
impl Transpiler {
pub fn transpile(&mut self, input: String, indent: u32, variables: HashMap<String, Variable>) -> String {
pub fn transpile(
&mut self,
input: String,
indent: u32,
variables: HashMap<String, Variable>,
) -> String {
let mut variables = variables;
let mut result = String::new();
if indent == 0 {
@@ -50,11 +55,13 @@ impl Transpiler {
};
for ast in full_ast.parse() {
variables = full_ast.variables.clone();
if ast.ast_type == AstType::Other && ast.tokens[0].token_type == TokenType::Identifier
&& ast.tokens[0].value.contains(&self.peek) && self.peek!="" {
if ast.ast_type == AstType::Other
&& ast.tokens[0].token_type == TokenType::Identifier
&& ast.tokens[0].value.contains(&self.peek)
&& self.peek != ""
{
let ctoken = &ast.tokens[0];
let pname = ctoken.value.split(&self.peek).next().unwrap();
// let pname = ctoken.value.split(&self.peek).next().unwrap();
for (name, var) in variables.clone() {
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
self.matched_vars.insert(name, var);
@@ -93,7 +100,11 @@ impl Transpiler {
ast.tokens[1].value,
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
ast.tokens[0].value,
self.transpile(ast.tokens[3].value.clone(), indent + 1, variables.clone())
self.transpile(
ast.tokens[3].value.clone(),
indent + 1,
variables.clone()
)
)
.as_str();
} else if ast.ast_type == AstType::VoidFunctionDeceleration {
@@ -104,7 +115,11 @@ impl Transpiler {
"fn {}({}) {}",
ast.tokens[1].value,
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
self.transpile(ast.tokens[3].value.clone(), indent + 1, variables.clone())
self.transpile(
ast.tokens[3].value.clone(),
indent + 1,
variables.clone()
)
)
.as_str();
} else if ast.ast_type == AstType::StructDeceleration {
@@ -142,8 +157,10 @@ impl Transpiler {
} else if ast.ast_type == AstType::Other
&& ast.tokens[0].token_type == TokenType::Round
{
result +=
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
result += format!(
"({})",
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
{
@@ -154,7 +171,8 @@ impl Transpiler {
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}, variables.clone()
},
variables.clone()
)
)
.as_str();
@@ -176,7 +194,8 @@ impl Transpiler {
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column,
}, variables.clone(),
},
variables.clone(),
)
.as_str();
// }
@@ -221,14 +240,22 @@ impl Transpiler {
"{} {} {}",
ast.tokens[0].value.clone(),
self.transpile_round(ast.tokens[1].value.clone(), variables.clone()),
self.transpile(ast.tokens[2].value.clone(), indent + 1, variables.clone()),
self.transpile(
ast.tokens[2].value.clone(),
indent + 1,
variables.clone()
),
)
.as_str();
} else if ast.ast_type == AstType::State2 {
result += format!(
"{} {}",
ast.tokens[0].value.clone(),
self.transpile(ast.tokens[1].value.clone(), indent + 1, variables.clone()),
self.transpile(
ast.tokens[1].value.clone(),
indent + 1,
variables.clone()
),
)
.as_str();
} else if ast.ast_type == AstType::Namespace {
@@ -341,7 +368,11 @@ impl Transpiler {
.expect("Error writing file");
modname
}
pub fn transpile_round(&mut self, input: String, variables: HashMap<String, Variable>) -> String {
pub fn transpile_round(
&mut self,
input: String,
variables: HashMap<String, Variable>,
) -> String {
let mut result = String::new();
let lexer_out = lex(input.as_str(), false, self.state);
match lexer_out {
@@ -383,8 +414,10 @@ impl Transpiler {
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
{
result +=
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
result += format!(
"({})",
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
{
@@ -395,7 +428,8 @@ impl Transpiler {
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}, variables.clone()
},
variables.clone()
)
)
.as_str();
@@ -409,7 +443,8 @@ impl Transpiler {
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column,
}, variables.clone(),
},
variables.clone(),
)
.as_str();
} else if ast.ast_type == AstType::StructCall {
@@ -436,7 +471,12 @@ impl Transpiler {
}
}
pub fn transpile_square(&mut self, input: String, state: LexerState, variables: HashMap<String, Variable>) -> String {
pub fn transpile_square(
&mut self,
input: String,
state: LexerState,
variables: HashMap<String, Variable>,
) -> String {
let mut result = String::new();
let lexer_out = lex(input.as_str(), false, state);
match lexer_out {
@@ -476,8 +516,10 @@ impl Transpiler {
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
{
result +=
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
result += format!(
"({})",
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
{
@@ -488,7 +530,8 @@ impl Transpiler {
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}, variables.clone()
},
variables.clone()
)
)
.as_str();
@@ -502,7 +545,8 @@ impl Transpiler {
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column,
}, variables.clone(),
},
variables.clone(),
)
.as_str();
} else if ast.ast_type == AstType::StructCall {
@@ -526,7 +570,12 @@ impl Transpiler {
}
}
pub fn transpile_json(&mut self, input: String, state: LexerState, variables: HashMap<String, Variable>) -> String {
pub fn transpile_json(
&mut self,
input: String,
state: LexerState,
variables: HashMap<String, Variable>,
) -> String {
let mut result = String::new();
let lexer_out = lex(input.as_str(), false, state);
match lexer_out {
@@ -568,8 +617,10 @@ impl Transpiler {
result += ")";
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
{
result +=
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
result += format!(
"({})",
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
{
@@ -580,7 +631,8 @@ impl Transpiler {
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}, variables.clone()
},
variables.clone()
)
)
.as_str();
@@ -592,7 +644,8 @@ impl Transpiler {
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column,
}, variables.clone(),
},
variables.clone(),
)
.as_str();
} else {