Skip to content

Commit

Permalink
More parsing, readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
DMoore12 committed Aug 25, 2024
1 parent d548b2a commit 62dbf3a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 88 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ description = "A simple SystemVerilog simulation tool written in rust"
repository = "https://github.com/DMoore12/sv-sim"
readme = "README.md"
license = "MIT"
categories = ["compilers", "parsing", "simulation"]
keywords = ["systemverilog", "sv-sim", "sv"]
categories = ["parsing"]
keywords = ["systemverilog", "parser"]
exclude = [".*", "Better-Rust-Documentation*", "generate_docs.sh", "sv/*"]

[badges]
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

_A simple SystemVerilog simulation tool written in rust_

> This project is in a very early state
## Project Scope

- Provide a simple SystemVerilog parser
- Provide simple analysis tools
- Allow design verification for simple projects
- [X] Provide an incomplete, slightly broken SystemVerilog parser
- [ ] Provide a simple SystemVerilog parser
- [ ] Provide simple analysis tools
- [ ] Allow design verification for simple projects

## Repository Contents

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub enum LexingError {

/// Bit width determined to be negative
NegativeBitWidth,

/// Module wire parsing failed
ModuleWireNotFound,
}

impl Into<String> for LexingError {
Expand Down
138 changes: 57 additions & 81 deletions src/module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::var_types::*;
use crate::var_types::{self, *};
use crate::{parse_comment, LexingError, Token};
use log::{debug, error, trace};
use logos::Lexer;
Expand All @@ -22,100 +22,30 @@ pub struct Module {

impl fmt::Debug for Module {
fn fmt(&self, _: &mut std::fmt::Formatter) -> fmt::Result {
debug!("Module {:?}", self.name);
debug!("MODULE: {:?}", self.name);
format!("{0:?}", self.io);
for var in self.vars.clone() {
debug!("{:?}", var);
debug!("VAR: {:?}", var);
}
Ok(())
}
}

/// Parses a module to completion
pub fn parse_module<'source>(lexer: &mut Lexer<'source, Token>) -> Result<Module, LexingError> {
let mut in_wire = false;
let mut in_reg = false;
let mut vars: Vec<Var> = Vec::new();

trace!("parsing module");

let io = match parse_module_io(lexer) {
Ok(ret) => ret,
Err(_) => ModuleIO::default(),
};

while let Some(token) = lexer.next() {
if in_wire {
match token {
Ok(Token::Word) => match parse_name(lexer) {
Ok(name) => {
vars.push(Var {
name,
var_type: VarType::Wire,
..Default::default()
});
in_wire = false;
}
Err(_) => {
error!(
"unexpected error occurred parsing module wire name: '{}'",
lexer.slice()
);
in_wire = false;
}
},
Ok(Token::Comment) => match crate::parse_comment(lexer) {
_ => (),
},
Ok(Token::WhiteSpace) => (),
Err(e) => {
error!(
"unexpected error occurred parsing module wire: '{}'",
lexer.slice()
);
return Err(e);
}
_ => (),
}
continue;
} else if in_reg {
match token {
Ok(Token::Word) => match parse_name(lexer) {
Ok(name) => {
vars.push(Var {
name,
var_type: VarType::Reg,
..Default::default()
});
in_reg = false;
}
Err(_) => {
error!(
"unexpected error occurred parsing module reg name: '{}'",
lexer.slice()
);
in_reg = false;
}
},
Ok(Token::Comment) => match crate::parse_comment(lexer) {
_ => (),
},
Ok(Token::WhiteSpace) => (),
Err(e) => {
error!(
"unexpected error occurred parsing module reg: '{}'",
lexer.slice()
);
return Err(e);
}
_ => (),
}
continue;
}
trace!("parsing module");

while let Some(token) = lexer.next() {
match token {
Ok(Token::Wire) => in_wire = true,
Ok(Token::Reg) => in_reg = true,
Ok(Token::Wire) => vars.push(parse_module_var(lexer, VarType::Wire)?),
Ok(Token::Reg) => vars.push(parse_module_var(lexer, VarType::Reg)?),
Ok(Token::Comment) => match parse_comment(lexer) {
Ok(_) => (),
Err(e) => {
Expand Down Expand Up @@ -147,6 +77,52 @@ pub fn parse_module<'source>(lexer: &mut Lexer<'source, Token>) -> Result<Module
})
}

fn parse_module_var<'source>(
lexer: &mut Lexer<'source, Token>,
var_type: VarType,
) -> Result<Var, LexingError> {
let mut width = 1;

trace!("parsing module variable of type {:?}", var_type);

while let Some(token) = lexer.next() {
match token {
Ok(Token::Word) => match parse_name(lexer) {
Ok(name) => {
return Ok(Var {
name,
var_type,
width,
..Default::default()
});
}
Err(_) => {
error!(
"unexpected error occurred parsing module wire name: '{}'",
lexer.slice()
);
break;
}
},
Ok(Token::OpenBracket) => width = var_types::parse_width(lexer)?,
Ok(Token::Comment) => match crate::parse_comment(lexer) {
_ => (),
},
Ok(Token::WhiteSpace) => (),
Err(e) => {
error!(
"unexpected error occurred parsing module wire: '{}'",
lexer.slice()
);
return Err(e);
}
_ => (),
}
}

Err(LexingError::ModuleWireNotFound)
}

/// Module I/O information
///
/// Stores all inputs, outputs, and inouts for a given module
Expand All @@ -167,15 +143,15 @@ pub struct ModuleIO {

impl fmt::Debug for ModuleIO {
fn fmt(&self, _: &mut std::fmt::Formatter) -> fmt::Result {
debug!("ModuleIO {:?}", self.name);
debug!("MODULE I/O: {:?}", self.name);
for input in self.inputs.clone() {
debug!("{:?}", input);
debug!("IO: {:?}", input);
}
for output in self.outputs.clone() {
debug!("{:?}", output);
debug!("IO: {:?}", output);
}
for inout in self.inouts.clone() {
debug!("{:?}", inout);
debug!("IO: {:?}", inout);
}
Ok(())
}
Expand Down
16 changes: 14 additions & 2 deletions src/var_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl From<&str> for VarType {
}
}

#[derive(Default, Debug, Clone)]
#[derive(Debug, Clone)]
pub struct Var {
pub name: String,
pub width: u64,
Expand All @@ -46,6 +46,18 @@ pub struct Var {
pub hi_z: bool,
}

impl Default for Var {
fn default() -> Self {
Self {
name: String::new(),
width: 1,
var_type: VarType::default(),
state: false,
hi_z: false,
}
}
}

pub fn parse_input<'source>(lexer: &mut Lexer<'source, Token>) -> Result<Input, LexingError> {
trace!("parsing input");

Expand Down Expand Up @@ -184,7 +196,7 @@ pub fn parse_name<'source>(lexer: &mut Lexer<'source, Token>) -> Result<String,
Ok(name)
}

fn parse_width<'source>(lexer: &mut Lexer<'source, Token>) -> Result<u64, LexingError> {
pub fn parse_width<'source>(lexer: &mut Lexer<'source, Token>) -> Result<u64, LexingError> {
let mut start = 0;
let mut end = 0;
let mut end_found = false;
Expand Down

0 comments on commit 62dbf3a

Please sign in to comment.