Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree-sitter grammar: support Enums #5357

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions backend/testfiles/execution/stdlib/parser.dark
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,23 @@ module TextToTextRoundtripping =
hasPet: Bool
pet: Pet }"""

// Enum type
("type Color = | Red | Green | Blue" |> roundtripCliScript) = "type Color =\n | Red\n | Green\n | Blue"
("type MyEnum = | A of Int64" |> roundtripCliScript) = "type MyEnum =\n | A of Int64"
("type MyEnum = | A of Int64 * Int64" |> roundtripCliScript) = "type MyEnum =\n | A of Int64 * Int64"
("type MyEnum = | A of Int64 * Bool * String | B of Int64" |> roundtripCliScript) = "type MyEnum =\n | A of Int64 * Bool * String\n | B of Int64"
("type MyEnum = | A of x:Int64 * y:Int64" |> roundtripCliScript) = "type MyEnum =\n | A of x: Int64 * y: Int64"

("type Color =\n | Red\n | Green\n | Blue" |> roundtripCliScript) = "type Color =\n | Red\n | Green\n | Blue"
("type MyEnum =\n | A of Int64\n | B of String" |> roundtripCliScript) = "type MyEnum =\n | A of Int64\n | B of String"
("type MyEnum =\n | A of x: Int64\n | B of y: String" |> roundtripCliScript) = "type MyEnum =\n | A of x: Int64\n | B of y: String"

("type MyEnum =\n | A of x: Int64 * y: Int64\n | B of z: String"
|> roundtripCliScript) = "type MyEnum =\n | A of x: Int64 * y: Int64\n | B of z: String"




module Expr =
// units
("()" |> roundtripCliScript) = "()"
Expand Down Expand Up @@ -330,6 +347,16 @@ module TextToTextRoundtripping =
("Person {name =\"John\"; age = 30L; hasPet = true; pet = Pet {name = \"Luna\"}} "
|> roundtripCliScript) = "Person { name = \"John\"; age = 30L; hasPet = true; pet = Pet { name = \"Luna\" } }"

// enum literal
("Color.Red" |> roundtripCliScript) = "Color.Red"
("Stdlib.Option.None" |> roundtripCliScript) = "Stdlib.Option.None"
("PACKAGE.Darklang.Stdlib.Option.Option.None" |> roundtripCliScript) = "PACKAGE.Darklang.Stdlib.Option.Option.None"
("PACKAGE.Darklang.Stdlib.Option.Option.Some(1L)" |> roundtripCliScript) = "PACKAGE.Darklang.Stdlib.Option.Option.Some(1L)"
("MyEnum.A(1L, 2L)" |> roundtripCliScript) = "MyEnum.A(1L, 2L)"




// variables and let bindings
("assumedlyAVariableName" |> roundtripCliScript) = "assumedlyAVariableName"
// TODO: this is ugly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ module Darklang =
"parameter" // [7] for function parameter identifiers
"variable" // [8] for general identifiers
"function" // [9] for function names/identifiers
"property" ] // [10] for field names
"property" // [10] for field names
"enumMember" ] // [11] for enum case names

let tokenModifiers = []

Expand All @@ -53,6 +54,7 @@ module Darklang =
| FunctionName -> 9UL

| Property -> 10UL
| EnumMember -> 11UL

let toRelativeTokens
(tokens: List<LanguageTools.SemanticTokens.SemanticToken>)
Expand Down
122 changes: 122 additions & 0 deletions packages/darklang/languageTools/parser.dark
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,72 @@ module Darklang =
(WrittenTypes.TypeDeclaration.Definition.Record recordFields)
|> Stdlib.Result.Result.Ok


| [ child ] when child.typ == "type_decl_def_enum" ->
match findNodeByFieldName child "content" with
| Some contentNode ->
let enumCases =
contentNode.children
|> Stdlib.List.filter (fun caseNode ->
caseNode.typ != "indent" && caseNode.typ != "dedent")

|> Stdlib.List.map (fun caseNode ->
let caseNameNode = findNodeByFieldName caseNode "case_name"

let keywordOf =
Stdlib.Option.map
(findNodeByFieldName caseNode "keyword_of")
(fun node -> node.sourceRange)

let fields =
caseNode.children
|> Stdlib.List.filter (fun field ->
field.typ == "type_decl_enum_field")
|> Stdlib.List.map (fun field ->
match findNodeByFieldName field "type" with
| Some fieldTypeNode ->
let label =
(findNodeByFieldName field "identifier")
|> Stdlib.Option.map (fun ln ->
(ln.sourceRange, ln.text))

let colonSymbol =
(findNodeByFieldName field "symbol_colon")
|> Stdlib.Option.map (fun ln -> ln.sourceRange)

let fieldType = TypeReference.parse fieldTypeNode

match fieldType with
| Ok fieldType ->
WrittenTypes.TypeDeclaration.EnumField
{ range = field.sourceRange
typ = fieldType
label = label
description = ""
symbolColon = colonSymbol }
| Error _ -> WrittenTypes.Unparseable { source = field }
| None -> WrittenTypes.Unparseable { source = field })

match caseNameNode with
| Some caseNameNode ->
(WrittenTypes.TypeDeclaration.EnumCase
{ range = caseNode.sourceRange
name = (caseNameNode.sourceRange, caseNameNode.text)
fields = fields
description = ""
keywordOf = keywordOf })
|> Stdlib.Result.Result.Ok
| None ->
(WrittenTypes.Unparseable { source = caseNode })
|> Stdlib.Result.Result.Error)

enumCases
|> Stdlib.Result.collect
|> Stdlib.Result.map (fun enumCases ->
WrittenTypes.TypeDeclaration.Definition.Enum enumCases)

| None -> []

| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
Expand All @@ -562,6 +628,7 @@ module Darklang =
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error


let parse
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.TypeDeclaration.TypeDeclaration, WrittenTypes.Unparseable> =
Expand Down Expand Up @@ -1259,6 +1326,59 @@ module Darklang =
|> Stdlib.Result.Result.Error


let parseEnumLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "enum_literal" then
let typeNameNode =
(findNodeByFieldName node "type_name")
|> Stdlib.Option.toResult "No type_name node found in enum_literal"

let symbolDotNode =
(findNodeByFieldName node "symbol_dot")
|> Stdlib.Option.toResult "No symbol_dot node found in enum_literal"

let caseNameNode =
(findNodeByFieldName node "case_name")
|> Stdlib.Option.toResult "No case_name node found in enum_literal"

let enumFieldsNode =
(findNodeByFieldName node "enum_fields")
|> Stdlib.Option.map (fun enumFieldsNode ->
enumFieldsNode.children
|> Stdlib.List.chunkBySize 2L
|> Builtin.unwrap
|> Stdlib.List.map (fun chunk ->
match chunk with
| [ fieldNode ] ->
match Expr.parse fieldNode with
| Ok field -> field
| Error _ -> (WrittenTypes.Unparseable { source = fieldNode })

| [ fieldNode; _separator ] ->
match Expr.parse fieldNode with
| Ok field -> field
| Error _ -> (WrittenTypes.Unparseable { source = fieldNode })))

|> Stdlib.Option.withDefault []


match typeNameNode, symbolDotNode, caseNameNode with
| Ok typeNameNode, Ok symbolDotNode, Ok caseNameNode ->

(WrittenTypes.Expr.EEnum(
node.sourceRange,
(typeNameNode.sourceRange, [ typeNameNode.text ]),
(caseNameNode.sourceRange, caseNameNode.text),
enumFieldsNode,
symbolDotNode.sourceRange
))
|> Stdlib.Result.Result.Ok

| _ ->
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error


let parseLetExpr
(node: ParsedNode)
Expand Down Expand Up @@ -1547,6 +1667,8 @@ module Darklang =

| "record_literal" -> parseRecordLiteral node

| "enum_literal" -> parseEnumLiteral node

// assigning and accessing variables
| "let_expression" -> parseLetExpr node
| "variable_identifier" ->
Expand Down
70 changes: 69 additions & 1 deletion packages/darklang/languageTools/semanticTokens.dark
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Darklang =

| TypeParameter

| EnumCase // in Option.Some, this would be `Some`
| EnumMember // in Option.Some, this would be `Some`
| Property // ? maybe this should be used for fields
| Method // ? or maybe this should be used for fields
| RegularExpression
Expand Down Expand Up @@ -180,6 +180,45 @@ module Darklang =
|> Stdlib.List.flatten)
|> Stdlib.List.flatten

| Enum cases ->
cases
|> Stdlib.List.map (fun case ->
let (enumCaseNameRange, _) = case.name

let keywordOf =
match case.keywordOf with
| Some range -> [ makeToken range TokenType.Keyword ]
| None -> []

let labelTokens =
case.fields
|> Stdlib.List.map (fun field ->
let label =
match field.label with
| Some((range, _)) -> [ range ]
| None -> []

let colon =
match field.symbolColon with
| Some range -> [ range ]
| None -> []

Stdlib.List.append label colon)
|> Stdlib.List.flatten
|> Stdlib.List.map (fun range -> makeToken range TokenType.Property)

let typeRange =
case.fields
|> Stdlib.List.map (fun field -> TypeReference.tokenize field.typ)

[ [ makeToken enumCaseNameRange TokenType.EnumMember ]
keywordOf
labelTokens
typeRange |> Stdlib.List.flatten ]
|> Stdlib.List.flatten)
|> Stdlib.List.flatten


// type ID = UInt64
let tokenize
(t: WrittenTypes.TypeDeclaration.TypeDeclaration)
Expand Down Expand Up @@ -382,6 +421,35 @@ module Darklang =
[ makeToken symbolCloseBrace TokenType.Symbol ] ]
|> Stdlib.List.flatten


// Option.Option.Some 1L
| EEnum(range, typeName, caseName, fields, symbolDot) ->
let typeName =
match typeName with
| (range, _) -> [ makeToken range TokenType.TypeName ]
| _ -> []

let caseName =
match caseName with
| (range, _) -> [ makeToken range TokenType.EnumMember ]
| _ -> []

let fields =
fields
|> Stdlib.List.map (fun expr -> Expr.tokenize expr)
|> Stdlib.List.flatten

[ // Option.Option.Some
typeName
// .
[ makeToken symbolDot TokenType.Symbol ]
// Some
caseName
// 1L
fields ]
|> Stdlib.List.flatten


// let x = 2
// x + 1
| ELet(range, lp, expr, body, keywordLet, symbolEquals) ->
Expand Down
23 changes: 23 additions & 0 deletions packages/darklang/languageTools/writtenTypes.dark
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,24 @@ module Darklang =
description: String
symbolColon: SourceRange }

type EnumField =
{ range: SourceRange
typ: TypeReference.TypeReference
label: Stdlib.Option.Option<SourceRange * String>
description: String
symbolColon: Stdlib.Option.Option<SourceRange> }

type EnumCase =
{ range: SourceRange
name: SourceRange * String
fields: List<EnumField>
description: String
keywordOf: Stdlib.Option.Option<SourceRange> }

type Definition =
| Alias of TypeReference.TypeReference
| Record of List<RecordField>
| Enum of List<EnumCase>

type TypeDeclaration =
{ range: SourceRange
Expand Down Expand Up @@ -217,6 +232,14 @@ module Darklang =
symbolOpenBrace: SourceRange *
symbolCloseBrace: SourceRange

| EEnum of
SourceRange *
typeName: (SourceRange * List<String>) *
caseName: (SourceRange * String) *
fields: List<Expr> *
/// between the typeName and the caseName
symbolDot: SourceRange
OceanOak marked this conversation as resolved.
Show resolved Hide resolved

| ELet of
SourceRange *
LetPattern *
Expand Down
Loading