Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashbang lexer support #1631

Merged
merged 21 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions boa/src/syntax/lexer/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
lexer::{Token, TokenKind},
},
};
use core::convert::TryFrom;
use std::io::Read;

/// Lexes a single line comment.
Expand Down Expand Up @@ -90,3 +91,40 @@ impl<R> Tokenizer<R> for MultiLineComment {
))
}
}

///Lexes a first line Hashbang comment
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-ecmascript-language-lexical-grammar

pub(super) struct HashbangComment;

impl<R> Tokenizer<R> for HashbangComment {
fn lex(&mut self, cursor: &mut Cursor<R>, start_pos: Position) -> Result<Token, Error>
where
R: Read,
{
let _timer = BoaProfiler::global().start_event("Hashbang", "Lexing");

loop {
if let Some(ch) = cursor.next_char()? {
jedel1043 marked this conversation as resolved.
Show resolved Hide resolved
if let Ok(c) = char::try_from(ch) {
if c == '\r' || c == '\n' || c == '\u{2028}' || c == '\u{2029}' {
break;
}
} else {
cursor.next_byte()?.expect("No byte returned");
}
} else {
return Err(Error::syntax("unterminated hashbang", cursor.pos()));
}
}

Ok(Token::new(
TokenKind::Comment,
Span::new(start_pos, cursor.pos()),
))
}
}
13 changes: 12 additions & 1 deletion boa/src/syntax/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod token;
mod tests;

use self::{
comment::{MultiLineComment, SingleLineComment},
comment::{HashbangComment, MultiLineComment, SingleLineComment},
cursor::Cursor,
identifier::Identifier,
number::NumberLiteral,
Expand Down Expand Up @@ -191,6 +191,17 @@ impl<R> Lexer<R> {
}
};

//handle hashbang here so the below match block still throws error on
//# if position isn't (1, 1)
if start.column_number() == 1 && start.line_number() == 1 && next_ch == 0x23 {
if let Some(hashbang_peek) = self.cursor.peek()? {
if hashbang_peek == 0x21 {
let _token = HashbangComment.lex(&mut self.cursor, start);
return self.next();
}
}
};

if let Ok(c) = char::try_from(next_ch) {
let token = match c {
'\r' | '\n' | '\u{2028}' | '\u{2029}' => Ok(Token::new(
Expand Down
36 changes: 36 additions & 0 deletions boa/src/syntax/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,39 @@ fn empty_statement() {
],
);
}

#[test]
fn hashbang_use_strict_no_with() {
check_parser(
r#"#!\"use strict"
"#,
vec![]
);
}

#[test]
fn hashbang_use_strict_with_with_statement() {
check_parser(
r#"#!\"use strict"

with({}) {}
"#,
vec![]
);
}

#[test]
fn hashbang_comment() {
check_parser(
r"#!Comment Here",
vec![]
);
}

#[test]
fn hashbang_line_separator_test() {
check_parser(
r"#!This contains a line separator
",
vec![]
);
}