diff --git a/tools/witx/Cargo.toml b/tools/witx/Cargo.toml index 90a2b48b..edf10f88 100644 --- a/tools/witx/Cargo.toml +++ b/tools/witx/Cargo.toml @@ -18,5 +18,5 @@ path = "src/main.rs" [dependencies] clap = "2" -wast = "3.0.1" +wast = { git = "https://github.com/alexcrichton/wat", branch = "doc-blocks" } failure = "0.1" diff --git a/tools/witx/src/parser.rs b/tools/witx/src/parser.rs index 9e699267..341ea48c 100644 --- a/tools/witx/src/parser.rs +++ b/tools/witx/src/parser.rs @@ -1,3 +1,4 @@ +use wast::lexer::Comment; use wast::parser::{Cursor, Parse, Parser, Peek, Result}; ///! Parser turns s-expressions into unvalidated syntax constructs. @@ -96,6 +97,46 @@ impl Parse<'_> for BuiltinType { } } +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct CommentSyntax<'a> { + pub comments: Vec<&'a str>, +} + +impl<'a> Parse<'a> for CommentSyntax<'a> { + fn parse(parser: Parser<'a>) -> Result> { + let comments = parser.step(|mut cursor| { + let mut comments = Vec::new(); + loop { + let (comment, c) = match cursor.comment() { + Some(pair) => pair, + None => break, + }; + cursor = c; + comments.push(match comment { + Comment::Block(s) => &s[2..s.len() - 2], + Comment::Line(s) => &s[2..], + }); + } + Ok((comments, cursor)) + })?; + Ok(CommentSyntax { comments }) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct Documented<'a, T> { + pub comments: CommentSyntax<'a>, + pub item: T, +} + +impl<'a, T: Parse<'a>> Parse<'a> for Documented<'a, T> { + fn parse(parser: Parser<'a>) -> Result { + let comments = parser.parse()?; + let item = parser.parens(T::parse)?; + Ok(Documented { comments, item }) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum DatatypeIdentSyntax<'a> { Builtin(BuiltinType),