From b87fe78011d60c85a3ce363b0f167a68c50399a7 Mon Sep 17 00:00:00 2001 From: dalance Date: Wed, 13 Nov 2024 18:04:44 +0900 Subject: [PATCH] Merge aligner into emitter/formatter --- Cargo.lock | 9 + Cargo.toml | 1 + crates/aligner/Cargo.toml | 14 + crates/aligner/src/lib.rs | 171 +++ crates/emitter/Cargo.toml | 1 + crates/emitter/src/aligner.rs | 1009 ----------------- crates/emitter/src/emitter.rs | 439 +++++-- crates/emitter/src/lib.rs | 1 - crates/emitter/src/tests.rs | 8 +- crates/formatter/Cargo.toml | 1 + crates/formatter/src/aligner.rs | 907 --------------- crates/formatter/src/formatter.rs | 402 ++++++- crates/formatter/src/lib.rs | 1 - crates/metadata/src/lockfile.rs | 2 - .../map/testcases/sv/07_statement.sv.map | 2 +- .../testcases/sv/45_var_let_in_always.sv.map | 2 +- .../testcases/sv/46_var_let_anywhere.sv.map | 2 +- .../map/testcases/sv/58_generic_struct.sv.map | 2 +- testcases/sv/45_var_let_in_always.sv | 2 +- testcases/sv/46_var_let_anywhere.sv | 2 +- testcases/sv/58_generic_struct.sv | 6 +- 21 files changed, 902 insertions(+), 2082 deletions(-) create mode 100644 crates/aligner/Cargo.toml create mode 100644 crates/aligner/src/lib.rs delete mode 100644 crates/emitter/src/aligner.rs delete mode 100644 crates/formatter/src/aligner.rs diff --git a/Cargo.lock b/Cargo.lock index 5e093b1d..7983275e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4917,6 +4917,13 @@ dependencies = [ "veryl-sourcemap", ] +[[package]] +name = "veryl-aligner" +version = "0.13.2" +dependencies = [ + "veryl-parser", +] + [[package]] name = "veryl-analyzer" version = "0.13.2" @@ -4939,6 +4946,7 @@ dependencies = [ "serde", "strnum_bitwidth", "toml 0.8.19", + "veryl-aligner", "veryl-analyzer", "veryl-metadata", "veryl-parser", @@ -4949,6 +4957,7 @@ dependencies = [ name = "veryl-formatter" version = "0.13.2" dependencies = [ + "veryl-aligner", "veryl-metadata", "veryl-parser", ] diff --git a/Cargo.toml b/Cargo.toml index 186a7efe..4e0ab387 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "crates/aligner", "crates/analyzer", "crates/emitter", "crates/formatter", diff --git a/crates/aligner/Cargo.toml b/crates/aligner/Cargo.toml new file mode 100644 index 00000000..f3ac2186 --- /dev/null +++ b/crates/aligner/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "veryl-aligner" +version = "0.13.2" +authors.workspace = true +repository.workspace = true +keywords.workspace = true +categories.workspace = true +license.workspace = true +readme.workspace = true +description.workspace = true +edition = "2021" + +[dependencies] +veryl-parser = {version = "0.13.2", path = "../parser"} diff --git a/crates/aligner/src/lib.rs b/crates/aligner/src/lib.rs new file mode 100644 index 00000000..17b214f4 --- /dev/null +++ b/crates/aligner/src/lib.rs @@ -0,0 +1,171 @@ +use std::collections::HashMap; +use veryl_parser::veryl_token::{Token, VerylToken}; + +#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)] +pub struct Location { + pub line: u32, + pub column: u32, + pub length: u32, + pub duplicated: Option, +} + +impl From<&Token> for Location { + fn from(x: &Token) -> Self { + Self { + line: x.line, + column: x.column, + length: x.length, + duplicated: None, + } + } +} + +impl From for Location { + fn from(x: Token) -> Self { + Self { + line: x.line, + column: x.column, + length: x.length, + duplicated: None, + } + } +} + +#[derive(Default)] +pub struct Align { + enable: bool, + index: usize, + max_width: u32, + width: u32, + line: u32, + rest: Vec<(Location, u32)>, + additions: HashMap, + pub last_location: Option, +} + +impl Align { + fn finish_group(&mut self) { + for (loc, width) in &self.rest { + self.additions.insert(*loc, self.max_width - width); + } + self.rest.clear(); + self.max_width = 0; + } + + pub fn finish_item(&mut self) { + // check overlap from start to finish + assert!(self.enable); + + self.enable = false; + if let Some(loc) = self.last_location { + if self.line > loc.line || loc.line - self.line > 1 { + self.finish_group(); + } + self.max_width = u32::max(self.max_width, self.width); + self.line = loc.line; + self.rest.push((loc, self.width)); + + self.width = 0; + self.index += 1; + } + } + + pub fn start_item(&mut self) { + // check overlap from start to finish + assert!(!self.enable); + + self.enable = true; + self.width = 0; + } + + fn token(&mut self, x: &VerylToken) { + if self.enable { + self.width += x.token.length; + let loc: Location = x.token.into(); + self.last_location = Some(loc); + } + } + + pub fn dummy_location(&mut self, x: Location) { + if self.enable { + self.width += 0; // 0 length token + self.last_location = Some(x); + } + } + + pub fn dummy_token(&mut self, x: &VerylToken) { + if self.enable { + self.width += 0; // 0 length token + let loc: Location = x.token.into(); + self.last_location = Some(loc); + } + } + + pub fn duplicated_token(&mut self, x: &VerylToken, i: usize) { + if self.enable { + self.width += x.token.length; + let mut loc: Location = x.token.into(); + loc.duplicated = Some(i); + self.last_location = Some(loc); + } + } + + fn space(&mut self, x: usize) { + if self.enable { + self.width += x as u32; + } + } +} + +pub mod align_kind { + pub const IDENTIFIER: usize = 0; + pub const TYPE: usize = 1; + pub const EXPRESSION: usize = 2; + pub const WIDTH: usize = 3; + pub const ARRAY: usize = 4; + pub const ASSIGNMENT: usize = 5; + pub const PARAMETER: usize = 6; + pub const DIRECTION: usize = 7; + pub const CLOCK_DOMAIN: usize = 8; +} + +#[derive(Default)] +pub struct Aligner { + pub additions: HashMap, + pub aligns: [Align; 9], +} + +impl Aligner { + pub fn new() -> Self { + Default::default() + } + + pub fn token(&mut self, x: &VerylToken) { + for i in 0..self.aligns.len() { + self.aligns[i].token(x); + } + } + + pub fn space(&mut self, x: usize) { + for i in 0..self.aligns.len() { + self.aligns[i].space(x); + } + } + + pub fn finish_group(&mut self) { + for i in 0..self.aligns.len() { + self.aligns[i].finish_group(); + } + } + + pub fn gather_additions(&mut self) { + for align in &self.aligns { + for (x, y) in &align.additions { + self.additions + .entry(*x) + .and_modify(|val| *val += *y) + .or_insert(*y); + } + } + } +} diff --git a/crates/emitter/Cargo.toml b/crates/emitter/Cargo.toml index da5d90cb..98b7bfc8 100644 --- a/crates/emitter/Cargo.toml +++ b/crates/emitter/Cargo.toml @@ -15,6 +15,7 @@ edition = "2021" [dependencies] serde = {workspace = true} strnum_bitwidth = {workspace = true} +veryl-aligner = {version = "0.13.2", path = "../aligner"} veryl-analyzer = {version = "0.13.2", path = "../analyzer"} veryl-metadata = {version = "0.13.2", path = "../metadata"} veryl-parser = {version = "0.13.2", path = "../parser"} diff --git a/crates/emitter/src/aligner.rs b/crates/emitter/src/aligner.rs deleted file mode 100644 index 5366d5e9..00000000 --- a/crates/emitter/src/aligner.rs +++ /dev/null @@ -1,1009 +0,0 @@ -use crate::emitter::{identifier_with_prefix_suffix, symbol_string, SymbolContext}; -use std::collections::HashMap; -use veryl_analyzer::symbol::{GenericMap, SymbolKind}; -use veryl_analyzer::symbol_table; -use veryl_metadata::{Build, BuiltinType, Metadata}; -use veryl_parser::resource_table::StrId; -use veryl_parser::veryl_grammar_trait::*; -use veryl_parser::veryl_token::{Token, VerylToken}; -use veryl_parser::veryl_walker::VerylWalker; -use veryl_parser::Stringifier; - -#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)] -pub struct Location { - pub line: u32, - pub column: u32, - pub length: u32, - pub duplicated: Option, -} - -impl From<&Token> for Location { - fn from(x: &Token) -> Self { - Self { - line: x.line, - column: x.column, - length: x.length, - duplicated: None, - } - } -} - -impl From for Location { - fn from(x: Token) -> Self { - Self { - line: x.line, - column: x.column, - length: x.length, - duplicated: None, - } - } -} - -#[derive(Default)] -pub struct Align { - enable: bool, - index: usize, - max_width: u32, - width: u32, - line: u32, - rest: Vec<(Location, u32)>, - additions: HashMap, - last_location: Option, -} - -impl Align { - fn finish_group(&mut self) { - for (loc, width) in &self.rest { - self.additions.insert(*loc, self.max_width - width); - } - self.rest.clear(); - self.max_width = 0; - } - - fn finish_item(&mut self) { - // check overlap from start to finish - assert!(self.enable); - - self.enable = false; - if let Some(loc) = self.last_location { - if loc.line - self.line > 1 { - self.finish_group(); - } - self.max_width = u32::max(self.max_width, self.width); - self.line = loc.line; - self.rest.push((loc, self.width)); - - self.width = 0; - self.index += 1; - } - } - - fn start_item(&mut self) { - // check overlap from start to finish - assert!(!self.enable); - - self.enable = true; - self.width = 0; - } - - fn token(&mut self, x: &VerylToken) { - if self.enable { - self.width += x.token.length; - let loc: Location = x.token.into(); - self.last_location = Some(loc); - } - } - - fn dummy_location(&mut self, x: Location) { - if self.enable { - self.width += 0; // 0 length token - self.last_location = Some(x); - } - } - - fn duplicated_token(&mut self, x: &VerylToken, i: usize) { - if self.enable { - self.width += x.token.length; - let mut loc: Location = x.token.into(); - loc.duplicated = Some(i); - self.last_location = Some(loc); - } - } - - fn space(&mut self, x: usize) { - if self.enable { - self.width += x as u32; - } - } -} - -mod align_kind { - pub const IDENTIFIER: usize = 0; - pub const TYPE: usize = 1; - pub const EXPRESSION: usize = 2; - pub const WIDTH: usize = 3; - pub const ARRAY: usize = 4; - pub const ASSIGNMENT: usize = 5; - pub const PARAMETER: usize = 6; - pub const DIRECTION: usize = 7; -} - -#[derive(Default)] -pub struct Aligner { - pub additions: HashMap, - aligns: [Align; 8], - in_expression: Vec<()>, - in_import: bool, - project_name: Option, - build_opt: Build, - generic_map: Vec, -} - -impl Aligner { - pub fn new() -> Self { - Default::default() - } - - pub fn set_metadata(&mut self, metadata: &Metadata) { - self.project_name = Some(metadata.project.name.as_str().into()); - self.build_opt = metadata.build.clone(); - } - - pub fn align(&mut self, input: &Veryl) { - self.veryl(input); - self.finish_group(); - for align in &self.aligns { - for (x, y) in &align.additions { - self.additions - .entry(*x) - .and_modify(|val| *val += *y) - .or_insert(*y); - } - } - } - - fn finish_group(&mut self) { - for i in 0..self.aligns.len() { - self.aligns[i].finish_group(); - } - } - - fn reset_align(&mut self) { - self.finish_group(); - } - - fn space(&mut self, repeat: usize) { - for i in 0..self.aligns.len() { - self.aligns[i].space(repeat); - } - } - - fn is_implicit_scalar_type(&mut self, x: &ScalarType) -> bool { - let mut stringifier = Stringifier::new(); - stringifier.scalar_type(x); - let r#type = match stringifier.as_str() { - "u32" => Some(BuiltinType::U32), - "u64" => Some(BuiltinType::U64), - "i32" => Some(BuiltinType::I32), - "i64" => Some(BuiltinType::I64), - "f32" => Some(BuiltinType::F32), - "f64" => Some(BuiltinType::F64), - "string" => Some(BuiltinType::String), - _ => None, - }; - if let Some(x) = r#type { - self.build_opt.implicit_parameter_types.contains(&x) - } else { - false - } - } - - fn is_implicit_type(&mut self) -> bool { - self.build_opt - .implicit_parameter_types - .contains(&BuiltinType::Type) - } -} - -impl VerylWalker for Aligner { - /// Semantic action for non-terminal 'VerylToken' - fn veryl_token(&mut self, arg: &VerylToken) { - for i in 0..self.aligns.len() { - self.aligns[i].token(arg); - } - } - - /// Semantic action for non-terminal 'Clock' - fn clock(&mut self, arg: &Clock) { - self.veryl_token(&arg.clock_token.replace("logic")); - } - - /// Semantic action for non-terminal 'ClockPosedge' - fn clock_posedge(&mut self, arg: &ClockPosedge) { - self.veryl_token(&arg.clock_posedge_token.replace("logic")); - } - - /// Semantic action for non-terminal 'ClockNegedge' - fn clock_negedge(&mut self, arg: &ClockNegedge) { - self.veryl_token(&arg.clock_negedge_token.replace("logic")); - } - - /// Semantic action for non-terminal 'Const' - fn r#const(&mut self, arg: &Const) { - self.veryl_token(&arg.const_token.replace("localparam")); - } - - /// Semantic action for non-terminal 'Reset' - fn reset(&mut self, arg: &Reset) { - self.veryl_token(&arg.reset_token.replace("logic")); - } - - /// Semantic action for non-terminal 'ResetAsyncHigh' - fn reset_async_high(&mut self, arg: &ResetAsyncHigh) { - self.veryl_token(&arg.reset_async_high_token.replace("logic")); - } - - /// Semantic action for non-terminal 'ResetAsyncLow' - fn reset_async_low(&mut self, arg: &ResetAsyncLow) { - self.veryl_token(&arg.reset_async_low_token.replace("logic")); - } - - /// Semantic action for non-terminal 'ResetSyncHigh' - fn reset_sync_high(&mut self, arg: &ResetSyncHigh) { - self.veryl_token(&arg.reset_sync_high_token.replace("logic")); - } - - /// Semantic action for non-terminal 'ResetSyncLow' - fn reset_sync_low(&mut self, arg: &ResetSyncLow) { - self.veryl_token(&arg.reset_sync_low_token.replace("logic")); - } - - /// Semantic action for non-terminal 'F32' - fn f32(&mut self, arg: &F32) { - self.veryl_token(&arg.f32_token.replace("shortreal")); - } - - /// Semantic action for non-terminal 'F64' - fn f64(&mut self, arg: &F64) { - self.veryl_token(&arg.f64_token.replace("real")); - } - - /// Semantic action for non-terminal 'I32' - fn i32(&mut self, arg: &I32) { - self.veryl_token(&arg.i32_token.replace("int signed")); - } - - /// Semantic action for non-terminal 'I64' - fn i64(&mut self, arg: &I64) { - self.veryl_token(&arg.i64_token.replace("longint signed")); - } - - /// Semantic action for non-terminal 'Param' - fn param(&mut self, arg: &Param) { - self.veryl_token(&arg.param_token.replace("parameter")); - } - - /// Semantic action for non-terminal 'U32' - fn u32(&mut self, arg: &U32) { - self.veryl_token(&arg.u32_token.replace("int unsigned")); - } - - /// Semantic action for non-terminal 'U64' - fn u64(&mut self, arg: &U64) { - self.veryl_token(&arg.u64_token.replace("longint unsigned")); - } - - /// Semantic action for non-terminal 'Identifier' - fn identifier(&mut self, arg: &Identifier) { - let (prefix, suffix) = if let Ok(found) = symbol_table::resolve(arg) { - match &found.found.kind { - SymbolKind::Port(x) => (x.prefix.clone(), x.suffix.clone()), - SymbolKind::Variable(x) => (x.prefix.clone(), x.suffix.clone()), - _ => (None, None), - } - } else { - (None, None) - }; - self.veryl_token(&identifier_with_prefix_suffix(arg, &prefix, &suffix)); - } - - /// Semantic action for non-terminal 'HierarchicalIdentifier' - fn hierarchical_identifier(&mut self, arg: &HierarchicalIdentifier) { - let list_len = &arg.hierarchical_identifier_list0.len(); - let (prefix, suffix) = if let Ok(found) = symbol_table::resolve(arg) { - match &found.found.kind { - SymbolKind::Port(x) => (x.prefix.clone(), x.suffix.clone()), - SymbolKind::Variable(x) => (x.prefix.clone(), x.suffix.clone()), - _ => (None, None), - } - } else { - unreachable!() - }; - - if *list_len == 0 { - self.veryl_token(&identifier_with_prefix_suffix( - &arg.identifier, - &prefix, - &suffix, - )); - } else { - self.identifier(&arg.identifier); - } - - for x in &arg.hierarchical_identifier_list { - self.select(&x.select); - } - - for (i, x) in arg.hierarchical_identifier_list0.iter().enumerate() { - self.dot(&x.dot); - if (i + 1) == *list_len { - self.veryl_token(&identifier_with_prefix_suffix( - &x.identifier, - &prefix, - &suffix, - )); - } else { - self.identifier(&x.identifier); - } - for x in &x.hierarchical_identifier_list0_list { - self.select(&x.select); - } - } - } - - /// Semantic action for non-terminal 'ScopedIdentifier' - fn scoped_identifier(&mut self, arg: &ScopedIdentifier) { - if let Ok(symbol) = symbol_table::resolve(arg) { - let context: SymbolContext = self.into(); - let text = symbol_string(arg.identifier(), &symbol.found, &context); - self.veryl_token(&arg.identifier().replace(&text)); - } - } - - /// Semantic action for non-terminal 'Expression' - // Add `#[inline(never)]` to `expression*` as a workaround for long time compilation - // https://github.com/rust-lang/rust/issues/106211 - #[inline(never)] - fn expression(&mut self, arg: &Expression) { - self.in_expression.push(()); - self.expression01(&arg.expression01); - for x in &arg.expression_list { - self.space(1); - self.operator01(&x.operator01); - self.space(1); - self.expression01(&x.expression01); - } - self.in_expression.pop(); - } - - /// Semantic action for non-terminal 'Expression01' - #[inline(never)] - fn expression01(&mut self, arg: &Expression01) { - self.expression02(&arg.expression02); - for x in &arg.expression01_list { - self.space(1); - self.operator02(&x.operator02); - self.space(1); - self.expression02(&x.expression02); - } - } - - /// Semantic action for non-terminal 'Expression02' - #[inline(never)] - fn expression02(&mut self, arg: &Expression02) { - self.expression03(&arg.expression03); - for x in &arg.expression02_list { - self.space(1); - self.operator03(&x.operator03); - self.space(1); - self.expression03(&x.expression03); - } - } - - /// Semantic action for non-terminal 'Expression03' - #[inline(never)] - fn expression03(&mut self, arg: &Expression03) { - self.expression04(&arg.expression04); - for x in &arg.expression03_list { - self.space(1); - self.operator04(&x.operator04); - self.space(1); - self.expression04(&x.expression04); - } - } - - /// Semantic action for non-terminal 'Expression04' - #[inline(never)] - fn expression04(&mut self, arg: &Expression04) { - self.expression05(&arg.expression05); - for x in &arg.expression04_list { - self.space(1); - self.operator05(&x.operator05); - self.space(1); - self.expression05(&x.expression05); - } - } - - /// Semantic action for non-terminal 'Expression05' - #[inline(never)] - fn expression05(&mut self, arg: &Expression05) { - self.expression06(&arg.expression06); - for x in &arg.expression05_list { - self.space(1); - self.operator06(&x.operator06); - self.space(1); - self.expression06(&x.expression06); - } - } - - /// Semantic action for non-terminal 'Expression06' - #[inline(never)] - fn expression06(&mut self, arg: &Expression06) { - self.expression07(&arg.expression07); - for x in &arg.expression06_list { - self.space(1); - self.operator07(&x.operator07); - self.space(1); - self.expression07(&x.expression07); - } - } - - /// Semantic action for non-terminal 'Expression07' - #[inline(never)] - fn expression07(&mut self, arg: &Expression07) { - self.expression08(&arg.expression08); - for x in &arg.expression07_list { - self.space(1); - self.operator08(&x.operator08); - self.space(1); - self.expression08(&x.expression08); - } - } - - /// Semantic action for non-terminal 'Expression08' - #[inline(never)] - fn expression08(&mut self, arg: &Expression08) { - self.expression09(&arg.expression09); - for x in &arg.expression08_list { - self.space(1); - self.operator09(&x.operator09); - self.space(1); - self.expression09(&x.expression09); - } - } - - /// Semantic action for non-terminal 'Expression09' - #[inline(never)] - fn expression09(&mut self, arg: &Expression09) { - self.expression10(&arg.expression10); - for x in &arg.expression09_list { - self.space(1); - match &*x.expression09_list_group { - Expression09ListGroup::Operator10(x) => self.operator10(&x.operator10), - Expression09ListGroup::Star(x) => self.star(&x.star), - } - self.space(1); - self.expression10(&x.expression10); - } - } - - /// Semantic action for non-terminal 'Expression10' - #[inline(never)] - fn expression10(&mut self, arg: &Expression10) { - self.expression11(&arg.expression11); - for x in &arg.expression10_list { - self.space(1); - self.operator11(&x.operator11); - self.space(1); - self.expression11(&x.expression11); - } - } - - /// Semantic action for non-terminal 'ArgumentList' - fn argument_list(&mut self, arg: &ArgumentList) { - self.argument_item(&arg.argument_item); - for x in &arg.argument_list_list { - self.comma(&x.comma); - self.space(1); - self.argument_item(&x.argument_item); - } - if let Some(ref x) = arg.argument_list_opt { - self.comma(&x.comma); - } - } - - /// Semantic action for non-terminal 'Width' - fn width(&mut self, arg: &Width) { - self.l_angle(&arg.l_angle); - self.expression(&arg.expression); - self.space("-1:0".len()); - for x in &arg.width_list { - self.space("][".len()); - self.expression(&x.expression); - self.space("-1:0".len()); - } - self.r_angle(&arg.r_angle); - } - - /// Semantic action for non-terminal 'Array' - fn array(&mut self, arg: &Array) { - self.l_bracket(&arg.l_bracket); - self.expression(&arg.expression); - self.space("-1:0".len()); - for x in &arg.array_list { - self.space("][".len()); - self.expression(&x.expression); - self.space("-1:0".len()); - } - self.r_bracket(&arg.r_bracket); - } - - /// Semantic action for non-terminal 'FactorType' - fn factor_type(&mut self, arg: &FactorType) { - match arg.factor_type_group.as_ref() { - FactorTypeGroup::VariableTypeFactorTypeOpt(x) => { - self.variable_type(&x.variable_type); - if let Some(ref x) = x.factor_type_opt { - self.space(1); - self.width(&x.width); - } - } - FactorTypeGroup::FixedType(x) => self.fixed_type(&x.fixed_type), - } - } - - /// Semantic action for non-terminal 'ScalarType' - fn scalar_type(&mut self, arg: &ScalarType) { - if !self.in_expression.is_empty() { - return; - } - - self.aligns[align_kind::TYPE].start_item(); - // dummy space for implicit type - self.space(1); - for x in &arg.scalar_type_list { - self.type_modifier(&x.type_modifier); - self.space(1); - } - match &*arg.scalar_type_group { - ScalarTypeGroup::UserDefinedTypeScalarTypeOpt(x) => { - self.user_defined_type(&x.user_defined_type); - self.aligns[align_kind::TYPE].finish_item(); - self.aligns[align_kind::WIDTH].start_item(); - if let Some(ref x) = x.scalar_type_opt { - self.space(1); - self.width(&x.width); - } else { - let loc = self.aligns[align_kind::TYPE].last_location; - self.aligns[align_kind::WIDTH].dummy_location(loc.unwrap()); - } - } - ScalarTypeGroup::FactorType(x) => match x.factor_type.factor_type_group.as_ref() { - FactorTypeGroup::VariableTypeFactorTypeOpt(x) => { - self.variable_type(&x.variable_type); - self.aligns[align_kind::TYPE].finish_item(); - self.aligns[align_kind::WIDTH].start_item(); - if let Some(ref x) = x.factor_type_opt { - self.space(1); - self.width(&x.width); - } else { - let loc = self.aligns[align_kind::TYPE].last_location; - self.aligns[align_kind::WIDTH].dummy_location(loc.unwrap()); - } - } - FactorTypeGroup::FixedType(x) => { - self.fixed_type(&x.fixed_type); - self.aligns[align_kind::TYPE].finish_item(); - self.aligns[align_kind::WIDTH].start_item(); - let loc = self.aligns[align_kind::TYPE].last_location; - self.aligns[align_kind::WIDTH].dummy_location(loc.unwrap()); - } - }, - } - self.aligns[align_kind::WIDTH].finish_item(); - } - - /// Semantic action for non-terminal 'ArrayType' - fn array_type(&mut self, arg: &ArrayType) { - self.scalar_type(&arg.scalar_type); - self.aligns[align_kind::ARRAY].start_item(); - if let Some(ref x) = arg.array_type_opt { - self.space(1); - self.array(&x.array); - } else { - let loc = self.aligns[align_kind::IDENTIFIER].last_location; - self.aligns[align_kind::ARRAY].dummy_location(loc.unwrap()); - } - self.aligns[align_kind::ARRAY].finish_item(); - } - - /// Semantic action for non-terminal 'LetStatement' - fn let_statement(&mut self, arg: &LetStatement) { - self.r#let(&arg.r#let); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - self.array_type(&arg.array_type); - self.equ(&arg.equ); - self.expression(&arg.expression); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'IdentifierStatement' - fn identifier_statement(&mut self, arg: &IdentifierStatement) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.expression_identifier(&arg.expression_identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - match &*arg.identifier_statement_group { - IdentifierStatementGroup::FunctionCall(x) => { - self.function_call(&x.function_call); - } - IdentifierStatementGroup::Assignment(x) => { - self.assignment(&x.assignment); - } - } - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'Assignment' - fn assignment(&mut self, arg: &Assignment) { - self.aligns[align_kind::ASSIGNMENT].start_item(); - match &*arg.assignment_group { - AssignmentGroup::Equ(x) => self.equ(&x.equ), - AssignmentGroup::AssignmentOperator(x) => { - self.assignment_operator(&x.assignment_operator) - } - } - self.aligns[align_kind::ASSIGNMENT].finish_item(); - self.expression(&arg.expression); - } - - /// Semantic action for non-terminal 'CaseItem' - fn case_item(&mut self, arg: &CaseItem) { - self.aligns[align_kind::EXPRESSION].start_item(); - match &*arg.case_item_group { - CaseItemGroup::CaseCondition(x) => { - self.range_item(&x.case_condition.range_item); - for x in &x.case_condition.case_condition_list { - self.comma(&x.comma); - self.space(1); - self.range_item(&x.range_item); - } - } - CaseItemGroup::Defaul(x) => self.defaul(&x.defaul), - } - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&arg.colon); - match &*arg.case_item_group0 { - CaseItemGroup0::Statement(x) => self.statement(&x.statement), - CaseItemGroup0::StatementBlock(x) => self.statement_block(&x.statement_block), - } - } - - /// Semantic action for non-terminal 'SwitchItem' - fn switch_item(&mut self, arg: &SwitchItem) { - self.aligns[align_kind::EXPRESSION].start_item(); - match &*arg.switch_item_group { - SwitchItemGroup::SwitchCondition(x) => { - self.expression(&x.switch_condition.expression); - for x in &x.switch_condition.switch_condition_list { - self.comma(&x.comma); - self.space(1); - self.expression(&x.expression); - } - } - SwitchItemGroup::Defaul(x) => self.defaul(&x.defaul), - } - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&arg.colon); - match &*arg.switch_item_group0 { - SwitchItemGroup0::Statement(x) => self.statement(&x.statement), - SwitchItemGroup0::StatementBlock(x) => self.statement_block(&x.statement_block), - } - } - - /// Semantic action for non-terminal 'LetDeclaration' - fn let_declaration(&mut self, arg: &LetDeclaration) { - self.r#let(&arg.r#let); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - self.array_type(&arg.array_type); - self.equ(&arg.equ); - self.expression(&arg.expression); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'VarDeclaration' - fn var_declaration(&mut self, arg: &VarDeclaration) { - self.var(&arg.var); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - self.array_type(&arg.array_type); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'ConstDeclaration' - fn const_declaration(&mut self, arg: &ConstDeclaration) { - self.r#const(&arg.r#const); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - match &*arg.const_declaration_group { - ConstDeclarationGroup::ArrayType(x) => { - if !self.is_implicit_scalar_type(&x.array_type.scalar_type) { - self.array_type(&x.array_type); - } else { - self.aligns[align_kind::TYPE].start_item(); - self.aligns[align_kind::TYPE] - .dummy_location(arg.r#const.const_token.token.into()); - self.aligns[align_kind::TYPE].finish_item(); - } - } - ConstDeclarationGroup::Type(x) => { - self.aligns[align_kind::TYPE].start_item(); - if !self.is_implicit_type() { - self.r#type(&x.r#type); - self.space(1); - } else { - self.aligns[align_kind::TYPE] - .dummy_location(arg.r#const.const_token.token.into()); - } - self.aligns[align_kind::TYPE].finish_item(); - } - } - self.equ(&arg.equ); - self.expression(&arg.expression); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'TypeDefDeclaration' - fn type_def_declaration(&mut self, arg: &TypeDefDeclaration) { - self.r#type(&arg.r#type); - self.scalar_type(&arg.array_type.scalar_type); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - if let Some(ato) = &arg.array_type.array_type_opt { - self.aligns[align_kind::ARRAY].start_item(); - self.array(&ato.array); - self.aligns[align_kind::ARRAY].finish_item(); - } - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'AssignDeclaration' - fn assign_declaration(&mut self, arg: &AssignDeclaration) { - self.assign(&arg.assign); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.hierarchical_identifier(&arg.hierarchical_identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.equ(&arg.equ); - self.expression(&arg.expression); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'ModportItem' - fn modport_item(&mut self, arg: &ModportItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - self.direction(&arg.direction); - } - - /// Semantic action for non-terminal 'StructItem' - fn struct_union_item(&mut self, arg: &StructUnionItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - self.scalar_type(&arg.scalar_type); - } - - /// Semantic action for non-terminal 'InstDeclaration' - fn inst_declaration(&mut self, arg: &InstDeclaration) { - let single_line = arg.inst_declaration_opt1.is_none(); - self.inst(&arg.inst); - if single_line { - self.aligns[align_kind::IDENTIFIER].start_item(); - } - self.identifier(&arg.identifier); - if single_line { - self.aligns[align_kind::IDENTIFIER].finish_item(); - } - self.colon(&arg.colon); - self.scoped_identifier(&arg.scoped_identifier); - // skip align at single line - if single_line { - return; - } - if let Some(ref x) = arg.inst_declaration_opt { - self.array(&x.array); - } - if let Some(ref x) = arg.inst_declaration_opt0 { - self.inst_parameter(&x.inst_parameter); - } - if let Some(ref x) = arg.inst_declaration_opt1 { - self.l_paren(&x.l_paren); - if let Some(ref x) = x.inst_declaration_opt2 { - self.inst_port_list(&x.inst_port_list); - } - self.r_paren(&x.r_paren); - } - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'InstParameterItem' - fn inst_parameter_item(&mut self, arg: &InstParameterItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - if let Some(ref x) = arg.inst_parameter_item_opt { - self.colon(&x.colon); - self.space(1); - self.aligns[align_kind::EXPRESSION].start_item(); - self.expression(&x.expression); - self.aligns[align_kind::EXPRESSION].finish_item(); - } else { - self.aligns[align_kind::EXPRESSION].start_item(); - self.aligns[align_kind::EXPRESSION] - .duplicated_token(&arg.identifier.identifier_token, 0); - self.aligns[align_kind::EXPRESSION].finish_item(); - } - } - - /// Semantic action for non-terminal 'InstPortItem' - fn inst_port_item(&mut self, arg: &InstPortItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - if let Some(ref x) = arg.inst_port_item_opt { - self.colon(&x.colon); - self.space(1); - self.aligns[align_kind::EXPRESSION].start_item(); - self.expression(&x.expression); - self.aligns[align_kind::EXPRESSION].finish_item(); - } else { - let (prefix, suffix) = if let Ok(found) = symbol_table::resolve(arg.identifier.as_ref()) - { - match &found.found.kind { - SymbolKind::Port(x) => (x.prefix.clone(), x.suffix.clone()), - SymbolKind::Variable(x) => (x.prefix.clone(), x.suffix.clone()), - _ => (None, None), - } - } else { - unreachable!() - }; - let token = identifier_with_prefix_suffix(&arg.identifier, &prefix, &suffix); - self.aligns[align_kind::EXPRESSION].start_item(); - self.aligns[align_kind::EXPRESSION].duplicated_token(&token, 0); - self.aligns[align_kind::EXPRESSION].finish_item(); - } - } - - /// Semantic action for non-terminal 'WithParameterItem' - fn with_parameter_item(&mut self, arg: &WithParameterItem) { - self.aligns[align_kind::PARAMETER].start_item(); - match &*arg.with_parameter_item_group { - WithParameterItemGroup::Param(x) => self.param(&x.param), - WithParameterItemGroup::Const(x) => self.r#const(&x.r#const), - }; - self.aligns[align_kind::PARAMETER].finish_item(); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - match &*arg.with_parameter_item_group0 { - WithParameterItemGroup0::ArrayType(x) => { - if !self.is_implicit_scalar_type(&x.array_type.scalar_type) { - self.array_type(&x.array_type); - } else { - self.aligns[align_kind::TYPE].start_item(); - let loc = self.aligns[align_kind::PARAMETER].last_location; - self.aligns[align_kind::TYPE].dummy_location(loc.unwrap()); - self.aligns[align_kind::TYPE].finish_item(); - } - } - WithParameterItemGroup0::Type(x) => { - self.aligns[align_kind::TYPE].start_item(); - if !self.is_implicit_type() { - self.r#type(&x.r#type); - self.space(1); - } else { - let loc = self.aligns[align_kind::PARAMETER].last_location; - self.aligns[align_kind::TYPE].dummy_location(loc.unwrap()); - } - self.aligns[align_kind::TYPE].finish_item(); - } - } - self.equ(&arg.equ); - self.aligns[align_kind::EXPRESSION].start_item(); - self.expression(&arg.expression); - self.aligns[align_kind::EXPRESSION].finish_item(); - } - - /// Semantic action for non-terminal 'PortDeclarationItem' - fn port_declaration_item(&mut self, arg: &PortDeclarationItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - match &*arg.port_declaration_item_group { - PortDeclarationItemGroup::PortTypeConcrete(x) => { - let x = x.port_type_concrete.as_ref(); - self.direction(&x.direction); - self.array_type(&x.array_type); - } - PortDeclarationItemGroup::PortTypeAbstract(x) => { - let x = x.port_type_abstract.as_ref(); - self.interface(&x.interface); - if let Some(ref x) = x.port_type_abstract_opt0 { - self.array(&x.array); - } - } - } - } - - /// Semantic action for non-terminal 'Direction' - fn direction(&mut self, arg: &Direction) { - if !matches!(arg, Direction::Modport(_)) { - self.aligns[align_kind::DIRECTION].start_item(); - } - match arg { - Direction::Input(x) => self.input(&x.input), - Direction::Output(x) => self.output(&x.output), - Direction::Inout(x) => self.inout(&x.inout), - Direction::Ref(x) => self.r#ref(&x.r#ref), - Direction::Modport(_) => (), - Direction::Import(x) => self.import(&x.import), - }; - if !matches!(arg, Direction::Modport(_)) { - self.aligns[align_kind::DIRECTION].finish_item(); - } - } - - /// Semantic action for non-terminal 'FunctionDeclaration' - fn function_declaration(&mut self, arg: &FunctionDeclaration) { - self.function(&arg.function); - self.identifier(&arg.identifier); - if let Some(ref x) = arg.function_declaration_opt0 { - self.port_declaration(&x.port_declaration); - } - if let Some(ref x) = arg.function_declaration_opt1 { - self.minus_g_t(&x.minus_g_t); - self.scalar_type(&x.scalar_type); - self.reset_align(); - } - self.statement_block(&arg.statement_block); - } - - /// Semantic action for non-terminal 'ImportDeclaration' - fn import_declaration(&mut self, arg: &ImportDeclaration) { - self.in_import = true; - self.import(&arg.import); - self.scoped_identifier(&arg.scoped_identifier); - if let Some(ref x) = arg.import_declaration_opt { - self.colon_colon(&x.colon_colon); - self.star(&x.star); - } - self.semicolon(&arg.semicolon); - self.in_import = false; - } -} - -impl From<&mut Aligner> for SymbolContext { - fn from(value: &mut Aligner) -> Self { - SymbolContext { - project_name: value.project_name, - build_opt: value.build_opt.clone(), - in_import: value.in_import, - generic_map: value.generic_map.clone(), - } - } -} diff --git a/crates/emitter/src/emitter.rs b/crates/emitter/src/emitter.rs index 3e26d831..498130f3 100644 --- a/crates/emitter/src/emitter.rs +++ b/crates/emitter/src/emitter.rs @@ -1,6 +1,6 @@ -use crate::aligner::{Aligner, Location}; use std::fs; use std::path::Path; +use veryl_aligner::{align_kind, Aligner, Location}; use veryl_analyzer::attribute::Attribute as Attr; use veryl_analyzer::attribute::{CondTypeItem, EnumEncodingItem}; use veryl_analyzer::attribute_table; @@ -32,7 +32,14 @@ pub enum AttributeType { Test, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Mode { + Emit, + Align, +} + pub struct Emitter { + mode: Mode, project_name: Option, build_opt: Build, format_opt: Format, @@ -50,6 +57,8 @@ pub struct Emitter { in_always_ff: bool, in_direction_modport: bool, in_import: bool, + in_scalar_type: bool, + in_expression: Vec<()>, signed: bool, default_clock: Option, default_reset: Option, @@ -67,6 +76,7 @@ pub struct Emitter { impl Default for Emitter { fn default() -> Self { Self { + mode: Mode::Emit, project_name: None, build_opt: Build::default(), format_opt: Format::default(), @@ -84,6 +94,8 @@ impl Default for Emitter { in_always_ff: false, in_direction_modport: false, in_import: false, + in_scalar_type: false, + in_expression: Vec::new(), signed: false, default_clock: None, default_reset: None, @@ -102,16 +114,13 @@ impl Default for Emitter { impl Emitter { pub fn new(metadata: &Metadata, src_path: &Path, dst_path: &Path, map_path: &Path) -> Self { - let mut aligner = Aligner::new(); - aligner.set_metadata(metadata); - let source_map = SourceMap::new(src_path, dst_path, map_path); Self { project_name: Some(metadata.project.name.as_str().into()), build_opt: metadata.build.clone(), format_opt: metadata.format.clone(), - aligner, + aligner: Aligner::new(), source_map: Some(source_map), ..Default::default() } @@ -119,7 +128,11 @@ impl Emitter { pub fn emit(&mut self, project_name: &str, input: &Veryl) { namespace_table::set_default(&[project_name.into()]); - self.aligner.align(input); + self.mode = Mode::Align; + self.veryl(input); + self.aligner.finish_group(); + self.aligner.gather_additions(); + self.mode = Mode::Emit; self.veryl(input); } @@ -132,18 +145,29 @@ impl Emitter { } fn str(&mut self, x: &str) { - self.string.push_str(x); - - let new_lines = x.matches('\n').count() as u32; - self.dst_line += new_lines; - if new_lines == 0 { - self.dst_column += x.len() as u32; - } else { - self.dst_column = (x.len() - x.rfind('\n').unwrap_or(0)) as u32; + match self.mode { + Mode::Emit => { + self.string.push_str(x); + + let new_lines = x.matches('\n').count() as u32; + self.dst_line += new_lines; + if new_lines == 0 { + self.dst_column += x.len() as u32; + } else { + self.dst_column = (x.len() - x.rfind('\n').unwrap_or(0)) as u32; + } + } + Mode::Align => { + self.aligner.space(x.len()); + } } } fn truncate(&mut self, x: usize) { + if self.mode == Mode::Align { + return; + } + let removed = self.string.split_off(x); let removed_lines = removed.matches('\n').count() as u32; @@ -156,6 +180,10 @@ impl Emitter { } fn unindent(&mut self) { + if self.mode == Mode::Align { + return; + } + let indent_width = self.indent * self.format_opt.indent_width + self.case_item_indent.unwrap_or(0); if self.string.ends_with(&" ".repeat(indent_width)) { @@ -164,12 +192,20 @@ impl Emitter { } fn indent(&mut self) { + if self.mode == Mode::Align { + return; + } + let indent_width = self.indent * self.format_opt.indent_width + self.case_item_indent.unwrap_or(0); self.str(&" ".repeat(indent_width)); } fn newline_push(&mut self) { + if self.mode == Mode::Align { + return; + } + self.unindent(); if !self.consumed_next_newline { self.str(NEWLINE); @@ -182,6 +218,10 @@ impl Emitter { } fn newline_pop(&mut self) { + if self.mode == Mode::Align { + return; + } + self.unindent(); if !self.consumed_next_newline { self.str(NEWLINE); @@ -194,6 +234,10 @@ impl Emitter { } fn newline(&mut self) { + if self.mode == Mode::Align { + return; + } + self.unindent(); if !self.consumed_next_newline { self.str(NEWLINE); @@ -257,44 +301,51 @@ impl Emitter { } fn process_token(&mut self, x: &VerylToken, will_push: bool, duplicated: Option) { - self.push_token(&x.token); - - let mut loc: Location = x.token.into(); - loc.duplicated = duplicated; - if let Some(width) = self.aligner.additions.get(&loc) { - self.space(*width as usize); - } + match self.mode { + Mode::Emit => { + self.push_token(&x.token); + + let mut loc: Location = x.token.into(); + loc.duplicated = duplicated; + if let Some(width) = self.aligner.additions.get(&loc) { + self.space(*width as usize); + } - // skip to emit comments - if duplicated.is_some() || self.build_opt.strip_comments { - return; - } + // skip to emit comments + if duplicated.is_some() || self.build_opt.strip_comments { + return; + } - // temporary indent to adjust indent of comments with the next push - if will_push { - self.indent += 1; - } - // detect line comment newline which will consume the next newline - self.consumed_next_newline = false; - for x in &x.comments { - // insert space between comments in the same line - if x.line == self.src_line && !self.in_start_token { - self.space(1); + // temporary indent to adjust indent of comments with the next push + if will_push { + self.indent += 1; + } + // detect line comment newline which will consume the next newline + self.consumed_next_newline = false; + for x in &x.comments { + // insert space between comments in the same line + if x.line == self.src_line && !self.in_start_token { + self.space(1); + } + for _ in 0..x.line - self.src_line { + self.unindent(); + self.str(NEWLINE); + self.indent(); + } + self.push_token(x); + } + if will_push { + self.indent -= 1; + } + if self.consumed_next_newline { + self.unindent(); + self.str(NEWLINE); + self.indent(); + } } - for _ in 0..x.line - self.src_line { - self.unindent(); - self.str(NEWLINE); - self.indent(); + Mode::Align => { + self.aligner.token(x); } - self.push_token(x); - } - if will_push { - self.indent -= 1; - } - if self.consumed_next_newline { - self.unindent(); - self.str(NEWLINE); - self.indent(); } } @@ -307,7 +358,37 @@ impl Emitter { } fn duplicated_token(&mut self, x: &VerylToken, i: usize) { - self.process_token(x, false, Some(i)) + if self.mode == Mode::Emit { + self.process_token(x, false, Some(i)) + } + } + + fn align_start(&mut self, kind: usize) { + if self.mode == Mode::Align { + self.aligner.aligns[kind].start_item(); + } + } + + fn align_finish(&mut self, kind: usize) { + if self.mode == Mode::Align { + self.aligner.aligns[kind].finish_item(); + } + } + + fn align_last_location(&mut self, kind: usize) -> Option { + self.aligner.aligns[kind].last_location + } + + fn align_dummy_location(&mut self, kind: usize, loc: Option) { + if self.mode == Mode::Align { + self.aligner.aligns[kind].dummy_location(loc.unwrap()); + } + } + + fn align_duplicated_token(&mut self, kind: usize, x: &VerylToken, i: usize) { + if self.mode == Mode::Align { + self.aligner.aligns[kind].duplicated_token(x, i); + } } fn case_inside_statement(&mut self, arg: &CaseStatement) { @@ -329,6 +410,7 @@ impl Emitter { fn case_inside_item(&mut self, arg: &CaseItem, force_default: bool) { let start = self.dst_column; + self.align_start(align_kind::EXPRESSION); match &*arg.case_item_group { CaseItemGroup::CaseCondition(x) => { if force_default { @@ -344,6 +426,7 @@ impl Emitter { } CaseItemGroup::Defaul(x) => self.defaul(&x.defaul), } + self.align_finish(align_kind::EXPRESSION); self.colon(&arg.colon); self.space(1); match arg.case_item_group0.as_ref() { @@ -375,6 +458,7 @@ impl Emitter { fn case_expanded_item(&mut self, lhs: &Expression, item: &CaseItem, force_default: bool) { let start: u32 = self.dst_column; + self.align_start(align_kind::EXPRESSION); match &*item.case_item_group { CaseItemGroup::CaseCondition(x) => { if force_default { @@ -390,6 +474,7 @@ impl Emitter { } CaseItemGroup::Defaul(x) => self.defaul(&x.defaul), } + self.align_finish(align_kind::EXPRESSION); self.colon(&item.colon); self.space(1); match item.case_item_group0.as_ref() { @@ -1052,6 +1137,7 @@ impl VerylWalker for Emitter { // https://github.com/rust-lang/rust/issues/106211 #[inline(never)] fn expression(&mut self, arg: &Expression) { + self.in_expression.push(()); self.expression01(&arg.expression01); for x in &arg.expression_list { self.space(1); @@ -1059,6 +1145,7 @@ impl VerylWalker for Emitter { self.space(1); self.expression01(&x.expression01); } + self.in_expression.pop(); } /// Semantic action for non-terminal 'Expression01' @@ -1598,17 +1685,47 @@ impl VerylWalker for Emitter { self.space(1); self.str("signed"); } + if self.in_scalar_type { + self.align_finish(align_kind::TYPE); + self.align_start(align_kind::WIDTH); + } if let Some(ref x) = x.factor_type_opt { self.space(1); self.width(&x.width); + } else { + if self.in_scalar_type { + let loc = self.align_last_location(align_kind::TYPE); + self.align_dummy_location(align_kind::WIDTH, loc); + } + } + } + FactorTypeGroup::FixedType(x) => { + self.fixed_type(&x.fixed_type); + if self.in_scalar_type { + self.align_finish(align_kind::TYPE); + self.align_start(align_kind::WIDTH); + let loc = self.align_last_location(align_kind::TYPE); + self.align_dummy_location(align_kind::WIDTH, loc); } } - FactorTypeGroup::FixedType(x) => self.fixed_type(&x.fixed_type), } } /// Semantic action for non-terminal 'ScalarType' fn scalar_type(&mut self, arg: &ScalarType) { + self.in_scalar_type = true; + + // disable align in Expression + if self.mode == Mode::Align && !self.in_expression.is_empty() { + self.in_scalar_type = false; + return; + } + + self.align_start(align_kind::TYPE); + if self.mode == Mode::Align { + // dummy space for implicit type + self.space(1); + } for x in &arg.scalar_type_list { self.type_modifier(&x.type_modifier); } @@ -1619,14 +1736,21 @@ impl VerylWalker for Emitter { self.space(1); self.str("signed"); } + self.align_finish(align_kind::TYPE); + self.align_start(align_kind::WIDTH); if let Some(ref x) = x.scalar_type_opt { self.space(1); self.width(&x.width); + } else { + let loc = self.align_last_location(align_kind::TYPE); + self.align_dummy_location(align_kind::WIDTH, loc); } } ScalarTypeGroup::FactorType(x) => self.factor_type(&x.factor_type), } self.signed = false; + self.in_scalar_type = false; + self.align_finish(align_kind::WIDTH); } /// Semantic action for non-terminal 'StatementBlock' @@ -1646,7 +1770,9 @@ impl VerylWalker for Emitter { //} //self.str(";"); //self.newline(); + self.align_start(align_kind::IDENTIFIER); self.str(&arg.identifier.identifier_token.to_string()); + self.align_finish(align_kind::IDENTIFIER); self.space(1); self.equ(&arg.equ); self.space(1); @@ -1656,8 +1782,10 @@ impl VerylWalker for Emitter { /// Semantic action for non-terminal 'IdentifierStatement' fn identifier_statement(&mut self, arg: &IdentifierStatement) { + self.align_start(align_kind::IDENTIFIER); self.expression_identifier(&arg.expression_identifier); self.assignment_lefthand_side = Some(*arg.expression_identifier.clone()); + self.align_finish(align_kind::IDENTIFIER); match &*arg.identifier_statement_group { IdentifierStatementGroup::FunctionCall(x) => { self.function_call(&x.function_call); @@ -1691,9 +1819,13 @@ impl VerylWalker for Emitter { self.space(1); if is_nba { + self.align_start(align_kind::ASSIGNMENT); self.str("<"); match &*arg.assignment_group { - AssignmentGroup::Equ(x) => self.equ(&x.equ), + AssignmentGroup::Equ(x) => { + self.equ(&x.equ); + self.align_finish(align_kind::ASSIGNMENT); + } AssignmentGroup::AssignmentOperator(x) => { let token = format!( "{}", @@ -1701,7 +1833,8 @@ impl VerylWalker for Emitter { ); // remove trailing `=` from assignment operator let token = &token[0..token.len() - 1]; - self.str("="); + self.token(&x.assignment_operator.assignment_operator_token.replace("=")); + self.align_finish(align_kind::ASSIGNMENT); self.space(1); let identifier = self.assignment_lefthand_side.take().unwrap(); self.expression_identifier(&identifier); @@ -1718,12 +1851,14 @@ impl VerylWalker for Emitter { self.expression(&arg.expression); } } else { + self.align_start(align_kind::ASSIGNMENT); match &*arg.assignment_group { AssignmentGroup::Equ(x) => self.equ(&x.equ), AssignmentGroup::AssignmentOperator(x) => { self.assignment_operator(&x.assignment_operator) } } + self.align_finish(align_kind::ASSIGNMENT); self.space(1); self.expression(&arg.expression); } @@ -1900,6 +2035,7 @@ impl VerylWalker for Emitter { /// Semantic action for non-terminal 'SwitchItem' fn switch_item(&mut self, arg: &SwitchItem) { let start = self.dst_column; + self.align_start(align_kind::EXPRESSION); match &*arg.switch_item_group { SwitchItemGroup::SwitchCondition(x) => { self.expression(&x.switch_condition.expression); @@ -1911,6 +2047,7 @@ impl VerylWalker for Emitter { } SwitchItemGroup::Defaul(x) => self.defaul(&x.defaul), } + self.align_finish(align_kind::EXPRESSION); self.colon(&arg.colon); self.space(1); self.case_item_indent = Some((self.dst_column - start) as usize); @@ -2019,11 +2156,18 @@ impl VerylWalker for Emitter { self.scalar_type(&arg.array_type.scalar_type); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); + self.align_start(align_kind::ARRAY); if let Some(ref x) = arg.array_type.array_type_opt { self.space(1); self.array(&x.array); + } else { + let loc = self.align_last_location(align_kind::IDENTIFIER); + self.align_dummy_location(align_kind::ARRAY, loc); } + self.align_finish(align_kind::ARRAY); self.str(";"); self.newline(); if is_tri { @@ -2044,11 +2188,18 @@ impl VerylWalker for Emitter { fn var_declaration(&mut self, arg: &VarDeclaration) { self.scalar_type(&arg.array_type.scalar_type); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); + self.align_start(align_kind::ARRAY); if let Some(ref x) = arg.array_type.array_type_opt { self.space(1); self.array(&x.array); + } else { + let loc = self.align_last_location(align_kind::IDENTIFIER); + self.align_dummy_location(align_kind::ARRAY, loc); } + self.align_finish(align_kind::ARRAY); self.semicolon(&arg.semicolon); } @@ -2061,19 +2212,42 @@ impl VerylWalker for Emitter { if !self.is_implicit_scalar_type(&x.array_type.scalar_type) { self.scalar_type(&x.array_type.scalar_type); self.space(1); + } else { + self.align_start(align_kind::TYPE); + self.align_dummy_location( + align_kind::TYPE, + Some(arg.r#const.const_token.token.into()), + ); + self.align_finish(align_kind::TYPE); } + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); + self.align_start(align_kind::ARRAY); if let Some(ref x) = x.array_type.array_type_opt { self.space(1); self.array(&x.array); + } else { + let loc = self.align_last_location(align_kind::IDENTIFIER); + self.align_dummy_location(align_kind::ARRAY, loc); } + self.align_finish(align_kind::ARRAY); } ConstDeclarationGroup::Type(x) => { + self.align_start(align_kind::TYPE); if !self.is_implicit_type() { self.r#type(&x.r#type); self.space(1); + } else { + self.align_dummy_location( + align_kind::TYPE, + Some(arg.r#const.const_token.token.into()), + ); } + self.align_finish(align_kind::TYPE); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); } } self.space(1); @@ -2089,11 +2263,14 @@ impl VerylWalker for Emitter { self.space(1); self.scalar_type(&arg.array_type.scalar_type); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); - + self.align_finish(align_kind::IDENTIFIER); if let Some(ato) = &arg.array_type.array_type_opt { self.space(1); + self.align_start(align_kind::ARRAY); self.array(&ato.array); + self.align_finish(align_kind::ARRAY); } self.semicolon(&arg.semicolon); } @@ -2236,7 +2413,9 @@ impl VerylWalker for Emitter { self.token(&arg.assign.assign_token.replace("always_comb")); } self.space(1); + self.align_start(align_kind::IDENTIFIER); self.hierarchical_identifier(&arg.hierarchical_identifier); + self.align_finish(align_kind::IDENTIFIER); self.space(1); self.equ(&arg.equ); self.space(1); @@ -2291,7 +2470,9 @@ impl VerylWalker for Emitter { fn modport_item(&mut self, arg: &ModportItem) { self.direction(&arg.direction); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); } /// Semantic action for non-terminal 'EnumDeclaration' @@ -2464,7 +2645,9 @@ impl VerylWalker for Emitter { fn struct_union_item(&mut self, arg: &StructUnionItem) { self.scalar_type(&arg.scalar_type); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); } /// Semantic action for non-terminal 'InitialDeclaration' @@ -2483,17 +2666,24 @@ impl VerylWalker for Emitter { /// Semantic action for non-terminal 'InstDeclaration' fn inst_declaration(&mut self, arg: &InstDeclaration) { - if arg.inst_declaration_opt1.is_none() { - self.single_line = true; - } + self.single_line = arg.inst_declaration_opt1.is_none(); self.token(&arg.inst.inst_token.replace("")); self.scoped_identifier(&arg.scoped_identifier); self.space(1); if let Some(ref x) = arg.inst_declaration_opt0 { - self.inst_parameter(&x.inst_parameter); + // skip align at single line + if self.mode == Mode::Emit || !self.single_line { + self.inst_parameter(&x.inst_parameter); + } self.space(1); } + if self.single_line { + self.align_start(align_kind::IDENTIFIER); + } self.identifier(&arg.identifier); + if self.single_line { + self.align_finish(align_kind::IDENTIFIER); + } if let Some(ref x) = arg.inst_declaration_opt { self.space(1); self.array(&x.array); @@ -2570,14 +2760,25 @@ impl VerylWalker for Emitter { /// Semantic action for non-terminal 'InstParameterItem' fn inst_parameter_item(&mut self, arg: &InstParameterItem) { self.str("."); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.space(1); self.str("("); if let Some(ref x) = arg.inst_parameter_item_opt { self.token(&x.colon.colon_token.replace("")); + self.align_start(align_kind::EXPRESSION); self.expression(&x.expression); + self.align_finish(align_kind::EXPRESSION); } else { + self.align_start(align_kind::EXPRESSION); + self.align_duplicated_token( + align_kind::EXPRESSION, + &arg.identifier.identifier_token, + 0, + ); self.duplicated_token(&arg.identifier.identifier_token, 0); + self.align_finish(align_kind::EXPRESSION); } self.str(")"); } @@ -2614,16 +2815,20 @@ impl VerylWalker for Emitter { /// Semantic action for non-terminal 'InstPortItem' fn inst_port_item(&mut self, arg: &InstPortItem) { self.str("."); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.space(1); self.str("("); if let Some(ref x) = arg.inst_port_item_opt { self.token(&x.colon.colon_token.replace("")); + self.align_start(align_kind::EXPRESSION); let mut stringifier = Stringifier::new(); stringifier.expression(&x.expression); if stringifier.as_str() != "_" { self.expression(&x.expression); } + self.align_finish(align_kind::EXPRESSION); } else { let (prefix, suffix) = if let Ok(found) = symbol_table::resolve(arg.identifier.as_ref()) { @@ -2636,7 +2841,10 @@ impl VerylWalker for Emitter { unreachable!() }; let token = identifier_with_prefix_suffix(&arg.identifier, &prefix, &suffix); + self.align_start(align_kind::EXPRESSION); + self.align_duplicated_token(align_kind::EXPRESSION, &token, 0); self.duplicated_token(&token, 0); + self.align_finish(align_kind::EXPRESSION); } self.str(")"); } @@ -2690,35 +2898,58 @@ impl VerylWalker for Emitter { /// Semantic action for non-terminal 'WithParameterItem' fn with_parameter_item(&mut self, arg: &WithParameterItem) { + self.align_start(align_kind::PARAMETER); match &*arg.with_parameter_item_group { WithParameterItemGroup::Param(x) => self.param(&x.param), WithParameterItemGroup::Const(x) => self.r#const(&x.r#const), }; + self.align_finish(align_kind::PARAMETER); self.space(1); match &*arg.with_parameter_item_group0 { WithParameterItemGroup0::ArrayType(x) => { if !self.is_implicit_scalar_type(&x.array_type.scalar_type) { self.scalar_type(&x.array_type.scalar_type); self.space(1); + } else { + self.align_start(align_kind::TYPE); + let loc = self.align_last_location(align_kind::PARAMETER); + self.align_dummy_location(align_kind::TYPE, loc); + self.align_finish(align_kind::TYPE); } + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); + self.align_start(align_kind::ARRAY); if let Some(ref x) = x.array_type.array_type_opt { self.space(1); self.array(&x.array); + } else { + let loc = self.align_last_location(align_kind::IDENTIFIER); + self.align_dummy_location(align_kind::ARRAY, loc); } + self.align_finish(align_kind::ARRAY); } WithParameterItemGroup0::Type(x) => { + self.align_start(align_kind::TYPE); if !self.is_implicit_type() { self.r#type(&x.r#type); self.space(1); + } else { + let loc = self.align_last_location(align_kind::PARAMETER); + self.align_dummy_location(align_kind::TYPE, loc); } + self.align_finish(align_kind::TYPE); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); } } self.space(1); self.equ(&arg.equ); self.space(1); + self.align_start(align_kind::EXPRESSION); self.expression(&arg.expression); + self.align_finish(align_kind::EXPRESSION); } /// Semantic action for non-terminal 'PortDeclaration' @@ -2779,28 +3010,45 @@ impl VerylWalker for Emitter { } self.scalar_type(&x.array_type.scalar_type); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); + self.align_start(align_kind::ARRAY); if let Some(ref x) = x.array_type.array_type_opt { self.space(1); self.array(&x.array); + } else { + let loc = self.align_last_location(align_kind::IDENTIFIER); + self.align_dummy_location(align_kind::ARRAY, loc); } + self.align_finish(align_kind::ARRAY); self.in_direction_modport = false; } PortDeclarationItemGroup::PortTypeAbstract(x) => { let x = x.port_type_abstract.as_ref(); self.interface(&x.interface); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); + self.align_start(align_kind::ARRAY); if let Some(ref x) = x.port_type_abstract_opt0 { self.space(1); self.array(&x.array); + } else { + let loc = self.align_last_location(align_kind::IDENTIFIER); + self.align_dummy_location(align_kind::ARRAY, loc); } + self.align_finish(align_kind::ARRAY); } } } /// Semantic action for non-terminal 'Direction' fn direction(&mut self, arg: &Direction) { + if !matches!(arg, Direction::Modport(_)) { + self.align_start(align_kind::DIRECTION); + } match arg { Direction::Input(x) => self.input(&x.input), Direction::Output(x) => self.output(&x.output), @@ -2809,6 +3057,9 @@ impl VerylWalker for Emitter { Direction::Modport(_) => (), Direction::Import(x) => self.import(&x.import), }; + if !matches!(arg, Direction::Modport(_)) { + self.align_finish(align_kind::DIRECTION); + } } /// Semantic action for non-terminal 'FunctionDeclaration' @@ -2826,7 +3077,7 @@ impl VerylWalker for Emitter { self.space(1); self.str("automatic"); self.space(1); - if let Some(ref x) = &arg.function_declaration_opt1 { + if let Some(ref x) = arg.function_declaration_opt1 { self.scalar_type(&x.scalar_type); } else { self.str("void"); @@ -3309,40 +3560,50 @@ impl VerylWalker for Emitter { /// Semantic action for non-terminal 'Veryl' fn veryl(&mut self, arg: &Veryl) { - self.in_start_token = true; - self.start(&arg.start); - self.in_start_token = false; - if !arg.start.start_token.comments.is_empty() { - self.newline(); - } - for x in &arg.veryl_list { - let items: Vec = x.description_group.as_ref().into(); - for item in items { - if let DescriptionItem::ImportDeclaration(x) = item { - let mut emitter = Emitter { - project_name: self.project_name, - build_opt: self.build_opt.clone(), - format_opt: self.format_opt.clone(), - ..Default::default() - }; - emitter.import_declaration(&x.import_declaration); - self.file_scope_import.push(emitter.as_str().to_string()); + match self.mode { + Mode::Emit => { + self.in_start_token = true; + self.start(&arg.start); + self.in_start_token = false; + if !arg.start.start_token.comments.is_empty() { + self.newline(); + } + for x in &arg.veryl_list { + let items: Vec = x.description_group.as_ref().into(); + for item in items { + if let DescriptionItem::ImportDeclaration(x) = item { + let mut emitter = Emitter { + project_name: self.project_name, + build_opt: self.build_opt.clone(), + format_opt: self.format_opt.clone(), + ..Default::default() + }; + emitter.import_declaration(&x.import_declaration); + self.file_scope_import.push(emitter.as_str().to_string()); + } + } + } + for (i, x) in arg.veryl_list.iter().enumerate() { + if i != 0 { + self.newline(); + } + self.description_group(&x.description_group); } - } - } - for (i, x) in arg.veryl_list.iter().enumerate() { - if i != 0 { self.newline(); - } - self.description_group(&x.description_group); - } - self.newline(); - // build map and insert link to map - if self.build_opt.sourcemap_target != SourceMapTarget::None { - self.source_map.as_mut().unwrap().build(); - self.str(&self.source_map.as_ref().unwrap().get_link()); - self.newline(); + // build map and insert link to map + if self.build_opt.sourcemap_target != SourceMapTarget::None { + self.source_map.as_mut().unwrap().build(); + self.str(&self.source_map.as_ref().unwrap().get_link()); + self.newline(); + } + } + Mode::Align => { + self.start(&arg.start); + for x in &arg.veryl_list { + self.description_group(&x.description_group); + } + } } } } diff --git a/crates/emitter/src/lib.rs b/crates/emitter/src/lib.rs index 5222e9c1..e37ebf72 100644 --- a/crates/emitter/src/lib.rs +++ b/crates/emitter/src/lib.rs @@ -1,4 +1,3 @@ -pub mod aligner; pub mod emitter; pub use emitter::Emitter; #[cfg(test)] diff --git a/crates/emitter/src/tests.rs b/crates/emitter/src/tests.rs index 96237f58..a29c54d9 100644 --- a/crates/emitter/src/tests.rs +++ b/crates/emitter/src/tests.rs @@ -264,10 +264,10 @@ fn expand_case_statement() { a = 1; a = 1; end - (x) ==? (3), (x) ==? (4 ): a = 1; - ((x) >= (5)) && ((x) <= (7 )): a = 1; - (x) ==? (y - 1 ): a = 1; - default: a = 1; + (x) ==? (3), (x) ==? (4 ): a = 1; + ((x) >= (5)) && ((x) <= (7)): a = 1; + (x) ==? (y - 1 ): a = 1; + default : a = 1; endcase end endmodule diff --git a/crates/formatter/Cargo.toml b/crates/formatter/Cargo.toml index 82fca2b2..e61e4d67 100644 --- a/crates/formatter/Cargo.toml +++ b/crates/formatter/Cargo.toml @@ -13,5 +13,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +veryl-aligner = {version = "0.13.2", path = "../aligner"} veryl-metadata = {version = "0.13.2", path = "../metadata"} veryl-parser = {version = "0.13.2", path = "../parser"} diff --git a/crates/formatter/src/aligner.rs b/crates/formatter/src/aligner.rs deleted file mode 100644 index bc654d59..00000000 --- a/crates/formatter/src/aligner.rs +++ /dev/null @@ -1,907 +0,0 @@ -use std::collections::HashMap; -use veryl_parser::veryl_grammar_trait::*; -use veryl_parser::veryl_token::{Token, VerylToken}; -use veryl_parser::veryl_walker::VerylWalker; - -#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)] -pub struct Location { - pub line: u32, - pub column: u32, - pub length: u32, -} - -impl From<&Token> for Location { - fn from(x: &Token) -> Self { - Self { - line: x.line, - column: x.column, - length: x.length, - } - } -} - -impl From for Location { - fn from(x: Token) -> Self { - Self { - line: x.line, - column: x.column, - length: x.length, - } - } -} - -#[derive(Default)] -pub struct Align { - enable: bool, - index: usize, - max_width: u32, - width: u32, - line: u32, - rest: Vec<(Location, u32)>, - additions: HashMap, - last_location: Option, -} - -impl Align { - fn finish_group(&mut self) { - for (loc, width) in &self.rest { - self.additions.insert(*loc, self.max_width - width); - } - self.rest.clear(); - self.max_width = 0; - } - - fn finish_item(&mut self) { - self.enable = false; - if let Some(loc) = self.last_location { - if loc.line - self.line > 1 { - self.finish_group(); - } - self.max_width = u32::max(self.max_width, self.width); - self.line = loc.line; - self.rest.push((loc, self.width)); - - self.width = 0; - self.index += 1; - } - } - - fn start_item(&mut self) { - self.enable = true; - self.width = 0; - } - - fn token(&mut self, x: &VerylToken) { - if self.enable { - self.width += x.token.length; - let loc: Location = x.token.into(); - self.last_location = Some(loc); - } - } - - fn dummy_location(&mut self, x: Location) { - if self.enable { - self.width += 0; // 0 length token - self.last_location = Some(x); - } - } - - fn dummy_token(&mut self, x: &VerylToken) { - if self.enable { - self.width += 0; // 0 length token - let loc: Location = x.token.into(); - self.last_location = Some(loc); - } - } - - fn space(&mut self, x: usize) { - if self.enable { - self.width += x as u32; - } - } -} - -mod align_kind { - pub const IDENTIFIER: usize = 0; - pub const TYPE: usize = 1; - pub const EXPRESSION: usize = 2; - pub const WIDTH: usize = 3; - pub const ARRAY: usize = 4; - pub const ASSIGNMENT: usize = 5; - pub const PARAMETER: usize = 6; - pub const DIRECTION: usize = 7; - pub const CLOCK_DOMAIN: usize = 8; -} - -#[derive(Default)] -pub struct Aligner { - pub additions: HashMap, - aligns: [Align; 9], - in_expression: Vec<()>, -} - -impl Aligner { - pub fn new() -> Self { - Default::default() - } - - pub fn align(&mut self, input: &Veryl) { - self.veryl(input); - self.finish_group(); - for align in &self.aligns { - for (x, y) in &align.additions { - self.additions - .entry(*x) - .and_modify(|val| *val += *y) - .or_insert(*y); - } - } - } - - fn finish_group(&mut self) { - for i in 0..self.aligns.len() { - self.aligns[i].finish_group(); - } - } - - fn reset_align(&mut self) { - self.finish_group(); - } - - fn insert(&mut self, token: &VerylToken, width: usize) { - let loc: Location = token.token.into(); - self.additions - .entry(loc) - .and_modify(|val| *val += width as u32) - .or_insert(width as u32); - } - - fn space(&mut self, repeat: usize) { - for i in 0..self.aligns.len() { - self.aligns[i].space(repeat); - } - } -} - -impl VerylWalker for Aligner { - /// Semantic action for non-terminal 'VerylToken' - fn veryl_token(&mut self, arg: &VerylToken) { - for i in 0..self.aligns.len() { - self.aligns[i].token(arg); - } - } - - /// Semantic action for non-terminal 'Expression' - // Add `#[inline(never)]` to `expression*` as a workaround for long time compilation - // https://github.com/rust-lang/rust/issues/106211 - #[inline(never)] - fn expression(&mut self, arg: &Expression) { - self.in_expression.push(()); - self.expression01(&arg.expression01); - for x in &arg.expression_list { - self.space(1); - self.operator01(&x.operator01); - self.space(1); - self.expression01(&x.expression01); - } - self.in_expression.pop(); - } - - /// Semantic action for non-terminal 'Expression01' - #[inline(never)] - fn expression01(&mut self, arg: &Expression01) { - self.expression02(&arg.expression02); - for x in &arg.expression01_list { - self.space(1); - self.operator02(&x.operator02); - self.space(1); - self.expression02(&x.expression02); - } - } - - /// Semantic action for non-terminal 'Expression02' - #[inline(never)] - fn expression02(&mut self, arg: &Expression02) { - self.expression03(&arg.expression03); - for x in &arg.expression02_list { - self.space(1); - self.operator03(&x.operator03); - self.space(1); - self.expression03(&x.expression03); - } - } - - /// Semantic action for non-terminal 'Expression03' - #[inline(never)] - fn expression03(&mut self, arg: &Expression03) { - self.expression04(&arg.expression04); - for x in &arg.expression03_list { - self.space(1); - self.operator04(&x.operator04); - self.space(1); - self.expression04(&x.expression04); - } - } - - /// Semantic action for non-terminal 'Expression04' - #[inline(never)] - fn expression04(&mut self, arg: &Expression04) { - self.expression05(&arg.expression05); - for x in &arg.expression04_list { - self.space(1); - self.operator05(&x.operator05); - self.space(1); - self.expression05(&x.expression05); - } - } - - /// Semantic action for non-terminal 'Expression05' - #[inline(never)] - fn expression05(&mut self, arg: &Expression05) { - self.expression06(&arg.expression06); - for x in &arg.expression05_list { - self.space(1); - self.operator06(&x.operator06); - self.space(1); - self.expression06(&x.expression06); - } - } - - /// Semantic action for non-terminal 'Expression06' - #[inline(never)] - fn expression06(&mut self, arg: &Expression06) { - self.expression07(&arg.expression07); - for x in &arg.expression06_list { - self.space(1); - self.operator07(&x.operator07); - self.space(1); - self.expression07(&x.expression07); - } - } - - /// Semantic action for non-terminal 'Expression07' - #[inline(never)] - fn expression07(&mut self, arg: &Expression07) { - self.expression08(&arg.expression08); - for x in &arg.expression07_list { - self.space(1); - self.operator08(&x.operator08); - self.space(1); - self.expression08(&x.expression08); - } - } - - /// Semantic action for non-terminal 'Expression08' - #[inline(never)] - fn expression08(&mut self, arg: &Expression08) { - self.expression09(&arg.expression09); - for x in &arg.expression08_list { - self.space(1); - self.operator09(&x.operator09); - self.space(1); - self.expression09(&x.expression09); - } - } - - /// Semantic action for non-terminal 'Expression09' - #[inline(never)] - fn expression09(&mut self, arg: &Expression09) { - self.expression10(&arg.expression10); - for x in &arg.expression09_list { - self.space(1); - match &*x.expression09_list_group { - Expression09ListGroup::Operator10(x) => self.operator10(&x.operator10), - Expression09ListGroup::Star(x) => self.star(&x.star), - } - self.space(1); - self.expression10(&x.expression10); - } - } - - /// Semantic action for non-terminal 'Expression10' - #[inline(never)] - fn expression10(&mut self, arg: &Expression10) { - self.expression11(&arg.expression11); - for x in &arg.expression10_list { - self.space(1); - self.operator11(&x.operator11); - self.space(1); - self.expression11(&x.expression11); - } - } - - /// Semantic action for non-terminal 'Expression11' - #[inline(never)] - fn expression11(&mut self, arg: &Expression11) { - self.expression12(&arg.expression12); - if let Some(x) = &arg.expression11_opt { - self.space(1); - self.r#as(&x.r#as); - self.space(1); - self.casting_type(&x.casting_type); - } - } - - /// Semantic action for non-terminal 'ArgumentList' - fn argument_list(&mut self, arg: &ArgumentList) { - self.argument_item(&arg.argument_item); - for x in &arg.argument_list_list { - self.comma(&x.comma); - self.space(1); - self.argument_item(&x.argument_item); - } - if let Some(ref x) = arg.argument_list_opt { - self.comma(&x.comma); - } - } - - /// Semantic action for non-terminal 'CaseExpression' - fn case_expression(&mut self, arg: &CaseExpression) { - self.case(&arg.case); - self.expression(&arg.expression); - self.l_brace(&arg.l_brace); - self.aligns[align_kind::EXPRESSION].start_item(); - self.case_condition(&arg.case_condition); - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&arg.colon); - self.expression(&arg.expression0); - self.comma(&arg.comma); - for x in &arg.case_expression_list { - self.aligns[align_kind::EXPRESSION].start_item(); - self.case_condition(&x.case_condition); - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&x.colon); - self.expression(&x.expression); - self.comma(&x.comma); - } - self.aligns[align_kind::EXPRESSION].start_item(); - self.defaul(&arg.defaul); - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&arg.colon0); - self.expression(&arg.expression1); - if let Some(ref x) = arg.case_expression_opt { - self.comma(&x.comma); - } - self.r_brace(&arg.r_brace); - } - - /// Semantic action for non-terminal 'SwitchExpression' - fn switch_expression(&mut self, arg: &SwitchExpression) { - self.switch(&arg.switch); - self.l_brace(&arg.l_brace); - self.aligns[align_kind::EXPRESSION].start_item(); - self.switch_condition(&arg.switch_condition); - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&arg.colon); - self.expression(&arg.expression); - self.comma(&arg.comma); - for x in &arg.switch_expression_list { - self.aligns[align_kind::EXPRESSION].start_item(); - self.switch_condition(&x.switch_condition); - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&x.colon); - self.expression(&x.expression); - self.comma(&x.comma); - } - self.aligns[align_kind::EXPRESSION].start_item(); - self.defaul(&arg.defaul); - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&arg.colon0); - self.expression(&arg.expression0); - if let Some(ref x) = arg.switch_expression_opt { - self.comma(&x.comma); - } - self.r_brace(&arg.r_brace); - } - - /// Semantic action for non-terminal 'SelectOperator' - fn select_operator(&mut self, arg: &SelectOperator) { - match arg { - SelectOperator::Colon(x) => self.colon(&x.colon), - SelectOperator::PlusColon(x) => self.plus_colon(&x.plus_colon), - SelectOperator::MinusColon(x) => self.minus_colon(&x.minus_colon), - SelectOperator::Step(x) => { - self.space(1); - self.step(&x.step); - self.space(1); - } - } - } - - /// Semantic action for non-terminal 'Width' - fn width(&mut self, arg: &Width) { - self.l_angle(&arg.l_angle); - self.expression(&arg.expression); - for x in &arg.width_list { - self.comma(&x.comma); - self.space(1); - self.expression(&x.expression); - } - self.r_angle(&arg.r_angle); - } - - /// Semantic action for non-terminal 'Array' - fn array(&mut self, arg: &Array) { - self.l_bracket(&arg.l_bracket); - self.expression(&arg.expression); - for x in &arg.array_list { - self.comma(&x.comma); - self.space(1); - self.expression(&x.expression); - } - self.r_bracket(&arg.r_bracket); - } - - /// Semantic action for non-terminal 'ScalarType' - fn scalar_type(&mut self, arg: &ScalarType) { - if !self.in_expression.is_empty() { - return; - } - - self.aligns[align_kind::TYPE].start_item(); - for x in &arg.scalar_type_list { - self.type_modifier(&x.type_modifier); - self.space(1); - } - match &*arg.scalar_type_group { - ScalarTypeGroup::UserDefinedTypeScalarTypeOpt(x) => { - self.user_defined_type(&x.user_defined_type); - self.aligns[align_kind::TYPE].finish_item(); - self.aligns[align_kind::WIDTH].start_item(); - if let Some(ref x) = x.scalar_type_opt { - self.width(&x.width); - } else { - let loc = self.aligns[align_kind::TYPE].last_location; - let loc = loc.unwrap(); - self.aligns[align_kind::WIDTH].dummy_location(loc); - } - } - ScalarTypeGroup::FactorType(x) => match x.factor_type.factor_type_group.as_ref() { - FactorTypeGroup::VariableTypeFactorTypeOpt(x) => { - self.variable_type(&x.variable_type); - self.aligns[align_kind::TYPE].finish_item(); - self.aligns[align_kind::WIDTH].start_item(); - if let Some(ref x) = x.factor_type_opt { - self.width(&x.width); - } else { - let loc = self.aligns[align_kind::TYPE].last_location; - let loc = loc.unwrap(); - self.aligns[align_kind::WIDTH].dummy_location(loc); - } - } - FactorTypeGroup::FixedType(x) => { - self.fixed_type(&x.fixed_type); - self.aligns[align_kind::TYPE].finish_item(); - self.aligns[align_kind::WIDTH].start_item(); - let loc = self.aligns[align_kind::TYPE].last_location; - self.aligns[align_kind::WIDTH].dummy_location(loc.unwrap()); - } - }, - } - self.aligns[align_kind::WIDTH].finish_item(); - } - - /// Semantic action for non-terminal 'ArrayType' - fn array_type(&mut self, arg: &ArrayType) { - self.scalar_type(&arg.scalar_type); - self.aligns[align_kind::ARRAY].start_item(); - if let Some(ref x) = arg.array_type_opt { - self.space(1); - self.array(&x.array); - } else { - let loc = self.aligns[align_kind::WIDTH].last_location; - self.aligns[align_kind::ARRAY].dummy_location(loc.unwrap()); - } - self.aligns[align_kind::ARRAY].finish_item(); - } - - /// Semantic action for non-terminal 'LetStatement' - fn let_statement(&mut self, arg: &LetStatement) { - self.r#let(&arg.r#let); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - if let Some(ref x) = arg.let_statement_opt { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.clock_domain(&x.clock_domain); - self.space(1); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } else { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.aligns[align_kind::CLOCK_DOMAIN].dummy_token(&arg.colon.colon_token); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } - self.array_type(&arg.array_type); - self.equ(&arg.equ); - self.expression(&arg.expression); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'IdentifierStatement' - fn identifier_statement(&mut self, arg: &IdentifierStatement) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.expression_identifier(&arg.expression_identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - match &*arg.identifier_statement_group { - IdentifierStatementGroup::FunctionCall(x) => { - self.function_call(&x.function_call); - } - IdentifierStatementGroup::Assignment(x) => { - self.assignment(&x.assignment); - } - } - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'Assignment' - fn assignment(&mut self, arg: &Assignment) { - self.aligns[align_kind::ASSIGNMENT].start_item(); - match &*arg.assignment_group { - AssignmentGroup::Equ(x) => self.equ(&x.equ), - AssignmentGroup::AssignmentOperator(x) => { - self.assignment_operator(&x.assignment_operator) - } - } - self.aligns[align_kind::ASSIGNMENT].finish_item(); - self.expression(&arg.expression); - } - - /// Semantic action for non-terminal 'CaseItem' - fn case_item(&mut self, arg: &CaseItem) { - self.aligns[align_kind::EXPRESSION].start_item(); - match &*arg.case_item_group { - CaseItemGroup::CaseCondition(x) => self.case_condition(&x.case_condition), - CaseItemGroup::Defaul(x) => self.defaul(&x.defaul), - } - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&arg.colon); - match &*arg.case_item_group0 { - CaseItemGroup0::Statement(x) => self.statement(&x.statement), - CaseItemGroup0::StatementBlock(x) => self.statement_block(&x.statement_block), - } - } - - /// Semantic action for non-terminal 'CaseCondition' - fn case_condition(&mut self, arg: &CaseCondition) { - self.range_item(&arg.range_item); - for x in &arg.case_condition_list { - self.comma(&x.comma); - self.space(1); - self.range_item(&x.range_item); - } - } - - /// Semantic action for non-terminal 'SwitchItem' - fn switch_item(&mut self, arg: &SwitchItem) { - self.aligns[align_kind::EXPRESSION].start_item(); - match &*arg.switch_item_group { - SwitchItemGroup::SwitchCondition(x) => self.switch_condition(&x.switch_condition), - SwitchItemGroup::Defaul(x) => self.defaul(&x.defaul), - } - self.aligns[align_kind::EXPRESSION].finish_item(); - self.colon(&arg.colon); - match &*arg.switch_item_group0 { - SwitchItemGroup0::Statement(x) => self.statement(&x.statement), - SwitchItemGroup0::StatementBlock(x) => self.statement_block(&x.statement_block), - } - } - - /// Semantic action for non-terminal 'SwitchCondition' - fn switch_condition(&mut self, arg: &SwitchCondition) { - self.expression(&arg.expression); - for x in &arg.switch_condition_list { - self.comma(&x.comma); - self.space(1); - self.expression(&x.expression); - } - } - - /// Semantic action for non-terminal 'LetDeclaration' - fn let_declaration(&mut self, arg: &LetDeclaration) { - self.r#let(&arg.r#let); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - if let Some(ref x) = arg.let_declaration_opt { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.clock_domain(&x.clock_domain); - self.space(1); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } else { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.aligns[align_kind::CLOCK_DOMAIN].dummy_token(&arg.colon.colon_token); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } - self.array_type(&arg.array_type); - self.equ(&arg.equ); - self.expression(&arg.expression); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'VarDeclaration' - fn var_declaration(&mut self, arg: &VarDeclaration) { - self.var(&arg.var); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - if let Some(ref x) = arg.var_declaration_opt { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.clock_domain(&x.clock_domain); - self.space(1); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } else { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.aligns[align_kind::CLOCK_DOMAIN].dummy_token(&arg.colon.colon_token); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } - self.array_type(&arg.array_type); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'ConstDeclaration' - fn const_declaration(&mut self, arg: &ConstDeclaration) { - self.r#const(&arg.r#const); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - match &*arg.const_declaration_group { - ConstDeclarationGroup::ArrayType(x) => { - self.array_type(&x.array_type); - } - ConstDeclarationGroup::Type(x) => { - self.aligns[align_kind::TYPE].start_item(); - self.r#type(&x.r#type); - self.aligns[align_kind::TYPE].finish_item(); - } - } - self.equ(&arg.equ); - self.expression(&arg.expression); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'TypeDefDeclaration' - fn type_def_declaration(&mut self, arg: &TypeDefDeclaration) { - self.r#type(&arg.r#type); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.equ(&arg.equ); - self.array_type(&arg.array_type); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'AssignDeclaration' - fn assign_declaration(&mut self, arg: &AssignDeclaration) { - self.assign(&arg.assign); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.hierarchical_identifier(&arg.hierarchical_identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.equ(&arg.equ); - self.expression(&arg.expression); - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'ModportItem' - fn modport_item(&mut self, arg: &ModportItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - self.direction(&arg.direction); - } - - /// Semantic action for non-terminal 'StructItem' - fn struct_union_item(&mut self, arg: &StructUnionItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - self.scalar_type(&arg.scalar_type); - } - - /// Semantic action for non-terminal 'InstDeclaration' - fn inst_declaration(&mut self, arg: &InstDeclaration) { - let single_line = arg.inst_declaration_opt1.is_none(); - self.inst(&arg.inst); - if single_line { - self.aligns[align_kind::IDENTIFIER].start_item(); - } - self.identifier(&arg.identifier); - if single_line { - self.aligns[align_kind::IDENTIFIER].finish_item(); - } - self.colon(&arg.colon); - self.scoped_identifier(&arg.scoped_identifier); - // skip align at single line - if single_line { - return; - } - if let Some(ref x) = arg.inst_declaration_opt { - self.array(&x.array); - } - if let Some(ref x) = arg.inst_declaration_opt0 { - self.inst_parameter(&x.inst_parameter); - } - if let Some(ref x) = arg.inst_declaration_opt1 { - self.l_paren(&x.l_paren); - if let Some(ref x) = x.inst_declaration_opt2 { - self.inst_port_list(&x.inst_port_list); - } - self.r_paren(&x.r_paren); - } - self.semicolon(&arg.semicolon); - } - - /// Semantic action for non-terminal 'InstParameterItem' - fn inst_parameter_item(&mut self, arg: &InstParameterItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - if let Some(ref x) = arg.inst_parameter_item_opt { - self.colon(&x.colon); - self.space(1); - self.aligns[align_kind::EXPRESSION].start_item(); - self.expression(&x.expression); - self.aligns[align_kind::EXPRESSION].finish_item(); - } else { - self.insert(&arg.identifier.identifier_token, ": ".len()); - self.aligns[align_kind::EXPRESSION].start_item(); - self.aligns[align_kind::EXPRESSION].dummy_token(&arg.identifier.identifier_token); - self.aligns[align_kind::EXPRESSION].finish_item(); - } - } - - /// Semantic action for non-terminal 'InstPortItem' - fn inst_port_item(&mut self, arg: &InstPortItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - if let Some(ref x) = arg.inst_port_item_opt { - self.colon(&x.colon); - self.space(1); - self.aligns[align_kind::EXPRESSION].start_item(); - self.expression(&x.expression); - self.aligns[align_kind::EXPRESSION].finish_item(); - } else { - self.insert(&arg.identifier.identifier_token, ": ".len()); - self.aligns[align_kind::EXPRESSION].start_item(); - self.aligns[align_kind::EXPRESSION].dummy_token(&arg.identifier.identifier_token); - self.aligns[align_kind::EXPRESSION].finish_item(); - } - } - - /// Semantic action for non-terminal 'WithParameterItem' - fn with_parameter_item(&mut self, arg: &WithParameterItem) { - self.aligns[align_kind::PARAMETER].start_item(); - match &*arg.with_parameter_item_group { - WithParameterItemGroup::Param(x) => self.param(&x.param), - WithParameterItemGroup::Const(x) => self.r#const(&x.r#const), - }; - self.aligns[align_kind::PARAMETER].finish_item(); - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - match &*arg.with_parameter_item_group0 { - WithParameterItemGroup0::ArrayType(x) => { - self.array_type(&x.array_type); - } - WithParameterItemGroup0::Type(x) => { - self.aligns[align_kind::TYPE].start_item(); - self.r#type(&x.r#type); - self.aligns[align_kind::TYPE].finish_item(); - } - } - self.equ(&arg.equ); - self.aligns[align_kind::EXPRESSION].start_item(); - self.expression(&arg.expression); - self.aligns[align_kind::EXPRESSION].finish_item(); - } - - /// Semantic action for non-terminal 'WithGenericArgumentList' - fn with_generic_argument_list(&mut self, arg: &WithGenericArgumentList) { - self.with_generic_argument_item(&arg.with_generic_argument_item); - for x in &arg.with_generic_argument_list_list { - self.comma(&x.comma); - self.space(1); - self.with_generic_argument_item(&x.with_generic_argument_item); - } - if let Some(ref x) = arg.with_generic_argument_list_opt { - self.comma(&x.comma); - } - } - - /// Semantic action for non-terminal 'PortDeclarationItem' - fn port_declaration_item(&mut self, arg: &PortDeclarationItem) { - self.aligns[align_kind::IDENTIFIER].start_item(); - self.identifier(&arg.identifier); - self.aligns[align_kind::IDENTIFIER].finish_item(); - self.colon(&arg.colon); - match &*arg.port_declaration_item_group { - PortDeclarationItemGroup::PortTypeConcrete(x) => { - let x = x.port_type_concrete.as_ref(); - self.direction(&x.direction); - if let Some(ref x) = x.port_type_concrete_opt { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.clock_domain(&x.clock_domain); - self.space(1); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } else { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - match x.direction.as_ref() { - Direction::Input(x) => { - self.aligns[align_kind::CLOCK_DOMAIN].dummy_token(&x.input.input_token) - } - Direction::Output(x) => self.aligns[align_kind::CLOCK_DOMAIN] - .dummy_token(&x.output.output_token), - Direction::Inout(x) => { - self.aligns[align_kind::CLOCK_DOMAIN].dummy_token(&x.inout.inout_token) - } - Direction::Ref(x) => { - self.aligns[align_kind::CLOCK_DOMAIN].dummy_token(&x.r#ref.ref_token) - } - Direction::Modport(x) => self.aligns[align_kind::CLOCK_DOMAIN] - .dummy_token(&x.modport.modport_token), - Direction::Import(x) => self.aligns[align_kind::CLOCK_DOMAIN] - .dummy_token(&x.import.import_token), - } - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } - self.array_type(&x.array_type); - } - PortDeclarationItemGroup::PortTypeAbstract(x) => { - let x = x.port_type_abstract.as_ref(); - if let Some(ref x) = x.port_type_abstract_opt { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.clock_domain(&x.clock_domain); - self.space(1); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } else { - self.aligns[align_kind::CLOCK_DOMAIN].start_item(); - self.aligns[align_kind::CLOCK_DOMAIN].dummy_token(&arg.colon.colon_token); - self.aligns[align_kind::CLOCK_DOMAIN].finish_item(); - } - self.interface(&x.interface); - if let Some(ref x) = x.port_type_abstract_opt0 { - self.array(&x.array); - } - } - } - } - - /// Semantic action for non-terminal 'Direction' - fn direction(&mut self, arg: &Direction) { - self.aligns[align_kind::DIRECTION].start_item(); - match arg { - Direction::Input(x) => self.input(&x.input), - Direction::Output(x) => self.output(&x.output), - Direction::Inout(x) => self.inout(&x.inout), - Direction::Ref(x) => self.r#ref(&x.r#ref), - Direction::Modport(x) => self.modport(&x.modport), - Direction::Import(x) => self.import(&x.import), - }; - self.aligns[align_kind::DIRECTION].finish_item(); - } - - /// Semantic action for non-terminal 'FunctionDeclaration' - fn function_declaration(&mut self, arg: &FunctionDeclaration) { - self.function(&arg.function); - self.identifier(&arg.identifier); - if let Some(ref x) = arg.function_declaration_opt { - self.with_generic_parameter(&x.with_generic_parameter); - } - if let Some(ref x) = arg.function_declaration_opt0 { - self.port_declaration(&x.port_declaration); - } - if let Some(ref x) = arg.function_declaration_opt1 { - self.minus_g_t(&x.minus_g_t); - self.scalar_type(&x.scalar_type); - self.reset_align(); - } - self.statement_block(&arg.statement_block); - } -} diff --git a/crates/formatter/src/formatter.rs b/crates/formatter/src/formatter.rs index fc3411f7..280bbe9b 100644 --- a/crates/formatter/src/formatter.rs +++ b/crates/formatter/src/formatter.rs @@ -1,4 +1,4 @@ -use crate::aligner::{Aligner, Location}; +use veryl_aligner::{align_kind, Aligner, Location}; use veryl_metadata::{Format, Metadata}; use veryl_parser::resource_table; use veryl_parser::veryl_grammar_trait::*; @@ -10,7 +10,14 @@ const NEWLINE: &str = "\r\n"; #[cfg(not(target_os = "windows"))] const NEWLINE: &str = "\n"; +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Mode { + Emit, + Align, +} + pub struct Formatter { + mode: Mode, format_opt: Format, string: String, indent: usize, @@ -21,11 +28,14 @@ pub struct Formatter { single_line: bool, adjust_line: bool, case_item_indent: Vec, + in_scalar_type: bool, + in_expression: Vec<()>, } impl Default for Formatter { fn default() -> Self { Self { + mode: Mode::Emit, format_opt: Format::default(), string: String::new(), indent: 0, @@ -36,6 +46,8 @@ impl Default for Formatter { single_line: false, adjust_line: false, case_item_indent: Vec::new(), + in_scalar_type: false, + in_expression: Vec::new(), } } } @@ -49,7 +61,11 @@ impl Formatter { } pub fn format(&mut self, input: &Veryl) { - self.aligner.align(input); + self.mode = Mode::Align; + self.veryl(input); + self.aligner.finish_group(); + self.aligner.gather_additions(); + self.mode = Mode::Emit; self.veryl(input); } @@ -62,10 +78,21 @@ impl Formatter { } fn str(&mut self, x: &str) { - self.string.push_str(x); + match self.mode { + Mode::Emit => { + self.string.push_str(x); + } + Mode::Align => { + self.aligner.space(x.len()); + } + } } fn unindent(&mut self) { + if self.mode == Mode::Align { + return; + } + let indent_width = self.indent * self.format_opt.indent_width + self.case_item_indent.last().unwrap_or(&0); if self.string.ends_with(&" ".repeat(indent_width)) { @@ -74,12 +101,20 @@ impl Formatter { } fn indent(&mut self) { + if self.mode == Mode::Align { + return; + } + let indent_width = self.indent * self.format_opt.indent_width + self.case_item_indent.last().unwrap_or(&0); self.str(&" ".repeat(indent_width)); } fn newline_push(&mut self) { + if self.mode == Mode::Align { + return; + } + self.unindent(); if !self.consumed_next_newline { self.str(NEWLINE); @@ -92,6 +127,10 @@ impl Formatter { } fn newline_pop(&mut self) { + if self.mode == Mode::Align { + return; + } + self.unindent(); if !self.consumed_next_newline { self.str(NEWLINE); @@ -104,6 +143,10 @@ impl Formatter { } fn newline(&mut self) { + if self.mode == Mode::Align { + return; + } + self.unindent(); if !self.consumed_next_newline { self.str(NEWLINE); @@ -154,38 +197,45 @@ impl Formatter { } fn process_token(&mut self, x: &VerylToken, will_push: bool) { - self.push_token(&x.token); + match self.mode { + Mode::Emit => { + self.push_token(&x.token); - let loc: Location = x.token.into(); - if let Some(width) = self.aligner.additions.get(&loc) { - self.space(*width as usize); - } + let loc: Location = x.token.into(); + if let Some(width) = self.aligner.additions.get(&loc) { + self.space(*width as usize); + } - // temporary indent to adjust indent of comments with the next push - if will_push { - self.indent += 1; - } - // detect line comment newline which will consume the next newline - self.consumed_next_newline = false; - for x in &x.comments { - // insert space between comments in the same line - if x.line == self.line && !self.in_start_token { - self.space(1); + // temporary indent to adjust indent of comments with the next push + if will_push { + self.indent += 1; + } + // detect line comment newline which will consume the next newline + self.consumed_next_newline = false; + for x in &x.comments { + // insert space between comments in the same line + if x.line == self.line && !self.in_start_token { + self.space(1); + } + for _ in 0..x.line - self.line { + self.unindent(); + self.str(NEWLINE); + self.indent(); + } + self.push_token(x); + } + if will_push { + self.indent -= 1; + } + if self.consumed_next_newline { + self.unindent(); + self.str(NEWLINE); + self.indent(); + } } - for _ in 0..x.line - self.line { - self.unindent(); - self.str(NEWLINE); - self.indent(); + Mode::Align => { + self.aligner.token(x); } - self.push_token(x); - } - if will_push { - self.indent -= 1; - } - if self.consumed_next_newline { - self.unindent(); - self.str(NEWLINE); - self.indent(); } } @@ -196,6 +246,51 @@ impl Formatter { fn token_will_push(&mut self, x: &VerylToken) { self.process_token(x, true) } + + fn align_start(&mut self, kind: usize) { + if self.mode == Mode::Align { + self.aligner.aligns[kind].start_item(); + } + } + + fn align_finish(&mut self, kind: usize) { + if self.mode == Mode::Align { + self.aligner.aligns[kind].finish_item(); + } + } + + fn align_last_location(&mut self, kind: usize) -> Option { + self.aligner.aligns[kind].last_location + } + + fn align_dummy_location(&mut self, kind: usize, loc: Option) { + if self.mode == Mode::Align { + self.aligner.aligns[kind].dummy_location(loc.unwrap()); + } + } + + fn align_dummy_token(&mut self, kind: usize, token: &VerylToken) { + if self.mode == Mode::Align { + self.aligner.aligns[kind].dummy_token(token); + } + } + + fn align_reset(&mut self) { + if self.mode == Mode::Align { + self.aligner.finish_group(); + } + } + + fn align_insert(&mut self, token: &VerylToken, width: usize) { + if self.mode == Mode::Align { + let loc: Location = token.token.into(); + self.aligner + .additions + .entry(loc) + .and_modify(|val| *val += width as u32) + .or_insert(width as u32); + } + } } impl VerylWalker for Formatter { @@ -209,6 +304,7 @@ impl VerylWalker for Formatter { // https://github.com/rust-lang/rust/issues/106211 #[inline(never)] fn expression(&mut self, arg: &Expression) { + self.in_expression.push(()); self.expression01(&arg.expression01); for x in &arg.expression_list { self.space(1); @@ -216,6 +312,7 @@ impl VerylWalker for Formatter { self.space(1); self.expression01(&x.expression01); } + self.in_expression.pop(); } /// Semantic action for non-terminal 'Expression01' @@ -468,21 +565,27 @@ impl VerylWalker for Formatter { self.space(1); self.token_will_push(&arg.l_brace.l_brace_token); self.newline_push(); + self.align_start(align_kind::EXPRESSION); self.case_condition(&arg.case_condition); + self.align_finish(align_kind::EXPRESSION); self.colon(&arg.colon); self.space(1); self.expression(&arg.expression0); self.comma(&arg.comma); self.newline(); for x in &arg.case_expression_list { + self.align_start(align_kind::EXPRESSION); self.case_condition(&x.case_condition); + self.align_finish(align_kind::EXPRESSION); self.colon(&x.colon); self.space(1); self.expression(&x.expression); self.comma(&x.comma); self.newline(); } + self.align_start(align_kind::EXPRESSION); self.defaul(&arg.defaul); + self.align_finish(align_kind::EXPRESSION); self.colon(&arg.colon0); self.space(1); self.expression(&arg.expression1); @@ -501,22 +604,28 @@ impl VerylWalker for Formatter { self.space(1); self.token_will_push(&arg.l_brace.l_brace_token); self.newline_push(); + self.align_start(align_kind::EXPRESSION); self.switch_condition(&arg.switch_condition); + self.align_finish(align_kind::EXPRESSION); self.colon(&arg.colon); self.space(1); self.expression(&arg.expression); self.comma(&arg.comma); self.newline(); for x in &arg.switch_expression_list { + self.align_start(align_kind::EXPRESSION); self.switch_condition(&x.switch_condition); + self.align_finish(align_kind::EXPRESSION); self.colon(&x.colon); self.space(1); self.expression(&x.expression); self.comma(&x.comma); self.newline(); } + self.align_start(align_kind::EXPRESSION); self.defaul(&arg.defaul); - self.colon(&arg.colon); + self.align_finish(align_kind::EXPRESSION); + self.colon(&arg.colon0); self.space(1); self.expression(&arg.expression0); if let Some(ref x) = arg.switch_expression_opt { @@ -607,8 +716,48 @@ impl VerylWalker for Formatter { self.r_bracket(&arg.r_bracket); } + /// Semantic action for non-terminal 'FactorType' + #[track_caller] + fn factor_type(&mut self, arg: &FactorType) { + match arg.factor_type_group.as_ref() { + FactorTypeGroup::VariableTypeFactorTypeOpt(x) => { + self.variable_type(&x.variable_type); + if self.in_scalar_type { + self.align_finish(align_kind::TYPE); + self.align_start(align_kind::WIDTH); + } + if let Some(ref x) = x.factor_type_opt { + self.width(&x.width); + } else { + if self.in_scalar_type { + let loc = self.align_last_location(align_kind::TYPE); + self.align_dummy_location(align_kind::WIDTH, loc); + } + } + } + FactorTypeGroup::FixedType(x) => { + self.fixed_type(&x.fixed_type); + if self.in_scalar_type { + self.align_finish(align_kind::TYPE); + self.align_start(align_kind::WIDTH); + let loc = self.align_last_location(align_kind::TYPE); + self.align_dummy_location(align_kind::WIDTH, loc); + } + } + } + } + /// Semantic action for non-terminal 'ScalarType' fn scalar_type(&mut self, arg: &ScalarType) { + self.in_scalar_type = true; + + // disable align in Expression + if self.mode == Mode::Align && !self.in_expression.is_empty() { + self.in_scalar_type = false; + return; + } + + self.align_start(align_kind::TYPE); for x in &arg.scalar_type_list { self.type_modifier(&x.type_modifier); self.space(1); @@ -616,33 +765,53 @@ impl VerylWalker for Formatter { match &*arg.scalar_type_group { ScalarTypeGroup::UserDefinedTypeScalarTypeOpt(x) => { self.user_defined_type(&x.user_defined_type); + self.align_finish(align_kind::TYPE); + self.align_start(align_kind::WIDTH); if let Some(ref x) = x.scalar_type_opt { self.width(&x.width); + } else { + let loc = self.align_last_location(align_kind::TYPE); + self.align_dummy_location(align_kind::WIDTH, loc); } } ScalarTypeGroup::FactorType(x) => self.factor_type(&x.factor_type), - }; + } + self.align_finish(align_kind::WIDTH); + self.in_scalar_type = false; } /// Semantic action for non-terminal 'ArrayType' fn array_type(&mut self, arg: &ArrayType) { self.scalar_type(&arg.scalar_type); + self.align_start(align_kind::ARRAY); if let Some(ref x) = arg.array_type_opt { self.space(1); self.array(&x.array); + } else { + let loc = self.align_last_location(align_kind::WIDTH); + self.align_dummy_location(align_kind::ARRAY, loc); } + self.align_finish(align_kind::ARRAY); } /// Semantic action for non-terminal 'LetStatement' fn let_statement(&mut self, arg: &LetStatement) { self.r#let(&arg.r#let); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.colon(&arg.colon); self.space(1); if let Some(ref x) = arg.let_statement_opt { + self.align_start(align_kind::CLOCK_DOMAIN); self.clock_domain(&x.clock_domain); self.space(1); + self.align_finish(align_kind::CLOCK_DOMAIN); + } else { + self.align_start(align_kind::CLOCK_DOMAIN); + self.align_dummy_token(align_kind::CLOCK_DOMAIN, &arg.colon.colon_token); + self.align_finish(align_kind::CLOCK_DOMAIN); } self.array_type(&arg.array_type); self.space(1); @@ -652,15 +821,33 @@ impl VerylWalker for Formatter { self.semicolon(&arg.semicolon); } + /// Semantic action for non-terminal 'IdentifierStatement' + fn identifier_statement(&mut self, arg: &IdentifierStatement) { + self.align_start(align_kind::IDENTIFIER); + self.expression_identifier(&arg.expression_identifier); + self.align_finish(align_kind::IDENTIFIER); + match &*arg.identifier_statement_group { + IdentifierStatementGroup::FunctionCall(x) => { + self.function_call(&x.function_call); + } + IdentifierStatementGroup::Assignment(x) => { + self.assignment(&x.assignment); + } + } + self.semicolon(&arg.semicolon); + } + /// Semantic action for non-terminal 'Assignment' fn assignment(&mut self, arg: &Assignment) { self.space(1); + self.align_start(align_kind::ASSIGNMENT); match &*arg.assignment_group { AssignmentGroup::Equ(x) => self.equ(&x.equ), AssignmentGroup::AssignmentOperator(x) => { self.assignment_operator(&x.assignment_operator) } } + self.align_finish(align_kind::ASSIGNMENT); self.space(1); self.expression(&arg.expression); } @@ -796,10 +983,12 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'CaseItem' fn case_item(&mut self, arg: &CaseItem) { let start = self.column(); + self.align_start(align_kind::EXPRESSION); match &*arg.case_item_group { CaseItemGroup::CaseCondition(x) => self.case_condition(&x.case_condition), CaseItemGroup::Defaul(x) => self.defaul(&x.defaul), } + self.align_finish(align_kind::EXPRESSION); self.colon(&arg.colon); self.space(1); match &*arg.case_item_group0 { @@ -838,10 +1027,12 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'SwitchItem' fn switch_item(&mut self, arg: &SwitchItem) { let start = self.column(); + self.align_start(align_kind::EXPRESSION); match &*arg.switch_item_group { SwitchItemGroup::SwitchCondition(x) => self.switch_condition(&x.switch_condition), SwitchItemGroup::Defaul(x) => self.defaul(&x.defaul), } + self.align_finish(align_kind::EXPRESSION); self.colon(&arg.colon); self.space(1); match &*arg.switch_item_group0 { @@ -881,12 +1072,20 @@ impl VerylWalker for Formatter { fn let_declaration(&mut self, arg: &LetDeclaration) { self.r#let(&arg.r#let); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.colon(&arg.colon); self.space(1); if let Some(ref x) = arg.let_declaration_opt { + self.align_start(align_kind::CLOCK_DOMAIN); self.clock_domain(&x.clock_domain); self.space(1); + self.align_finish(align_kind::CLOCK_DOMAIN); + } else { + self.align_start(align_kind::CLOCK_DOMAIN); + self.align_dummy_token(align_kind::CLOCK_DOMAIN, &arg.colon.colon_token); + self.align_finish(align_kind::CLOCK_DOMAIN); } self.array_type(&arg.array_type); self.space(1); @@ -900,12 +1099,20 @@ impl VerylWalker for Formatter { fn var_declaration(&mut self, arg: &VarDeclaration) { self.var(&arg.var); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.colon(&arg.colon); self.space(1); if let Some(ref x) = arg.var_declaration_opt { + self.align_start(align_kind::CLOCK_DOMAIN); self.clock_domain(&x.clock_domain); self.space(1); + self.align_finish(align_kind::CLOCK_DOMAIN); + } else { + self.align_start(align_kind::CLOCK_DOMAIN); + self.align_dummy_token(align_kind::CLOCK_DOMAIN, &arg.colon.colon_token); + self.align_finish(align_kind::CLOCK_DOMAIN); } self.array_type(&arg.array_type); self.semicolon(&arg.semicolon); @@ -915,7 +1122,9 @@ impl VerylWalker for Formatter { fn const_declaration(&mut self, arg: &ConstDeclaration) { self.r#const(&arg.r#const); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.colon(&arg.colon); self.space(1); match &*arg.const_declaration_group { @@ -923,7 +1132,9 @@ impl VerylWalker for Formatter { self.array_type(&x.array_type); } ConstDeclarationGroup::Type(x) => { + self.align_start(align_kind::TYPE); self.r#type(&x.r#type); + self.align_finish(align_kind::TYPE); } } self.space(1); @@ -937,7 +1148,9 @@ impl VerylWalker for Formatter { fn type_def_declaration(&mut self, arg: &TypeDefDeclaration) { self.r#type(&arg.r#type); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.space(1); self.equ(&arg.equ); self.space(1); @@ -989,7 +1202,9 @@ impl VerylWalker for Formatter { fn assign_declaration(&mut self, arg: &AssignDeclaration) { self.assign(&arg.assign); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.hierarchical_identifier(&arg.hierarchical_identifier); + self.align_finish(align_kind::IDENTIFIER); self.space(1); self.equ(&arg.equ); self.space(1); @@ -1045,7 +1260,9 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'ModportItem' fn modport_item(&mut self, arg: &ModportItem) { + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.colon(&arg.colon); self.space(1); self.direction(&arg.direction); @@ -1166,7 +1383,9 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'StructUnionItem' fn struct_union_item(&mut self, arg: &StructUnionItem) { + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.colon(&arg.colon); self.space(1); self.scalar_type(&arg.scalar_type); @@ -1188,15 +1407,23 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'InstDeclaration' fn inst_declaration(&mut self, arg: &InstDeclaration) { - if arg.inst_declaration_opt1.is_none() { - self.single_line = true; - } + self.single_line = arg.inst_declaration_opt1.is_none(); self.inst(&arg.inst); self.space(1); + if self.single_line { + self.align_start(align_kind::IDENTIFIER); + } self.identifier(&arg.identifier); + if self.single_line { + self.align_finish(align_kind::IDENTIFIER); + } self.colon(&arg.colon); self.space(1); self.scoped_identifier(&arg.scoped_identifier); + // skip align at single line + if self.mode == Mode::Align && self.single_line { + return; + } if let Some(ref x) = arg.inst_declaration_opt { self.space(1); self.array(&x.array); @@ -1278,11 +1505,20 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'InstParameterItem' fn inst_parameter_item(&mut self, arg: &InstParameterItem) { + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); if let Some(ref x) = arg.inst_parameter_item_opt { self.colon(&x.colon); self.space(1); + self.align_start(align_kind::EXPRESSION); self.expression(&x.expression); + self.align_finish(align_kind::EXPRESSION); + } else { + self.align_insert(&arg.identifier.identifier_token, ": ".len()); + self.align_start(align_kind::EXPRESSION); + self.align_dummy_token(align_kind::EXPRESSION, &arg.identifier.identifier_token); + self.align_finish(align_kind::EXPRESSION); } } @@ -1321,11 +1557,20 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'InstPortItem' fn inst_port_item(&mut self, arg: &InstPortItem) { + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); if let Some(ref x) = arg.inst_port_item_opt { self.colon(&x.colon); self.space(1); + self.align_start(align_kind::EXPRESSION); self.expression(&x.expression); + self.align_finish(align_kind::EXPRESSION); + } else { + self.align_insert(&arg.identifier.identifier_token, ": ".len()); + self.align_start(align_kind::EXPRESSION); + self.align_dummy_token(align_kind::EXPRESSION, &arg.identifier.identifier_token); + self.align_finish(align_kind::EXPRESSION); } } @@ -1382,12 +1627,16 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'WithParameterItem' fn with_parameter_item(&mut self, arg: &WithParameterItem) { + self.align_start(align_kind::PARAMETER); match &*arg.with_parameter_item_group { WithParameterItemGroup::Param(x) => self.param(&x.param), WithParameterItemGroup::Const(x) => self.r#const(&x.r#const), }; + self.align_finish(align_kind::PARAMETER); self.space(1); + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.colon(&arg.colon); self.space(1); match &*arg.with_parameter_item_group0 { @@ -1395,13 +1644,17 @@ impl VerylWalker for Formatter { self.array_type(&x.array_type); } WithParameterItemGroup0::Type(x) => { + self.align_start(align_kind::TYPE); self.r#type(&x.r#type); + self.align_finish(align_kind::TYPE); } } self.space(1); self.equ(&arg.equ); self.space(1); + self.align_start(align_kind::EXPRESSION); self.expression(&arg.expression); + self.align_finish(align_kind::EXPRESSION); } /// Semantic action for non-terminal 'WithGenericParameterList' @@ -1495,41 +1748,69 @@ impl VerylWalker for Formatter { /// Semantic action for non-terminal 'PortDeclarationItem' fn port_declaration_item(&mut self, arg: &PortDeclarationItem) { + self.align_start(align_kind::IDENTIFIER); self.identifier(&arg.identifier); + self.align_finish(align_kind::IDENTIFIER); self.colon(&arg.colon); self.space(1); match &*arg.port_declaration_item_group { PortDeclarationItemGroup::PortTypeConcrete(x) => { - self.port_type_concrete(&x.port_type_concrete); + let x = x.port_type_concrete.as_ref(); + self.direction(&x.direction); + self.space(1); + if let Some(ref x) = x.port_type_concrete_opt { + self.align_start(align_kind::CLOCK_DOMAIN); + self.clock_domain(&x.clock_domain); + self.space(1); + self.align_finish(align_kind::CLOCK_DOMAIN); + } else { + self.align_start(align_kind::CLOCK_DOMAIN); + let token = match x.direction.as_ref() { + Direction::Input(x) => &x.input.input_token, + Direction::Output(x) => &x.output.output_token, + Direction::Inout(x) => &x.inout.inout_token, + Direction::Ref(x) => &x.r#ref.ref_token, + Direction::Modport(x) => &x.modport.modport_token, + Direction::Import(x) => &x.import.import_token, + }; + self.align_dummy_token(align_kind::CLOCK_DOMAIN, token); + self.align_finish(align_kind::CLOCK_DOMAIN); + } + self.array_type(&x.array_type); } PortDeclarationItemGroup::PortTypeAbstract(x) => { - self.port_type_abstract(&x.port_type_abstract); + let x = x.port_type_abstract.as_ref(); + if let Some(ref x) = x.port_type_abstract_opt { + self.align_start(align_kind::CLOCK_DOMAIN); + self.clock_domain(&x.clock_domain); + self.space(1); + self.align_finish(align_kind::CLOCK_DOMAIN); + } else { + self.align_start(align_kind::CLOCK_DOMAIN); + self.align_dummy_token(align_kind::CLOCK_DOMAIN, &arg.colon.colon_token); + self.align_finish(align_kind::CLOCK_DOMAIN); + } + self.interface(&x.interface); + if let Some(ref x) = x.port_type_abstract_opt0 { + self.space(1); + self.array(&x.array); + } } } } - /// Semantic action for non-terminal 'PortTypeConcrete' - fn port_type_concrete(&mut self, arg: &PortTypeConcrete) { - self.direction(&arg.direction); - self.space(1); - if let Some(ref x) = arg.port_type_concrete_opt { - self.clock_domain(&x.clock_domain); - self.space(1); - } - self.array_type(&arg.array_type); - } - - /// Semantic action for non-terminal 'PortTypeAbstract' - fn port_type_abstract(&mut self, arg: &PortTypeAbstract) { - if let Some(ref x) = arg.port_type_abstract_opt { - self.clock_domain(&x.clock_domain); - self.space(1); - } - self.interface(&arg.interface); - if let Some(ref x) = arg.port_type_abstract_opt0 { - self.space(1); - self.array(&x.array); - } + /// Semantic action for non-terminal 'Direction' + fn direction(&mut self, arg: &Direction) { + self.align_start(align_kind::DIRECTION); + match arg { + Direction::Input(x) => self.input(&x.input), + Direction::Output(x) => self.output(&x.output), + Direction::Inout(x) => self.inout(&x.inout), + Direction::Ref(x) => self.r#ref(&x.r#ref), + Direction::Modport(x) => self.modport(&x.modport), + Direction::Import(x) => self.import(&x.import), + }; + self.align_finish(align_kind::DIRECTION); } /// Semantic action for non-terminal 'FunctionDeclaration' @@ -1550,6 +1831,7 @@ impl VerylWalker for Formatter { self.space(1); self.scalar_type(&x.scalar_type); self.space(1); + self.align_reset(); } self.statement_block(&arg.statement_block); } diff --git a/crates/formatter/src/lib.rs b/crates/formatter/src/lib.rs index ad4a631d..b946b674 100644 --- a/crates/formatter/src/lib.rs +++ b/crates/formatter/src/lib.rs @@ -1,3 +1,2 @@ -pub mod aligner; pub mod formatter; pub use formatter::Formatter; diff --git a/crates/metadata/src/lockfile.rs b/crates/metadata/src/lockfile.rs index b2620f21..9a8ae144 100644 --- a/crates/metadata/src/lockfile.rs +++ b/crates/metadata/src/lockfile.rs @@ -186,11 +186,9 @@ impl Lockfile { let resolve_path = Self::resolve_path(&lock.url)?; let dependency_path = Self::dependency_path(&lock.url, &lock.revision)?; if resolve_path.exists() { - dbg!(&resolve_path); fs::remove_dir_all(&resolve_path)?; } if dependency_path.exists() { - dbg!(&dependency_path); fs::remove_dir_all(&dependency_path)?; } } diff --git a/testcases/map/testcases/sv/07_statement.sv.map b/testcases/map/testcases/sv/07_statement.sv.map index 0a0aea4a..8e0bc03c 100644 --- a/testcases/map/testcases/sv/07_statement.sv.map +++ b/testcases/map/testcases/sv/07_statement.sv.map @@ -1 +1 @@ -{"version":3,"file":"07_statement.sv.map","sources":["../../../veryl/07_statement.veryl"],"names":["","module","Module07",";","logic","a","aa","clk","=","1","always_comb","begin","+=","-=","*=","/=","%=","&=","|=","^=","<<=",">>=","<<<=",">>>=","if","end","else","for","int unsigned","i","0","10","+","2","==","break","j","&&","always_ff","(",")","endmodule"],"mappings":"AAAAA,AAAAC,sBAAOC,QAASC;IACHC,MAALC,GAAUF;IACLC,MAALE,GAAUH;IACLC,MAALG;oBAAWC,EAAEC,CAACN;;IAElBO,YAAYC;;QAERN,EAAEG,KAAKC,CAACN;QACRE,EAAEO,KAAKH,CAACN;QACRE,EAAEQ,KAAKJ,CAACN;QACRE,EAAES,KAAKL,CAACN;QACRE,EAAEU,KAAKN,CAACN;QACRE,EAAEW,KAAKP,CAACN;QACRE,EAAEY,KAAKR,CAACN;QACRE,EAAEa,KAAKT,CAACN;QACRE,EAAEc,KAAKV,CAACN;QACRE,EAAEe,KAAKX,CAACN;QACRE,EAAEgB,KAAKZ,CAACN;QACRE,EAAEiB,KAAKb,CAACN;QACRE,EAAEkB,KAAKd,CAACN;;;QAGRqB,IAAGnB,GAAEM;YACDN,GAAGG,EAAEC,CAACN;YACNG,GAAGE,EAAEC,CAACN;QACVsB,IAAEC,KAAKF,IAAGnB,GAAEM;YACRN,GAAGG,EAAEC,CAACN;YACNG,GAAGE,EAAEC,CAACN;QACVsB,IAAEC,KAAKf;YACHN,GAAGG,EAAEC,CAACN;YACNG,GAAGE,EAAEC,CAACN;QACVsB;;;QAGAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,KAAgBlB;YAChBN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;QACdsB;;;QAGAE,KAAOC,aAAHC,IAAUC,GAAVD,KAAcE,IAAdF,KAAiBlB;YACjBN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;QACdsB;;;QAGAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,EAAqBjB,GAAGqB,GAAEtB;YAC1BN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;QACdsB;QACAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,EAAqBf,GAAGmB,GAAEtB;YAC1BN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;QACdsB;;;QAGAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,KAAgBlB;YAChBN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;YACVqB,IAAGK,EAAEK,GAAGJ,GAAEnB;gBACNwB,KAAKhC;YACTsB;QACJA;;QAEAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,KAAgBlB;YAChBgB,KAAOC,aAAHQ,IAAUN,GAAVM,IAAaL,IAAbK,KAAgBzB;gBAChBN,GAAGG,EAAEqB,CAAC1B;gBACNG,GAAGE,EAAEqB,EAAEG,EAAEI,CAACjC;gBACVqB,IAAGK,EAAEK,GAAGJ,EAAEO,GAAGD,EAAEF,GAAGJ,GAAEnB;oBAChBwB,KAAKhC;gBACTsB;YACJA;QACJA;IACJA;;IAEAa,YAAUC,SAAChC,GAAGiC,EAAE7B;QACZN,KAAAA,KAAOI,EAACN;QACRE,KAAAA,KAAOI,EAACN;QACRE,KAAAA,KAAOI,EAACN;QACRE,KAAAA,KAAOI,EAACN;QACRE,KAAAA,KAAOI,EAACN;QACRE,KAAAA,KAAOI,EAACN;QACRE,KAAAA,KAAOI,EAACN;QACRE,KAAAA,KAAOI,EAACN;QACRE,KAAAA,MAAOI,EAACN;QACRE,KAAAA,MAAOI,EAACN;QACRE,KAAAA,OAAOI,EAACN;QACRE,KAAAA,OAAOI,EAACN;IACZsB;AACJgB"} \ No newline at end of file +{"version":3,"file":"07_statement.sv.map","sources":["../../../veryl/07_statement.veryl"],"names":["","module","Module07",";","logic","a","aa","clk","=","1","always_comb","begin","+=","-=","*=","/=","%=","&=","|=","^=","<<=",">>=","<<<=",">>>=","if","end","else","for","int unsigned","i","0","10","+","2","==","break","j","&&","always_ff","(",")","endmodule"],"mappings":"AAAAA,AAAAC,sBAAOC,QAASC;IACHC,MAALC,GAAUF;IACLC,MAALE,GAAUH;IACLC,MAALG;oBAAWC,EAAEC,CAACN;;IAElBO,YAAYC;;QAERN,EAAEG,KAAKC,CAACN;QACRE,EAAEO,KAAKH,CAACN;QACRE,EAAEQ,KAAKJ,CAACN;QACRE,EAAES,KAAKL,CAACN;QACRE,EAAEU,KAAKN,CAACN;QACRE,EAAEW,KAAKP,CAACN;QACRE,EAAEY,KAAKR,CAACN;QACRE,EAAEa,KAAKT,CAACN;QACRE,EAAEc,KAAKV,CAACN;QACRE,EAAEe,KAAKX,CAACN;QACRE,EAAEgB,KAAKZ,CAACN;QACRE,EAAEiB,KAAKb,CAACN;QACRE,EAAEkB,KAAKd,CAACN;;;QAGRqB,IAAGnB,GAAEM;YACDN,GAAGG,EAAEC,CAACN;YACNG,GAAGE,EAAEC,CAACN;QACVsB,IAAEC,KAAKF,IAAGnB,GAAEM;YACRN,GAAGG,EAAEC,CAACN;YACNG,GAAGE,EAAEC,CAACN;QACVsB,IAAEC,KAAKf;YACHN,GAAGG,EAAEC,CAACN;YACNG,GAAGE,EAAEC,CAACN;QACVsB;;;QAGAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,KAAgBlB;YAChBN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;QACdsB;;;QAGAE,KAAOC,aAAHC,IAAUC,GAAVD,KAAcE,IAAdF,KAAiBlB;YACjBN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;QACdsB;;;QAGAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,EAAqBjB,GAAGqB,GAAEtB;YAC1BN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;QACdsB;QACAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,EAAqBf,GAAGmB,GAAEtB;YAC1BN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;QACdsB;;;QAGAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,KAAgBlB;YAChBN,GAAGG,EAAEqB,CAAC1B;YACNG,GAAGE,EAAEqB,EAAEG,EAAEvB,CAACN;YACVqB,IAAGK,EAAEK,GAAGJ,GAAEnB;gBACNwB,KAAKhC;YACTsB;QACJA;;QAEAE,KAAOC,aAAHC,IAAUC,GAAVD,IAAaE,IAAbF,KAAgBlB;YAChBgB,KAAOC,aAAHQ,IAAUN,GAAVM,IAAaL,IAAbK,KAAgBzB;gBAChBN,GAAGG,EAAEqB,CAAC1B;gBACNG,GAAGE,EAAEqB,EAAEG,EAAEI,CAACjC;gBACVqB,IAAGK,EAAEK,GAAGJ,EAAEO,GAAGD,EAAEF,GAAGJ,GAAEnB;oBAChBwB,KAAKhC;gBACTsB;YACJA;QACJA;IACJA;;IAEAa,YAAUC,SAAChC,GAAGiC,EAAE7B;QACZN,GAAEG,EAAFH,KAAOI,EAACN;QACRE,GAAEG,EAAFH,KAAOI,EAACN;QACRE,GAAEG,EAAFH,KAAOI,EAACN;QACRE,GAAEG,EAAFH,KAAOI,EAACN;QACRE,GAAEG,EAAFH,KAAOI,EAACN;QACRE,GAAEG,EAAFH,KAAOI,EAACN;QACRE,GAAEG,EAAFH,KAAOI,EAACN;QACRE,GAAEG,EAAFH,KAAOI,EAACN;QACRE,GAAEG,EAAFH,MAAOI,EAACN;QACRE,GAAEG,EAAFH,MAAOI,EAACN;QACRE,GAAEG,EAAFH,OAAOI,EAACN;QACRE,GAAEG,EAAFH,OAAOI,EAACN;IACZsB;AACJgB"} \ No newline at end of file diff --git a/testcases/map/testcases/sv/45_var_let_in_always.sv.map b/testcases/map/testcases/sv/45_var_let_in_always.sv.map index 5a844e48..7534c1b5 100644 --- a/testcases/map/testcases/sv/45_var_let_in_always.sv.map +++ b/testcases/map/testcases/sv/45_var_let_in_always.sv.map @@ -1 +1 @@ -{"version":3,"file":"45_var_let_in_always.sv.map","sources":["../../../veryl/45_var_let_in_always.veryl"],"names":["","module","Module45",";","logic","a","=","1","[","10","]","b","c","always_ff","(",")","begin","x","*","end","always_comb","y","endmodule"],"mappings":"AAAAA,AAAAC,sBAAOC,QAASC;IACLC,eAAHC;kBAAaC,EAAEC,CAACJ;IACbC,MAAKI,CAACC,MAAEC,EAAXC,CAAYR;IACTC,MAAKI,CAACC,MAAEC,EAAXE,CAAYT;;IAEhBU,YAAUC,SAACT,CAACU,EAAEC;QACHZ,MAAKI,CAACC,MAAEC,EAAXO,CAAYd;QAChBc,EAAEX,EAAEC,CAACJ;QACLQ,GAAEL,EAAEW,EAAEC,EAAEX,CAACJ;IACbgB;;IAEAC,YAAYJ;QACDZ,MAAKI,CAACC,MAAEC,EAAXW;UAAaf,EAAEC,CAACJ;QACpBS,EAAEN,EAAEe,EAAEH,EAAEX,CAACJ;IACbgB;AACJG"} \ No newline at end of file +{"version":3,"file":"45_var_let_in_always.sv.map","sources":["../../../veryl/45_var_let_in_always.veryl"],"names":["","module","Module45",";","logic","a","=","1","[","10","]","b","c","always_ff","(",")","begin","x","*","end","always_comb","y","endmodule"],"mappings":"AAAAA,AAAAC,sBAAOC,QAASC;IACLC,eAAHC;kBAAaC,EAAEC,CAACJ;IACbC,MAAKI,CAACC,MAAEC,EAAXC,CAAYR;IACTC,MAAKI,CAACC,MAAEC,EAAXE,CAAYT;;IAEhBU,YAAUC,SAACT,CAACU,EAAEC;QACHZ,MAAKI,CAACC,MAAEC,EAAXO,CAAYd;QAChBc,EAAEX,GAAEC,CAACJ;QACLQ,GAAEL,EAAEW,EAAEC,EAAEX,CAACJ;IACbgB;;IAEAC,YAAYJ;QACDZ,MAAKI,CAACC,MAAEC,EAAXW;UAAaf,EAAEC,CAACJ;QACpBS,EAAEN,EAAEe,EAAEH,EAAEX,CAACJ;IACbgB;AACJG"} \ No newline at end of file diff --git a/testcases/map/testcases/sv/46_var_let_anywhere.sv.map b/testcases/map/testcases/sv/46_var_let_anywhere.sv.map index 4529265c..b3393af0 100644 --- a/testcases/map/testcases/sv/46_var_let_anywhere.sv.map +++ b/testcases/map/testcases/sv/46_var_let_anywhere.sv.map @@ -1 +1 @@ -{"version":3,"file":"46_var_let_anywhere.sv.map","sources":["../../../veryl/46_var_let_anywhere.veryl"],"names":["","module","Module46",";","logic","a","=","1","[","10","]","b","c","d","e","always_ff","(",")","begin","x","*","end","always_comb","y","function","FuncA","input",",","output","ref","int unsigned","/","+","return","2","endfunction","FuncB","endmodule"],"mappings":"AAAAA,AAAAC,sBAAOC,QAASC;IACLC,eAAHC;kBAAaC,EAAEC,CAACJ;IACbC,MAAKI,CAACC,MAAEC,EAAXC,CAAYR;IACTC,MAAKI,CAACC,MAAEC,EAAXE,CAAYT;IACTC,MAAKI,CAACC,MAAEC,EAAXG,CAAYV;IACTC,MAAKI,CAACC,MAAEC,EAAXI,CAAYX;;IAEhBY,YAAUC,SAACX,CAACY,EAAEC;QAEHd,MAAKI,CAACC,MAAEC,EAAXS,CAAYhB;QADhBU,GAAEP,EAAEC,CAACJ;;QAELgB,EAAEb,EAAEC,CAACJ;QACLQ,GAAEL,EAAEa,EAAEC,EAAEb,CAACJ;IACbkB;;IAEAC,YAAYJ;QAEDd,MAAKI,CAACC,MAAEC,EAAXa;QADJT,EAAER,EAAEC,CAACJ;UACYG,EAAEC,CAACJ;QACpBS,EAAEN,EAAEiB,EAAEH,EAAEb,CAACJ;IACbkB;;IAEAG,mBAIKpB,MAAKI,CAACC,MAAEC,EAJJe,KAAMT;QACRU,OAAOtB,MAAKI,CAACC,MAAEC,EAAlBL,CAAmBsB;QAChBC,OAAOxB,MAAKI,CAACC,MAAEC,EAAlBC,CAAmBgB;QAChBE,OAAOzB,MAAKI,CAACC,MAAEC,EAAlBE,CAAmBZ;IACvBiB,EAAEjB,CAAaA;QAEJ8B,aAAHjB,CAAMV;QADVS,EAAEN,EAAED,EAAE0B,EAAExB,CAACJ;;QAETU,EAAEP,EAAEC,CAACJ;QACLQ,EAAEL,EAAED,EAAE2B,EAAEzB,EAAEyB,EAAEnB,CAACV;QACb8B,OAAO5B,EAAE2B,EAAEE,CAAC/B;IAChBgC;;IAEAX,mBAIKpB,MAAKI,CAACC,MAAEC,EAJJ0B,KAAMpB;QACRU,OAAOtB,MAAKI,CAACC,MAAEC,EAAlBL,CAAmBsB;QAChBC,OAAOxB,MAAKI,CAACC,MAAEC,EAAlBC,CAAmBgB;QAChBE,OAAOzB,MAAKI,CAACC,MAAEC,EAAlBE,CAAmBZ;IACvBiB,EAAEjB,CAAaA;QAEJ8B,aAAHjB;QADJD,EAAEN,EAAED,EAAE0B,EAAExB,CAACJ;UACEG,EAAEC,CAACJ;QACdQ,EAAEL,EAAED,EAAE2B,EAAEzB,EAAEyB,EAAEnB,CAACV;QACb8B,OAAO5B,EAAE2B,EAAEE,CAAC/B;IAChBgC;AACJE"} \ No newline at end of file +{"version":3,"file":"46_var_let_anywhere.sv.map","sources":["../../../veryl/46_var_let_anywhere.veryl"],"names":["","module","Module46",";","logic","a","=","1","[","10","]","b","c","d","e","always_ff","(",")","begin","x","*","end","always_comb","y","function","FuncA","input",",","output","ref","int unsigned","/","+","return","2","endfunction","FuncB","endmodule"],"mappings":"AAAAA,AAAAC,sBAAOC,QAASC;IACLC,eAAHC;kBAAaC,EAAEC,CAACJ;IACbC,MAAKI,CAACC,MAAEC,EAAXC,CAAYR;IACTC,MAAKI,CAACC,MAAEC,EAAXE,CAAYT;IACTC,MAAKI,CAACC,MAAEC,EAAXG,CAAYV;IACTC,MAAKI,CAACC,MAAEC,EAAXI,CAAYX;;IAEhBY,YAAUC,SAACX,CAACY,EAAEC;QAEHd,MAAKI,CAACC,MAAEC,EAAXS,CAAYhB;QADhBU,GAAEP,EAAEC,CAACJ;;QAELgB,EAAEb,GAAEC,CAACJ;QACLQ,GAAEL,EAAEa,EAAEC,EAAEb,CAACJ;IACbkB;;IAEAC,YAAYJ;QAEDd,MAAKI,CAACC,MAAEC,EAAXa;QADJT,EAAER,EAAEC,CAACJ;UACYG,EAAEC,CAACJ;QACpBS,EAAEN,EAAEiB,EAAEH,EAAEb,CAACJ;IACbkB;;IAEAG,mBAIKpB,MAAKI,CAACC,MAAEC,EAJJe,KAAMT;QACRU,OAAOtB,MAAKI,CAACC,MAAEC,EAAlBL,CAAmBsB;QAChBC,OAAOxB,MAAKI,CAACC,MAAEC,EAAlBC,CAAmBgB;QAChBE,OAAOzB,MAAKI,CAACC,MAAEC,EAAlBE,CAAmBZ;IACvBiB,EAAEjB,CAAaA;QAEJ8B,aAAHjB,CAAMV;QADVS,EAAEN,EAAED,EAAE0B,EAAExB,CAACJ;;QAETU,EAAEP,EAAEC,CAACJ;QACLQ,EAAEL,EAAED,EAAE2B,EAAEzB,EAAEyB,EAAEnB,CAACV;QACb8B,OAAO5B,EAAE2B,EAAEE,CAAC/B;IAChBgC;;IAEAX,mBAIKpB,MAAKI,CAACC,MAAEC,EAJJ0B,KAAMpB;QACRU,OAAOtB,MAAKI,CAACC,MAAEC,EAAlBL,CAAmBsB;QAChBC,OAAOxB,MAAKI,CAACC,MAAEC,EAAlBC,CAAmBgB;QAChBE,OAAOzB,MAAKI,CAACC,MAAEC,EAAlBE,CAAmBZ;IACvBiB,EAAEjB,CAAaA;QAEJ8B,aAAHjB;QADJD,EAAEN,EAAED,EAAE0B,EAAExB,CAACJ;UACEG,EAAEC,CAACJ;QACdQ,EAAEL,EAAED,EAAE2B,EAAEzB,EAAEyB,EAAEnB,CAACV;QACb8B,OAAO5B,EAAE2B,EAAEE,CAAC/B;IAChBgC;AACJE"} \ No newline at end of file diff --git a/testcases/map/testcases/sv/58_generic_struct.sv.map b/testcases/map/testcases/sv/58_generic_struct.sv.map index a14d7df9..aff638ed 100644 --- a/testcases/map/testcases/sv/58_generic_struct.sv.map +++ b/testcases/map/testcases/sv/58_generic_struct.sv.map @@ -1 +1 @@ -{"version":3,"file":"58_generic_struct.sv.map","sources":["../../../veryl/58_generic_struct.veryl"],"names":["","module","Module58",";","typedef struct packed","{","veryl_testcase_Package58::B","A","veryl_testcase_Package58::C","C","typedef","int signed","B","__StructA__Package58_B","_a","__StructA__Package58_C","_b","__StructA__C","_c","__StructB__Package58_C","_d","_f","__StructB__C","_e","__StructC__C__C","_g","endmodule","package","Package58","int unsigned","longint unsigned","endpackage"],"mappings":"AAAAA,AAAAC,sBAAOC,QAASC;IACZC,sBAA0BC;QACnBC,4BAAHC,CAAIJ;6BACRH;IAFAI,sBAA0BC;QACnBG,4BAAHD,CAAIJ;6BACRH;IAFAI,sBAA0BC;QACnBI,EAAHF,CAAIJ;mBACRH;;IAEAU,QAASC,WAAJF,CAAON;;IAEZC,sBAA8BC;QACvBG,4BAAHI,CAAIT;6BACRH;IAFAI,sBAA8BC;QACvBI,EAAHG,CAAIT;mBACRH;;IAEAI,sBAAmCC;QAC5BI,EAAHG,CAAIT;QACDM,EAAHA,CAAIN;sBACRH;;IAEQa,uBAAJC,EAA2BX;IACvBY,uBAAJC,EAA2Bb;IACvBc,uBAAJC,EAA2Bf;IACvBgB,uBAAJC,EAA2BjB;IACvBgB,uBAAJE,EAA2BlB;IACvBmB,aAAJC,EAA2BpB;IACvBqB,uBAAJC,EAA2BtB;AACnCuB;;AAEAC,uBAAQC,SAAUzB;IACdO,QAASmB,iBAAJjB,CAAOT;IACZO,QAASoB,iBAAJrB,CAAON;AAChB4B"} \ No newline at end of file +{"version":3,"file":"58_generic_struct.sv.map","sources":["../../../veryl/58_generic_struct.veryl"],"names":["","module","Module58",";","typedef struct packed","{","veryl_testcase_Package58::B","A","veryl_testcase_Package58::C","C","typedef","int signed","B","__StructA__Package58_B","_a","__StructA__Package58_C","_b","__StructA__C","_c","__StructB__Package58_C","_d","_f","__StructB__C","_e","__StructC__C__C","_g","endmodule","package","Package58","int unsigned","longint unsigned","endpackage"],"mappings":"AAAAA,AAAAC,sBAAOC,QAASC;IACZC,sBAA0BC;QACnBC,4BAAHC,CAAIJ;6BACRH;IAFAI,sBAA0BC;QACnBG,4BAAHD,CAAIJ;6BACRH;IAFAI,sBAA0BC;QACnBI,4BAAHF,CAAIJ;mBACRH;;IAEAU,QAASC,WAAJF,CAAON;;IAEZC,sBAA8BC;QACvBG,4BAAHI,CAAIT;6BACRH;IAFAI,sBAA8BC;QACvBI,4BAAHG,CAAIT;mBACRH;;IAEAI,sBAAmCC;QAC5BI,EAAHG,CAAIT;QACDM,EAAHA,CAAIN;sBACRH;;IAEQa,uBAAJC,EAA2BX;IACvBY,uBAAJC,EAA2Bb;IACvBc,uBAAJC,EAA2Bf;IACvBgB,uBAAJC,EAA2BjB;IACvBgB,uBAAJE,EAA2BlB;IACvBmB,uBAAJC,EAA2BpB;IACvBqB,uBAAJC,EAA2BtB;AACnCuB;;AAEAC,uBAAQC,SAAUzB;IACdO,QAASmB,iBAAJjB,CAAOT;IACZO,QAASoB,iBAAJrB,CAAON;AAChB4B"} \ No newline at end of file diff --git a/testcases/sv/45_var_let_in_always.sv b/testcases/sv/45_var_let_in_always.sv index 6afbc749..b74c503b 100644 --- a/testcases/sv/45_var_let_in_always.sv +++ b/testcases/sv/45_var_let_in_always.sv @@ -6,7 +6,7 @@ module veryl_testcase_Module45; always_ff @ (posedge a) begin logic [10-1:0] x; - x = 1; + x = 1; b <= x * 1; end diff --git a/testcases/sv/46_var_let_anywhere.sv b/testcases/sv/46_var_let_anywhere.sv index 677c5f37..c4494f33 100644 --- a/testcases/sv/46_var_let_anywhere.sv +++ b/testcases/sv/46_var_let_anywhere.sv @@ -10,7 +10,7 @@ module veryl_testcase_Module46; logic [10-1:0] x; d <= 1; - x = 1; + x = 1; b <= x * 1; end diff --git a/testcases/sv/58_generic_struct.sv b/testcases/sv/58_generic_struct.sv index 0901372b..0b11cbf5 100644 --- a/testcases/sv/58_generic_struct.sv +++ b/testcases/sv/58_generic_struct.sv @@ -6,7 +6,7 @@ module veryl_testcase_Module58; veryl_testcase_Package58::C A; } __StructA__Package58_C; typedef struct packed { - C A; + C A; } __StructA__C; typedef int signed C; @@ -15,7 +15,7 @@ module veryl_testcase_Module58; veryl_testcase_Package58::C B; } __StructB__Package58_C; typedef struct packed { - C B; + C B; } __StructB__C; typedef struct packed { @@ -28,7 +28,7 @@ module veryl_testcase_Module58; __StructA__C _c; __StructB__Package58_C _d; __StructB__Package58_C _f; - __StructB__C _e; + __StructB__C _e; __StructC__C__C _g; endmodule