Skip to content

Commit

Permalink
dbg! in xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Dec 2, 2023
1 parent 45abd39 commit 273c8a0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
23 changes: 17 additions & 6 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,12 @@ impl Parser {
/// - `bytes`: a slice to search a new XML event. Should contain text in
/// ASCII-compatible encoding
pub fn feed(&mut self, bytes: &[u8]) -> Result<FeedResult, SyntaxError> {
dbg!((self.0, crate::utils::Bytes(bytes)));
for (offset, &byte) in bytes.iter().enumerate() {
let trail = &bytes[offset..];
let start = offset + 1;
let rest = &bytes[start..];
dbg!((self.0, offset, byte as char, crate::utils::Bytes(trail), crate::utils::Bytes(rest)));
self.0 = match self.0 {
State::Start => match byte {
0x00 => State::Bom(BomParser::X00),
Expand Down Expand Up @@ -549,6 +551,7 @@ impl Parser {
/// - `offset`: a position of `bytes` sub-slice in the one that was passed to `feed()`
#[inline]
fn parse_text(&mut self, bytes: &[u8], offset: usize) -> FeedResult {
dbg!((self.0, offset, crate::utils::Bytes(bytes)));
self.0 = State::Text;
match bytes.iter().position(|&b| b == b'<') {
Some(i) => FeedResult::EmitText(offset + i),
Expand All @@ -570,6 +573,7 @@ impl Parser {
offset: usize,
mut parser: CommentParser,
) -> FeedResult {
dbg!((self.0, offset, crate::utils::Bytes(bytes), parser));
match parser.feed(bytes) {
Some(i) => {
self.0 = State::Text;
Expand All @@ -593,6 +597,7 @@ impl Parser {
/// - `offset`: a position of `bytes` sub-slice in the one that was passed to `feed()`
/// - `braces_left`: count of braces that wasn't seen yet in the end of previous data chunk
fn parse_cdata(&mut self, bytes: &[u8], offset: usize, mut parser: CDataParser) -> FeedResult {
dbg!((self.0, offset, crate::utils::Bytes(bytes), parser));
match parser.feed(bytes) {
Some(i) => {
self.0 = State::Text;
Expand All @@ -611,8 +616,9 @@ impl Parser {
offset: usize,
mut parser: QuotedParser,
) -> Result<FeedResult, SyntaxError> {
dbg!((self.0, offset, crate::utils::Bytes(bytes), parser));
// Search `[` (start of DTD definitions) or `>` (end of <!DOCTYPE> tag)
match parser.one_of(bytes) {
match dbg!(parser.one_of(bytes)) {
OneOf::Open(i) => self.parse_dtd(&bytes[i..], offset + i, DtdParser::default()),
OneOf::Close(i) => {
self.0 = State::Text;
Expand All @@ -639,8 +645,9 @@ impl Parser {
mut offset: usize,
mut parser: DtdParser,
) -> Result<FeedResult, SyntaxError> {
dbg!((self.0, offset, crate::utils::Bytes(bytes), parser));
loop {
let result = match parser.feed(bytes) {
let result = match dbg!(parser.feed(bytes)) {
// Skip recognized DTD structure
// TODO: Emit DTD events while parsing
quick_dtd::FeedResult::EmitPI(off)
Expand Down Expand Up @@ -669,7 +676,8 @@ impl Parser {
}

fn parse_doctype_finish(&mut self, bytes: &[u8], offset: usize) -> FeedResult {
match bytes.iter().position(|&b| b == b'>') {
dbg!((self.0, offset, crate::utils::Bytes(bytes)));
match dbg!(bytes.iter().position(|&b| b == b'>')) {
Some(i) => {
self.0 = State::Text;
// +1 for `>` which should be included in event
Expand All @@ -692,7 +700,8 @@ impl Parser {
/// - `offset`: a position of `bytes` sub-slice in the one that was passed to `feed()`
/// - `has_mark`: a flag that indicates was the previous fed data ended with `?`
fn parse_pi(&mut self, bytes: &[u8], offset: usize, mut parser: PiParser) -> FeedResult {
match parser.feed(bytes) {
dbg!((self.0, offset, crate::utils::Bytes(bytes), parser));
match dbg!(parser.feed(bytes)) {
Some(i) => {
self.0 = State::Text;
FeedResult::EmitPI(offset + i)
Expand All @@ -711,7 +720,8 @@ impl Parser {
/// That sub-slice begins on the byte that represents a tag name
/// - `offset`: a position of `bytes` sub-slice in the one that was passed to `feed()`
fn parse_end(&mut self, bytes: &[u8], offset: usize) -> FeedResult {
match bytes.iter().position(|&b| b == b'>') {
dbg!((self.0, offset, crate::utils::Bytes(bytes)));
match dbg!(bytes.iter().position(|&b| b == b'>')) {
Some(i) => {
self.0 = State::Text;
// +1 for `>` which should be included in event
Expand Down Expand Up @@ -740,7 +750,8 @@ impl Parser {
mut parser: QuotedParser,
has_slash: bool,
) -> FeedResult {
match parser.feed(bytes) {
dbg!((self.0, offset, crate::utils::Bytes(bytes), parser, has_slash));
match dbg!(parser.feed(bytes)) {
Some(0) if has_slash => {
self.0 = State::Text;
// +1 for `>` which should be included in event
Expand Down
8 changes: 5 additions & 3 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,22 @@ macro_rules! read_event_impl {
$self:ident, $buf:ident
$(, $await:ident)?
) => {{
dbg!("===============================================================");
if let Some(end) = $self.state.pending_end() {
return Ok(end);
}
// Content in buffer before call is not a part of next event
let start = $buf.len();
let offset = $self.state.offset;
loop {
break match $self.reader.fill_buf() $(.$await)? {
dbg!("--------------------------------");
break match dbg!($self.reader.fill_buf() $(.$await)?) {
Ok(bytes) if bytes.is_empty() => {
let content = &$buf[start..];
if content.is_empty() {
Ok(Event::Eof)
} else
if let Err(error) = $self.state.parser.finish() {
if let Err(error) = dbg!($self.state.parser.finish()) {
$self.state.last_error_offset = offset;
Err(Error::Syntax(error))
} else {
Expand All @@ -226,7 +228,7 @@ macro_rules! read_event_impl {
Ok(Event::Text(BytesText::wrap(content, $self.decoder())))
}
}
Ok(bytes) => match $self.state.parse_into(bytes, $buf)? {
Ok(bytes) => match dbg!($self.state.parse_into(bytes, $buf))? {
ParseOutcome::Consume(offset, result) => {
$self.reader.consume(offset);
$self.state.make_event(result, &$buf[start..])
Expand Down
3 changes: 2 additions & 1 deletion src/reader/slice_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ impl<'a> Reader<&'a [u8]> {
/// ```
#[inline]
pub fn read_event(&mut self) -> Result<Event<'a>> {
dbg!(self.state.parser);
if let Some(end) = self.state.pending_end() {
return Ok(end);
}
loop {
if self.reader.is_empty() {
return Ok(Event::Eof);
}
let result = self.state.parser.feed(self.reader)?;
let result = dbg!(self.state.parser.feed(self.reader))?;
return match result {
FeedResult::NeedData => {
let offset = self.reader.len();
Expand Down
3 changes: 2 additions & 1 deletion src/reader/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ impl ReaderState {
bytes: &'a [u8],
buf: &'b mut Vec<u8>,
) -> Result<ParseOutcome> {
let result = self.parser.feed(bytes)?;
dbg!(&self);
let result = dbg!(self.parser.feed(bytes))?;
match result {
FeedResult::NeedData => {
let mut content = bytes;
Expand Down

0 comments on commit 273c8a0

Please sign in to comment.