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 Constant Declaration #5377

Merged
merged 7 commits into from
Jun 3, 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
135 changes: 96 additions & 39 deletions backend/testfiles/execution/stdlib/parser.dark
Original file line number Diff line number Diff line change
Expand Up @@ -111,42 +111,47 @@ module ParseNodeToWrittenTypes =
.CliScript(
PACKAGE.Darklang.LanguageTools.WrittenTypes.CliScript
{ range = range (0L, 0L) (0L, 17L)
typesAndFns =
[ PACKAGE.Darklang.LanguageTools.WrittenTypes.CliScriptTypeOrFn.Type(
(PACKAGE.Darklang.LanguageTools.WrittenTypes.TypeDeclaration.TypeDeclaration
{ range = range (0L, 0L) (0L, 17L)
name =
PACKAGE.Darklang.LanguageTools.WrittenTypes.TypeIdentifier
{ range = range (0L, 5L) (0L, 9L)
name = "MyID" }
typeParams = []
definition =
PACKAGE
.Darklang
.LanguageTools
.WrittenTypes
.TypeDeclaration
.Definition
.Alias(
PACKAGE
.Darklang
.LanguageTools
.WrittenTypes
.TypeReference
.TypeReference
.Builtin(
PACKAGE
.Darklang
.LanguageTools
.WrittenTypes
.TypeReference
.Builtin
.TInt64(range (0L, 12L) (0L, 17L))
)
)
keywordType = range (0L, 0L) (0L, 4L)
symbolEquals = range (0L, 10L) (0L, 11L) })
) ]
typesAndFnsAndConsts =
[ PACKAGE
.Darklang
.LanguageTools
.WrittenTypes
.CliScriptTypeOrFnOrConst
.Type(
(PACKAGE.Darklang.LanguageTools.WrittenTypes.TypeDeclaration.TypeDeclaration
{ range = range (0L, 0L) (0L, 17L)
name =
PACKAGE.Darklang.LanguageTools.WrittenTypes.TypeIdentifier
{ range = range (0L, 5L) (0L, 9L)
name = "MyID" }
typeParams = []
definition =
PACKAGE
.Darklang
.LanguageTools
.WrittenTypes
.TypeDeclaration
.Definition
.Alias(
PACKAGE
.Darklang
.LanguageTools
.WrittenTypes
.TypeReference
.TypeReference
.Builtin(
PACKAGE
.Darklang
.LanguageTools
.WrittenTypes
.TypeReference
.Builtin
.TInt64(range (0L, 12L) (0L, 17L))
)
)
keywordType = range (0L, 0L) (0L, 4L)
symbolEquals = range (0L, 10L) (0L, 11L) })
) ]
unparseableStuff = []
exprsToEval = [] }
)
Expand All @@ -165,8 +170,8 @@ module TextToTextRoundtripping =
match parsedFile with
| CliScript script -> script

let typesAndFnsPart = // List<String>
(Stdlib.List.fold script.typesAndFns [] (fun acc typeOrFn ->
let typesAndFnsAndConstsPart = // List<String>
(Stdlib.List.fold script.typesAndFnsAndConsts [] (fun acc typeOrFn ->
match typeOrFn with
| Type typeDecl ->
let prettyPrinted =
Expand All @@ -188,6 +193,17 @@ module TextToTextRoundtripping =
fnDecl)
|> PACKAGE.Darklang.PrettyPrinter.ProgramTypes.packageFn

acc |> Stdlib.List.push prettyPrinted

| Constant constDecl ->
let prettyPrinted =
(PACKAGE.Darklang.LanguageTools.WrittenTypesToProgramTypes.ConstantDeclaration.toPackageConstPT
onMissing
owner
modules
constDecl)
|> PACKAGE.Darklang.PrettyPrinter.ProgramTypes.packageConstant

acc |> Stdlib.List.push prettyPrinted))
|> Stdlib.List.reverse

Expand All @@ -203,7 +219,7 @@ module TextToTextRoundtripping =
acc |> Stdlib.List.push prettyPrinted))
|> Stdlib.List.reverse

[ typesAndFnsPart; exprsPart ]
[ typesAndFnsAndConstsPart; exprsPart ]
|> Stdlib.List.flatten
|> Stdlib.String.join "\n\n"

Expand Down Expand Up @@ -313,6 +329,47 @@ module TextToTextRoundtripping =
|> roundtripCliScript) = "type MyEnum =\n | A of x: Int64 * y: Int64\n | B of z: String"


