Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Feb 9, 2023
1 parent 81ec90d commit 65ff4a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub mod types;

mod lexer;
mod tokens;
mod writer;

use lexer::lex;
use tokens::LexedSlice;
Expand Down Expand Up @@ -118,7 +119,7 @@ pub trait Parse<'token>: Sized {
Ok((unread, def))
}

fn write(&self) -> String;
fn write<T: writer::MarkupCallback>(&self, callback: &T) -> T::ReturnType;
}

/// Parses WebIDL definitions. It is the root struct for a complete WebIDL definition.
Expand Down
6 changes: 3 additions & 3 deletions src/literal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nom::Parser;
use weedle_derive::Weedle;

use crate::{term::Token, Parse};
use crate::{term::Token, Parse, writer::MarkupElement};

/// Parses `-?[1-9][0-9]*`
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
Expand Down Expand Up @@ -68,14 +68,14 @@ impl<'a> IntegerLit<'a> {
impl<'a> Parse<'a> for Token<'a, IntegerLit<'a>> {
parser!(eat!(Integer));

fn write(&self) -> String {
fn write<T: crate::writer::MarkupCallback>(&self, callback: &T) -> T::ReturnType {
let trivia = self.trivia;
let variant = match self.value {
IntegerLit::Dec(lit) => lit.0,
IntegerLit::Hex(lit) => lit.0,
IntegerLit::Oct(lit) => lit.0,
};
format!("{trivia}{variant}")
T::ReturnType::wrap(&[T::ReturnType::new(trivia), T::ReturnType::new(variant)])
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::Parsed;

pub fn walk_markup<T: MarkupCallback>(parsed: &Parsed, callback: &T) -> T::ReturnType {
// TODO: Can't write() do the walk?
// Pass `Vec<dyn Parse>` to callbacks so that they can get the current tree ancestors
// Both webidl2.js and widlparser support construct-level markup but I don't think neither of ReSpec/bikeshed need it
// ReSpec: it's used but it doesn't really have to be a block?
// Let's just defer it as theoretically it's still possible to implement it later
for def in parsed.definitions.iter() {}
}

// TODO: I don't think this kind of fancy structure can work with Wasm... or can it?
// take a look at wasm-bindgen

pub trait MarkupElement : Sized {
fn new(s: &str) -> Self;
fn wrap(items: &[Self]) -> Self;
}

pub trait MarkupCallback {
type ReturnType: MarkupElement;

fn identifier(&self) -> Self::ReturnType;
}

0 comments on commit 65ff4a7

Please sign in to comment.