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
use crate::{SyntaxKind::EOF, TextRange, TextSize, Token};
use rslint_lexer::is_linebreak;
use std::collections::HashSet;

/// The source of tokens for the parser
pub struct TokenSource<'t> {
    source: &'t str,
    /// Hashset of offsets for tokens which occur after a linebreak.
    /// This is required for things such as ASI and postfix expressions
    tokens_after_linebreaks: HashSet<TextSize>,

    /// A vector of tokens and their offset from the start.
    token_offset_pairs: Vec<(rslint_lexer::Token, TextSize)>,
    /// A list of the tokens including whitespace.
    pub raw_tokens: &'t [rslint_lexer::Token],

    /// Current token and position
    cur: (Token, usize),
}

fn mk_token(pos: usize, token_offset_pairs: &[(rslint_lexer::Token, TextSize)]) -> Token {
    let kind = match token_offset_pairs.get(pos) {
        Some((token, _)) => token.kind,
        None => EOF,
    };
    let range = token_offset_pairs
        .get(pos)
        .map(|x| {
            let start: usize = x.1.into();
            let end = start + x.0.len;
            start..end
        })
        .unwrap_or_else(|| {
            token_offset_pairs
                .last()
                .map(|x| {
                    let start: usize = x.1.into();
                    let end = start + x.0.len;
                    start..end
                })
                .unwrap_or(0..0)
        });

    Token {
        kind,
        range: range.to_owned(),
        len: TextSize::from(range.len() as u32),
    }
}

impl<'t> TokenSource<'t> {
    /// Generate input from tokens(except comments and whitespace).
    ///
    /// # Panics
    /// This method will panic in case the source and raw tokens do not match
    /// as it relies on the source code for checking if trivia contains linebreaks
    pub fn new(source: &'t str, raw_tokens: &'t [rslint_lexer::Token]) -> TokenSource<'t> {
        let mut tokens_after_linebreaks = HashSet::new();
        let mut token_offset_pairs = Vec::with_capacity(raw_tokens.len() / 2);

        let mut len: TextSize = 0.into();
        let mut has_linebreak = false;

        for token in raw_tokens {
            if token.kind.is_trivia() {
                let src = source
                    .get(len.into()..(usize::from(len) + token.len))
                    .expect("src and tokens do not match");
                if !has_linebreak && src.chars().any(is_linebreak) {
                    has_linebreak = true;
                }
            } else {
                if has_linebreak {
                    tokens_after_linebreaks.insert(len);
                    has_linebreak = false;
                }
                token_offset_pairs.push((*token, len));
            };

            len += TextSize::from(token.len as u32);
        }

        let first = mk_token(0, token_offset_pairs.as_slice());
        TokenSource {
            source,
            token_offset_pairs,
            cur: (first, 0),
            tokens_after_linebreaks,
            raw_tokens,
        }
    }

    /// Rewind the current position to a former position.
    pub fn rewind(&mut self, pos: usize) {
        self.cur = (mk_token(pos, &self.token_offset_pairs), pos);
    }

    pub fn last(&self) -> Option<Token> {
        if self.cur.1 == 0 {
            return None;
        }
        Some(mk_token(self.cur.1 - 1, &self.token_offset_pairs))
    }

    pub fn current(&self) -> Token {
        self.cur.0.to_owned()
    }

    pub fn source(&self) -> &str {
        self.source
    }

    pub fn lookahead_nth(&self, n: usize) -> Token {
        mk_token(self.cur.1 + n, &self.token_offset_pairs)
    }

    pub fn bump(&mut self) {
        if self.cur.0.kind == EOF {
            return;
        }

        let pos = self.cur.1 + 1;
        self.cur = (mk_token(pos, &self.token_offset_pairs), pos);
    }

    pub fn is_keyword(&self, kw: &str) -> bool {
        self.token_offset_pairs
            .get(self.cur.1)
            .map(|(token, offset)| {
                &self.source[TextRange::at(*offset, TextSize::from(token.len as u32))] == kw
            })
            .unwrap_or(false)
    }

    pub fn had_linebreak_before_nth(&self, n: usize) -> bool {
        if let Some(i) = self.token_offset_pairs.get(self.cur.1 + n) {
            self.tokens_after_linebreaks.contains(&i.1)
        } else {
            false
        }
    }

    pub fn cur_pos(&self) -> usize {
        self.token_offset_pairs[self.cur.1].1.into()
    }

    pub fn cur_token_idx(&self) -> usize {
        self.cur.1
    }

    pub fn size_hint(&self) -> usize {
        self.token_offset_pairs.len()
    }
}