Skip to content

Commit e94a305

Browse files
authored
Merge pull request #5418 from OceanOak/more-fixes
More parser fixes
2 parents 6930ff4 + 939ecc8 commit e94a305

File tree

8 files changed

+61
-42
lines changed

8 files changed

+61
-42
lines changed

backend/src/LibExecution/ProgramTypesToDarkTypes.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,9 @@ module Expr =
921921
Exception.raiseInternal "Invalid record update" [ "update", update ])
922922
PT.ERecordUpdate(uint64 id, fromDT record, updates)
923923

924+
| DEnum(_, _, [], "EConstant", [ DInt64 id; name ]) ->
925+
PT.EConstant(uint64 id, NameResolution.fromDT FQConstantName.fromDT name)
926+
924927
| e -> Exception.raiseInternal "Invalid Expr" [ "e", e ]
925928

926929

@@ -946,7 +949,7 @@ module Const =
946949
| PT.Const.CUInt128 i -> "CUInt128", [ DUInt128 i ]
947950
| PT.Const.CFloat(sign, w, f) ->
948951
"CFloat", [ Sign.toDT sign; DString w; DString f ]
949-
| PT.Const.CChar c -> "CChar", [ DChar c ]
952+
| PT.Const.CChar c -> "CChar", [ DString c ]
950953
| PT.Const.CString s -> "CString", [ DString s ]
951954

952955
| PT.Const.CTuple(first, second, theRest) ->
@@ -986,7 +989,7 @@ module Const =
986989
| DEnum(_, _, [], "CUInt128", [ DUInt128 i ]) -> PT.Const.CUInt128 i
987990
| DEnum(_, _, [], "CBool", [ DBool b ]) -> PT.Const.CBool b
988991
| DEnum(_, _, [], "CString", [ DString s ]) -> PT.Const.CString s
989-
| DEnum(_, _, [], "CChar", [ DChar c ]) -> PT.Const.CChar c
992+
| DEnum(_, _, [], "CChar", [ DString c ]) -> PT.Const.CChar c
990993
| DEnum(_, _, [], "CFloat", [ sign; DString w; DString f ]) ->
991994
PT.Const.CFloat(Sign.fromDT sign, w, f)
992995
| DEnum(_, _, [], "CUnit", []) -> PT.Const.CUnit

backend/tests/Tests/NewParser.Tests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ let exprs =
617617
t
618618
"dict with double_backtick_identifier"
619619
"Dict { ``Content-Length`` = 1L }"
620-
"Dict { ``Content-Length`` = 1L }"
620+
"Dict { Content-Length = 1L }"
621621
[]
622622
[]
623623
[]

packages/darklang/languageTools/nameResolver.dark

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ module Darklang =
171171
|> Stdlib.String.join "."
172172

173173
if owner == "Builtin" && modules == [] then
174-
if builtinThingExists nameForLookup then
174+
if builtinThingExists name.name then
175175
(ProgramTypes.FQConstantName.Builtin
176176
{ name = name.name
177177
version = name.version })
178178
|> ProgramTypes.FQConstantName.FQConstantName.Builtin
179179
|> Stdlib.Result.Result.Ok
180180
else
181-
Error()
181+
Stdlib.Result.Result.Error()
182182
else
183183
let find = pm.findConstant
184184

packages/darklang/languageTools/parser/core.dark

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ module Darklang =
4545
let getText (node: ParsedNode) : String = node.text
4646
let getRange (node: ParsedNode) : Range = node.range
4747

48+
let emptyVar = "___"
49+
let nameOrBlank (v: String) : String = if v == emptyVar then "" else v
50+
4851
let findNodeByFieldName
4952
(node: ParsedNode)
5053
(fieldName: String)
@@ -228,7 +231,13 @@ module Darklang =
228231
| [ dictPairNode; _separator ] ->
229232
let keyNode =
230233
findAndParseRequired dictPairNode "key" (fun node ->
231-
(node.range, Parser.getText node) |> Stdlib.Result.Result.Ok)
234+
match node.typ with
235+
| "double_backtick_identifier" ->
236+
let key = node.text |> Stdlib.String.slice 2L -2L
237+
(node.range, key) |> Stdlib.Result.Result.Ok
238+
| _ ->
239+
(node.range, nameOrBlank (Parser.getText node))
240+
|> Stdlib.Result.Result.Ok)
232241

233242
let symbolEqualsNode = findField dictPairNode "symbol_equals"
234243

@@ -242,7 +251,13 @@ module Darklang =
242251
| [ dictPairNode ] ->
243252
let keyNode =
244253
findAndParseRequired dictPairNode "key" (fun node ->
245-
(node.range, Parser.getText node) |> Stdlib.Result.Result.Ok)
254+
match node.typ with
255+
| "double_backtick_identifier" ->
256+
let key = node.text |> Stdlib.String.slice 2L -2L
257+
(node.range, key) |> Stdlib.Result.Result.Ok
258+
| _ ->
259+
(node.range, nameOrBlank (Parser.getText node))
260+
|> Stdlib.Result.Result.Ok)
246261

247262
let symbolEqualsNode = findField dictPairNode "symbol_equals"
248263

packages/darklang/languageTools/parser/expr.dark

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ module Darklang =
490490
(WrittenTypes.Expr.EFieldAccess(
491491
node.range,
492492
expr,
493-
(field.range, field.text),
493+
(field.range, nameOrBlank field.text),
494494
symbolDot.range
495495
))
496496
|> Stdlib.Result.Result.Ok
@@ -597,8 +597,15 @@ module Darklang =
597597
))
598598
|> Stdlib.Result.Result.Ok)
599599

600-
| _ -> createUnparseableError child
600+
| _ -> createUnparseableError node
601601

