Skip to content

Commit b1fa258

Browse files
authored
Add KeywordString to SynConst. (#11482)
* Add KeywordString to SynConst. * Renamed KeywordString to SourceIdentifier.
1 parent 2706fc3 commit b1fa258

File tree

7 files changed

+81
-12
lines changed

7 files changed

+81
-12
lines changed

src/fsharp/CheckExpressions.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,8 @@ let TcConst cenv ty m env c =
810810
| SynConst.Measure(SynConst.UInt64 i, _, _) when expandedMeasurablesEnabled -> unifyMeasureArg (i=0UL) cenv.g.puint64_tcr c; Const.UInt64 i
811811
| SynConst.Measure(SynConst.UIntPtr i, _, _) when expandedMeasurablesEnabled -> unifyMeasureArg (i=0UL) cenv.g.punativeint_tcr c; Const.UIntPtr i
812812
| SynConst.Char c -> unif cenv.g.char_ty; Const.Char c
813-
| SynConst.String (s, _, _) -> unif cenv.g.string_ty; Const.String s
813+
| SynConst.String (s, _, _)
814+
| SynConst.SourceIdentifier (_, s, _) -> unif cenv.g.string_ty; Const.String s
814815
| SynConst.UserNum _ -> error (InternalError(FSComp.SR.tcUnexpectedBigRationalConstant(), m))
815816
| SynConst.Measure _ -> error (Error(FSComp.SR.tcInvalidTypeForUnitsOfMeasure(), m))
816817
| SynConst.UInt16s _ -> error (InternalError(FSComp.SR.tcUnexpectedConstUint16Array(), m))

src/fsharp/SyntaxTree.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,14 @@ type SynConst =
116116
| UInt16s of uint16[]
117117

118118
| Measure of constant: SynConst * constantRange: Range * SynMeasure
119+
120+
| SourceIdentifier of constant: string * value: string * range: Range
119121

120122
member c.Range dflt =
121123
match c with
122-
| SynConst.String (_, _, m0) | SynConst.Bytes (_, _, m0) -> m0
124+
| SynConst.String (_, _, m0)
125+
| SynConst.Bytes (_, _, m0)
126+
| SynConst.SourceIdentifier(_, _, m0) -> m0
123127
| _ -> dflt
124128

125129
[<NoEquality; NoComparison; RequireQualifiedAccess>]

src/fsharp/SyntaxTree.fsi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ type SynConst =
152152

153153
/// Old comment: "we never iterate, so the const here is not another SynConst.Measure"
154154
| Measure of constant: SynConst * constantRange: range * SynMeasure
155+
156+
/// Source Line, File, and Path Identifiers
157+
/// Containing both the original value as the evaluated value.
158+
| SourceIdentifier of constant: string * value: string * range: Range
155159

156160
/// Gets the syntax range of this construct
157161
member Range: dflt: range -> range

src/fsharp/lexhelp.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,11 @@ module Keywords =
396396

397397
if String.IsNullOrEmpty dirname then dirname
398398
else PathMap.applyDir args.pathMap dirname
399-
|> KEYWORD_STRING
399+
|> fun dir -> KEYWORD_STRING(s, dir)
400400
| "__SOURCE_FILE__" ->
401-
KEYWORD_STRING (System.IO.Path.GetFileName((FileIndex.fileOfFileIndex lexbuf.StartPos.FileIndex)))
401+
KEYWORD_STRING (s, System.IO.Path.GetFileName((FileIndex.fileOfFileIndex lexbuf.StartPos.FileIndex)))
402402
| "__LINE__" ->
403-
KEYWORD_STRING (string lexbuf.StartPos.Line)
403+
KEYWORD_STRING (s, string lexbuf.StartPos.Line)
404404
| _ ->
405405
IdentifierToken args lexbuf s
406406

src/fsharp/pars.fsy

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ let rangeOfLongIdent(lid:LongIdent) =
202202
%token <string * ParseHelpers.LexerContinuation> INTERP_STRING_END
203203
%token <ParseHelpers.LexerContinuation> LBRACE RBRACE
204204

205-
%token <string> KEYWORD_STRING // Like __SOURCE_DIRECTORY__
205+
%token <string * string> KEYWORD_STRING // Like __SOURCE_DIRECTORY__
206206
%token <string> IDENT
207207
%token <string> HASH_IDENT
208208
%token <string> INFIX_STAR_STAR_OP
@@ -646,7 +646,7 @@ hashDirectiveArgs:
646646

647647
/* One argument to a #directive */
648648
hashDirectiveArg:
649-
| stringOrKeywordString
649+
| string
650650
{ let s, _ = $1
651651
s }
652652

@@ -2214,7 +2214,7 @@ tyconDefnOrSpfnSimpleRepr:
22142214
SynTypeDefnSimpleRepr.Record ($2, $3, lhs parseState) }
22152215

