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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::borrow::Cow;

pub use self::error::ParseError;
use self::util::parse_start_tag;

use crate::{parser::util::parse_end_tag, Attribute, Block, BlockName, Section};

mod error;

/// Parse the given input as a Vue SFC.
///
/// # Errors
/// Will return an error if parsing fails.
///
/// # Example
/// ```rust
/// use vue_sfc::{Section, Block};
///
/// let sfc = vue_sfc::parse("<!-- your input -->").unwrap();
///
/// for section in sfc {
///     match section {
///         Section::Block(Block { name, attributes, content }) => {
///             println!(
///                 "Got a block named `{}` with {} attributes, content is {} bytes long.",
///                 name,
///                 attributes.len(),
///                 content.len()
///             )
///         }
///         Section::Raw(content) => {
///             println!(
///                 "Got a raw section, {} bytes long.",
///                 content.len()
///             )
///         }
///     }
/// }
/// ```
pub fn parse(mut input: &str) -> Result<Vec<Section<'_>>, ParseError> {
    #[derive(Debug)]
    enum State<'a> {
        OutsideBlock,
        InsideBlock {
            name: BlockName<'a>,
            attributes: Vec<Attribute<'a>>,
        },
    }

    let mut buffer = Vec::new();
    let mut index = 0;
    let mut state = State::OutsideBlock;

    while !input.is_empty() {
        match state {
            State::OutsideBlock => {
                if let Ok((remaining, (name, attributes))) = parse_start_tag(&input[index..]) {
                    if !input[..index].is_empty() {
                        buffer.push(Section::Raw(Cow::Borrowed(&input[..index])));
                    }

                    state = State::InsideBlock { name, attributes };
                    index = 0;
                    input = remaining;
                } else if let Some(j) = input.get((index + 1)..).and_then(|input| input.find('<')) {
                    index += j + 1;
                } else {
                    if !input.is_empty() {
                        buffer.push(Section::Raw(Cow::Borrowed(input)));
                    }

                    return Ok(buffer);
                }
            }
            State::InsideBlock { ref name, .. } => {
                if let Ok((remaining, _)) = parse_end_tag(name, &input[index..]) {
                    match std::mem::replace(&mut state, State::OutsideBlock) {
                        State::InsideBlock { name, attributes } => {
                            buffer.push(Section::Block(Block {
                                name,
                                attributes,
                                content: Cow::Borrowed(&input[..index]),
                            }));
                        }
                        _ => unreachable!(),
                    }

                    index = 0;
                    input = remaining;
                } else if let Some(j) = input.get((index + 1)..).and_then(|input| input.find('<')) {
                    index += j + 1;
                } else {
                    return Err(ParseError::MissingEndTag(name.as_str().to_owned()));
                }
            }
        }
    }

    Ok(buffer)
}

mod util {
    use crate::{Attribute, AttributeName, AttributeValue, BlockName};

    use nom::{
        branch::alt,
        bytes::complete::{tag_no_case, take_until, take_while1},
        character::complete::{char, multispace0, multispace1},
        combinator::opt,
        multi::many0,
        sequence::{delimited, pair, preceded, tuple},
        IResult, Parser,
    };

    pub fn parse_end_tag<'a, 'b>(name: &'b str, input: &'a str) -> IResult<&'a str, &'a str> {
        delimited(
            tuple((char('<'), char('/'), multispace0)),
            tag_no_case(name),
            tuple((multispace0, char('>'))),
        )
        .parse(input)
    }

    pub fn parse_start_tag(input: &str) -> IResult<&str, (BlockName, Vec<Attribute>)> {
        delimited(
            char('<'),
            tuple((
                preceded(multispace0, parse_start_tag_name),
                many0(preceded(multispace1, parse_start_tag_attribute)),
            )),
            preceded(multispace0, char('>')),
        )
        .parse(input)
    }

    fn parse_start_tag_attribute(input: &str) -> IResult<&str, Attribute> {
        pair(
            parse_start_tag_attribute_name,
            opt(preceded(
                delimited(multispace0, char('='), multispace0),
                alt((
                    delimited(char('\u{0022}'), take_until("\u{0022}"), char('\u{0022}')),
                    delimited(char('\u{0027}'), take_until("\u{0027}"), char('\u{0027}')),
                ))
                .map(AttributeValue::new),
            )),
        )
        .parse(input)
    }

    fn parse_start_tag_attribute_name(input: &str) -> IResult<&str, AttributeName> {
        take_while1(|ch: char| {
            !matches!(
                ch,
                '\u{0009}'
                    | '\u{000A}'
                    | '\u{000C}'
                    | '\u{0020}'
                    | '\u{002F}'
                    | '\u{003D}'
                    | '\u{003E}'
            )
        })
        .map(AttributeName::new)
        .parse(input)
    }

    fn parse_start_tag_name(input: &str) -> IResult<&str, BlockName> {
        take_while1(|ch: char| {
            !matches!(
                ch,
                '\u{0009}' | '\u{000A}' | '\u{000C}' | '\u{0020}' | '\u{002F}' | '\u{003E}'
            )
        })
        .map(BlockName::new)
        .parse(input)
    }
}