1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
use super::{
entities::{Entity,Instance},
state::EntityState,
};
use crate::{
ParserResult,
Source, SourceEvent, ParserEvent, SourceResult, Local,
Parser, Runtime, PipeParser, IntoPipeParser,
};
/*
Entities: https://www.w3.org/TR/html5/entities.json
Semicolon may be optional (html legacy)
< < <
"&" + Name + ";"
// without parametr entities [ %name; ] ????
NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Name ::= NameStartChar (NameChar)*
*/
#[derive(Debug,Clone)]
pub struct Builder {
}
impl Builder {
pub fn new() -> Builder {
Builder { }
}
pub fn create(self) -> EntityParser {
EntityParser(Runtime::new(()))
}
}
pub struct EntityParser(Runtime<EntityState,Entity,()>);
impl Parser for EntityParser {
type Data = Entity;
fn next_event<S: Source>(&mut self, src: &mut S) -> ParserResult<Entity> {
self.0.next_event(src)
}
}
impl IntoPipeParser for EntityParser {
type Piped = PipedEntityParser;
fn into_piped(self) -> Self::Piped {
PipedEntityParser {
parser: self.0,
tmp: None,
}
}
}
pub struct PipedEntityParser {
parser: Runtime<EntityState,Entity,()>,
tmp: Option<Local<SourceEvent>>,
}
impl PipeParser for PipedEntityParser {
fn next_char<S: Source>(&mut self, src: &mut S) -> SourceResult {
Ok(match self.tmp.take() {
Some(local_se) => Some(local_se),
None => match self.parser.next_event(src)? {
Some(local_ent) => {
let (local,ent) = local_ent.into_inner();
match ent {
ParserEvent::Char(c) => Some(local.local(SourceEvent::Char(c))),
ParserEvent::Breaker(b) => Some(local.local(SourceEvent::Breaker(b))),
ParserEvent::Parsed(ent) => match ent.entity {
Instance::Char(c) => Some(local.local(SourceEvent::Char(c))),
Instance::Char2(c1,c2) => {
self.tmp = Some(local.local(SourceEvent::Char(c2)));
Some(local.local(SourceEvent::Char(c1)))
},
},
}
},
None => None,
},
})
}
}
#[cfg(test)]
mod tests {
//use crate::*;
//use super::*;
/*#[test]
fn basic() {
let mut src = " &blabla; � " & &⪢ 💯 ❤".into_source();
let mut parser = Builder::new().create();
while let Some(local_event) = parser.next_event(&mut src).unwrap() {
println!("{:?}",local_event);
}
panic!();
}
#[test]
fn basic_piped() {
let mut src = " &blabla; � " & &⪢ 💯 ❤".into_source();
let mut parser = Builder::new().create().into_piped();
while let Some(local_se) = parser.next_char(&mut src).unwrap() {
println!("{:?}",local_se);
}
panic!();
}
#[test]
fn basic_piped_2() {
let mut src = " &blabla; � " & &⪢ 💯 ❤"
.into_source()
.pipe(Builder::new().create().into_piped());
while let Some(local_se) = src.next_char().unwrap() {
println!("{:?}",local_se);
}
panic!();
}
#[test]
fn basic_piped_3() {
let mut src = " &blabla; � " & &⪢ 💯 ❤"
.into_source()
.pipe(Builder::new().create().piped(|_| None));
while let Some(local_se) = src.next_char().unwrap() {
println!("{:?}",local_se);
}
panic!();
}
*/
}