added json. By using HashMap
This commit is contained in:
+30
-2
@@ -72,7 +72,7 @@ const SYNTAX: [Node; 14] = [
|
||||
},
|
||||
Node {
|
||||
token_type: TokenType::Keyword,
|
||||
token_regex: Lazy::new(|| Regex::new(r"^(mut|try|catch|return|fn|let|use)\b").unwrap())
|
||||
token_regex: Lazy::new(|| Regex::new(r"^(mut|try|catch|return|fn|let|use|cb)\b").unwrap())
|
||||
},
|
||||
Node {
|
||||
token_type: TokenType::Identifier,
|
||||
@@ -80,7 +80,7 @@ const SYNTAX: [Node; 14] = [
|
||||
},
|
||||
Node {
|
||||
token_type: TokenType::Number,
|
||||
token_regex: Lazy::new(|| Regex::new(r"^(:?.)?(:?0[x|X])?\d+(:?.\d+)?\b").unwrap())
|
||||
token_regex: Lazy::new(|| Regex::new(r"^\d+").unwrap())
|
||||
},
|
||||
Node {
|
||||
token_type: TokenType::Ptr,
|
||||
@@ -153,7 +153,21 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
|
||||
br_state.line = state.line;
|
||||
br_state.column = state.column;
|
||||
}
|
||||
} else if brln == 0 {
|
||||
tokens.push(Token {
|
||||
token_type: TokenType::Operator,
|
||||
value: "/".to_string(),
|
||||
column: state.column,
|
||||
line: state.line
|
||||
});
|
||||
}
|
||||
} else if brln == 0 {
|
||||
tokens.push(Token {
|
||||
token_type: TokenType::Operator,
|
||||
value: "*".to_string(),
|
||||
column: state.column,
|
||||
line: state.line
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,7 +187,21 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
|
||||
line: br_state.line
|
||||
});
|
||||
brstr = String::new();
|
||||
} else if brln == 0 {
|
||||
tokens.push(Token {
|
||||
token_type: TokenType::Operator,
|
||||
value: "*".to_string(),
|
||||
column: state.column,
|
||||
line: state.line
|
||||
});
|
||||
}
|
||||
} else if brln == 0 {
|
||||
tokens.push(Token {
|
||||
token_type: TokenType::Operator,
|
||||
value: "*".to_string(),
|
||||
column: state.column,
|
||||
line: state.line
|
||||
});
|
||||
}
|
||||
}
|
||||
"\"" => {
|
||||
|
||||
+22
-7
@@ -13,6 +13,7 @@ pub enum AstType {
|
||||
Include,
|
||||
IncludeLocal,
|
||||
CodeBlock,
|
||||
Json,
|
||||
Other
|
||||
}
|
||||
|
||||
@@ -53,7 +54,8 @@ pub struct Parser {
|
||||
pub tokens: Vec<Token>,
|
||||
pub index: u32,
|
||||
pub include_regex: Lazy<Regex>,
|
||||
pub include_regex_local: Lazy<Regex>
|
||||
pub include_regex_local: Lazy<Regex>,
|
||||
pub json: bool
|
||||
}
|
||||
|
||||
impl Parser {
|
||||
@@ -62,7 +64,8 @@ impl Parser {
|
||||
tokens: tokens,
|
||||
index: 0,
|
||||
include_regex: Lazy::new(|| Regex::new(r"^(#include *)<(.*?)>").unwrap()),
|
||||
include_regex_local: Lazy::new(|| Regex::new(r#"^(#include *)"(.*?)""#).unwrap())
|
||||
include_regex_local: Lazy::new(|| Regex::new(r#"^(#include *)"(.*?)""#).unwrap()),
|
||||
json: false
|
||||
}
|
||||
}
|
||||
pub fn next(&mut self) -> Ast {
|
||||
@@ -70,6 +73,12 @@ impl Parser {
|
||||
let index = self.index as usize;
|
||||
if index == self.tokens.len() {panic!("Reached the end of tokens")}
|
||||
let token = &self.tokens[index];
|
||||
if self.json && self.tokens.len() - (self.index as usize) > 2 && self.tokens[index+1].value == ":" {
|
||||
ast_res.ast_type = AstType::Json;
|
||||
ast_res.tokens.push(self.tokens[index].clone());
|
||||
ast_res.tokens.push(self.tokens[index+2].clone());
|
||||
self.index += 2;
|
||||
} else {
|
||||
match token.token_type {
|
||||
TokenType::Identifier => {
|
||||
ast_res.tokens.push(self.tokens[index].clone());
|
||||
@@ -84,11 +93,7 @@ impl Parser {
|
||||
}
|
||||
self.index += 3;
|
||||
} else if self.tokens.len()-index > 1 {
|
||||
if token.value == "cb" && self.tokens[index+1].token_type == TokenType::Curly {
|
||||
ast_res.tokens.push(self.tokens[index+1].clone());
|
||||
ast_res.ast_type = AstType::CodeBlock;
|
||||
self.index += 1;
|
||||
} else if self.tokens[index+1].token_type==TokenType::Identifier {
|
||||
if self.tokens[index+1].token_type==TokenType::Identifier {
|
||||
ast_res.tokens.push(self.tokens[index+1].clone());
|
||||
ast_res.ast_type = AstType::VariableDeceleration;
|
||||
self.index += 1;
|
||||
@@ -122,10 +127,20 @@ impl Parser {
|
||||
ast_res.ast_type = AstType::Include;
|
||||
}
|
||||
}
|
||||
TokenType::Keyword => {
|
||||
if token.value == "cb" && self.tokens[index+1].token_type == TokenType::Curly {
|
||||
ast_res.tokens.push(self.tokens[index+1].clone());
|
||||
ast_res.ast_type = AstType::CodeBlock;
|
||||
self.index += 1;
|
||||
} else {
|
||||
ast_res.tokens.push(token.clone());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
ast_res.tokens.push(token.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
self.index += 1;
|
||||
ast_res
|
||||
}
|
||||
|
||||
+82
-8
@@ -80,14 +80,23 @@ pub fn transpile(input: String, indent: u32, state: LexerState) -> String {
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
|
||||
if ast.tokens[0].token_type == TokenType::Newline {
|
||||
result += (ast.tokens[0].value.as_str().to_owned()
|
||||
+ (" ".repeat((indent as usize) * 2).as_str()))
|
||||
.as_str();
|
||||
} else {
|
||||
} else if ast.ast_type == AstType::CodeBlock {
|
||||
println!("this is fine");
|
||||
result+="{";
|
||||
result+=ast.tokens[0].value.as_str();
|
||||
}
|
||||
result+="}";
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
|
||||
// if ast.tokens[0].token_type == TokenType::Newline {
|
||||
// result += (ast.tokens[0].value.as_str().to_owned()
|
||||
// + (" ".repeat((indent as usize) * 2).as_str()))
|
||||
// .as_str();
|
||||
// } else {
|
||||
// result += ast.tokens[0].value.as_str();
|
||||
result += transpile_json(ast.tokens[0].value.as_str().to_string(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
}).as_str();
|
||||
// }
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
|
||||
result+=".";
|
||||
} // flp
|
||||
@@ -141,7 +150,6 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
|
||||
match lexer_out {
|
||||
Ok(tokens) => {
|
||||
let mut full_ast = Parser::new(tokens.clone());
|
||||
|
||||
while full_ast.tokens.len() > full_ast.index as usize {
|
||||
let ast = full_ast.next();
|
||||
if ast.ast_type == AstType::VariableDeceleration {
|
||||
@@ -170,6 +178,11 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
|
||||
result+=".";
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
|
||||
result += transpile_json(ast.tokens[0].value.as_str().to_string(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
}).as_str();
|
||||
} // flp
|
||||
else {
|
||||
result += ast.tokens[0].value.as_str();
|
||||
@@ -224,6 +237,11 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
|
||||
result+=".";
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
|
||||
result += transpile_json(ast.tokens[0].value.as_str().to_string(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
}).as_str();
|
||||
} // flp
|
||||
else {
|
||||
result += ast.tokens[0].value.as_str();
|
||||
@@ -239,3 +257,59 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transpile_json(input: String, state: LexerState) -> String {
|
||||
let mut result = String::new();
|
||||
let lexer_out = lex(input.as_str(), false, state);
|
||||
|
||||
match lexer_out {
|
||||
Ok(tokens) => {
|
||||
let mut full_ast = Parser::new(tokens.clone());
|
||||
full_ast.json = true;
|
||||
result += "HashMap::from([";
|
||||
while full_ast.tokens.len() > full_ast.index as usize {
|
||||
let ast = full_ast.next();
|
||||
println!("{ast}");
|
||||
if ast.ast_type == AstType::Json {
|
||||
result += "(";
|
||||
result += ast.tokens[0].value.as_str();
|
||||
result += ",";
|
||||
result += ast.tokens[1].value.as_str();
|
||||
result += ")";
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round {
|
||||
result += format!(
|
||||
"({})",
|
||||
transpile_round(ast.tokens[0].value.clone(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square {
|
||||
result += format!(
|
||||
"[{}]",
|
||||
transpile_square(ast.tokens[0].value.clone(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
})
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
|
||||
result += transpile_json(ast.tokens[0].value.as_str().to_string(), LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
column: ast.tokens[0].column
|
||||
}).as_str();
|
||||
} else {
|
||||
result += ast.tokens[0].value.as_str();
|
||||
result += " ";
|
||||
}
|
||||
}
|
||||
result += "])";
|
||||
result = result.trim_end().to_string();
|
||||
result
|
||||
}
|
||||
Err((state, _tokens)) => {
|
||||
panic!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user