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: usize,
pub column: usize,
pub length: usize,
}
impl From<&Token> for Location {
fn from(x: &Token) -> Self {
Self {
line: x.line,
column: x.column,
length: x.length,
}
}
}
impl From<Token> for Location {
fn from(x: Token) -> Self {
Self {
line: x.line,
column: x.column,
length: x.length,
}
}
}
#[derive(Default)]
pub struct Align {
index: usize,
max_width: usize,
width: usize,
line: usize,
rest: Vec<(Location, usize)>,
additions: HashMap<Location, usize>,
last_location: Option<Location>,
}
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) {
let last_location = self.last_location.take();
if let Some(loc) = last_location {
if loc.line - self.line > 1 {
self.finish_group();
}
self.max_width = usize::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.width = 0;
}
fn token(&mut self, x: &VerylToken) {
self.width += x.token.length;
let loc: Location = x.token.into();
self.last_location = Some(loc);
}
fn dummy_token(&mut self, x: &VerylToken) {
self.width += 0; let loc: Location = x.token.into();
self.last_location = Some(loc);
}
fn dummy_location(&mut self, x: Location) {
self.width += 0; self.last_location = Some(x);
}
fn space(&mut self, x: usize) {
self.width += x;
}
}
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 ASSIGNMENT: usize = 4;
pub const PARAMETER: usize = 5;
pub const DIRECTION: usize = 6;
}
#[derive(Default)]
pub struct Aligner {
pub additions: HashMap<Location, usize>,
aligns: [Align; 7],
}
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 insert(&mut self, token: &VerylToken, width: usize) {
let loc: Location = token.token.into();
self.additions
.entry(loc)
.and_modify(|val| *val += width)
.or_insert(width);
}
fn space(&mut self, repeat: usize) {
for i in 0..self.aligns.len() {
self.aligns[i].space(repeat);
}
}
}
impl VerylWalker for Aligner {
fn veryl_token(&mut self, arg: &VerylToken) {
for i in 0..self.aligns.len() {
self.aligns[i].token(arg);
}
}
fn expression(&mut self, arg: &Expression) {
self.expression01(&arg.expression01);
for x in &arg.expression_list {
self.space(1);
self.operator01(&x.operator01);
self.space(1);
self.expression01(&x.expression01);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
fn function_call_arg(&mut self, arg: &FunctionCallArg) {
self.expression(&arg.expression);
for x in &arg.function_call_arg_list {
self.comma(&x.comma);
self.space(1);
self.expression(&x.expression);
}
if let Some(ref x) = arg.function_call_arg_opt {
self.comma(&x.comma);
}
}
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.expression(&arg.expression0);
self.aligns[align_kind::EXPRESSION].finish_item();
self.colon(&arg.colon);
self.expression(&arg.expression1);
self.comma(&arg.comma);
for x in &arg.case_expression_list {
self.aligns[align_kind::EXPRESSION].start_item();
self.expression(&x.expression);
self.aligns[align_kind::EXPRESSION].finish_item();
self.colon(&x.colon);
self.expression(&x.expression0);
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.expression2);
if let Some(ref x) = arg.case_expression_opt {
self.comma(&x.comma);
}
self.r_brace(&arg.r_brace);
}
fn range_operator(&mut self, arg: &RangeOperator) {
match arg {
RangeOperator::Colon(x) => self.colon(&x.colon),
RangeOperator::PlusColon(x) => self.plus_colon(&x.plus_colon),
RangeOperator::MinusColon(x) => self.minus_colon(&x.minus_colon),
RangeOperator::Step(x) => {
self.space(1);
self.step(&x.step);
self.space(1);
}
}
}
fn r#type(&mut self, arg: &Type) {
self.aligns[align_kind::TYPE].start_item();
if let Some(ref x) = arg.type_opt {
self.type_modifier(&x.type_modifier);
self.space(1);
}
match &*arg.type_group {
TypeGroup::BuiltinType(x) => self.builtin_type(&x.builtin_type),
TypeGroup::ScopedIdentifier(x) => self.scoped_identifier(&x.scoped_identifier),
};
let loc = self.aligns[align_kind::TYPE].last_location;
self.aligns[align_kind::TYPE].finish_item();
self.aligns[align_kind::WIDTH].start_item();
if arg.type_list.is_empty() {
let loc = loc.unwrap();
self.aligns[align_kind::WIDTH].dummy_location(loc);
} else {
for x in &arg.type_list {
self.width(&x.width);
}
}
self.aligns[align_kind::WIDTH].finish_item();
}
fn assignment_statement(&mut self, arg: &AssignmentStatement) {
self.aligns[align_kind::IDENTIFIER].start_item();
self.hierarchical_identifier(&arg.hierarchical_identifier);
self.aligns[align_kind::IDENTIFIER].finish_item();
self.aligns[align_kind::ASSIGNMENT].start_item();
match &*arg.assignment_statement_group {
AssignmentStatementGroup::Equ(x) => self.equ(&x.equ),
AssignmentStatementGroup::AssignmentOperator(x) => {
self.assignment_operator(&x.assignment_operator)
}
}
self.aligns[align_kind::ASSIGNMENT].finish_item();
self.expression(&arg.expression);
self.semicolon(&arg.semicolon);
}
fn case_item(&mut self, arg: &CaseItem) {
self.aligns[align_kind::EXPRESSION].start_item();
match &*arg.case_item_group {
CaseItemGroup::Expression(x) => self.expression(&x.expression),
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::LBraceCaseItemGroup0ListRBrace(x) => {
self.l_brace(&x.l_brace);
for x in &x.case_item_group0_list {
self.statement(&x.statement);
}
self.r_brace(&x.r_brace);
}
}
}
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.r#type(&arg.r#type);
if let Some(ref x) = arg.var_declaration_opt {
self.equ(&x.equ);
self.expression(&x.expression);
}
self.semicolon(&arg.semicolon);
}
fn localparam_declaration(&mut self, arg: &LocalparamDeclaration) {
self.localparam(&arg.localparam);
self.aligns[align_kind::IDENTIFIER].start_item();
self.identifier(&arg.identifier);
self.aligns[align_kind::IDENTIFIER].finish_item();
self.colon(&arg.colon);
self.r#type(&arg.r#type);
self.equ(&arg.equ);
self.expression(&arg.expression);
self.semicolon(&arg.semicolon);
}
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);
}
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);
}
fn struct_item(&mut self, arg: &StructItem) {
self.aligns[align_kind::IDENTIFIER].start_item();
self.identifier(&arg.identifier);
self.aligns[align_kind::IDENTIFIER].finish_item();
self.colon(&arg.colon);
self.r#type(&arg.r#type);
}
fn inst_declaration(&mut self, arg: &InstDeclaration) {
self.inst(&arg.inst);
self.aligns[align_kind::IDENTIFIER].start_item();
self.identifier(&arg.identifier);
self.aligns[align_kind::IDENTIFIER].finish_item();
self.colon(&arg.colon);
self.identifier(&arg.identifier0);
if arg.inst_declaration_opt1.is_none() {
return;
}
if let Some(ref x) = arg.inst_declaration_opt {
self.width(&x.width);
}
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);
}
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();
}
}
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();
}
}
fn with_parameter_item(&mut self, arg: &WithParameterItem) {
self.aligns[align_kind::PARAMETER].start_item();
match &*arg.with_parameter_item_group {
WithParameterItemGroup::Parameter(x) => self.parameter(&x.parameter),
WithParameterItemGroup::Localparam(x) => self.localparam(&x.localparam),
};
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);
self.r#type(&arg.r#type);
self.equ(&arg.equ);
self.aligns[align_kind::EXPRESSION].start_item();
self.expression(&arg.expression);
self.aligns[align_kind::EXPRESSION].finish_item();
}
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::DirectionType(x) => {
self.direction(&x.direction);
self.r#type(&x.r#type);
}
PortDeclarationItemGroup::Interface(x) => self.interface(&x.interface),
}
}
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),
};
self.aligns[align_kind::DIRECTION].finish_item();
}
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_parameter(&x.with_parameter);
}
if let Some(ref x) = arg.function_declaration_opt0 {
self.port_declaration(&x.port_declaration);
}
self.minus_g_t(&arg.minus_g_t);
self.l_brace(&arg.l_brace);
for x in &arg.function_declaration_list {
self.function_item(&x.function_item);
}
self.r_brace(&arg.r_brace);
}
}