Skip to content

Commit

Permalink
add the new metadata stuff to the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
EclecticGriffin committed Jan 17, 2025
1 parent 6cbd850 commit f1833fb
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,34 @@ impl std::fmt::Debug for MetadataTableError {
}

pub type MetadataResult<T> = Result<T, MetadataTableError>;

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use crate::parser::CalyxParser;

#[test]
fn test_parse_metadata() {
let input_str = r#"fileinfo #{
FILES
0: "test.calyx"
1: "test2.calyx"
2: "test3.calyx"
POSITIONS
0: 0 0
1: 0 1
2: 0 2
}#"#;

let metadata = CalyxParser::parse_metadata(input_str).unwrap();
let file = metadata.lookup_file_path(1.into());
assert_eq!(file, &PathBuf::from("test2.calyx"));

let pos = metadata.lookup_position(1.into());
assert_eq!(pos.file, 0.into());
assert_eq!(pos.line, 1.into());

dbg!(metadata);
}
}
30 changes: 0 additions & 30 deletions calyx-frontend/src/metadata/metadata.pest

This file was deleted.

2 changes: 0 additions & 2 deletions calyx-frontend/src/metadata/mod.rs

This file was deleted.

110 changes: 0 additions & 110 deletions calyx-frontend/src/metadata/parser.rs

This file was deleted.

91 changes: 86 additions & 5 deletions calyx-frontend/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ use super::ast::{
};
use super::Attributes;
use crate::{
attribute::SetAttribute, attributes::ParseAttributeWrapper, Attribute,
Direction, PortDef, Primitive, Width,
attribute::SetAttribute,
attributes::ParseAttributeWrapper,
metadata::{FileId as MetadataFileId, LineNum, MetadataTable, PositionId},
Attribute, Direction, PortDef, Primitive, Width,
};
use calyx_utils::{self, float, CalyxResult, Id, PosString};
use calyx_utils::{FileIdx, GPosIdx, GlobalPositionTable};
use pest::pratt_parser::{Assoc, Op, PrattParser};
use pest_consume::{match_nodes, Error, Parser};
use std::fs;
use std::io::Read;
use std::path::Path;
use std::str::FromStr;
use std::{fs, path::PathBuf};

type ParseResult<T> = Result<T, Error<Rule>>;
type ComponentDef = ast::ComponentDef;
Expand Down Expand Up @@ -188,6 +190,20 @@ impl CalyxParser {
})
.parse(pairs)
}

#[cfg(test)]
/// A test helper for parsing the new metadata table
pub fn parse_metadata(input: &str) -> ParseResult<MetadataTable> {
let inputs = CalyxParser::parse_with_userdata(
Rule::metadata_table,
input,
UserData {
file: GlobalPositionTable::add_file("".into(), "".into()),
},
)?;
let input = inputs.single()?;
CalyxParser::metadata_table(input)
}
}

#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -1339,18 +1355,83 @@ impl CalyxParser {
))
}

fn metadata(input: Node) -> ParseResult<String> {
fn metadata_legacy(input: Node) -> ParseResult<String> {
Ok(match_nodes!(input.into_children();
[metadata_char(c)..] => c.collect::<String>().trim().into()
))
}

// New Metadata
fn quote(_input: Node) -> ParseResult<()> {
Ok(())
}

fn path_text(input: Node) -> ParseResult<PathBuf> {
Ok(PathBuf::from(input.as_str()))
}

fn path(input: Node) -> ParseResult<PathBuf> {
Ok(match_nodes!(input.into_children();
[quote(_), path_text(p), quote(_)] => p
))
}

fn file_entry(input: Node) -> ParseResult<(MetadataFileId, PathBuf)> {
Ok(match_nodes!(input.into_children();
[bitwidth(n), path(p)] => (MetadataFileId::new(n.try_into().expect("file ids must fit in a u32")), p)
))
}

fn file_header(_input: Node) -> ParseResult<()> {
Ok(())
}

fn file_table(
input: Node,
) -> ParseResult<impl IntoIterator<Item = (MetadataFileId, PathBuf)>> {
Ok(match_nodes!(input.into_children();
[file_header(_), file_entry(e)..] => e))
}

fn position_header(_input: Node) -> ParseResult<()> {
Ok(())
}

fn position_entry(
input: Node,
) -> ParseResult<(PositionId, MetadataFileId, LineNum)> {
Ok(match_nodes!(input.into_children();
[bitwidth(pos_num), bitwidth(file_num), bitwidth(line_no)] => {
let pos_num = pos_num.try_into().expect("position ids must fit in a u32");
let file_num = file_num.try_into().expect("file ids must fit in a u32");
let line_no = line_no.try_into().expect("line numbers must fit in a u32");
(PositionId::new(pos_num), MetadataFileId::new(file_num), LineNum::new(line_no))}
))
}

fn position_table(
input: Node,
) -> ParseResult<
impl IntoIterator<Item = (PositionId, MetadataFileId, LineNum)>,
> {
Ok(match_nodes!(input.into_children();
[position_header(_), position_entry(e)..] => e))
}

fn metadata_table(input: Node) -> ParseResult<MetadataTable> {
Ok(match_nodes!(input.into_children();
[file_table(f), position_table(p)] => MetadataTable::new(f, p)
))
}

// end new metadata

fn file(input: Node) -> ParseResult<ast::NamespaceDef> {
Ok(match_nodes!(
input.into_children();
// There really seems to be no straightforward way to resolve this
// duplication
[imports(imports), externs_and_comps(mixed), metadata(m), EOI(_)] => {
[imports(imports), externs_and_comps(mixed), metadata_legacy(m), EOI(_)] => {
let mut namespace =
ast::NamespaceDef {
imports,
Expand Down
34 changes: 31 additions & 3 deletions calyx-frontend/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ file = {
SOI
~ imports
~ externs_and_comps
~ metadata?
~ metadata_legacy?
~ EOI
}

Expand Down Expand Up @@ -384,9 +384,37 @@ control = {
"control" ~ (("{" ~ "}") | block)
}

// metadata
// metadata Legacy

any_char = { ANY }
metadata_char = ${ !"}#" ~ any_char }

metadata = ${ ^"metadata" ~ WHITESPACE* ~ "#{" ~ metadata_char* ~ "}#"}
metadata_legacy = ${ ^"metadata" ~ WHITESPACE* ~ "#{" ~ metadata_char* ~ "}#"}

// New Metadata

file_header = {"FILES"}

quote = {"\""}

path_text = {(ASCII_ALPHANUMERIC | "\\" | "/" | "." | "_" | "-" )*}

path = { quote ~ path_text ~ quote }

file_entry = { bitwidth ~ ":" ~ path }

file_table = {
file_header ~ file_entry+
}

position_header = {"POSITIONS"}

position_entry = {bitwidth ~ ":" ~ bitwidth ~ bitwidth }

position_table = {
position_header ~ position_entry+
}

metadata_table = {
^"fileinfo" ~ "#{" ~ file_table ~ position_table ~ "}#"
}

0 comments on commit f1833fb

Please sign in to comment.