Skip to content

Commit

Permalink
A parser for llvm.pointer type
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Sep 18, 2023
1 parent 4102b29 commit 3649578
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/dialects/builtin/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
dialect::Dialect,
error::CompilerError,
impl_type,
parsable::{Parsable, StateStream},
parsable::{spaced, Parsable, StateStream},
printable::{self, ListSeparator, Printable, PrintableIter},
r#type::{Type, TypeObj},
storage_uniquer::TypeValueHash,
Expand Down Expand Up @@ -67,9 +67,10 @@ impl Parsable for IntegerType {
));

// followed by an integer.
let mut parser = choicer
let parser = choicer
.and(many1::<String, _, _>(digit()).map(|digits| digits.parse::<u64>().unwrap()));

let mut parser = spaced(parser);
parser
.parse_stream(&mut state_stream.stream)
.map(|(signedness, width)| IntegerType::get(state_stream.state.ctx, width, signedness))
Expand Down Expand Up @@ -254,7 +255,7 @@ mod tests {
let expected_err_msg = expect![[r#"
Parse error at line: 1, column: 1
Unexpected `a`
Expected si, ui or i
Expected whitespaces, si, ui or i
"#]];
expected_err_msg.assert_eq(&err_msg);
}
Expand Down
30 changes: 24 additions & 6 deletions src/dialects/llvm/types.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use combine::{easy, ParseResult};
use combine::{easy, token, ParseResult, Parser};

use crate::{
common_traits::Verify,
context::{Context, Ptr},
dialect::Dialect,
error::CompilerError,
impl_type,
parsable::{Parsable, StateStream},
parsable::{spaced, Parsable, StateStream},
printable::{self, Printable},
r#type::{Type, TypeObj},
r#type::{type_parser, Type, TypeObj},
storage_uniquer::TypeValueHash,
};

Expand Down Expand Up @@ -255,12 +255,12 @@ impl Parsable for PointerType {
type Parsed = Ptr<TypeObj>;

fn parse<'a>(
_state_stream: &mut StateStream<'a>,
state_stream: &mut StateStream<'a>,
) -> ParseResult<Self::Parsed, easy::ParseError<StateStream<'a>>>
where
Self: Sized,
{
todo!()
spaced(combine::between(token('<'), token('>'), type_parser())).parse_stream(state_stream)
}
}

Expand All @@ -277,14 +277,17 @@ pub fn register(dialect: &mut Dialect) {

#[cfg(test)]
mod tests {

use crate::{
context::Context,
dialects::{
self,
builtin::types::{IntegerType, Signedness},
llvm::types::{PointerType, StructType},
},
parsable::{self, state_stream_from_iterator},
printable::Printable,
r#type::Type,
r#type::{type_parser, Type},
};

#[test]
Expand Down Expand Up @@ -369,4 +372,19 @@ mod tests {
== int64_ptr
);
}

#[test]
fn test_pointer_type_parsing() {
let mut ctx = Context::new();
dialects::builtin::register(&mut ctx);
dialects::llvm::register(&mut ctx);

let state_stream = state_stream_from_iterator(
"llvm.ptr <builtin.integer si64>".chars(),
parsable::State { ctx: &mut ctx },
);

let res = type_parser().parse(state_stream).unwrap().0;
assert_eq!(&res.disp(&ctx).to_string(), "si64");
}
}
8 changes: 8 additions & 0 deletions src/parsable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::context::Context;
use combine::{
easy,
parser::char::spaces,
stream::{
self, buffered,
position::{self, SourcePosition},
Expand Down Expand Up @@ -134,3 +135,10 @@ pub fn parse_id<Input: Stream<Token = char>>() -> impl Parser<Input, Output = St
.and(many::<String, _, _>(char::alpha_num().or(char::char('_'))))
.map(|(c, rest)| c.to_string() + &rest)
}

/// Parse from `parser`, ignoring whitespace(s) before and after.
pub fn spaced<Input: Stream<Token = char>, Output>(
parser: impl Parser<Input, Output = Output>,
) -> impl Parser<Input, Output = Output> {
combine::between(spaces(), spaces(), parser)
}
16 changes: 8 additions & 8 deletions src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
use crate::common_traits::Verify;
use crate::context::{private::ArenaObj, ArenaCell, Context, Ptr};
use crate::dialect::{Dialect, DialectName};
use crate::parsable::{parse_id, Parsable, ParserFn, StateStream};
use crate::parsable::{parse_id, spaced, Parsable, ParserFn, StateStream};
use crate::printable::{self, Printable};
use crate::storage_uniquer::TypeValueHash;

use combine::parser::char::spaces;
use combine::{easy, parser, ParseResult, Parser, Positioned};
use downcast_rs::{impl_downcast, Downcast};
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -322,9 +321,9 @@ pub fn type_parse<'a>(
state_stream: &mut StateStream<'a>,
) -> ParseResult<Ptr<TypeObj>, easy::ParseError<StateStream<'a>>> {
let position = state_stream.stream.position();
let type_id_parser = TypeId::parser().skip(spaces());
let type_id_parser = TypeId::parser();

let mut type_parser = type_id_parser.then(|type_id: TypeId| {
let type_parser = type_id_parser.then(|type_id: TypeId| {
combine::parser(move |parsable_state: &mut StateStream<'a>| {
let state = &parsable_state.state;
let dialect = state
Expand All @@ -345,6 +344,7 @@ pub fn type_parse<'a>(
})
});

let mut type_parser = spaced(type_parser);
type_parser.parse_stream(state_stream)
}

Expand Down Expand Up @@ -393,10 +393,10 @@ mod test {
let err_msg = format!("{}", res.err().unwrap());

let expected_err_msg = expect![[r#"
Parse error at line: 1, column: 17
Unexpected `a`
Expected si, ui or i
"#]];
Parse error at line: 1, column: 17
Unexpected `a`
Expected whitespace, si, ui or i
"#]];
expected_err_msg.assert_eq(&err_msg);

let state_stream = state_stream_from_iterator(
Expand Down

0 comments on commit 3649578

Please sign in to comment.