Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidawave committed Nov 22, 2024
1 parent 5c0a622 commit bea95b9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
5 changes: 3 additions & 2 deletions parser/examples/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ fn parse_path(
eprintln!("parsing {:?} ({:?} bytes)", path.display(), source.len());
let now = Instant::now();
let mut local_parse_options = *parse_options;
if path.extension().and_then(std::ffi::OsStr::to_str).is_some_and(|ext| ext.ends_with("js")) {
local_parse_options.type_annotations = false;
if let Some(extension) = path.extension().and_then(std::ffi::OsStr::to_str) {
local_parse_options.type_annotations = extension.contains("ts");
local_parse_options.jsx = extension.contains('x');
}

let on = source.clone();
Expand Down
32 changes: 20 additions & 12 deletions parser/src/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,12 +507,12 @@ impl Expression {
}

let name = reader.parse_identifier("variable reference expression")?;
// if keyword.is_invalid_identifier() {
// return Err(ParseError::new(
// ParseErrors::ReservedIdentifier,
// token.get_span(),
// ));
// }
if !crate::lexer::utilities::is_valid_variable_identifier(name) {
return Err(ParseError::new(
ParseErrors::ReservedIdentifier,
start.with_length(name.len()),
));
}

if reader.get_options().interpolation_points && name == crate::marker::MARKER {
let position = start.with_length(0);
Expand Down Expand Up @@ -578,6 +578,17 @@ impl Expression {
is_optional: false,
};
continue;
} else if reader.is_operator_advance("//") {
let content = reader.parse_comment_literal(false)?.to_owned();
let position = top.get_position().union(reader.get_end());
top = Expression::Comment {
is_multiline: false,
content,
position,
on: Box::new(top),
prefix: false,
};
continue;
}

reader.skip();
Expand All @@ -596,14 +607,11 @@ impl Expression {
return Ok(top);
}

// This does `eat` comments that could be statements, oh well
if reader.starts_with_str("//") || reader.starts_with_str("/*") {
let is_multiline = reader.starts_with_str("/*");
reader.advance(2);
let content = reader.parse_comment_literal(is_multiline)?.to_owned();
if reader.is_operator_advance("/*") {
let content = reader.parse_comment_literal(true)?.to_owned();
let position = top.get_position().union(reader.get_end());
top = Expression::Comment {
is_multiline,
is_multiline: true,
content,
position,
on: Box::new(top),
Expand Down
32 changes: 29 additions & 3 deletions parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl<'a> Lexer<'a> {
possibles: &[&'static str],
) -> Result<(&'a str, &'static str), ()> {
let current = self.get_current();
for i in 0.. {
for i in 0..current.len() {
if let Some(until) = possibles.iter().find(|s| current[i..].starts_with(**s)) {
self.head += (i + until.len()) as u32;
return Ok((&current[..i], until));
Expand All @@ -437,7 +437,7 @@ impl<'a> Lexer<'a> {
possibles: &[&'static str],
) -> Result<(&'a str, &'static str), ()> {
let current = self.get_current();
for i in 0.. {
for i in 0..current.len() {
if let Some(until) = possibles.iter().find(|s| current[i..].starts_with(**s)) {
self.head += i as u32;
let content = &current[..i];
Expand Down Expand Up @@ -915,7 +915,10 @@ impl<'a> Lexer<'a> {

pub fn is_semi_colon(&mut self) -> bool {
self.skip();
self.starts_with('}') || self.starts_with(';') || self.last_was_from_new_line() > 0
self.starts_with('}')
|| self.starts_with(';')
|| self.last_was_from_new_line() > 0
|| self.get_current().is_empty()
}

pub fn is_arrow_function(&mut self) -> (bool, Option<crate::types::TypeAnnotation>) {
Expand Down Expand Up @@ -973,6 +976,29 @@ pub(crate) mod utilities {
chr.is_alphanumeric() || chr == '_' || chr == '$'
}

pub fn is_valid_variable_identifier(identifier: &str) -> bool {
!matches!(
identifier,
"const"
| "var" | "if"
| "else" | "for"
| "while" | "do"
| "switch" | "class"
| "function" | "new"
| "super" | "case"
| "return" | "continue"
| "break" | "import"
| "export" | "default"
| "in" | "typeof"
| "instanceof"
| "void" | "delete"
| "debugger" | "try"
| "catch" | "finally"
| "throw" | "extends"
| "enum"
)
}

// TODO move
pub fn next_empty_occurance(on: &str) -> usize {
let mut chars = on.char_indices();
Expand Down
2 changes: 1 addition & 1 deletion parser/src/statements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use try_catch_statement::TryCatchStatement;
use visitable_derive::Visitable;
pub use while_statement::{DoWhileStatement, WhileStatement};

/// A statement. See [Declaration]s and [StatementAndDeclaration] for more
/// A statement. See [Declaration]s and [StatementOrDeclaration] for more
#[apply(derive_ASTNode)]
#[derive(Debug, Clone, Visitable, EnumFrom, EnumTryInto, PartialEqExtras, GetFieldByType)]
#[get_field_by_type_target(Span)]
Expand Down

0 comments on commit bea95b9

Please sign in to comment.