Skip to content

Commit

Permalink
wip: parser fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OceanOak committed Sep 2, 2024
1 parent 6930ff4 commit 1893fa2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 40 deletions.
4 changes: 4 additions & 0 deletions backend/src/LibExecution/ProgramTypesToDarkTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,9 @@ module Expr =
Exception.raiseInternal "Invalid record update" [ "update", update ])
PT.ERecordUpdate(uint64 id, fromDT record, updates)

| DEnum(_, _, [], "EConstant", [ DInt64 id; name ]) ->
PT.EConstant(uint64 id, NameResolution.fromDT FQConstantName.fromDT name)

| e -> Exception.raiseInternal "Invalid Expr" [ "e", e ]


Expand Down Expand Up @@ -987,6 +990,7 @@ module Const =
| DEnum(_, _, [], "CBool", [ DBool b ]) -> PT.Const.CBool b
| DEnum(_, _, [], "CString", [ DString s ]) -> PT.Const.CString s
| DEnum(_, _, [], "CChar", [ DChar c ]) -> PT.Const.CChar c
| DEnum(_, _, [], "CChar", [ DString c ]) -> PT.Const.CChar c
| DEnum(_, _, [], "CFloat", [ sign; DString w; DString f ]) ->
PT.Const.CFloat(Sign.fromDT sign, w, f)
| DEnum(_, _, [], "CUnit", []) -> PT.Const.CUnit
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/Tests/NewParser.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ let exprs =
t
"dict with double_backtick_identifier"
"Dict { ``Content-Length`` = 1L }"
"Dict { ``Content-Length`` = 1L }"
"Dict { Content-Length = 1L }"
[]
[]
[]
Expand Down
4 changes: 2 additions & 2 deletions packages/darklang/languageTools/nameResolver.dark
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ module Darklang =
|> Stdlib.String.join "."

if owner == "Builtin" && modules == [] then
if builtinThingExists nameForLookup then
if builtinThingExists name.name then
(ProgramTypes.FQConstantName.Builtin
{ name = name.name
version = name.version })
|> ProgramTypes.FQConstantName.FQConstantName.Builtin
|> Stdlib.Result.Result.Ok
else
Error()
Stdlib.Result.Result.Error()
else
let find = pm.findConstant

Expand Down
20 changes: 18 additions & 2 deletions packages/darklang/languageTools/parser/core.dark
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ module Darklang =
let getText (node: ParsedNode) : String = node.text
let getRange (node: ParsedNode) : Range = node.range

let emptyVar = "___"
let nameOrBlank (v: String) : String = if v == emptyVar then "" else v

let findNodeByFieldName
(node: ParsedNode)
(fieldName: String)
Expand Down Expand Up @@ -228,7 +231,14 @@ module Darklang =
| [ dictPairNode; _separator ] ->
let keyNode =
findAndParseRequired dictPairNode "key" (fun node ->
(node.range, Parser.getText node) |> Stdlib.Result.Result.Ok)
// TODO: maybe extract this into a helper function
match node.typ with
| "double_backtick_identifier" ->
let key = node.text |> Stdlib.String.slice 2L -2L
(node.range, key) |> Stdlib.Result.Result.Ok
| _ ->
(node.range, nameOrBlank (Parser.getText node))
|> Stdlib.Result.Result.Ok)

let symbolEqualsNode = findField dictPairNode "symbol_equals"

Expand All @@ -242,7 +252,13 @@ module Darklang =
| [ dictPairNode ] ->
let keyNode =
findAndParseRequired dictPairNode "key" (fun node ->
(node.range, Parser.getText node) |> Stdlib.Result.Result.Ok)
match node.typ with
| "double_backtick_identifier" ->
let key = node.text |> Stdlib.String.slice 2L -2L
(node.range, key) |> Stdlib.Result.Result.Ok
| _ ->
(node.range, nameOrBlank (Parser.getText node))
|> Stdlib.Result.Result.Ok)

let symbolEqualsNode = findField dictPairNode "symbol_equals"

Expand Down
12 changes: 10 additions & 2 deletions packages/darklang/languageTools/parser/expr.dark
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ module Darklang =
(WrittenTypes.Expr.EFieldAccess(
node.range,
expr,
(field.range, field.text),
(field.range, nameOrBlank (field.text)),
symbolDot.range
))
|> Stdlib.Result.Result.Ok
Expand Down Expand Up @@ -597,8 +597,15 @@ module Darklang =
))
|> Stdlib.Result.Result.Ok)

| _ -> createUnparseableError child
| _ -> createUnparseableError node

// Helper function for parseLambda
let skipEmptyVar (node: List<ParsedNode>) : List<ParsedNode> =
node
|> Stdlib.List.filter (fun c ->
match c.typ with
| "let_pattern" when c.text == "___" -> false
| _ -> true)

