swc_xml_parser/parser/
input.rs

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
use std::{fmt::Debug, mem::take};

use swc_common::{BytePos, Span};
use swc_xml_ast::{Token, TokenAndSpan};

use super::PResult;
use crate::error::Error;

pub trait ParserInput: Iterator<Item = TokenAndSpan> {
    fn start_pos(&mut self) -> BytePos;

    fn last_pos(&mut self) -> BytePos;

    fn take_errors(&mut self) -> Vec<Error>;
}

#[derive(Debug)]
pub(super) struct Buffer<I>
where
    I: ParserInput,
{
    cur: Option<TokenAndSpan>,
    input: I,
}

impl<I> Buffer<I>
where
    I: ParserInput,
{
    pub fn new(input: I) -> Self {
        Buffer { cur: None, input }
    }

    /// Last start position
    pub fn start_pos(&mut self) -> PResult<BytePos> {
        Ok(self.input.start_pos())
    }

    /// Last end position
    pub fn last_pos(&mut self) -> PResult<BytePos> {
        Ok(self.input.last_pos())
    }

    pub fn cur_span(&mut self) -> PResult<Span> {
        if self.cur.is_none() {
            self.bump_inner()?;
        }

        Ok(self.cur.as_ref().map(|cur| cur.span).unwrap_or_default())
    }

    pub fn cur(&mut self) -> PResult<Option<&Token>> {
        if self.cur.is_none() {
            self.bump_inner()?;
        }

        Ok(self.cur.as_ref().map(|v| &v.token))
    }

    #[track_caller]
    pub fn bump(&mut self) -> PResult<Option<TokenAndSpan>> {
        debug_assert!(
            self.cur.is_some(),
            "bump() is called without checking current token"
        );

        let token = self.cur.take();

        Ok(token)
    }

    fn bump_inner(&mut self) -> PResult<()> {
        self.cur = None;

        if self.cur.is_none() {
            let result = self.input.next();

            if let Some(result) = result {
                self.cur = Some(result);
            } else {
                return Ok(());
            }
        }

        Ok(())
    }

    pub fn take_errors(&mut self) -> Vec<Error> {
        take(&mut self.input.take_errors())
    }
}