-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup tokens so they are usable. Also some reformatting through rustfmt
- Loading branch information
Showing
11 changed files
with
3,198 additions
and
3,217 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,68 @@ | ||
use std::fmt::{Display, Formatter}; | ||
use crate::html5_parser::tokenizer::Token; | ||
|
||
pub(crate) trait Emitter: Display { | ||
fn emit(&mut self, t: Token); | ||
} | ||
|
||
// Emitter that will send the output to a string | ||
struct StrEmitter { | ||
output: String | ||
} | ||
|
||
impl StrEmitter { | ||
pub fn new() -> Self { | ||
StrEmitter { | ||
output: String::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Display for StrEmitter { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.output) | ||
} | ||
} | ||
|
||
impl Emitter for StrEmitter { | ||
fn emit(&mut self, _t: Token) { | ||
// self.output.add(&*t.to_string()); | ||
} | ||
} | ||
|
||
// Default emitter that will emit tokens to the std output | ||
pub struct IoEmitter { | ||
} | ||
|
||
impl IoEmitter { | ||
pub fn new() -> Self { | ||
IoEmitter{} | ||
} | ||
} | ||
|
||
impl Display for IoEmitter { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "") | ||
} | ||
} | ||
|
||
// Implement the emit() function | ||
impl Emitter for IoEmitter { | ||
fn emit(&mut self, t: Token) { | ||
println!("{}", t.to_string()); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
|
||
// #[test] | ||
// fn test_emit() { | ||
// let e = StrEmitter::new(); | ||
// e.emit(Token::String(String::from("hello world"))); | ||
// assert_eq!(e.output, "hello world"); | ||
// | ||
// let e = StrEmitter::new(); | ||
// e.emit(Token::StartTag(StartTag::new("tag", true, None, ""))); | ||
// assert_eq!(e.output, "<tag/>"); | ||
// } | ||
} | ||
|
||
use crate::html5_parser::token::Token; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
pub(crate) trait Emitter: Display { | ||
fn emit(&mut self, t: Token); | ||
} | ||
|
||
// Emitter that will send the output to a string | ||
struct StrEmitter { | ||
output: String, | ||
} | ||
|
||
impl StrEmitter { | ||
pub fn new() -> Self { | ||
StrEmitter { | ||
output: String::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Display for StrEmitter { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.output) | ||
} | ||
} | ||
|
||
impl Emitter for StrEmitter { | ||
fn emit(&mut self, _t: Token) { | ||
// self.output.add(&*t.to_string()); | ||
} | ||
} | ||
|
||
// Default emitter that will emit tokens to the std output | ||
pub struct IoEmitter {} | ||
|
||
impl IoEmitter { | ||
pub fn new() -> Self { | ||
IoEmitter {} | ||
} | ||
} | ||
|
||
impl Display for IoEmitter { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "") | ||
} | ||
} | ||
|
||
// Implement the emit() function | ||
impl Emitter for IoEmitter { | ||
fn emit(&mut self, t: Token) { | ||
println!("{}", t.to_string()); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
|
||
// #[test] | ||
// fn test_emit() { | ||
// let e = StrEmitter::new(); | ||
// e.emit(Token::String(String::from("hello world"))); | ||
// assert_eq!(e.output, "hello world"); | ||
// | ||
// let e = StrEmitter::new(); | ||
// e.emit(Token::StartTag(StartTag::new("tag", true, None, ""))); | ||
// assert_eq!(e.output, "<tag/>"); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,41 @@ | ||
pub mod input_stream; | ||
|
||
mod node; | ||
mod tokenizer; | ||
mod token_replacements; | ||
mod token_states; | ||
mod consume_char_refs; | ||
mod token_named_characters; | ||
mod emitter; | ||
|
||
use tokenizer::Tokenizer; | ||
use input_stream::InputStream; | ||
use node::Node; | ||
|
||
pub struct Html5Parser<'a> { | ||
tokenizer: Tokenizer<'a>, | ||
} | ||
|
||
impl<'a> Html5Parser<'a> { | ||
// Creates a new parser object with the given input stream | ||
pub fn new(stream: &'a mut InputStream) -> Self { | ||
Html5Parser { | ||
tokenizer: Tokenizer::new(stream), | ||
} | ||
} | ||
|
||
// Parses the input stream into a Node tree | ||
pub fn parse(&mut self) -> Node { | ||
// Tokenize stuff | ||
|
||
for _ in 1..=20 { | ||
let t = self.tokenizer.next_token(); | ||
println!("{}", t); | ||
} | ||
|
||
let mut n = Node::new("root"); | ||
n.add_child(Node::new("child")); | ||
return n; | ||
} | ||
} | ||
pub mod input_stream; | ||
|
||
mod consume_char_refs; | ||
mod emitter; | ||
mod node; | ||
mod token; | ||
mod token_named_characters; | ||
mod token_replacements; | ||
mod token_states; | ||
mod tokenizer; | ||
|
||
use input_stream::InputStream; | ||
use node::Node; | ||
use tokenizer::Tokenizer; | ||
|
||
pub struct Html5Parser<'a> { | ||
tokenizer: Tokenizer<'a>, | ||
} | ||
|
||
impl<'a> Html5Parser<'a> { | ||
// Creates a new parser object with the given input stream | ||
pub fn new(stream: &'a mut InputStream) -> Self { | ||
Html5Parser { | ||
tokenizer: Tokenizer::new(stream), | ||
} | ||
} | ||
|
||
// Parses the input stream into a Node tree | ||
pub fn parse(&mut self) -> Node { | ||
// Tokenize stuff | ||
|
||
for _ in 1..=20 { | ||
let t = self.tokenizer.next_token(); | ||
println!("{}", t.to_string()); | ||
} | ||
|
||
let mut n = Node::new("root"); | ||
n.add_child(Node::new("child")); | ||
return n; | ||
} | ||
} |
Oops, something went wrong.