Skip to content

Commit

Permalink
Parse ineaxct object type annotation syntax (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbarabash authored Aug 4, 2024
1 parent 757888e commit e08d0aa
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/Escalier.Data/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ module Syntax =
| Property of Property
| Mapped of Mapped

type ObjTypeAnn = { Elems: list<ObjTypeAnnElem>; Immutable: bool; Exact: bool }

type KeywordTypeAnn =
| Boolean
| Number
Expand Down Expand Up @@ -556,7 +558,7 @@ module Syntax =
type TypeAnnKind =
| Literal of Common.Literal
| Keyword of keyword: KeywordTypeAnn
| Object of Common.Object<ObjTypeAnnElem>
| Object of ObjTypeAnn
| Tuple of Common.Tuple<TypeAnn>
| Array of elem: TypeAnn
| Range of Common.Range<TypeAnn>
Expand Down
12 changes: 10 additions & 2 deletions src/Escalier.Interop/Migrate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ module rec Migrate =
TypeAnnKind.Typeof name
| TsType.TsTypeLit { Members = members } ->
let elems: list<ObjTypeAnnElem> = List.map migrateTypeElement members
TypeAnnKind.Object { Elems = elems; Immutable = false }

TypeAnnKind.Object
{ Elems = elems
Immutable = false
Exact = false }
| TsType.TsArrayType { ElemType = elem } ->
TypeAnnKind.Array(migrateType elem)
| TsType.TsTupleType { ElemTypes = elems } ->
Expand Down Expand Up @@ -462,7 +466,11 @@ module rec Migrate =
TypeAnn = migrateType typeAnn }

let elem: ObjTypeAnnElem = ObjTypeAnnElem.Mapped mapped
TypeAnnKind.Object { Elems = [ elem ]; Immutable = false }

TypeAnnKind.Object
{ Elems = [ elem ]
Immutable = false
Exact = false }
| TsType.TsLitType { Lit = lit } ->
match lit with
| TsLit.Number { Value = value } ->
Expand Down
31 changes: 31 additions & 0 deletions src/Escalier.Parser.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,37 @@ let ParseGetterSetterInObjectType () =

Verifier.Verify(result, settings).ToTask() |> Async.AwaitTask

[<Fact>]
let ParseInexactObjectTypes () =
let src =
"""
type Obj = {
msg: string,
flag: boolean,
...
};
"""

let ast = Parser.parseScript src
let result = $"input: %s{src}\noutput: %A{ast}"

Verifier.Verify(result, settings).ToTask() |> Async.AwaitTask

[<Fact>]
let ParseInexactRecordTypes () =
let src =
"""
type Rec = #{
msg: string,
flag: boolean,
...
};
"""

let ast = Parser.parseScript src
let result = $"input: %s{src}\noutput: %A{ast}"

Verifier.Verify(result, settings).ToTask() |> Async.AwaitTask

[<Fact>]
let ParseForLoop () =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ output: Ok
InferredType = None }
Throws = None
IsAsync = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 21)
Stop = (Ln: 5, Col: 6) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ output: Ok
Optional = false
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 4, Col: 11)
Stop = (Ln: 4, Col: 33) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ output: Ok
InferredType = None }
Optional = false }
Throws = None }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 16)
Stop = (Ln: 5, Col: 6) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
input:
type Obj = {
msg: string,
flag: boolean,
...
};

output: Ok
{ Items =
[Stmt
{ Kind =
Decl
{ Kind =
TypeDecl
{ Name = "Obj"
TypeAnn =
{ Kind =
Object
{ Elems =
[Property
{ Name = String "msg"
TypeAnn = { Kind = Keyword String
Span = { Start = (Ln: 3, Col: 12)
Stop = (Ln: 3, Col: 18) }
InferredType = None }
Optional = false
Readonly = false
Static = false };
Property
{ Name = String "flag"
TypeAnn = { Kind = Keyword Boolean
Span = { Start = (Ln: 4, Col: 13)
Stop = (Ln: 4, Col: 20) }
InferredType = None }
Optional = false
Readonly = false
Static = false }]
Immutable = false
Exact = false }
Span = { Start = (Ln: 2, Col: 16)
Stop = (Ln: 6, Col: 6) }
InferredType = None }
TypeParams = None }
Span = { Start = (Ln: 2, Col: 5)
Stop = (Ln: 7, Col: 5) } }
Span = { Start = (Ln: 2, Col: 5)
Stop = (Ln: 7, Col: 5) } }] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
input:
type Rec = #{
msg: string,
flag: boolean,
...
};

