Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move parser helpers to irfmt::parsers #29

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ use crate::{
error::Result,
identifier::Identifier,
input_err,
irfmt::parsers::spaced,
location::Located,
parsable::{spaced, Parsable, ParseResult, ParserFn, StateStream},
parsable::{Parsable, ParseResult, ParserFn, StateStream},
printable::{self, Printable},
};

Expand Down Expand Up @@ -107,6 +108,43 @@ impl Printable for AttrObj {
}
}

impl Parsable for AttrObj {
type Arg = ();
type Parsed = AttrObj;

fn parse<'a>(
state_stream: &mut StateStream<'a>,
_arg: Self::Arg,
) -> ParseResult<'a, Self::Parsed> {
let loc = state_stream.loc();
let attr_id_parser = spaced(AttrId::parser(()));

let mut attr_parser = attr_id_parser.then(move |attr_id: AttrId| {
let loc = loc.clone();
combine::parser(move |parsable_state: &mut StateStream<'a>| {
let state = &parsable_state.state;
let dialect = state
.ctx
.dialects
.get(&attr_id.dialect)
.expect("Dialect name parsed but dialect isn't registered");
let Some(attr_parser) = dialect.attributes.get(&attr_id) else {
input_err!(
loc.clone(),
"Unregistered attribute {}",
attr_id.disp(state.ctx)
)?
};
attr_parser(&(), ())
.parse_stream(parsable_state)
.into_result()
})
});

attr_parser.parse_stream(state_stream).into_result()
}
}

/// Cast reference to an [Attribute] object to an interface reference.
pub fn attr_cast<T: ?Sized + Attribute>(attr: &dyn Attribute) -> Option<&T> {
attr.cast::<T>()
Expand Down Expand Up @@ -214,42 +252,6 @@ impl Parsable for AttrId {
}
}

/// Parse an identified attribute, which is [AttrId] followed by its contents.
pub fn attr_parse<'a>(state_stream: &mut StateStream<'a>) -> ParseResult<'a, AttrObj> {
let loc = state_stream.loc();
let attr_id_parser = spaced(AttrId::parser(()));

let mut attr_parser = attr_id_parser.then(move |attr_id: AttrId| {
let loc = loc.clone();
combine::parser(move |parsable_state: &mut StateStream<'a>| {
let state = &parsable_state.state;
let dialect = state
.ctx
.dialects
.get(&attr_id.dialect)
.expect("Dialect name parsed but dialect isn't registered");
let Some(attr_parser) = dialect.attributes.get(&attr_id) else {
input_err!(
loc.clone(),
"Unregistered attribute {}",
attr_id.disp(state.ctx)
)?
};
attr_parser(&(), ())
.parse_stream(parsable_state)
.into_result()
})
});

attr_parser.parse_stream(state_stream).into_result()
}

