| 1 | //! The parser. |
| 2 | |
| 3 | use craterust-analyzer cargo svg 0.18.0 crate/::noderust-analyzer cargo svg 0.18.0 node/::elementrust-analyzer cargo svg 0.18.0 node/element/::tagrust-analyzer cargo svg 0.18.0 node/element/tag/::{Tagrust-analyzer cargo svg 0.18.0 node/element/tag/Tag#, Typerust-analyzer cargo svg 0.18.0 node/element/tag/Type#}; |
| 4 | use craterust-analyzer cargo svg 0.18.0 crate/::noderust-analyzer cargo svg 0.18.0 node/::Attributesrust-analyzer cargo svg 0.18.0 node/Attributes#; |
| 5 | |
| 6 | mod errorrust-analyzer cargo svg 0.18.0 parser/error/; |
| 7 | mod readerrust-analyzer cargo svg 0.18.0 parser/reader/; |
| 8 | |
| 9 | pub use selfrust-analyzer cargo svg 0.18.0 parser/::errorrust-analyzer cargo svg 0.18.0 parser/error/::Errorrust-analyzer cargo svg 0.18.0 parser/error/Error#; |
| 10 | |
| 11 | #[doc(hidden)] |
| 12 | pub use selfrust-analyzer cargo svg 0.18.0 parser/::readerrust-analyzer cargo svg 0.18.0 parser/reader/::Readerrust-analyzer cargo svg 0.18.0 parser/reader/Reader#; |
| 13 | |
| 14 | /// A parser. |
| 15 | pub struct Parserrust-analyzer cargo svg 0.18.0 parser/Parser#<'l> { |
| 16 | readerrust-analyzer cargo svg 0.18.0 parser/Parser#reader.: Readerrust-analyzer cargo svg 0.18.0 parser/reader/Reader#<'l>, |
| 17 | } |
| 18 | |
| 19 | /// An event. |
| 20 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 21 | pub enum Eventrust-analyzer cargo svg 0.18.0 parser/Event#<'l> { |
| 22 | /// An error. |
| 23 | Errorrust-analyzer cargo svg 0.18.0 parser/Event#Error#(Errorrust-analyzer cargo svg 0.18.0 parser/error/Error#), |
| 24 | /// A tag. |
| 25 | Tagrust-analyzer cargo svg 0.18.0 parser/Event#Tag#(&'l str, Typerust-analyzer cargo svg 0.18.0 node/element/tag/Type#, Attributesrust-analyzer cargo svg 0.18.0 node/Attributes#), |
| 26 | /// A text. |
| 27 | Textrust-analyzer cargo svg 0.18.0 parser/Event#Text#(&'l str), |
| 28 | /// A comment. |
| 29 | Commentrust-analyzer cargo svg 0.18.0 parser/Event#Comment#(&'l str), |
| 30 | /// A declaration. |
| 31 | Declarationrust-analyzer cargo svg 0.18.0 parser/Event#Declaration#(&'l str), |
| 32 | /// An instruction. |
| 33 | Instructionrust-analyzer cargo svg 0.18.0 parser/Event#Instruction#(&'l str), |
| 34 | } |
| 35 | |
| 36 | /// A result. |
| 37 | pub type Resultrust-analyzer cargo svg 0.18.0 parser/Result#<T> = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo svg 0.18.0 parser/error/Error#>; |
| 38 | |
| 39 | macro_rules! raiserust-analyzer cargo svg 0.18.0 parser/raise!( |
| 40 | ($parser:expr, $($argument:tt)*) => ( |
| 41 | return Some(Event::Error(Error::new($parser.reader.position(), format!($($argument)*)))) |
| 42 | ); |
| 43 | ); |
| 44 | |
| 45 | impl<'l> Parserrust-analyzer cargo svg 0.18.0 parser/Parser#<'l> { |
| 46 | /// Create a parser. |
| 47 | #[inline] |
| 48 | pub fn newrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]new().(content: &'l str) -> Selfrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`] { |
| 49 | Parserrust-analyzer cargo svg 0.18.0 parser/Parser# { |
| 50 | readerrust-analyzer cargo svg 0.18.0 parser/Parser#reader.: Readerrust-analyzer cargo svg 0.18.0 parser/reader/Reader#::newrust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]new().(content), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn next_anglerust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]next_angle().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Eventrust-analyzer cargo svg 0.18.0 parser/Event#<'l>> { |
| 55 | let content: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# = self.readerrust-analyzer cargo svg 0.18.0 parser/Parser#reader..peek_manyrust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]peek_many().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(4).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 56 | if content.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() { |
| 57 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 58 | } else if content.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("<!--") { |
| 59 | self.read_commentrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]read_comment().() |
| 60 | } else if content.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("<!") { |
| 61 | self.read_declarationrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]read_declaration().() |
| 62 | } else if content.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("<?") { |
| 63 | self.read_instructionrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]read_instruction().() |
| 64 | } else if content.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('<') { |
| 65 | self.read_tagrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]read_tag().() |
| 66 | } else { |
| 67 | raiserust-analyzer cargo svg 0.18.0 parser/raise!!(self, "found an unknown sequence"); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn next_textrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]next_text().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Eventrust-analyzer cargo svg 0.18.0 parser/Event#<'l>> { |
| 72 | self.readerrust-analyzer cargo svg 0.18.0 parser/Parser#reader. |
| 73 | .capturerust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]capture().(|reader| reader.consume_until_charrust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]consume_until_char().('<')) |
| 74 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Textrust-analyzer cargo svg 0.18.0 parser/Event#Text#) |
| 75 | } |
| 76 | |
| 77 | fn read_commentrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]read_comment().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Eventrust-analyzer cargo svg 0.18.0 parser/Event#<'l>> { |
| 78 | match self.readerrust-analyzer cargo svg 0.18.0 parser/Parser#reader..capturerust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]capture().(|reader| reader.consume_commentrust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]consume_comment().()) { |
| 79 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => raiserust-analyzer cargo svg 0.18.0 parser/raise!!(self, "found a malformed comment"), |
| 80 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(content) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Commentrust-analyzer cargo svg 0.18.0 parser/Event#Comment#(content)), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | fn read_declarationrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]read_declaration().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Eventrust-analyzer cargo svg 0.18.0 parser/Event#<'l>> { |
| 85 | match self.readerrust-analyzer cargo svg 0.18.0 parser/Parser#reader..capturerust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]capture().(|reader| reader.consume_declarationrust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]consume_declaration().()) { |
| 86 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => raiserust-analyzer cargo svg 0.18.0 parser/raise!!(self, "found a malformed declaration"), |
| 87 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(content) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Declarationrust-analyzer cargo svg 0.18.0 parser/Event#Declaration#(content)), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn read_instructionrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]read_instruction().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Eventrust-analyzer cargo svg 0.18.0 parser/Event#<'l>> { |
| 92 | match self.readerrust-analyzer cargo svg 0.18.0 parser/Parser#reader..capturerust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]capture().(|reader| reader.consume_instructionrust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]consume_instruction().()) { |
| 93 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => raiserust-analyzer cargo svg 0.18.0 parser/raise!!(self, "found a malformed instruction"), |
| 94 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(content) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Instructionrust-analyzer cargo svg 0.18.0 parser/Event#Instruction#(content)), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fn read_tagrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]read_tag().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Eventrust-analyzer cargo svg 0.18.0 parser/Event#<'l>> { |
| 99 | match self.readerrust-analyzer cargo svg 0.18.0 parser/Parser#reader..capturerust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]capture().(|reader| reader.consume_tagrust-analyzer cargo svg 0.18.0 parser/reader/impl#[`Reader<'l>`]consume_tag().()) { |
| 100 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => raiserust-analyzer cargo svg 0.18.0 parser/raise!!(self, "found a malformed tag"), |
| 101 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(content) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(match Tagrust-analyzer cargo svg 0.18.0 node/element/tag/Tag#::parserust-analyzer cargo svg 0.18.0 node/element/tag/impl#[`Tag<'l>`]parse().(&content[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#content.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) { |
| 102 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Tagrust-analyzer cargo svg 0.18.0 node/element/tag/Tag#(name, kind, attributes)) => Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Tagrust-analyzer cargo svg 0.18.0 parser/Event#Tag#(name, kind, attributes), |
| 103 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Errorrust-analyzer cargo svg 0.18.0 parser/Event#Error#(error), |
| 104 | }), |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl<'l> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Parserrust-analyzer cargo svg 0.18.0 parser/Parser#<'l> { |
| 110 | type Itemrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`][Iterator]Item# = Eventrust-analyzer cargo svg 0.18.0 parser/Event#<'l>; |
| 111 | |
| 112 | fn nextrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 113 | self.next_textrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]next_text().().or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| self.next_anglerust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]next_angle().()) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | #[cfg(test)] |
| 118 | mod testsrust-analyzer cargo svg 0.18.0 parser/tests/ { |
| 119 | use craterust-analyzer cargo svg 0.18.0 crate/::parserrust-analyzer cargo svg 0.18.0 parser/::{Eventrust-analyzer cargo svg 0.18.0 parser/Event#, Parserrust-analyzer cargo svg 0.18.0 parser/Parser#}; |
| 120 | |
| 121 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 122 | fn next_tagrust-analyzer cargo svg 0.18.0 parser/tests/next_tag().() { |
| 123 | macro_rules! testrust-analyzer cargo svg 0.18.0 parser/tests/test!( |
| 124 | ($content:expr, $value:expr) => ({ |
| 125 | let mut parser = Parser::new($content); |
| 126 | match parser.next().unwrap() { |
| 127 | Event::Tag(value, _, _) => assert_eq!(value, $value), |
| 128 | _ => unreachable!(), |
| 129 | } |
| 130 | }) |
| 131 | ); |
| 132 | |
| 133 | testrust-analyzer cargo svg 0.18.0 parser/tests/test!!("<foo>", "foo"); |
| 134 | testrust-analyzer cargo svg 0.18.0 parser/tests/test!!("<foo/>", "foo"); |
| 135 | testrust-analyzer cargo svg 0.18.0 parser/tests/test!!(" <foo/>", "foo"); |
| 136 | } |
| 137 | |
| 138 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 139 | fn next_textrust-analyzer cargo svg 0.18.0 parser/tests/next_text().() { |
| 140 | macro_rules! testrust-analyzer cargo svg 0.18.0 parser/tests/test!( |
| 141 | ($content:expr, $value:expr) => ({ |
| 142 | let mut parser = Parser::new($content); |
| 143 | match parser.next().unwrap() { |
| 144 | Event::Text(value) => assert_eq!(value, $value), |
| 145 | _ => unreachable!(), |
| 146 | } |
| 147 | }) |
| 148 | ); |
| 149 | |
| 150 | testrust-analyzer cargo svg 0.18.0 parser/tests/test!!("foo <bar>", "foo"); |
| 151 | testrust-analyzer cargo svg 0.18.0 parser/tests/test!!(" foo<bar>", "foo"); |
| 152 | testrust-analyzer cargo svg 0.18.0 parser/tests/test!!("foo> <bar>", "foo>"); |
| 153 | } |
| 154 | } |