output: Ok
{ Items =
[Stmt
{ Kind =
Decl
{ Kind =
TypeDecl
{ Name = "Rec"
TypeAnn =
{ Kind =
Object
{ Elems =
[Property
{ Name = String "msg"
TypeAnn = { Kind = Keyword String
Span = { Start = (Ln: 3, Col: 12)
Stop = (Ln: 3, Col: 18) }
InferredType = None }
Optional = false
Readonly = false
Static = false };
Property
{ Name = String "flag"
TypeAnn = { Kind = Keyword Boolean
Span = { Start = (Ln: 4, Col: 13)
Stop = (Ln: 4, Col: 20) }
InferredType = None }
Optional = false
Readonly = false
Static = false }]
Immutable = true
Exact = false }
Span = { Start = (Ln: 2, Col: 16)
Stop = (Ln: 6, Col: 6) }
InferredType = None }
TypeParams = None }
Span = { Start = (Ln: 2, Col: 5)
Stop = (Ln: 7, Col: 5) } }
Span = { Start = (Ln: 2, Col: 5)
Stop = (Ln: 7, Col: 5) } }] }
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ output: Ok
InferredType = None }
Optional = None
Readonly = None }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 1, Col: 14)
Stop = (Ln: 1, Col: 46) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ output: Ok
Optional = false
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 18)
Stop = (Ln: 2, Col: 40) }
InferredType = None }
Expand Down Expand Up @@ -77,7 +78,8 @@ output: Ok
Optional = false
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 3, Col: 17)
Stop = (Ln: 3, Col: 39) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ output: Ok
Optional = false
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 18)
Stop = (Ln: 2, Col: 40) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,25 @@ output: Ok
Optional = true
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span =
{ Start = (Ln: 2, Col: 26)
Stop = (Ln: 2, Col: 38) }
InferredType = None }
Optional = true
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 21)
Stop = (Ln: 2, Col: 39) }
InferredType = None }
Optional = true
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 16)
Stop = (Ln: 2, Col: 40) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ output: Ok
Optional = false
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 18)
Stop = (Ln: 6, Col: 6) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ output: Ok
Optional = false
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 3, Col: 24)
Stop = (Ln: 4, Col: 7) }
InferredType = None }
Expand All @@ -98,7 +99,8 @@ output: Ok
Optional = false
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 54)
Stop = (Ln: 4, Col: 8) }
InferredType = None }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ output: Ok
Optional = false
Readonly = false
Static = false }]
Immutable = false }
Immutable = false
Exact = true }
Span = { Start = (Ln: 2, Col: 18)
Stop = (Ln: 2, Col: 50) }
InferredType = None }
Expand Down
31 changes: 24 additions & 7 deletions src/Escalier.Parser/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -360,20 +360,33 @@ module Parser =

let private objectTypeAnn =
withSpan (
between (strWs "{") (strWs "}") (sepEndBy objTypeAnnElem (strWs ","))
between
(strWs "{")
(strWs "}")
((sepEndBy objTypeAnnElem (strWs ",")) .>>. (opt (strWs "...")))
)
|>> fun (objElems, span) ->
|>> fun ((objElems, inexact), span) ->
{ TypeAnn.Kind =
TypeAnnKind.Object { Elems = objElems; Immutable = false }
TypeAnnKind.Object
{ Elems = objElems
Immutable = false
Exact = inexact.IsNone }
Span = span
InferredType = None }

let private imObjectTypeAnn =
withSpan (
between (strWs "#{") (strWs "}") (sepEndBy objTypeAnnElem (strWs ","))
between
(strWs "#{")
(strWs "}")
((sepEndBy objTypeAnnElem (strWs ",")) .>>. (opt (strWs "...")))
)
|>> fun (objElems, span) ->
{ TypeAnn.Kind = TypeAnnKind.Object { Elems = objElems; Immutable = true }
|>> fun ((objElems, inexact), span) ->
{ TypeAnn.Kind =
TypeAnnKind.Object
{ Elems = objElems
Immutable = true
Exact = inexact.IsNone }
Span = span
InferredType = None }

Expand Down Expand Up @@ -1559,7 +1572,11 @@ module Parser =
between (strWs "{") (strWs "}") (sepEndBy objTypeAnnElem (strWs ","))
)
|>> fun (objElems, span) ->
{ Kind = TypeAnnKind.Object { Elems = objElems; Immutable = false }
{ Kind =
TypeAnnKind.Object
{ Elems = objElems
Immutable = false
Exact = true }
Span = span
InferredType = None }

Expand Down

0 comments on commit e08d0aa

Please sign in to comment.