mirror of
https://github.com/apple/pkl.git
synced 2026-03-20 00:04:05 +01:00
Only allow shebangs in the beginning of a module (#1126)
This commit is contained in:
2
pkl-core/src/test/files/LanguageSnippetTests/input/errors/shebang.pkl
vendored
Normal file
2
pkl-core/src/test/files/LanguageSnippetTests/input/errors/shebang.pkl
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
foo = 1
|
||||
#!/usr/bin/env pkl eval
|
||||
2
pkl-core/src/test/files/LanguageSnippetTests/input/syntax/shebang.pkl
vendored
Normal file
2
pkl-core/src/test/files/LanguageSnippetTests/input/syntax/shebang.pkl
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env pkl eval
|
||||
foo = 1
|
||||
6
pkl-core/src/test/files/LanguageSnippetTests/output/errors/shebang.err
vendored
Normal file
6
pkl-core/src/test/files/LanguageSnippetTests/output/errors/shebang.err
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
–– Pkl Error ––
|
||||
Invalid token at position. Expected a class, typealias, method, or property.
|
||||
|
||||
x | #!/usr/bin/env pkl eval
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
at shebang (file:///$snippetsDir/input/errors/shebang.pkl)
|
||||
1
pkl-core/src/test/files/LanguageSnippetTests/output/syntax/shebang.pcf
vendored
Normal file
1
pkl-core/src/test/files/LanguageSnippetTests/output/syntax/shebang.pcf
vendored
Normal file
@@ -0,0 +1 @@
|
||||
foo = 1
|
||||
@@ -18,6 +18,7 @@ package org.pkl.parser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import org.pkl.parser.syntax.Annotation;
|
||||
@@ -112,6 +113,7 @@ public class Parser {
|
||||
if (lookahead == Token.EOF) {
|
||||
return new Module(Collections.singletonList(null), new Span(0, 0));
|
||||
}
|
||||
if (lookahead == Token.SHEBANG) next();
|
||||
var start = spanLookahead;
|
||||
Span end = null;
|
||||
ModuleDecl moduleDecl;
|
||||
@@ -1787,16 +1789,16 @@ public class Parser {
|
||||
private FullToken forceNext() {
|
||||
var tk = lexer.next();
|
||||
precededBySemicolon = false;
|
||||
while (tk == Token.LINE_COMMENT
|
||||
|| tk == Token.BLOCK_COMMENT
|
||||
|| tk == Token.SEMICOLON
|
||||
|| tk == Token.SHEBANG) {
|
||||
while (AFFIXES.contains(tk)) {
|
||||
precededBySemicolon = precededBySemicolon || tk == Token.SEMICOLON;
|
||||
tk = lexer.next();
|
||||
}
|
||||
return new FullToken(tk, lexer.span(), lexer.newLinesBetween);
|
||||
}
|
||||
|
||||
private static final EnumSet<Token> AFFIXES =
|
||||
EnumSet.of(Token.LINE_COMMENT, Token.BLOCK_COMMENT, Token.SEMICOLON);
|
||||
|
||||
// Like next, but don't ignore comments
|
||||
private FullToken nextComment() {
|
||||
prev = _lookahead;
|
||||
|
||||
@@ -95,3 +95,6 @@ danglingDocComment=\
|
||||
Dangling documentation comment.\n\
|
||||
\n\
|
||||
Documentation comments must be attached to modules, classes, typealiases, methods, or properties.
|
||||
|
||||
wrongShebangPosition=\
|
||||
Shebangs are only allowed at the very beginning of a file.
|
||||
|
||||
Reference in New Issue
Block a user