Skip to content

Commit

Permalink
Merge aligner into emitter/formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Nov 14, 2024
1 parent 6f4e154 commit 7b47d55
Show file tree
Hide file tree
Showing 21 changed files with 897 additions and 2,082 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"crates/aligner",
"crates/analyzer",
"crates/emitter",
"crates/formatter",
Expand Down
14 changes: 14 additions & 0 deletions crates/aligner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"}
171 changes: 171 additions & 0 deletions crates/aligner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<usize>,
}

impl From<&Token> for Location {
fn from(x: &Token) -> Self {
Self {
line: x.line,
column: x.column,
length: x.length,
duplicated: None,
}
}
}

impl From<Token> 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<Location, u32>,
pub 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;
}

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<Location, u32>,
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);
}
}
}
}
1 change: 1 addition & 0 deletions crates/emitter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Loading

0 comments on commit 7b47d55

Please sign in to comment.