Skip to content

Commit

Permalink
witx: add CommentSyntax and Documented parsers
Browse files Browse the repository at this point in the history
Derived from ca26654f7747e2c9ce9d80d05c23a4ed48ec126c in
https://github.com/alexcrichton/wat (PR #26)
  • Loading branch information
alexcrichton authored and Pat Hickey committed Nov 5, 2019
1 parent 98686c4 commit b2e217f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/witx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
41 changes: 41 additions & 0 deletions tools/witx/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use wast::lexer::Comment;
use wast::parser::{Cursor, Parse, Parser, Peek, Result};

///! Parser turns s-expressions into unvalidated syntax constructs.
Expand Down Expand Up @@ -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<CommentSyntax<'a>> {
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<Self> {
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),
Expand Down

0 comments on commit b2e217f

Please sign in to comment.