From 2c509692baf8bec5cdf9658c49c262599f89892d Mon Sep 17 00:00:00 2001 From: Rerumu Date: Fri, 5 Nov 2021 23:12:07 -0400 Subject: [PATCH 1/3] Add shebang support --- Ast/src/Parser.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Ast/src/Parser.cpp b/Ast/src/Parser.cpp index ee84542d5..47a910d2a 100644 --- a/Ast/src/Parser.cpp +++ b/Ast/src/Parser.cpp @@ -160,6 +160,9 @@ ParseResult Parser::parse(const char* buffer, size_t bufferSize, AstNameTable& n { std::vector hotcomments; + if (p.lexer.current().type == '#' && p.lexer.lookahead().type == '!') + p.lexer.nextline(); + while (isComment(p.lexer.current()) || (FFlag::LuauCaptureBrokenCommentSpans && p.lexer.current().type == Lexeme::BrokenComment)) { const char* text = p.lexer.current().data; From b37e6b7db8578aaba3af5a6c536527b397a38660 Mon Sep 17 00:00:00 2001 From: Rerumu Date: Sun, 7 Nov 2021 21:09:41 -0500 Subject: [PATCH 2/3] Move shebang skip to readFile --- Ast/src/Parser.cpp | 3 --- CLI/FileUtils.cpp | 11 +++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Ast/src/Parser.cpp b/Ast/src/Parser.cpp index 47a910d2a..ee84542d5 100644 --- a/Ast/src/Parser.cpp +++ b/Ast/src/Parser.cpp @@ -160,9 +160,6 @@ ParseResult Parser::parse(const char* buffer, size_t bufferSize, AstNameTable& n { std::vector hotcomments; - if (p.lexer.current().type == '#' && p.lexer.lookahead().type == '!') - p.lexer.nextline(); - while (isComment(p.lexer.current()) || (FFlag::LuauCaptureBrokenCommentSpans && p.lexer.current().type == Lexeme::BrokenComment)) { const char* text = p.lexer.current().data; diff --git a/CLI/FileUtils.cpp b/CLI/FileUtils.cpp index 0702b74f1..ae326f3e1 100644 --- a/CLI/FileUtils.cpp +++ b/CLI/FileUtils.cpp @@ -67,6 +67,17 @@ std::optional readFile(const std::string& name) if (read != size_t(length)) return std::nullopt; + // Skip first line if it's a comment + if (length != 0 && result[0] == '#') + { + auto newLine = result.find('\n'); + + if (newLine != std::string::npos) + result.erase(0, newLine); + else + result.clear(); + } + return result; } From 488a588aea080767896e282f995a16fc954cf352 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 9 Dec 2021 11:56:40 -0800 Subject: [PATCH 3/3] Detect shebang more precisely and simplify erase code --- CLI/FileUtils.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/CLI/FileUtils.cpp b/CLI/FileUtils.cpp index ae326f3e1..7673492a9 100644 --- a/CLI/FileUtils.cpp +++ b/CLI/FileUtils.cpp @@ -67,16 +67,9 @@ std::optional readFile(const std::string& name) if (read != size_t(length)) return std::nullopt; - // Skip first line if it's a comment - if (length != 0 && result[0] == '#') - { - auto newLine = result.find('\n'); - - if (newLine != std::string::npos) - result.erase(0, newLine); - else - result.clear(); - } + // Skip first line if it's a shebang + if (length > 2 && result[0] == '#' && result[1] == '!') + result.erase(0, result.find('\n')); return result; }