forked from darklang/dark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.dark
More file actions
1561 lines (1325 loc) · 59.9 KB
/
Copy pathparser.dark
File metadata and controls
1561 lines (1325 loc) · 59.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Darklang =
module LanguageTools =
/// Maps `TreeSitter.ParsedNode`s to structures defined in `WrittenTypes`
module Parser =
// TODO: these should be UInts of some size
// (UInt8 might even be enough - how many lines are over 255chars?)
type Point = { row: Int64; column: Int64 }
type Range = { start: Point; end_: Point }
type ParsedNode =
{
// e.g., a node of `typ` `let_expression` has a child node with a `body` field name
fieldName: Stdlib.Option.Option<String>
/// e.g. `source_file`, `fn_decl`, `expression`, `let_expression`
typ: String
/// The text of this node as it was in the unparsed source code
text: String
/// Where in the source code is this node written/contained
/// i.e. Line 1, Column 2 to Line 1, Column 5
sourceRange: Range
children: List<ParsedNode>
}
let parseToSimplifiedTree (text: String) : ParsedNode =
Builtin.parserParseToSimplifiedTree text
let parseName (fnName: String) : Stdlib.Result.Result<String> =
if (Stdlib.String.contains fnName "_v") then
// Todo : check if the name is valid
Stdlib.Result.Result.Ok fnName
else
Stdlib.Result.Result.Ok(fnName ++ "_v0")
// // TODO: maybe re-frame this into expected/actual or something
// type ParseError =
// { sourceRange: Range
// sourceText: String
// message: String }
// --------------------
// Helper functions
// --------------------
let getText (node: ParsedNode) : String = node.text
let getRange (node: ParsedNode) : Range = node.sourceRange
let findNodeByFieldName
(node: ParsedNode)
(fieldName: String)
: Stdlib.Option.Option<ParsedNode> =
match node.fieldName with
| Some fName when fName == fieldName -> Stdlib.Option.Option.Some node
| _ ->
// If no match on the current node, look among its direct children
let directChildMatch =
node.children
|> Stdlib.List.filter (fun c ->
match c.fieldName with
| Some fName -> fName == fieldName
| None -> false)
match directChildMatch with
| [ c ] -> Stdlib.Option.Option.Some c
| [] ->
// If no direct children match, recursively search deeper in each child
node.children
|> Stdlib.List.fold Stdlib.Option.Option.None (fun acc child ->
match acc with
| Some _ -> acc
| None -> findNodeByFieldName child fieldName)
| _ -> Stdlib.Option.Option.None // TODO: this should error, there are multiple matches
// --------------------
// Parsing to WrittenTypes
// --------------------
module Identifiers =
let extractModuleIdentifiersHelper
(modulesSoFarInReverse:
List<WrittenTypes.ModuleIdentifier * WrittenTypes.SourceRange>)
(nodes: List<ParsedNode>)
: (List<WrittenTypes.ModuleIdentifier * WrittenTypes.SourceRange> *
ParsedNode)
=
match nodes with
| modulePart :: symbolPart :: otherParts ->
if modulePart.typ != "module_identifier" then
$"Unexpected - modulePart should be a module_identifier but is {modulePart.typ}"
elif symbolPart.typ != "symbol" then
$"Unexpected - symbolPart should be a symbol but is {symbolPart.typ}"
else
let moduleIdentifier =
WrittenTypes.ModuleIdentifier
{ range = modulePart.sourceRange
name = modulePart.text }
let updatedModules =
Stdlib.List.push
modulesSoFarInReverse
((moduleIdentifier, symbolPart.sourceRange))
extractModuleIdentifiersHelper updatedModules otherParts
| [ lastPart ] -> (modulesSoFarInReverse, lastPart)
| [] ->
"Unexpected - there should be an odd number of parts to a qualified name"
/// Parses a qualified name (either type, or function, or (future) something else)
///
/// These names come in the form of `module1.module2.module3.name`,
/// and we need to parse out the `module1.module2.module3.` part,
/// ensuring that we take note of the `.` source ranges after each module name
let extractModuleIdentifiers
(nodes: List<ParsedNode>)
: (List<WrittenTypes.ModuleIdentifier * WrittenTypes.SourceRange> *
ParsedNode)
=
let (modulesInReverse, lastNode) = extractModuleIdentifiersHelper [] nodes
(Stdlib.List.reverse modulesInReverse, lastNode)
let parseVariable (n: ParsedNode) : WrittenTypes.VariableIdentifier =
WrittenTypes.VariableIdentifier { range = n.sourceRange; name = n.text }
let parseType (n: ParsedNode) : WrittenTypes.TypeIdentifier =
WrittenTypes.TypeIdentifier { range = n.sourceRange; name = n.text }
let parseFn (n: ParsedNode) : WrittenTypes.FnIdentifier =
WrittenTypes.FnIdentifier { range = n.sourceRange; name = n.text }
let parseQualifiedType
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.QualifiedTypeIdentifier, String> =
if node.typ == "qualified_type_name" then
let (modules, typeIdentifierNode) =
extractModuleIdentifiers node.children
(WrittenTypes.QualifiedTypeIdentifier
{ range = node.sourceRange
modules = modules
typ = parseType typeIdentifierNode })
|> Stdlib.Result.Result.Ok
else
Stdlib.Result.Result.Error
$"Can't parse qualified_type_name from {node.typ}"
let parseQualifiedFunction
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.QualifiedFnIdentifier, String> =
if node.typ == "qualified_fn_name" then
let (modules, fnIdentifierNode) = extractModuleIdentifiers node.children
(WrittenTypes.QualifiedFnIdentifier
{ range = node.sourceRange
modules = modules
fn = parseFn fnIdentifierNode })
|> Stdlib.Result.Result.Ok
else
Stdlib.Result.Result.Error
$"Can't parse qualified_fn_name from {node.typ}"
module TypeReference =
let parseBuiltIn
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.TypeReference.Builtin, WrittenTypes.Unparseable> =
if node.typ == "builtin_type" then
match node.text with
| "Unit" ->
(WrittenTypes.TypeReference.Builtin.TUnit node.sourceRange)
|> Stdlib.Result.Result.Ok
| "Bool" ->
(WrittenTypes.TypeReference.Builtin.TBool node.sourceRange)
|> Stdlib.Result.Result.Ok
| "Int8" ->
(WrittenTypes.TypeReference.Builtin.TInt8 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "UInt8" ->
(WrittenTypes.TypeReference.Builtin.TUInt8 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "Int16" ->
(WrittenTypes.TypeReference.Builtin.TInt16 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "UInt16" ->
(WrittenTypes.TypeReference.Builtin.TUInt16 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "Int32" ->
(WrittenTypes.TypeReference.Builtin.TInt32 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "UInt32" ->
(WrittenTypes.TypeReference.Builtin.TUInt32 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "Int64" ->
(WrittenTypes.TypeReference.Builtin.TInt64 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "UInt64" ->
(WrittenTypes.TypeReference.Builtin.TUInt64 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "Int128" ->
(WrittenTypes.TypeReference.Builtin.TInt128 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "UInt128" ->
(WrittenTypes.TypeReference.Builtin.TUInt128 node.sourceRange)
|> Stdlib.Result.Result.Ok
| "Float" ->
(WrittenTypes.TypeReference.Builtin.TFloat node.sourceRange)
|> Stdlib.Result.Result.Ok
| "Char" ->
(WrittenTypes.TypeReference.Builtin.TChar node.sourceRange)
|> Stdlib.Result.Result.Ok
| "String" ->
(WrittenTypes.TypeReference.Builtin.TString node.sourceRange)
|> Stdlib.Result.Result.Ok
| _ ->
let firstChild = node.children |> Stdlib.List.head |> Builtin.unwrap
match firstChild.typ with
| "dict_type_reference" ->
let dictKeyword =
(findNodeByFieldName firstChild "keyword_type_constructor")
|> Stdlib.Option.toResult
"No keyword_type_constructor node found in dict_type_reference"
let openBracket =
(findNodeByFieldName firstChild "symbol_open_angle")
|> Stdlib.Option.toResult
"No symbol_open_angle node found in dict_type_reference"
let valueTypeNode =
match findNodeByFieldName firstChild "value_type" with
| Some valueTypeNode -> parse valueTypeNode
| None ->
Stdlib.Result.Result.Error
"No type_param node found in dict_type_reference"
let closeBracket =
(findNodeByFieldName firstChild "symbol_close_angle")
|> Stdlib.Option.toResult
"No symbol_close_angle node found in dict_type_reference"
match dictKeyword, openBracket, valueTypeNode, closeBracket with
| Ok dictKeyword, Ok openBracket, Ok valueTypeNode, Ok closeBracket ->
(WrittenTypes.TypeReference.Builtin.TDict
firstChild.sourceRange
dictKeyword.sourceRange
openBracket.sourceRange
valueTypeNode
closeBracket.sourceRange)
|> Stdlib.Result.Result.Ok
| "tuple_type_reference" ->
let leftParen =
(findNodeByFieldName firstChild "symbol_left_paren")
|> Stdlib.Option.toResult
"No symbol_left_paren node found in tuple_type_reference"
let first =
match findNodeByFieldName firstChild "first" with
| Some firstNode -> TypeReference.parse firstNode
| None ->
Stdlib.Result.Result.Error
"No first node found in tuple_type_reference"
let second =
match findNodeByFieldName firstChild "second" with
| Some secondNode -> TypeReference.parse secondNode
| None ->
Stdlib.Result.Result.Error
"No second node found in tuple_type_reference"
let rest =
firstChild
|> findNodeByFieldName "rest"
|> Stdlib.Option.map (fun restNode ->
restNode.children
|> Stdlib.List.chunkBySize 2L
|> Builtin.unwrap
|> Stdlib.List.map (fun chunk ->
match chunk with
| [ symbol; typeNode ] ->
let typeRef = TypeReference.parse typeNode
match typeRef with
| Ok typeRef -> (symbol.sourceRange, typeRef)
| Error _ ->
(symbol.sourceRange,
WrittenTypes.Unparseable { source = typeNode })
| [ typeNode ] ->
let typeRef = TypeReference.parse typeNode
match typeRef with
| Ok typeRef -> (Stdlib.Option.Option.None, typeRef)
| Error _ ->
(Stdlib.Option.Option.None,
WrittenTypes.Unparseable { source = typeNode })
| _ ->
(Stdlib.Option.Option.None,
WrittenTypes.Unparseable { source = chunk })))
|> Stdlib.Option.withDefault []
let asterisk =
(findNodeByFieldName firstChild "symbol_asterisk")
|> Stdlib.Option.toResult
"No symbol_asterisk node found in tuple_type_reference"
let rightParen =
(findNodeByFieldName firstChild "symbol_right_paren")
|> Stdlib.Option.toResult
"No symbol_right_paren node found in tuple_type_reference"
match leftParen, first, asterisk, second, rightParen with
| Ok leftParen, Ok first, Ok asterisk, Ok second, Ok rightParen ->
(WrittenTypes.TypeReference.Builtin.TTuple(
node.sourceRange,
first,
asterisk.sourceRange,
second,
rest,
leftParen.sourceRange,
rightParen.sourceRange
))
|> Stdlib.Result.Result.Ok
| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
| "list_type_reference" ->
let listKeyword =
(findNodeByFieldName firstChild "keyword_type_constructor")
|> Stdlib.Option.toResult
"No keyword_type_constructor node found in list_type"
let openBracketNode =
(findNodeByFieldName firstChild "symbol_open_angle")
|> Stdlib.Option.toResult
"No symbol_open_angle node found in list_type"
let typeParamNode =
match findNodeByFieldName firstChild "typ_param" with
| Some typeParamNode -> parse typeParamNode
| None ->
Stdlib.Result.Result.Error
"No type_param node found in list_type"
let closeBracketNode =
(findNodeByFieldName firstChild "symbol_close_angle")
|> Stdlib.Option.toResult
"No symbol_close_angle node found in list_type"
match
listKeyword, openBracketNode, typeParamNode, closeBracketNode
with
| Ok listKeyword,
Ok openBracketNode,
Ok typeParam,
Ok closeBracketNode ->
(WrittenTypes.TypeReference.Builtin.TList
firstChild.sourceRange
listKeyword.sourceRange
openBracketNode.sourceRange
typeParam
closeBracketNode.sourceRange)
|> Stdlib.Result.Result.Ok
| _ ->
Stdlib.Result.Result.Error(
WrittenTypes.Unparseable { source = node }
)
else
Stdlib.Result.Result.Error(WrittenTypes.Unparseable { source = node })
let parse
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.TypeReference.TypeReference, WrittenTypes.Unparseable> =
if node.typ == "type_reference" then
match node.children with
| [ single ] ->
if single.typ == "builtin_type" then
match parseBuiltIn single with
| Ok builtin ->
(WrittenTypes.TypeReference.TypeReference.Builtin builtin)
|> Stdlib.Result.Result.Ok
| Error _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
elif single.typ == "qualified_type_name" then
match Identifiers.parseQualifiedType single with
| Ok qualifiedType ->
(WrittenTypes.TypeReference.TypeReference.QualifiedName
qualifiedType)
|> Stdlib.Result.Result.Ok
| Error _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
| _ -> Stdlib.Result.Result.Error "Not a single child"
else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
module TypeDeclaration =
let parseDefinition
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.TypeDeclaration.Definition, WrittenTypes.Unparseable> =
if node.typ == "type_reference" then
match TypeReference.parse node with
| Ok typeRef ->
(WrittenTypes.TypeDeclaration.Definition.Alias typeRef)
|> Stdlib.Result.Result.Ok
| Error _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
let parse
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.TypeDeclaration.TypeDeclaration, WrittenTypes.Unparseable> =
if node.typ == "type_decl" then
let nameNode =
match findNodeByFieldName node "name" with
| Some nameNode ->
(Identifiers.parseType nameNode) |> Stdlib.Result.Result.Ok
| None -> Stdlib.Result.Result.Error "No name node found in type_decl"
let defNode =
match findNodeByFieldName node "typ" with
| Some defNode -> (parseDefinition defNode)
| None ->
Stdlib.Result.Result.Error "No definition node found in type_decl"
let keywordTypeNode =
match findNodeByFieldName node "keyword_type" with
| Some keywordTypeNode ->
(getRange keywordTypeNode) |> Stdlib.Result.Result.Ok
| None ->
Stdlib.Result.Result.Error "No keyword_type node found in type_decl"
let symbolEqualsNode =
match findNodeByFieldName node "symbol_equals" with
| Some symbolEqualsNode ->
(getRange symbolEqualsNode) |> Stdlib.Result.Result.Ok
| None ->
Stdlib.Result.Result.Error "No symbol_equals node found in type_decl"
match nameNode, defNode, keywordTypeNode, symbolEqualsNode with
| Ok name, Ok def, Ok keywordTypeNode, Ok symbolEqualsNode ->
(WrittenTypes.TypeDeclaration.TypeDeclaration
{ range = node.sourceRange
name = name
definition = def
keywordType = keywordTypeNode
symbolEquals = symbolEqualsNode })
|> Stdlib.Result.Result.Ok
| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
module Expr =
let parseBoolLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "bool_literal" then
let b =
// TODO: error-handling
match getText node with
| "true" -> true |> Stdlib.Result.Result.Ok
| "false" -> false |> Stdlib.Result.Result.Ok
| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
match b with
| Ok b ->
(WrittenTypes.Expr.EBool(node.sourceRange, b))
|> Stdlib.Result.Result.Ok
| Error _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
let parseIntLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
let supportedInts =
[ "int8_literal"
"uint8_literal"
"int16_literal"
"uint16_literal"
"int32_literal"
"uint32_literal"
"int64_literal"
"uint64_literal"
"int128_literal"
"uint128_literal" ]
if Stdlib.List.member_v0 supportedInts node.typ then
let intPart =
(findNodeByFieldName node "digits")
|> Stdlib.Option.toResult "No digits node found in int_literal"
let suffixPart =
(findNodeByFieldName node "suffix")
|> Stdlib.Option.toResult "No suffix node found in int_literal"
match intPart, suffixPart with
| Ok intPart, Ok suffixPart ->
let intText = getText intPart
let parsedResult =
match node.typ with
| "int8_literal" ->
(Stdlib.Int8.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EInt8(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "uint8_literal" ->
(Stdlib.UInt8.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EUInt8(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "int16_literal" ->
(Stdlib.Int16.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EInt16(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "uint16_literal" ->
(Stdlib.UInt16.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EUInt16(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "int32_literal" ->
(Stdlib.Int32.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EInt32(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "uint32_literal" ->
(Stdlib.UInt32.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EUInt32(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "int64_literal" ->
(Stdlib.Int64.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EInt64(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "uint64_literal" ->
(Stdlib.UInt64.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EUInt64(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "int128_literal" ->
(Stdlib.Int128.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EInt128(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| "uint128_literal" ->
(Stdlib.UInt128.parse intText)
|> Stdlib.Result.map (fun i ->
WrittenTypes.Expr.EUInt128(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
match parsedResult with
| Ok expr -> Stdlib.Result.Result.Ok expr
| Error _ ->
Stdlib.Result.Result.Error(
WrittenTypes.Unparseable { source = node }
)
| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
let parseFloatLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "float_literal" then
let floatStr = getText node
match Stdlib.Float.parse floatStr with
| Ok floatValue ->
let (sign, whole, remainder) =
let (sign, unsignedFloat) =
if Stdlib.String.startsWith floatStr "-" then
(Sign.Negative, Stdlib.String.dropFirst floatStr 1L)
else
(Sign.Positive, floatStr)
let parts = Stdlib.String.split unsignedFloat "."
let whole =
parts |> Stdlib.List.head |> Stdlib.Option.withDefault "0"
let remainder =
parts
|> Stdlib.List.tail
|> Stdlib.Option.withDefault [ "0" ]
|> Stdlib.List.head
|> Stdlib.Option.withDefault "0"
(sign, whole, remainder)
(WrittenTypes.Expr.EFloat(node.sourceRange, sign, whole, remainder))
|> Stdlib.Result.Result.Ok
| Error _ ->
Stdlib.Result.Result.Error(WrittenTypes.Unparseable { source = node })
else
Stdlib.Result.Result.Error(WrittenTypes.Unparseable { source = node })
let parseStringLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "string_literal" then
let openQuoteNode =
(findNodeByFieldName node "symbol_open_quote")
|> Stdlib.Option.toResult
"No symbol_open_quote node found in string_literal"
let contents =
match findNodeByFieldName node "content" with
| Some contentPart ->
Stdlib.Option.Option.Some(
(contentPart.sourceRange, contentPart.text)
)
| None -> Stdlib.Option.Option.None
let closeQuoteNode =
(findNodeByFieldName node "symbol_close_quote")
|> Stdlib.Option.toResult
"No symbol_close_quote node found in string_literal"
match openQuoteNode, closeQuoteNode with
| Ok openQuoteNode, Ok closeQuoteNode ->
(WrittenTypes.Expr.EString(
node.sourceRange,
contents,
openQuoteNode.sourceRange,
closeQuoteNode.sourceRange
))
|> Stdlib.Result.Result.Ok
| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
let parseCharLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "char_literal" then
let openQuoteNode =
(findNodeByFieldName node "symbol_open_single_quote")
|> Stdlib.Option.toResult
"No symbol_open_single_quote node found in char_literal"
let charNode =
match findNodeByFieldName node "content" with
| Some charPart ->
Stdlib.Option.Option.Some((charPart.sourceRange, charPart.text))
| None -> Stdlib.Option.Option.None
let closeQuoteNode =
(findNodeByFieldName node "symbol_close_single_quote")
|> Stdlib.Option.toResult
"No symbol_close_single_quote node found in char_literal"
match openQuoteNode, closeQuoteNode with
| Ok openQuoteNode, Ok closeQuoteNode ->
(WrittenTypes.Expr.EChar(
node.sourceRange,
charNode,
openQuoteNode.sourceRange,
closeQuoteNode.sourceRange
))
|> Stdlib.Result.Result.Ok
| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
let parseListLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "list_literal" then
let openBracketNode =
(findNodeByFieldName node "symbol_open_bracket")
|> Stdlib.Option.toResult
"No symbol_open_bracket node found in list_literal"
let contents =
node
|> findNodeByFieldName "content"
|> Stdlib.Option.map (fun contentsNode ->
contentsNode.children
|> Stdlib.List.chunkBySize 2L
|> Builtin.unwrap
|> Stdlib.List.map (fun chunk ->
match chunk with
| [ exprNode; symbol ] ->
let expr = Expr.parse exprNode
match expr with
| Ok e -> (e, Stdlib.Option.Option.Some symbol.sourceRange)
| Error _ ->
(WrittenTypes.Unparseable { source = exprNode },
Stdlib.Option.Option.None)
| [ exprNode ] ->
let expr = Expr.parse exprNode
match expr with
| Ok e -> (e, Stdlib.Option.Option.None)
| Error _ ->
(WrittenTypes.Unparseable { source = exprNode },
Stdlib.Option.Option.None)
| _ ->
(WrittenTypes.Unparseable { source = contentsNode },
Stdlib.Option.Option.None)))
|> Stdlib.Option.withDefault []
let closeBracketNode =
(findNodeByFieldName node "symbol_close_bracket")
|> Stdlib.Option.toResult
"No symbol_close_bracket node found in list_literal"
match openBracketNode, closeBracketNode with
| Ok openBracketNode, Ok closeBracketNode ->
(WrittenTypes.Expr.EList(
node.sourceRange,
contents,
openBracketNode.sourceRange,
closeBracketNode.sourceRange
))
|> Stdlib.Result.Result.Ok
let parseDictLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "dict_literal" then
let keywordDictNode =
(findNodeByFieldName node "keyword_dict")
|> Stdlib.Option.toResult "No keyword_dict node found in dict_literal"
let openBraceNode =
(findNodeByFieldName node "symbol_open_brace")
|> Stdlib.Option.toResult
"No symbol_open_brace node found in dict_literal"
let contents =
node
|> findNodeByFieldName "content"
|> Stdlib.Option.map (fun contentsNode ->
contentsNode.children
|> Stdlib.List.chunkBySize 2L
|> Builtin.unwrap
|> Stdlib.List.map (fun chunk ->
match chunk with
| [ dictPairNode; _separator ] ->
let keyNode =
(findNodeByFieldName dictPairNode "key")
|> Stdlib.Option.toResult "No key node found in dict_pair"
let symbolEqualsNode =
(findNodeByFieldName dictPairNode "symbol_equals")
|> Stdlib.Option.toResult
"No symbol_equals node found in dict_pair"
let valueNode =
(findNodeByFieldName dictPairNode "value")
|> Stdlib.Option.toResult "No value node found in dict_pair"
match (keyNode, symbolEqualsNode, valueNode) with
| (Ok keyNode, Ok symbolEqualsNode, Ok valueNode) ->
let key = keyNode.text
let value = Expr.parse valueNode
match value with
| Ok value -> (symbolEqualsNode, key, value)
| _ ->
Stdlib.Result.Result.Error(
WrittenTypes.Unparseable { source = dictPairNode }
)
| _ ->
Stdlib.Result.Result.Error(
WrittenTypes.Unparseable { source = chunk }
)
| [ dictPairNode ] ->
let keyNode =
(findNodeByFieldName dictPairNode "key")
|> Stdlib.Option.toResult "No key node found in dict_pair"
let symbolEqualsNode =
(findNodeByFieldName dictPairNode "symbol_equals")
|> Stdlib.Option.toResult
"No symbol_equals node found in dict_pair"
let valueNode =
(findNodeByFieldName dictPairNode "value")
|> Stdlib.Option.toResult "No value node found in dict_pair"
match (keyNode, symbolEqualsNode, valueNode) with
| (Ok keyNode, Ok symbolEqualsNode, Ok valueNode) ->
let key = keyNode.text
let value = Expr.parse valueNode
match value with
| Ok value -> (symbolEqualsNode, key, value)
| _ ->
Stdlib.Result.Result.Error(
WrittenTypes.Unparseable { source = dictPairNode }
)
| _ ->
Stdlib.Result.Result.Error(
WrittenTypes.Unparseable { source = chunk }
)
| _ ->
Stdlib.Result.Result.Error(
WrittenTypes.Unparseable { source = chunk }
)))
|> Stdlib.Option.withDefault []
let closeBraceNode =
(findNodeByFieldName node "symbol_close_brace")
|> Stdlib.Option.toResult
"No symbol_close_brace node found in dict_literal"
match keywordDictNode, openBraceNode, closeBraceNode with
| Ok keywordDictNode, Ok openBraceNode, Ok closeBraceNode ->
(WrittenTypes.Expr.EDict(
node.sourceRange,
contents,
keywordDictNode.sourceRange,
openBraceNode.sourceRange,
closeBraceNode.sourceRange
))
|> Stdlib.Result.Result.Ok
| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
let parseTupleLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "tuple_literal" then
let openParenNode =
(findNodeByFieldName node "symbol_left_paren")
|> Stdlib.Option.toResult
"No symbol_open_paren node found in tuple_literal"
let first =
match findNodeByFieldName node "first" with
| Some firstNode -> Expr.parse firstNode
| None ->
Stdlib.Result.Result.Error "No first node found in tuple_literal"
let second =
match findNodeByFieldName node "second" with
| Some secondNode -> Expr.parse secondNode
| None ->
Stdlib.Result.Result.Error "No second node found in tuple_literal"
let rest =
(findNodeByFieldName node "rest")
|> Stdlib.Option.map (fun restNode ->
restNode.children
|> Stdlib.List.chunkBySize 2L
|> Builtin.unwrap
|> Stdlib.List.map (fun chunk ->
match chunk with
| [ symbol; exprNode ] ->
let expr = Expr.parse exprNode
match expr with
| Ok expr -> (symbol.sourceRange, expr)
| Error _ ->
(symbol.sourceRange,
WrittenTypes.Unparseable { source = exprNode })
| [ exprNode ] ->
let expr = Expr.parse exprNode
match expr with
| Ok expr -> (Stdlib.Option.Option.None, expr)
| Error _ ->
(Stdlib.Option.Option.None,
WrittenTypes.Unparseable { source = exprNode })
| _ ->
(Stdlib.Option.Option.None,
WrittenTypes.Unparseable { source = chunk })))
|> Stdlib.Option.withDefault []
let commaSymbol =
(findNodeByFieldName node "symbol_comma")
|> Stdlib.Option.toResult "No symbol_comma node found in tuple_literal"