Skip to content

Commit

Permalink
Parse patterns and statements (#2)
Browse files Browse the repository at this point in the history
* Parse patterns and statements

* start TypeAnnParser, reorganize the code into modules

* experiment with createParserForwardedToRef in new JsonParser

* cleanup ExprParser a bit

* add another test
  • Loading branch information
kevinbarabash committed Oct 31, 2023
1 parent 108082b commit e01ed02
Show file tree
Hide file tree
Showing 10 changed files with 531 additions and 29 deletions.
16 changes: 8 additions & 8 deletions src/Escalier.Data/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ module Syntax =
[<RequireQualifiedAccess>]
type PatternKind =
| Identifier of span: Span * name: string * is_mut: bool
| Object of elems: list<ObjElem>
| Object of elems: list<ObjPatElem>
| Tuple of elems: list<Pattern>
| Wildcard of span: Span
| Wildcard
| Literal of span: Span * value: Literal
| Is of span: Span * target: Pattern * id: string * is_mut: bool

Expand Down Expand Up @@ -145,6 +145,7 @@ module Syntax =
| Undefined
| Unknown
| Never
| Object

type TypeParam =
{ span: Span
Expand All @@ -171,9 +172,8 @@ module Syntax =
type MatchTypeCase =
{ extends: TypeAnn; true_type: TypeAnn }

type Ident = Span * string

type TypeAnnKind =
// TODO: collapse these into a single `Literal` type ann kind
| BooleanLiteral of value: bool
| NumberLiteral of value: string
| StringLiteral of value: string
Expand All @@ -183,11 +183,11 @@ module Syntax =
| Array of elem: TypeAnn
| Union of types: list<TypeAnn>
| Intersection of types: list<TypeAnn>
| TypeRef of name: string * type_args: list<TypeAnn>
| TypeRef of name: string * type_args: option<list<TypeAnn>>
| Function of FunctionType
| KeyOf of target: TypeAnn
| Keyof of target: TypeAnn
| Rest of target: TypeAnn
| TypeOf of target: Ident
| Typeof of target: Expr
| Index of target: TypeAnn * index: TypeAnn
| Condition of ConditionType
| Match of MatchType
Expand All @@ -198,7 +198,7 @@ module Syntax =
type TypeAnn =
{ kind: TypeAnnKind
span: Span
inferred_type: option<Type.Type> ref }
mutable inferred_type: option<Type.Type> }

module Type =
type TypeParam =
Expand Down
32 changes: 32 additions & 0 deletions src/Escalier.Parser.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,35 @@ let ParseCallThenIndexer () =
let result = sprintf "input: %s\noutput: %A" src expr

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

[<Fact>]
let ParseFuncDef () =
let src = "fn (x, y) { x }"
let expr = run ExprParser.func src
let result = sprintf "input: %s\noutput: %A" src expr

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

[<Fact>]
let ParseUnionType () =
let src = "number | string | boolean"
let expr = run ExprParser.typeAnn src
let result = sprintf "input: %s\noutput: %A" src expr

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

[<Fact>]
let ParseIntersectionType () =
let src = "number & string & boolean"
let expr = run ExprParser.typeAnn src
let result = sprintf "input: %s\noutput: %A" src expr

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

[<Fact>]
let ParseUnionAndIntersectionType () =
let src = "A & B | C & D"
let expr = run ExprParser.typeAnn src
let result = sprintf "input: %s\noutput: %A" src expr

Verifier.Verify(result, settings).ToTask() |> Async.AwaitTask
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
input: fn (x, y) { x }
output: Success: { kind =
Function
(["x"; "y"], Block { span = { start = 0
stop = 0 }
stmts = [{ span = { start = 12
stop = 14 }
kind = Expr { kind = Identifer "x"
span = { start = 12
stop = 14 }
inferred_type = None } }] })
span = { start = 0
stop = 15 }
inferred_type = None }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
input: number & string & boolean
output: Success: { kind =
Intersection
[{ kind = Keyword Number
span = { start = 0
stop = 7 }
inferred_type = None }; { kind = Keyword String
span = { start = 9
stop = 16 }
inferred_type = None };
{ kind = Keyword Boolean
span = { start = 18
stop = 25 }
inferred_type = None }]
span = { start = 0
stop = 25 }
inferred_type = None }
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
input: A & B | C & D
output: Success: { kind =
Union
[{ kind =
Intersection [{ kind = TypeRef ("A", None)
span = { start = 0
stop = 2 }
inferred_type = None }; { kind = TypeRef ("B", None)
span = { start = 4
stop = 6 }
inferred_type = None }]
span = { start = 0
stop = 6 }
inferred_type = None };
{ kind =
Intersection [{ kind = TypeRef ("C", None)
span = { start = 8
stop = 10 }
inferred_type = None }; { kind = TypeRef ("D", None)
span = { start = 12
stop = 13 }
inferred_type = None }]
span = { start = 8
stop = 13 }
inferred_type = None }]
span = { start = 0
stop = 13 }
inferred_type = None }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
input: number | string | boolean
output: Success: { kind =
Union
[{ kind = Keyword Number
span = { start = 0
stop = 7 }
inferred_type = None }; { kind = Keyword String
span = { start = 9
stop = 16 }
inferred_type = None };
{ kind = Keyword Boolean
span = { start = 18
stop = 25 }
inferred_type = None }]
span = { start = 0
stop = 25 }
inferred_type = None }
1 change: 1 addition & 0 deletions src/Escalier.Parser/Escalier.Parser.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ItemGroup>
<Compile Include="Library.fs" />
<Compile Include="ExprParser.fs" />
<Compile Include="JsonParser.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Escalier.Data\Escalier.Data.fsproj" />
Expand Down
Loading

0 comments on commit e01ed02

Please sign in to comment.