let parseLambda
(node: ParsedNode)
Expand All @@ -609,6 +616,7 @@ module Darklang =
match findField node "pats" with
| Ok paramsNode ->
paramsNode.children
|> skipEmptyVar
|> Stdlib.List.map (fun pat -> parseLetPattern pat)
|> Stdlib.Result.collect

Expand Down
7 changes: 6 additions & 1 deletion packages/darklang/languageTools/parser/matchPattern.dark
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,12 @@ module Darklang =
(findNodeByFieldName node "enum_fields")
|> Stdlib.Option.map (fun enumFieldsNode ->
enumFieldsNode.children
|> Stdlib.List.map (fun pat -> parseMatchPattern pat)
|> Stdlib.List.chunkBySize 2L
|> Builtin.unwrap
|> Stdlib.List.map (fun contentSymbolPair ->
match contentSymbolPair with
| [ contentNode; symbol ] -> parseMatchPattern contentNode
| [ contentNode ] -> parseMatchPattern contentNode)
|> Stdlib.Result.collect)

|> Stdlib.Option.withDefault ([] |> Stdlib.Result.Result.Ok)
Expand Down
39 changes: 12 additions & 27 deletions packages/darklang/languageTools/parser/pipeExpr.dark
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,18 @@ module Darklang =
let parsePipeEnum
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.PipeExpr, WrittenTypes.Unparseable> =
let typeNameNode = findField node "type_name"
let symbolDotNode = findField node "symbol_dot"
let caseNameNode = findField node "case_name"

let enumFieldsNode =
(findNodeByFieldName node "enum_fields")
|> Stdlib.Option.map (fun enumFieldsNode ->
enumFieldsNode.children
|> Stdlib.List.map (fun fieldNode -> Expr.parse fieldNode)
|> Stdlib.Result.collect)

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

match typeNameNode, symbolDotNode, caseNameNode, enumFieldsNode with
| Ok typeNameNode, Ok symbolDotNode, Ok caseNameNode, Ok enumFieldsNode ->
let typeName = typeNameNode.text |> Stdlib.String.split "."

(WrittenTypes.PipeExpr.EPipeEnum(
node.range,
(typeNameNode.range, typeName),
(caseNameNode.range, caseNameNode.text),
enumFieldsNode,
symbolDotNode.range
))
|> Stdlib.Result.Result.Ok

| _ -> createUnparseableError node
baseParseEnum
Expr.parse
node
(fun (range, typeName, caseName, enumFields, symbolDot) ->
(WrittenTypes.PipeExpr.EPipeEnum(
range,
typeName,
caseName,
enumFields,
symbolDot
))
|> Stdlib.Result.Result.Ok)


let parsePipeFnCall
Expand Down
14 changes: 9 additions & 5 deletions packages/darklang/languageTools/writtenTypesToProgramTypes.dark
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module Darklang =
(owner: String)
(currentModule: List<String>)
(c: WrittenTypes.QualifiedConstantOrFnIdentifier)
: ConstOrFn =
: Stdlib.Result.Result<ConstOrFn, ProgramTypes.NameResolution<ProgramTypes.FQConstantName.FQConstantName>> =
let nameToResolve =
Stdlib.List.append
(Stdlib.List.map c.modules (fun (id, _) -> id.name))
Expand All @@ -85,10 +85,10 @@ module Darklang =
(WrittenTypes.Name.Unresolved(c.range, nameToResolve))

match isFn with
| Ok _ -> isFn |> ConstOrFn.Fn
| Ok _ -> isFn |> ConstOrFn.Fn |> Stdlib.Result.Result.Ok
| Error _ ->
match isConstant with
| Ok _ -> isConstant |> ConstOrFn.Const
| Ok _ -> isConstant |> ConstOrFn.Const |> Stdlib.Result.Result.Ok
| Error _ -> isConstant

module Fn =
Expand Down Expand Up @@ -501,8 +501,12 @@ module Darklang =
Identifiers.QualifiedConstOrFn.toPT onMissing pm owner currentModule id

match constantOrFn with
| Const c -> ProgramTypes.Expr.EConstant(gid (), c)
| Fn f -> ProgramTypes.Expr.EFnName(gid (), f)
| Ok(Const c) -> ProgramTypes.Expr.EConstant(gid (), c)
| Ok(Fn f) -> ProgramTypes.Expr.EFnName(gid (), f)
| Error _ ->
ProgramTypes.Expr.EVariable
(gid ())
"TODO: we should allow it to fail for the first pass"

| EFieldAccess(_, expr, (_, fieldName), _) ->
ProgramTypes.Expr.EFieldAccess(
Expand Down

0 comments on commit 1893fa2

Please sign in to comment.