From 526d3b148c35e6ba21949e4f6c8d3ec98286dcd8 Mon Sep 17 00:00:00 2001 From: antosha417 Date: Sat, 23 Sep 2023 17:52:21 +0300 Subject: [PATCH] Support shebang. Fixes #351 --- corpus/comments.txt | 15 ++++++++++++++- grammar.js | 6 +++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/corpus/comments.txt b/corpus/comments.txt index 27bc42a..f1354d4 100644 --- a/corpus/comments.txt +++ b/corpus/comments.txt @@ -64,7 +64,7 @@ Using directives //> using jvm graalvm:21 //> using scala 3.3.0 //> using dep foo:bar:1,2,3,url=https://github.com -//> using exclude "examples/*" "*/resources/*" +//> using exclude "examples/*" "*/resources/*" // > just a comment -------------------------------------------------------------------------------- @@ -87,3 +87,16 @@ Using directives (using_directive_key) (using_directive_value))) (comment)) + +================================================================================ +Shebang +================================================================================ + +#!/usr/bin/env -S scala-cli shebang -S 3 + +"shebang" +-------------------------------------------------------------------------------- + +(compilation_unit + (comment) + (string)) diff --git a/grammar.js b/grammar.js index f3b9ccd..fa31f19 100644 --- a/grammar.js +++ b/grammar.js @@ -103,8 +103,10 @@ module.exports = grammar({ rules: { // TopStats ::= TopStat {semi TopStat} - compilation_unit: $ => + compilation_unit: $ => seq( + optional($._shebang), optional(trailingSep1($._semicolon, $._top_level_definition)), + ), _top_level_definition: $ => choice( @@ -1712,6 +1714,8 @@ module.exports = grammar({ repeat1($.guard), ), + _shebang: $ => alias(token(seq("#!", /.*/)), $.comment), + comment: $ => seq(token("//"), choice($.using_directive, $._comment_text)), _comment_text: $ => token(prec(PREC.comment, /.*/)),