From db0156e88f4904b4f65a3614f2a925d31438b559 Mon Sep 17 00:00:00 2001 From: light-ly Date: Thu, 3 Oct 2024 09:21:41 +0800 Subject: [PATCH] fix vhdl parser err --- src/core/vhdl_parser.rs | 48 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/src/core/vhdl_parser.rs b/src/core/vhdl_parser.rs index 50989ec..7f763d0 100644 --- a/src/core/vhdl_parser.rs +++ b/src/core/vhdl_parser.rs @@ -129,7 +129,7 @@ fn parse_tokens(tokens: Vec) -> Vec { (kind_str(tokens[end+1].kind) == "architecture" || get_value(&tokens[end+1]) == arch_name)) { end += 1; } - let end_pos = if get_value(&tokens[end+1]) == arch_name { + let end_pos = if end+1 < tokens.len() && get_value(&tokens[end+1]) == arch_name { // i = end + 2; tokens[end+2].pos.range.end } else if end + 3 < tokens.len() { @@ -161,7 +161,7 @@ fn parse_tokens(tokens: Vec) -> Vec { } "configuration" => { i += 1; - while !(kind_str(tokens[i].kind) == "end" && kind_str(tokens[i+1].kind) == "configuration") { + while i < tokens.len() && !(kind_str(tokens[i].kind) == "end" && kind_str(tokens[i+1].kind) == "configuration") { i += 1; } i += 1; @@ -169,7 +169,7 @@ fn parse_tokens(tokens: Vec) -> Vec { "package" => { i += 1; // println!("get package"); - while !(kind_str(tokens[i].kind) == "end" && kind_str(tokens[i+1].kind) == "package") { + while i < tokens.len() && !(kind_str(tokens[i].kind) == "end" && kind_str(tokens[i+1].kind) == "package") { i += 1; } i += 1; @@ -178,11 +178,23 @@ fn parse_tokens(tokens: Vec) -> Vec { i += 1; instance_type.insert(get_value(&tokens[i])); // println!("instance {:?}", kind_str(tokens[i].kind)); - while !(kind_str(tokens[i].kind) == "end" && kind_str(tokens[i+1].kind) == "component") { + while i < tokens.len() && !(kind_str(tokens[i].kind) == "end" && kind_str(tokens[i+1].kind) == "component") { i += 1; } i += 1; } + "signal" => { + i += 1; + while !(kind_str(tokens[i+1].kind) == ";") { + i += 1; + } + } + "attribute" => { + i += 1; + while !(kind_str(tokens[i+1].kind) == ";") { + i += 1; + } + } ":" => { if (i+2 < tokens.len()) && (kind_str(tokens[i+2].kind) != "use") { if instance_type.contains(&get_value(&tokens[i+1])) { @@ -222,6 +234,34 @@ fn parse_tokens(tokens: Vec) -> Vec { }; modules.last_mut().unwrap().instances.push(instance); i += 1; + } else { + let name = get_value(&tokens[i-1]); + let mut inst_type = String::new(); + while i < tokens.len() { + if kind_str(tokens[i].kind) == "generic" || kind_str(tokens[i].kind) == "port" || kind_str(tokens[i].kind) == "end" { + i = i - 1; + break; + } + inst_type = inst_type + &get_value(&tokens[i]); + i += 1; + } + let instance = Instance { + name, + inst_type, + instports: None, + instparams: None, + range: Range { + start: Position { + line: tokens[i-1].pos.range.start.line + 1, + character: tokens[i-1].pos.range.start.character + 1 + }, + end: Position { + line: tokens[i+1].pos.range.start.line + 1, + character: tokens[i+1].pos.range.start.character + 1 + } + } + }; + modules.last_mut().unwrap().instances.push(instance); } } }