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 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 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,36 @@ 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");

while let Some(ch) = cursor.next_char()? {
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");
}
}

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
31 changes: 31 additions & 0 deletions boa/src/syntax/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,34 @@ fn empty_statement() {
],
);
}

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

//commenting out this test for when with is enabled.
/*
#[test]
fn hashbang_use_strict_with_with_statement() {
check_parser(
r#"#!\"use strict"

with({}) {}
"#,
vec![]
);
}
*/
jedel1043 marked this conversation as resolved.
Show resolved Hide resolved

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