module ConstantDeclaration =
("const unitConst = ()" |> roundtripCliScript) = "const unitConst = ()"
("const maxInt8 = 127y" |> roundtripCliScript) = "const maxInt8 = 127y"
("const maxUInt8 = 255uy" |> roundtripCliScript) = "const maxUInt8 = 255uy"
("const maxInt16 = 32767s" |> roundtripCliScript) = "const maxInt16 = 32767s"
("const maxUInt16 = 65535us" |> roundtripCliScript) = "const maxUInt16 = 65535us"
("const maxInt32 = 2147483647l" |> roundtripCliScript) = "const maxInt32 = 2147483647l"
("const maxUInt32 = 4294967295ul" |> roundtripCliScript) = "const maxUInt32 = 4294967295ul"
("const maxInt64 = 9223372036854775807L" |> roundtripCliScript) = "const maxInt64 = 9223372036854775807L"
("const maxUInt64 = 18446744073709551615UL" |> roundtripCliScript) = "const maxUInt64 = 18446744073709551615UL"

("const maxInt128 = 170141183460469231731687303715884105727Q"
|> roundtripCliScript) = "const maxInt128 = 170141183460469231731687303715884105727Q"

("const maxUInt128 = 340282366920938463463374607431768211455Z"
|> roundtripCliScript) = "const maxUInt128 = 340282366920938463463374607431768211455Z"

("const trueConst = true" |> roundtripCliScript) = "const trueConst = true"
("const falseConst = false" |> roundtripCliScript) = "const falseConst = false"

("const greeting = \"hello\"" |> roundtripCliScript) = "const greeting = \"hello\""
("const newline = '\\n'" |> roundtripCliScript) = "const newline = '\\n'"

("const pi = 3.14159" |> roundtripCliScript) = "const pi = 3.14159"

("const emptyDict = Dict {}" |> roundtripCliScript) = "const emptyDict = Dict { }"
("const dict = Dict { a = 1L }" |> roundtripCliScript) = "const dict = Dict { a = 1L }"
("const dict = Dict { a = \"hello\"; b = \"test\" }" |> roundtripCliScript) = "const dict = Dict { a = \"hello\"; b = \"test\" }"

("const tuple2Const = (1L, 2L)" |> roundtripCliScript) = "const tuple2Const = (1L, 2L)"
("const tuple3Const = (1L, 2L, 3L)" |> roundtripCliScript) = "const tuple3Const = (1L, 2L, 3L)"

("const emptyList = []" |> roundtripCliScript) = "const emptyList = []"
("const listOfInts = [1L; 2L; 3L]" |> roundtripCliScript) = "const listOfInts = [1L; 2L; 3L]"
("const listOfLists = [[1L; 2L]; [3L; 4L]]" |> roundtripCliScript) = "const listOfLists = [[1L; 2L]; [3L; 4L]]"

("const none = Stdlib.Option.Option.None" |> roundtripCliScript) = "const none = PACKAGE.Darklang.Stdlib.Option.Option.None"
("const some = Stdlib.Option.Option.Some 1L" |> roundtripCliScript) = "const some = PACKAGE.Darklang.Stdlib.Option.Option.Some(1L)"

("const a = MyEnum.A(1L, 2L)" |> roundtripCliScript) = "const a = MyEnum.A((1L, 2L))"
("const a = MyEnum.A 1L 2L" |> roundtripCliScript) = "const a = MyEnum.A(1L, 2L)"


module Expr =
Expand Down
25 changes: 20 additions & 5 deletions packages/darklang/languageTools/parser/cliScript.dark
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,35 @@ module Darklang =
match TypeDeclaration.parse decl with
| Error e -> updateUnparseableStuff result e
| Ok parsedTypeDef ->
let newType = WrittenTypes.CliScriptTypeOrFn.Type parsedTypeDef
let newType = WrittenTypes.CliScriptTypeOrFnOrConst.Type parsedTypeDef

{ result with
typesAndFns = Stdlib.List.pushBack result.typesAndFns newType }
typesAndFnsAndConsts =
Stdlib.List.pushBack result.typesAndFnsAndConsts newType }

| "fn_decl" ->
match FunctionDeclaration.parse decl with
| Error e -> updateUnparseableStuff result e

| Ok parsedFnDecl ->
let newFn = WrittenTypes.CliScriptTypeOrFn.Function parsedFnDecl
let newFn =
WrittenTypes.CliScriptTypeOrFnOrConst.Function parsedFnDecl

{ result with
typesAndFns = Stdlib.List.pushBack result.typesAndFns newFn }
typesAndFnsAndConsts =
Stdlib.List.pushBack result.typesAndFnsAndConsts newFn }

| "const_decl" ->
match ConstantDeclaration.parse decl with
| Error e -> updateUnparseableStuff result e

| Ok parsedConstDecl ->
let newConst =
WrittenTypes.CliScriptTypeOrFnOrConst.Constant parsedConstDecl

{ result with
typesAndFnsAndConsts =
Stdlib.List.pushBack result.typesAndFnsAndConsts newConst }

| "expression" ->
match Expr.parse decl with
Expand All @@ -57,7 +72,7 @@ module Darklang =
let init =
WrittenTypes.CliScript
{ range = node.range
typesAndFns = []
typesAndFnsAndConsts = []
unparseableStuff = []
exprsToEval = [] }

Expand Down
Loading