22162216
/* An inline-assembly type definition, for FSharp.Core library only */
2217-
| opt_attributes opt_declVisibility LPAREN HASH stringOrKeywordString HASH rparen
2217+
| opt_attributes opt_declVisibility LPAREN HASH string HASH rparen
22182218
{ if not (isNil $1) then errorR(Error(FSComp.SR.parsAttributesIllegalHere(), rhs parseState 1))
22192219
let lhsm = lhs parseState
22202220
libraryOnlyError lhsm
@@ -2922,10 +2922,14 @@ rawConstant:
29222922
| BIGNUM
29232923
{ SynConst.UserNum $1 }
29242924

2925-
| stringOrKeywordString
2925+
| string
29262926
{ let s, synStringKind = $1
29272927
SynConst.String (s, synStringKind, lhs parseState) }
29282928

2929+
| sourceIdentifier
2930+
{ let c,v = $1
2931+
SynConst.SourceIdentifier (c, v, lhs parseState) }
2932+
29292933
| BYTEARRAY
29302934
{ let (v, synByteStringKind, _) = $1
29312935
SynConst.Bytes (v, synByteStringKind, lhs parseState) }
@@ -4518,7 +4522,7 @@ forLoopDirection:
45184522
| DOWNTO { false }
45194523

45204524
inlineAssemblyExpr:
4521-
| HASH stringOrKeywordString opt_inlineAssemblyTypeArg optCurriedArgExprs optInlineAssemblyReturnTypes HASH
4525+
| HASH string opt_inlineAssemblyTypeArg optCurriedArgExprs optInlineAssemblyReturnTypes HASH
45224526
{ libraryOnlyWarning (lhs parseState)
45234527
let (s, _), sm = $2, rhs parseState 2
45244528
(fun m ->
@@ -5556,11 +5560,13 @@ colonOrEquals:
55565560
| EQUALS { }
55575561

55585562
/* A literal string or a string from a keyword like __SOURCE_FILE__ */
5559-
stringOrKeywordString:
5563+
string:
55605564
| STRING
55615565
{ let (s, synStringKind, _) = $1
55625566
s, synStringKind }
5563-
| KEYWORD_STRING { $1, SynStringKind.Regular }
5567+
5568+
sourceIdentifier:
5569+
| KEYWORD_STRING { $1 }
55645570

55655571
interpolatedStringFill:
55665572
| declExpr

tests/FSharp.Compiler.Service.Tests/SurfaceArea.netstandard.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5788,6 +5788,12 @@ FSharp.Compiler.Syntax.SynConst+SByte: SByte Item
57885788
FSharp.Compiler.Syntax.SynConst+SByte: SByte get_Item()
57895789
FSharp.Compiler.Syntax.SynConst+Single: Single Item
57905790
FSharp.Compiler.Syntax.SynConst+Single: Single get_Item()
5791+
FSharp.Compiler.Syntax.SynConst+SourceIdentifier: FSharp.Compiler.Text.Range get_range()
5792+
FSharp.Compiler.Syntax.SynConst+SourceIdentifier: FSharp.Compiler.Text.Range range
5793+
FSharp.Compiler.Syntax.SynConst+SourceIdentifier: System.String constant
5794+
FSharp.Compiler.Syntax.SynConst+SourceIdentifier: System.String get_constant()
5795+
FSharp.Compiler.Syntax.SynConst+SourceIdentifier: System.String get_value()
5796+
FSharp.Compiler.Syntax.SynConst+SourceIdentifier: System.String value
57915797
FSharp.Compiler.Syntax.SynConst+String: FSharp.Compiler.Syntax.SynStringKind get_synStringKind()
57925798
FSharp.Compiler.Syntax.SynConst+String: FSharp.Compiler.Syntax.SynStringKind synStringKind
57935799
FSharp.Compiler.Syntax.SynConst+String: FSharp.Compiler.Text.Range get_range()
@@ -5807,6 +5813,7 @@ FSharp.Compiler.Syntax.SynConst+Tags: Int32 IntPtr
58075813
FSharp.Compiler.Syntax.SynConst+Tags: Int32 Measure
58085814
FSharp.Compiler.Syntax.SynConst+Tags: Int32 SByte
58095815
FSharp.Compiler.Syntax.SynConst+Tags: Int32 Single
5816+
FSharp.Compiler.Syntax.SynConst+Tags: Int32 SourceIdentifier
58105817
FSharp.Compiler.Syntax.SynConst+Tags: Int32 String
58115818
FSharp.Compiler.Syntax.SynConst+Tags: Int32 UInt16
58125819
FSharp.Compiler.Syntax.SynConst+Tags: Int32 UInt16s
@@ -5842,6 +5849,7 @@ FSharp.Compiler.Syntax.SynConst: Boolean IsIntPtr
58425849
FSharp.Compiler.Syntax.SynConst: Boolean IsMeasure
58435850
FSharp.Compiler.Syntax.SynConst: Boolean IsSByte
58445851
FSharp.Compiler.Syntax.SynConst: Boolean IsSingle
5852+
FSharp.Compiler.Syntax.SynConst: Boolean IsSourceIdentifier
58455853
FSharp.Compiler.Syntax.SynConst: Boolean IsString
58465854
FSharp.Compiler.Syntax.SynConst: Boolean IsUInt16
58475855
FSharp.Compiler.Syntax.SynConst: Boolean IsUInt16s
@@ -5863,6 +5871,7 @@ FSharp.Compiler.Syntax.SynConst: Boolean get_IsIntPtr()
58635871
FSharp.Compiler.Syntax.SynConst: Boolean get_IsMeasure()
58645872
FSharp.Compiler.Syntax.SynConst: Boolean get_IsSByte()
58655873
FSharp.Compiler.Syntax.SynConst: Boolean get_IsSingle()
5874+
FSharp.Compiler.Syntax.SynConst: Boolean get_IsSourceIdentifier()
58665875
FSharp.Compiler.Syntax.SynConst: Boolean get_IsString()
58675876
FSharp.Compiler.Syntax.SynConst: Boolean get_IsUInt16()
58685877
FSharp.Compiler.Syntax.SynConst: Boolean get_IsUInt16s()
@@ -5884,6 +5893,7 @@ FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst NewIntPtr(Int64
58845893
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst NewMeasure(FSharp.Compiler.Syntax.SynConst, FSharp.Compiler.Text.Range, FSharp.Compiler.Syntax.SynMeasure)
58855894
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst NewSByte(SByte)
58865895
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst NewSingle(Single)
5896+
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst NewSourceIdentifier(System.String, System.String, FSharp.Compiler.Text.Range)
58875897
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst NewString(System.String, FSharp.Compiler.Syntax.SynStringKind, FSharp.Compiler.Text.Range)
58885898
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst NewUInt16(UInt16)
58895899
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst NewUInt16s(UInt16[])
@@ -5906,6 +5916,7 @@ FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst+IntPtr
59065916
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst+Measure
59075917
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst+SByte
59085918
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst+Single
5919+
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst+SourceIdentifier
59095920
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst+String
59105921
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst+Tags
59115922
FSharp.Compiler.Syntax.SynConst: FSharp.Compiler.Syntax.SynConst+UInt16

tests/service/Symbols.fs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,4 +764,47 @@ with
764764
]) ])) ->
765765
assertRange (6, 2) (6, 21) range
766766
assertRange (6, 2) (6, 21) clause.Range
767+
| _ -> Assert.Fail "Could not get valid AST"
768+
769+
module SourceIdentifiers =
770+
[<Test>]
771+
let ``__LINE__`` () =
772+
let parseResults =
773+
getParseResults
774+
"""
775+
__LINE__"""
776+
777+
match parseResults with
778+
| ParsedInput.ImplFile (ParsedImplFileInput (modules = [ SynModuleOrNamespace.SynModuleOrNamespace(decls = [
779+
SynModuleDecl.DoExpr(expr = SynExpr.Const(SynConst.SourceIdentifier("__LINE__", "2", range), _))
780+
]) ])) ->
781+
assertRange (2, 0) (2, 8) range
782+
| _ -> Assert.Fail "Could not get valid AST"
783+
784+
[<Test>]
785+
let ``__SOURCE_DIRECTORY__`` () =
786+
let parseResults =
787+
getParseResults
788+
"""
789+
__SOURCE_DIRECTORY__"""
790+
791+
match parseResults with
792+
| ParsedInput.ImplFile (ParsedImplFileInput (modules = [ SynModuleOrNamespace.SynModuleOrNamespace(decls = [
793+
SynModuleDecl.DoExpr(expr = SynExpr.Const(SynConst.SourceIdentifier("__SOURCE_DIRECTORY__", _, range), _))
794+
]) ])) ->
795+
assertRange (2, 0) (2, 20) range
796+
| _ -> Assert.Fail "Could not get valid AST"
797+
798+
[<Test>]
799+
let ``__SOURCE_FILE__`` () =
800+
let parseResults =
801+
getParseResults
802+
"""
803+
__SOURCE_FILE__"""
804+
805+
match parseResults with
806+
| ParsedInput.ImplFile (ParsedImplFileInput (modules = [ SynModuleOrNamespace.SynModuleOrNamespace(decls = [
807+
SynModuleDecl.DoExpr(expr = SynExpr.Const(SynConst.SourceIdentifier("__SOURCE_FILE__", _, range), _))
808+
]) ])) ->
809+
assertRange (2, 0) (2, 15) range
767810
| _ -> Assert.Fail "Could not get valid AST"

0 commit comments

Comments
 (0)