luau_lexer/token/
comment.rs

1//! [`Comment`] struct
2
3use smol_str::SmolStr;
4
5use crate::prelude::{Lexable, Lexer, LuauString};
6
7/// A comment.
8#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
9#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
10pub enum Comment {
11    ///```lua
12    /// -- single line
13    /// ```
14    SingleLine(SmolStr),
15
16    ///```lua
17    /// --[[ multiline ]]
18    /// --[[
19    ///     multiline
20    /// ]]
21    /// --[==[
22    ///     multiline
23    /// ]==]
24    /// ```
25    MultiLine(SmolStr),
26}
27
28impl Comment {
29    /// Parses a [`Comment::SingleLine`].
30    fn parse_inner(lexer: &mut Lexer) -> SmolStr {
31        let mut characters = vec!['-', '-'];
32
33        while let Some(character) = lexer.current_char() {
34            if character == '\n' || character == '\r'  {
35                break;
36            }
37
38            characters.push(character);
39            lexer.increment_position_by_char(character);
40        }
41
42        characters.iter().collect::<String>().into()
43    }
44}
45
46impl Lexable for Comment {
47    fn try_lex(lexer: &mut Lexer) -> Option<Self> {
48        if lexer.current_char() == Some('[') {
49            Some(Self::MultiLine(
50                format!("--{}", LuauString::parse_multi_line(lexer)).into(),
51            ))
52        } else {
53            Some(Self::SingleLine(Self::parse_inner(lexer)))
54        }
55    }
56}