// ===============================
// 🟨 JavaScript Comment Parser
// ===============================
// A JavaScript file consists of comments, code, and string literals.
js_file = { SOI ~ (comment | str_literal | template_literal | any_non_comment)* ~ EOI }
// ===============================
// 📌 Comment Extraction
// ===============================
// Single-line comments: match '//' followed by any characters until newline.
line_comment = @{
"//" ~ (!NEWLINE ~ ANY)*
}
// Block comments: match C-style block comments "/* ... */".
block_comment = @{
"/*" ~ (!"*/" ~ ANY)* ~ "*/"
}
// General comment rule: captures both line comments and block comments.
comment = { line_comment | block_comment }
// ===============================
// 🚫 Ignoring String Literals
// ===============================
// String literals: either double-quoted or single-quoted strings.
str_literal = _{
"\"" ~ (!("\"" | "\\") ~ ANY | "\\" ~ ANY)* ~ "\"" |
"'" ~ (!("'" | "\\") ~ ANY | "\\" ~ ANY)* ~ "'"
}
// Template literals: backtick-delimited strings with interpolation.
template_literal = _{
"`" ~ (!("`" | "\\") ~ ANY | "\\" ~ ANY)* ~ "`"
}
// ===============================
// ❌ Any Other Non-Comment Code
// ===============================
// Anything that is NOT a comment or a string literal.
any_non_comment = { !(comment | str_literal | template_literal) ~ ANY }