plan b
This commit is contained in:
@@ -13,3 +13,4 @@ lspower = "1.5.0"
|
|||||||
tokio = { version = "1.38.0", features = ["full"] }
|
tokio = { version = "1.38.0", features = ["full"] }
|
||||||
tokio-macros = "2.3.0"
|
tokio-macros = "2.3.0"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
serde_json = "1.0"
|
||||||
|
|||||||
+16
-32
@@ -1,42 +1,23 @@
|
|||||||
// use std::collections::HashMap;
|
// use std::collections::HashMap;
|
||||||
// use std::fs;
|
// 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::parser::{new_vars, Variable};
|
||||||
use crate::transpiler::Transpiler;
|
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)]
|
#[derive(Debug)]
|
||||||
struct Backend {
|
struct Backend {
|
||||||
client: Client
|
client: Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
static DOCUMENTS: Lazy<HashMap<Url, String>> = Lazy::new(|| HashMap::default());
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[lspower::async_trait]
|
#[lspower::async_trait]
|
||||||
impl LanguageServer for Backend {
|
impl LanguageServer for Backend {
|
||||||
@@ -60,6 +41,11 @@ impl LanguageServer for Backend {
|
|||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn did_change(&self, params: lsp::DidChangeTextDocumentParams) {
|
||||||
|
// DOCUMENTS.insert(params.text_document.uri, d);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
async fn completion(
|
async fn completion(
|
||||||
&self,
|
&self,
|
||||||
params: lsp::CompletionParams,
|
params: lsp::CompletionParams,
|
||||||
@@ -124,8 +110,6 @@ pub fn get_completion(input: String, line: usize, column: usize) -> HashMap<Stri
|
|||||||
peek: rand_id.clone(),
|
peek: rand_id.clone(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
transpiler.transpile(
|
transpiler.transpile(place_at(input, rand_id, line, column), 0, new_vars());
|
||||||
place_at(input, rand_id, line, column)
|
|
||||||
, 0, new_vars());
|
|
||||||
transpiler.matched_vars
|
transpiler.matched_vars
|
||||||
}
|
}
|
||||||
+81
-28
@@ -31,7 +31,12 @@ impl Default for Transpiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 variables = variables;
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
if indent == 0 {
|
if indent == 0 {
|
||||||
@@ -50,11 +55,13 @@ impl Transpiler {
|
|||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
for ast in full_ast.parse() {
|
||||||
variables = full_ast.variables.clone();
|
variables = full_ast.variables.clone();
|
||||||
if ast.ast_type == AstType::Other && ast.tokens[0].token_type == TokenType::Identifier
|
if ast.ast_type == AstType::Other
|
||||||
&& ast.tokens[0].value.contains(&self.peek) && self.peek!="" {
|
&& ast.tokens[0].token_type == TokenType::Identifier
|
||||||
|
&& ast.tokens[0].value.contains(&self.peek)
|
||||||
|
&& self.peek != ""
|
||||||
|
{
|
||||||
let ctoken = &ast.tokens[0];
|
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() {
|
for (name, var) in variables.clone() {
|
||||||
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
||||||
self.matched_vars.insert(name, var);
|
self.matched_vars.insert(name, var);
|
||||||
@@ -93,7 +100,11 @@ impl Transpiler {
|
|||||||
ast.tokens[1].value,
|
ast.tokens[1].value,
|
||||||
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
|
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
|
||||||
ast.tokens[0].value,
|
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();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::VoidFunctionDeceleration {
|
} else if ast.ast_type == AstType::VoidFunctionDeceleration {
|
||||||
@@ -104,7 +115,11 @@ impl Transpiler {
|
|||||||
"fn {}({}) {}",
|
"fn {}({}) {}",
|
||||||
ast.tokens[1].value,
|
ast.tokens[1].value,
|
||||||
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
|
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();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::StructDeceleration {
|
} else if ast.ast_type == AstType::StructDeceleration {
|
||||||
@@ -142,8 +157,10 @@ impl Transpiler {
|
|||||||
} else if ast.ast_type == AstType::Other
|
} else if ast.ast_type == AstType::Other
|
||||||
&& ast.tokens[0].token_type == TokenType::Round
|
&& ast.tokens[0].token_type == TokenType::Round
|
||||||
{
|
{
|
||||||
result +=
|
result += format!(
|
||||||
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
|
"({})",
|
||||||
|
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
|
||||||
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
||||||
{
|
{
|
||||||
@@ -154,7 +171,8 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column
|
column: ast.tokens[0].column
|
||||||
}, variables.clone()
|
},
|
||||||
|
variables.clone()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -176,7 +194,8 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column,
|
column: ast.tokens[0].column,
|
||||||
}, variables.clone(),
|
},
|
||||||
|
variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
// }
|
// }
|
||||||
@@ -221,14 +240,22 @@ impl Transpiler {
|
|||||||
"{} {} {}",
|
"{} {} {}",
|
||||||
ast.tokens[0].value.clone(),
|
ast.tokens[0].value.clone(),
|
||||||
self.transpile_round(ast.tokens[1].value.clone(), variables.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();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::State2 {
|
} else if ast.ast_type == AstType::State2 {
|
||||||
result += format!(
|
result += format!(
|
||||||
"{} {}",
|
"{} {}",
|
||||||
ast.tokens[0].value.clone(),
|
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();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::Namespace {
|
} else if ast.ast_type == AstType::Namespace {
|
||||||
@@ -341,7 +368,11 @@ impl Transpiler {
|
|||||||
.expect("Error writing file");
|
.expect("Error writing file");
|
||||||
modname
|
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 mut result = String::new();
|
||||||
let lexer_out = lex(input.as_str(), false, self.state);
|
let lexer_out = lex(input.as_str(), false, self.state);
|
||||||
match lexer_out {
|
match lexer_out {
|
||||||
@@ -383,8 +414,10 @@ impl Transpiler {
|
|||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
|
||||||
{
|
{
|
||||||
result +=
|
result += format!(
|
||||||
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
|
"({})",
|
||||||
|
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
|
||||||
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
||||||
{
|
{
|
||||||
@@ -395,7 +428,8 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column
|
column: ast.tokens[0].column
|
||||||
}, variables.clone()
|
},
|
||||||
|
variables.clone()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -409,7 +443,8 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column,
|
column: ast.tokens[0].column,
|
||||||
}, variables.clone(),
|
},
|
||||||
|
variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::StructCall {
|
} 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 mut result = String::new();
|
||||||
let lexer_out = lex(input.as_str(), false, state);
|
let lexer_out = lex(input.as_str(), false, state);
|
||||||
match lexer_out {
|
match lexer_out {
|
||||||
@@ -476,8 +516,10 @@ impl Transpiler {
|
|||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
|
||||||
{
|
{
|
||||||
result +=
|
result += format!(
|
||||||
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
|
"({})",
|
||||||
|
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
|
||||||
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
||||||
{
|
{
|
||||||
@@ -488,7 +530,8 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column
|
column: ast.tokens[0].column
|
||||||
}, variables.clone()
|
},
|
||||||
|
variables.clone()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -502,7 +545,8 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column,
|
column: ast.tokens[0].column,
|
||||||
}, variables.clone(),
|
},
|
||||||
|
variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::StructCall {
|
} 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 mut result = String::new();
|
||||||
let lexer_out = lex(input.as_str(), false, state);
|
let lexer_out = lex(input.as_str(), false, state);
|
||||||
match lexer_out {
|
match lexer_out {
|
||||||
@@ -568,8 +617,10 @@ impl Transpiler {
|
|||||||
result += ")";
|
result += ")";
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
|
||||||
{
|
{
|
||||||
result +=
|
result += format!(
|
||||||
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
|
"({})",
|
||||||
|
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
|
||||||
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
||||||
{
|
{
|
||||||
@@ -580,7 +631,8 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column
|
column: ast.tokens[0].column
|
||||||
}, variables.clone()
|
},
|
||||||
|
variables.clone()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -592,7 +644,8 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column,
|
column: ast.tokens[0].column,
|
||||||
}, variables.clone(),
|
},
|
||||||
|
variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user