/// A parser combinator to parse [AttrId] followed by the attribute's contents.
pub fn attr_parser<'a>(
) -> Box<dyn Parser<StateStream<'a>, Output = AttrObj, PartialState = ()> + 'a> {
combine::parser(|parsable_state: &mut StateStream<'a>| attr_parse(parsable_state)).boxed()
}

/// Every attribute interface must have a function named `verify` with this type.
pub type AttrInterfaceVerifier = fn(&dyn Attribute, &Context) -> Result<()>;

Expand Down
9 changes: 6 additions & 3 deletions src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ use crate::{
error::Result,
identifier::Identifier,
indented_block,
irfmt::printers::{iter_with_sep, list_with_sep},
irfmt::{
parsers::{spaced, type_parser},
printers::{iter_with_sep, list_with_sep},
},
linked_list::{private, ContainsLinkedList, LinkedList},
location::{Located, Location},
operation::Operation,
parsable::{self, spaced, IntoParseResult, Parsable, ParseResult},
parsable::{self, IntoParseResult, Parsable, ParseResult},
printable::{self, indented_nl, ListSeparator, Printable},
r#type::{type_parser, TypeObj, Typed},
r#type::{TypeObj, Typed},
region::Region,
use_def_lists::{DefNode, Value},
};
Expand Down
12 changes: 8 additions & 4 deletions src/dialects/builtin/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ use crate::{
dialect::Dialect,
error::Result,
impl_attr_interface, input_err,
irfmt::printers::quoted,
irfmt::{
parsers::{spaced, type_parser},
printers::quoted,
},
location::Located,
parsable::{spaced, IntoParseResult, Parsable, ParseResult, StateStream},
parsable::{IntoParseResult, Parsable, ParseResult, StateStream},
printable::{self, Printable},
r#type::{type_parser, TypeObj, Typed},
r#type::{TypeObj, Typed},
verify_err_noloc,
};

Expand Down Expand Up @@ -485,7 +488,7 @@ mod tests {
use expect_test::expect;

use crate::{
attribute::{attr_cast, attr_parser},
attribute::attr_cast,
context::Context,
dialects::{
self,
Expand All @@ -495,6 +498,7 @@ mod tests {
types::{IntegerType, Signedness},
},
},
irfmt::parsers::attr_parser,
location,
parsable::{self, state_stream_from_iterator},
printable::Printable,
Expand Down
13 changes: 8 additions & 5 deletions src/dialects/builtin/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pliron_derive::def_op;
use thiserror::Error;

use crate::{
attribute::{attr_cast, attr_parser, AttrObj},
attribute::{attr_cast, AttrObj},
basic_block::BasicBlock,
common_traits::{Named, Verify},
context::{Context, Ptr},
Expand All @@ -12,14 +12,17 @@ use crate::{
error::Result,
identifier::Identifier,
impl_op_interface, input_err,
irfmt::printers::op::{region, symb_op_header, typed_symb_op_header},
irfmt::{
parsers::{attr_parser, process_parsed_ssa_defs, spaced, type_parser},
printers::op::{region, symb_op_header, typed_symb_op_header},
},
linked_list::ContainsLinkedList,
location::{Located, Location},
op::{Op, OpObj},
operation::{process_parsed_ssa_defs, Operation},
parsable::{spaced, IntoParseResult, Parsable, ParseResult, StateStream},
operation::Operation,
parsable::{IntoParseResult, Parsable, ParseResult, StateStream},
printable::{self, Printable},
r#type::{type_parser, TypeObj, Typed},
r#type::{TypeObj, Typed},
region::Region,
verify_err,
};
Expand Down
9 changes: 6 additions & 3 deletions src/dialects/builtin/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ use crate::{
context::{Context, Ptr},
dialect::Dialect,
error::Result,
irfmt::printers::{functional_type, list_with_sep},
parsable::{spaced, IntoParseResult, Parsable, ParseResult, StateStream},
irfmt::{
parsers::{spaced, type_parser},
printers::{functional_type, list_with_sep},
},
parsable::{IntoParseResult, Parsable, ParseResult, StateStream},
printable::{self, ListSeparator, Printable},
r#type::{type_parser, Type, TypeObj},
r#type::{Type, TypeObj},
};

#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug)]
Expand Down
3 changes: 2 additions & 1 deletion src/dialects/llvm/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use crate::{
error::Result,
identifier::Identifier,
impl_op_interface, input_err,
irfmt::parsers::ssa_opd_parser,
location::{Located, Location},
op::{Op, OpObj},
operation::{ssa_opd_parser, Operation},
operation::Operation,
parsable::{Parsable, ParseResult},
printable::{self, Printable},
use_def_lists::Value,
Expand Down
12 changes: 8 additions & 4 deletions src/dialects/llvm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ use crate::{
error::Result,
identifier::Identifier,
input_err_noloc,
irfmt::printers::{enclosed, list_with_sep},
irfmt::{
parsers::{spaced, type_parser},
printers::{enclosed, list_with_sep},
},
location::{Located, Location},
parsable::{spaced, IntoParseResult, Parsable, ParseResult, StateStream},
parsable::{IntoParseResult, Parsable, ParseResult, StateStream},
printable::{self, ListSeparator, Printable},
r#type::{type_parser, Type, TypeObj},
r#type::{Type, TypeObj},
verify_err_noloc,
};
use combine::{between, optional, parser::char::spaces, sep_by, token, Parser};
Expand Down Expand Up @@ -386,10 +389,11 @@ mod tests {
llvm::types::{PointerType, StructErr, StructField, StructType},
},
error::{Error, ErrorKind, Result},
irfmt::parsers::type_parser,
location,
parsable::{self, state_stream_from_iterator},
printable::Printable,
r#type::{type_parser, Type},
r#type::Type,
};

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/irfmt/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod parsers;
pub mod printers;
Loading
Loading