From 35653cff1f811d8f8508d3c20212db95bd5ba88f Mon Sep 17 00:00:00 2001 From: Leo Dev Date: Tue, 4 Jun 2024 17:29:16 +0200 Subject: [PATCH] still no fix --- src/lsp.rs | 47 ++++++++++++++++++++++++----------------------- src/parser.rs | 28 +++------------------------- src/transpiler.rs | 33 ++++++++------------------------- 3 files changed, 35 insertions(+), 73 deletions(-) diff --git a/src/lsp.rs b/src/lsp.rs index 284f5f7..5011cf9 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -1,14 +1,13 @@ // use std::collections::HashMap; // use std::fs; -use std::fs; +use std::vec; -use lspower; +use lspower::{self, LspService, Server}; use lspower::jsonrpc::Result; use lspower::lsp::{self, *}; use lspower::{Client, LanguageServer}; -use crate::transpiler::Transpiler; #[derive(Debug)] struct Backend { @@ -41,9 +40,8 @@ impl LanguageServer for Backend { &self, _params: lsp::CompletionParams, ) -> lspower::jsonrpc::Result> { - Ok(Some(CompletionResponse::Array(vec![ - CompletionItem::new_simple("mylabel".to_string(), "mydetail".to_string()), - ]))) + let completion_items: Vec = Vec::new(); + Ok(Some(CompletionResponse::Array(completion_items))) } async fn shutdown(&self) -> Result<()> { @@ -51,23 +49,26 @@ impl LanguageServer for Backend { } } -// pub async fn run_lsp_server() { -// let stdin = tokio::io::stdin(); -// let stdout = tokio::io::stdout(); - -// let (service, messages) = LspService::new(|client| Backend { client }); -// Server::new(stdin, stdout) -// .interleave(messages) -// .serve(service) -// .await; -// } - #[tokio::main] pub async fn run_lsp_server() { - let mut transpiler = Transpiler { - var_match: String::from("x"), - ..Default::default() - }; - let file_content = fs::read_to_string("main.wt").expect("Error reading file"); - transpiler.transpile(file_content, 0); + let stdin = tokio::io::stdin(); + let stdout = tokio::io::stdout(); + + let (service, messages) = LspService::new(|client| Backend { client }); + Server::new(stdin, stdout) + .interleave(messages) + .serve(service) + .await; } + +// #[tokio::main] +// pub async fn run_lsp_server() { +// let mut transpiler = Transpiler { +// var_match: String::from("x"), +// var_state: LexerState { line: 6, column: 9 }, +// ..Default::default() +// }; +// let file_content = fs::read_to_string("main.wt").expect("Error reading file"); +// transpiler.transpile(file_content, 0, HashMap::new()); +// println!("{:?}", transpiler.matched_vars); +// } diff --git a/src/parser.rs b/src/parser.rs index 347038e..c81a6d7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,7 +1,7 @@ use crate::lexer::{Token, TokenType}; use once_cell::sync::Lazy; use regex::Regex; -use std::{collections::HashMap, fmt}; +use std::fmt; #[derive(Clone, Debug, PartialEq, Eq)] pub enum AstType { @@ -64,29 +64,16 @@ pub struct Parser { pub include_regex: Lazy, pub include_regex_local: Lazy, pub json: bool, - pub variables: HashMap, -} -#[derive(Debug, PartialEq, Clone)] -pub enum VariableType { - Func, - Var, -} -#[derive(Debug, PartialEq, Clone)] -pub struct Variable { - pub vtype: VariableType, - pub indent: u32, - pub line: usize, - pub column: usize, } impl Parser { - pub fn new(tokens: Vec, variables: HashMap) -> Parser { + pub fn new(tokens: Vec) -> 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, } } @@ -179,15 +166,6 @@ impl Parser { } else { ast_res.ast_type = AstType::FunctionDeceleration; } - self.variables.insert( - self.tokens[index + 1].value.clone(), - Variable { - vtype: VariableType::Func, - indent: 0, - line: self.tokens[index + 1].line.clone(), - column: self.tokens[index + 1].column.clone(), - }, - ); self.index += 3; } else if self.tokens.len() - index > 1 && self.tokens[index + 1].token_type == TokenType::Curly diff --git a/src/transpiler.rs b/src/transpiler.rs index 458b1f5..e446102 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -1,6 +1,5 @@ use crate::lexer::{lex, LexerState, TokenType}; -use crate::parser::{Ast, AstType, Parser, Variable, VariableType}; -use std::collections::HashMap; +use crate::parser::{Ast, AstType, Parser}; use std::fs; #[derive(Debug, PartialEq, Clone)] @@ -11,9 +10,6 @@ pub struct Transpiler { pub auto_pub: bool, pub macros: Vec, pub modnum: u32, - pub var_match: String, - pub var_state: LexerState, - pub matched_vars: HashMap, } impl Default for Transpiler { @@ -25,9 +21,6 @@ impl Default for Transpiler { auto_pub: false, macros: vec![String::from("println")], modnum: 0, - var_state: LexerState { line: 0, column: 0 }, - var_match: String::new(), - matched_vars: HashMap::new(), } } } @@ -35,8 +28,6 @@ impl Default for Transpiler { impl Transpiler { pub fn transpile(&mut self, input: String, indent: u32) -> String { let mut result = String::new(); - let mut variables: HashMap = HashMap::new(); - if indent == 0 { // result += "type int = i32;\n"; } else { @@ -46,14 +37,12 @@ impl Transpiler { match lexer_out { Ok(tokens) => { - let mut full_ast = Parser::new(tokens.clone(), variables); + let mut full_ast = Parser::new(tokens.clone()); let mut last_ast = Ast { ast_type: AstType::Other, tokens: vec![], }; for ast in full_ast.parse() { - let variables = full_ast.variables.clone(); - println!("{:?}", variables); if last_ast.tokens.len() > 0 { let mut fl = 0; for t in &last_ast.tokens { @@ -290,7 +279,7 @@ impl Transpiler { result = result.trim_end().to_string(); - if self.var_match != "" && (self.state.line + input.len()) > self.var_state.line { + // if self.var_match != "" && (self.state.line + input.len()) > self.var_state.line { // for (name, var) in variables { // if (self.var_state.line > var.line // || (self.var_state.line == var.line @@ -300,8 +289,8 @@ impl Transpiler { // self.matched_vars.insert(name, var); // } // } - self.var_match = String::new(); - } + // self.var_match = String::new(); + // } if indent > 0 { result += "\n"; @@ -335,11 +324,9 @@ impl Transpiler { pub fn transpile_round(&mut self, input: String) -> String { let mut result = String::new(); let lexer_out = lex(input.as_str(), false, self.state); - let mut variables: HashMap = HashMap::new(); - match lexer_out { Ok(tokens) => { - let mut full_ast = Parser::new(tokens.clone(), variables); + let mut full_ast = Parser::new(tokens.clone()); let mut last_ast = Ast { ast_type: AstType::Other, tokens: vec![], @@ -432,11 +419,9 @@ impl Transpiler { pub fn transpile_square(&mut self, input: String, state: LexerState) -> String { let mut result = String::new(); let lexer_out = lex(input.as_str(), false, state); - let mut variables: HashMap = HashMap::new(); - match lexer_out { Ok(tokens) => { - let mut full_ast = Parser::new(tokens.clone(), variables); + let mut full_ast = Parser::new(tokens.clone()); let mut last_ast = Ast { ast_type: AstType::Other, tokens: vec![], @@ -524,11 +509,9 @@ impl Transpiler { pub fn transpile_json(&mut self, input: String, state: LexerState) -> String { let mut result = String::new(); let lexer_out = lex(input.as_str(), false, state); - let mut variables: HashMap = HashMap::new(); - match lexer_out { Ok(tokens) => { - let mut full_ast = Parser::new(tokens.clone(), variables); + let mut full_ast = Parser::new(tokens.clone()); full_ast.json = true; result += "HashMap::from(["; let mut last_ast = Ast {