#![deny(unsafe_code)]
use super::scan::{Cursor, Span, Tok};
pub fn lex(src: &str) -> Vec<Span> {
let mut out: Vec<Span> = Vec::new();
let mut c = Cursor::new(src);
let mut code_from = 0usize;
let mut templates: Vec<usize> = Vec::new();
let mut depth = 0usize;
let mut prev = 0u8;
let mut word = String::new();
let mut in_word = false;
while !c.done() {
let start = c.at;
let b = match c.peek(0) {
Some(b) => b,
None => break,
};
let tok = if c.at_str(b"//") {
c.skip_line();
Some(Tok::Line { doc: false })
} else if c.at_str(b"/*") {
let doc = c.peek(2) == Some(b'*');
block_comment(&mut c);
Some(Tok::Block { doc })
} else if b == b'/' && regex_here(prev, &word) {
regex(&mut c);
Some(Tok::Str)
} else if b == b'"' || b == b'\'' {
quoted(&mut c, b);
Some(Tok::Str)
} else if b == b'`' {
c.bump();
if template_body(&mut c) {
templates.push(depth);
depth += 1;
}
Some(Tok::Str)
} else {
match b {
b'{' => depth += 1,
b'}' => {
depth = depth.saturating_sub(1);
if templates.last() == Some(&depth) {
templates.pop();
c.bump();
if template_body(&mut c) {
templates.push(depth);
depth += 1;
}
flush(&mut out, &mut code_from, start);
out.push(Span {
tok: Tok::Str,
start,
end: c.at,
});
code_from = c.at;
prev = b'`';
continue;
}
}
_ => {}
}
c.bump();
match is_ident(b) {
true => {
if !in_word {
word.clear();
in_word = true;
}
word.push(b as char);
}
false => {
in_word = false;
if !b.is_ascii_whitespace() {
word.clear();
}
}
}
if !b.is_ascii_whitespace() {
prev = b;
}
None
};
if let Some(tok) = tok {
flush(&mut out, &mut code_from, start);
out.push(Span {
tok,
start,
end: c.at,
});
code_from = c.at;
if !matches!(tok, Tok::Line { .. } | Tok::Block { .. }) {
prev = b'x';
word.clear();
in_word = false;
}
}
}
if code_from < src.len() {
out.push(Span {
tok: Tok::Code,
start: code_from,
end: src.len(),
});
}
out
}
fn flush(out: &mut Vec<Span>, code_from: &mut usize, start: usize) {
if start > *code_from {
out.push(Span {
tok: Tok::Code,
start: *code_from,
end: start,
});
}
*code_from = start;
}
fn block_comment(c: &mut Cursor) {
c.skip(2);
while !c.done() {
if c.at_str(b"*/") {
c.skip(2);
return;
}
c.bump();
}
}
const OPERAND_EXPECTED: [&str; 14] = [
"return", "typeof", "instanceof", "in", "of", "new", "delete", "void",
"throw", "case", "do", "else", "yield", "await",
];
fn is_ident(b: u8) -> bool {
b.is_ascii_alphanumeric() || b == b'_' || b == b'$'
}
fn regex_here(prev: u8, word: &str) -> bool {
if is_ident(prev) {
return OPERAND_EXPECTED.contains(&word);
}
!matches!(prev, b')' | b']' | b'}' | b'<')
}
fn regex(c: &mut Cursor) {
c.bump();
let mut in_class = false;
while let Some(b) = c.bump() {
match b {
b'\\' => {
c.bump();
}
b'[' => in_class = true,
b']' => in_class = false,
b'/' if !in_class => return,
b'\n' => return, _ => {}
}
}
}
fn quoted(c: &mut Cursor, quote: u8) {
c.bump();
while let Some(b) = c.bump() {
match b {
b'\\' => {
c.bump();
}
b'\n' => return,
b if b == quote => return,
_ => {}
}
}
}
fn template_body(c: &mut Cursor) -> bool {
while let Some(b) = c.peek(0) {
match b {
b'\\' => {
c.skip(2);
}
b'`' => {
c.bump();
return false;
}
b'$' if c.peek(1) == Some(b'{') => {
c.skip(2);
return true;
}
_ => {
c.bump();
}
}
}
false
}
pub fn balanced(src: &str, toks: &[Span]) -> bool {
let bytes = src.as_bytes();
let (mut curly, mut square, mut round) = (0i32, 0i32, 0i32);
for s in toks {
match s.tok {
Tok::Line { .. } => continue,
Tok::Block { .. } => {
if s.end == src.len() && !src[s.start..s.end].ends_with("*/") {
return false;
}
continue;
}
Tok::Str => {
if s.end == src.len() && src[s.start..s.end].starts_with('`') {
let body = &src[s.start..s.end];
if body.len() < 2 || !body.ends_with('`') {
return false;
}
}
continue;
}
Tok::Code => {}
}
for &b in &bytes[s.start..s.end] {
match b {
b'{' => curly += 1,
b'}' => curly -= 1,
b'[' => square += 1,
b']' => square -= 1,
b'(' => round += 1,
b')' => round -= 1,
_ => {}
}
if curly < 0 || square < 0 || round < 0 {
return false;
}
}
}
curly == 0 && square == 0 && round == 0
}
#[cfg(test)]
#[path = "ts_tests.rs"]
mod tests;