602+
// Helper function for parseLambda
603+
let skipEmptyVar (node: List<ParsedNode>) : List<ParsedNode> =
604+
node
605+
|> Stdlib.List.filter (fun c ->
606+
match c.typ with
607+
| "let_pattern" when c.text == "___" -> false
608+
| _ -> true)
602609

603610
let parseLambda
604611
(node: ParsedNode)
@@ -609,6 +616,7 @@ module Darklang =
609616
match findField node "pats" with
610617
| Ok paramsNode ->
611618
paramsNode.children
619+
|> skipEmptyVar
612620
|> Stdlib.List.map (fun pat -> parseLetPattern pat)
613621
|> Stdlib.Result.collect
614622

packages/darklang/languageTools/parser/matchPattern.dark

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,12 @@ module Darklang =
256256
(findNodeByFieldName node "enum_fields")
257257
|> Stdlib.Option.map (fun enumFieldsNode ->
258258
enumFieldsNode.children
259-
|> Stdlib.List.map (fun pat -> parseMatchPattern pat)
259+
|> Stdlib.List.chunkBySize 2L
260+
|> Builtin.unwrap
261+
|> Stdlib.List.map (fun contentSymbolPair ->
262+
match contentSymbolPair with
263+
| [ contentNode; symbol ] -> parseMatchPattern contentNode
264+
| [ contentNode ] -> parseMatchPattern contentNode)
260265
|> Stdlib.Result.collect)
261266

262267
|> Stdlib.Option.withDefault ([] |> Stdlib.Result.Result.Ok)

packages/darklang/languageTools/parser/pipeExpr.dark

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,18 @@ module Darklang =
105105
let parsePipeEnum
106106
(node: ParsedNode)
107107
: Stdlib.Result.Result<WrittenTypes.PipeExpr, WrittenTypes.Unparseable> =
108-
let typeNameNode = findField node "type_name"
109-
let symbolDotNode = findField node "symbol_dot"
110-
let caseNameNode = findField node "case_name"
111-
112-
let enumFieldsNode =
113-
(findNodeByFieldName node "enum_fields")
114-
|> Stdlib.Option.map (fun enumFieldsNode ->
115-
enumFieldsNode.children
116-
|> Stdlib.List.map (fun fieldNode -> Expr.parse fieldNode)
117-
|> Stdlib.Result.collect)
118-
119-
|> Stdlib.Option.withDefault (Stdlib.Result.Result.Ok [])
120-
121-
match typeNameNode, symbolDotNode, caseNameNode, enumFieldsNode with
122-
| Ok typeNameNode, Ok symbolDotNode, Ok caseNameNode, Ok enumFieldsNode ->
123-
let typeName = typeNameNode.text |> Stdlib.String.split "."
124-
125-
(WrittenTypes.PipeExpr.EPipeEnum(
126-
node.range,
127-
(typeNameNode.range, typeName),
128-
(caseNameNode.range, caseNameNode.text),
129-
enumFieldsNode,
130-
symbolDotNode.range
131-
))
132-
|> Stdlib.Result.Result.Ok
133-
134-
| _ -> createUnparseableError node
108+
baseParseEnum
109+
Expr.parse
110+
node
111+
(fun (range, typeName, caseName, enumFields, symbolDot) ->
112+
(WrittenTypes.PipeExpr.EPipeEnum(
113+
range,
114+
typeName,
115+
caseName,
116+
enumFields,
117+
symbolDot
118+
))
119+
|> Stdlib.Result.Result.Ok)
135120

136121

137122
let parsePipeFnCall

packages/darklang/languageTools/writtenTypesToProgramTypes.dark

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module Darklang =
6262
(owner: String)
6363
(currentModule: List<String>)
6464
(c: WrittenTypes.QualifiedConstantOrFnIdentifier)
65-
: ConstOrFn =
65+
: Stdlib.Result.Result<ConstOrFn, ProgramTypes.NameResolution<ProgramTypes.FQConstantName.FQConstantName>> =
6666
let nameToResolve =
6767
Stdlib.List.append
6868
(Stdlib.List.map c.modules (fun (id, _) -> id.name))
@@ -85,10 +85,10 @@ module Darklang =
8585
(WrittenTypes.Name.Unresolved(c.range, nameToResolve))
8686

8787
match isFn with
88-
| Ok _ -> isFn |> ConstOrFn.Fn
88+
| Ok _ -> isFn |> ConstOrFn.Fn |> Stdlib.Result.Result.Ok
8989
| Error _ ->
9090
match isConstant with
91-
| Ok _ -> isConstant |> ConstOrFn.Const
91+
| Ok _ -> isConstant |> ConstOrFn.Const |> Stdlib.Result.Result.Ok
9292
| Error _ -> isConstant
9393

9494
module Fn =
@@ -501,8 +501,11 @@ module Darklang =
501501
Identifiers.QualifiedConstOrFn.toPT onMissing pm owner currentModule id
502502

503503
match constantOrFn with
504-
| Const c -> ProgramTypes.Expr.EConstant(gid (), c)
505-
| Fn f -> ProgramTypes.Expr.EFnName(gid (), f)
504+
| Ok(Const c) -> ProgramTypes.Expr.EConstant(gid (), c)
505+
| Ok(Fn f) -> ProgramTypes.Expr.EFnName(gid (), f)
506+
| Error _ ->
507+
// CLEANUP: Rethink this solution. It was added to allow failure during the first pass of parsing, where names aren't yet resolved
508+
ProgramTypes.Expr.EVariable (gid ()) id.constantOrFn.name
506509

507510
| EFieldAccess(_, expr, (_, fieldName), _) ->
508511
ProgramTypes.Expr.EFieldAccess(

0 commit comments

Comments
 (0)