Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdae1e6e

Browse files
rojeppKevinRansom
authored andcommitted
Only allow #! at the start of a file
1 parent8375316 commitdae1e6e

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

‎src/fsharp/FSComp.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ lexHashEndingNoMatchingIf,"#endif has no matching #if"
10631063
1168,lexHashEndifMustBeFirst,"#endif directive must appear as the first non-whitespace character on a line"
10641064
1169,lexHashIfMustHaveIdent,"#if directive should be immediately followed by an identifier"
10651065
1170,lexWrongNestedHashEndif,"Syntax error. Wrong nested #endif, unexpected tokens before it."
1066+
lexHashBangMustBeFirstInFile,"#! must appear at the start of the file."
10661067
1171,lexExpectedSingleLineComment,"Expected single line comment or end of line"
10671068
1172,memberOperatorDefinitionWithNoArguments,"Infix operator member '%s' has no arguments. Expected a tuple of 2 arguments, e.g. static member (+) (x,y) = ..."
10681069
1173,memberOperatorDefinitionWithNonPairArgument,"Infix operator member '%s' has %d initial argument(s). Expected a tuple of 2 arguments, e.g. static member (+) (x,y) = ..."

‎src/fsharp/lex.fsl‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ let shouldStartLine args lexbuf (m:range) err tok =
145145
if (m.StartColumn <> 0) then fail args lexbuf err tok
146146
else tok
147147

148+
let shouldStartFile args lexbuf (m:range) err tok =
149+
if (m.StartColumn <> 0 || m.StartLine <> 1) then fail args lexbuf err tok
150+
else tok
151+
148152
let extractIdentFromHashIf (lexed:string) =
149153
// Skip the '#if' token, then trim whitespace, then find the end of the identifier
150154
let lexed = lexed.Trim()
@@ -575,9 +579,10 @@ rule token args skip = parse
575579
| ".[]" | ".[]<-" | ".[,]<-" | ".[,,]<-" | ".[,,,]<-" | ".[,,,]" | ".[,,]" | ".[,]" | ".[..]" | ".[..,..]" | ".[..,..,..]" | ".[..,..,..,..]"
576580
| ".()" | ".()<-" { FUNKY_OPERATOR_NAME(lexeme lexbuf) }
577581
| "#!" op_char*
578-
{ // Treat shebangs like regular comments
582+
{ // Treat shebangs like regular comments, but they are only allowed at the start of a file
579583
let m = lexbuf.LexemeRange
580-
if not skip then (LINE_COMMENT (LexCont.SingleLineComment(!args.ifdefStack,1,m))) else singleLineComment (None,1,m,args) skip lexbuf }
584+
let tok = shouldStartFile args lexbuf m (0,FSComp.SR.lexHashBangMustBeFirstInFile()) (LINE_COMMENT (LexCont.SingleLineComment(!args.ifdefStack,1,m)))
585+
if not skip then tok else singleLineComment (None,1,m,args) skip lexbuf }
581586

582587
| "#light" anywhite*
583588
| ("#indent" | "#light") anywhite+ "\"on\""

‎tests/fsharp/core/syntax/test.fsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/bin/usr/env fsharpi--exec
2+
//The shebang above should be treated like a line comment. #! is only allowed in the first byte of a file.
3+
14
// #Regression #Conformance #Operators #SyntacticSugar #Exceptions #ControlFlow #Arrays #Tuples #Lists #Classes #Constants #Records
25
#if Portable
36
moduleCore_syntax
@@ -32,8 +35,6 @@ test "line number test" (__SOURCE_FILE__ = "file.fs")
3235
test"line number test"(__LINE__="29")
3336
test"line number test"(__SOURCE_FILE__="original-test-file.fs")
3437

35-
// This shebang should be treated like a line comment
36-
#!/bin/usr/env fsharpi--exec
3738

3839
#if NetCore
3940
#else

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp