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 the remaining signed and unsigned Int types #5315

Merged
merged 5 commits into from
Feb 28, 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
22 changes: 22 additions & 0 deletions backend/testfiles/execution/stdlib/parser.dark
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,16 @@ module TextToTextRoundtripping =
// all built-ins
("type MyUnit = Unit" |> roundtripCliScript) = "type MyUnit =\n Unit"
("type MyBool = Bool" |> roundtripCliScript) = "type MyBool =\n Bool"
("type MyInt8 = Int8" |> roundtripCliScript) = "type MyInt8 =\n Int8"
("type MyUInt8 = UInt8" |> roundtripCliScript) = "type MyUInt8 =\n UInt8"
("type MyInt16 = Int16" |> roundtripCliScript) = "type MyInt16 =\n Int16"
("type MyUInt16 = UInt16" |> roundtripCliScript) = "type MyUInt16 =\n UInt16"
("type MyInt32 = Int32" |> roundtripCliScript) = "type MyInt32 =\n Int32"
("type MyUInt32 = UInt32" |> roundtripCliScript) = "type MyUInt32 =\n UInt32"
("type MyInt64 = Int64" |> roundtripCliScript) = "type MyInt64 =\n Int64"
("type MyUInt64 = UInt64" |> roundtripCliScript) = "type MyUInt64 =\n UInt64"
("type MyInt128 = Int128" |> roundtripCliScript) = "type MyInt128 =\n Int128"
("type MyUInt128 = UInt128" |> roundtripCliScript) = "type MyUInt128 =\n UInt128"
("type MyFloat = Float" |> roundtripCliScript) = "type MyFloat =\n Float"
("type MyChar = Char" |> roundtripCliScript) = "type MyChar =\n Char"
("type MyString = String" |> roundtripCliScript) = "type MyString =\n String"
Expand Down Expand Up @@ -227,8 +236,21 @@ module TextToTextRoundtripping =
("(true)" |> roundtripCliScript) = "true"

// int literals
("1y" |> roundtripCliScript) = "1y"
("-1y" |> roundtripCliScript) = "-1y"
("1uy" |> roundtripCliScript) = "1uy"
("1s" |> roundtripCliScript) = "1s"
("1us" |> roundtripCliScript) = "1us"
("1l" |> roundtripCliScript) = "1l"
("-1l" |> roundtripCliScript) = "-1l"
("1ul" |> roundtripCliScript) = "1ul"
("0L" |> roundtripCliScript) = "0L"
("1900L" |> roundtripCliScript) = "1900L"
("-1900L" |> roundtripCliScript) = "-1900L"
("1UL" |> roundtripCliScript) = "1UL"
("1Q" |> roundtripCliScript) = "1Q"
("-1Q" |> roundtripCliScript) = "-1Q"
("1Q" |> roundtripCliScript) = "1Q"

// string literals
("\"\"" |> roundtripCliScript) = "\"\""
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/Tests/TreeSitter.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let toStringTest =

Expect.equal
(tree.Root.ToString())
"(source_file (fn_decl keyword_let: (keyword) name: (fn_identifier) params: (fn_decl_params (fn_decl_param symbol_left_paren: (symbol) identifier: (variable_identifier) symbol_colon: (symbol) typ: (type_reference (builtin_type)) symbol_right_paren: (symbol))) symbol_colon: (symbol) return_type: (type_reference (builtin_type)) symbol_equals: (symbol) body: (expression (infix_operation left: (expression (variable_identifier)) operator: (operator) right: (expression (int64_literal digits: (digits) suffix: (symbol)))))))"
"(source_file (fn_decl keyword_let: (keyword) name: (fn_identifier) params: (fn_decl_params (fn_decl_param symbol_left_paren: (symbol) identifier: (variable_identifier) symbol_colon: (symbol) typ: (type_reference (builtin_type)) symbol_right_paren: (symbol))) symbol_colon: (symbol) return_type: (type_reference (builtin_type)) symbol_equals: (symbol) body: (expression (infix_operation left: (expression (variable_identifier)) operator: (operator) right: (expression (int64_literal digits: (digits (positive_digits)) suffix: (symbol)))))))"
""

let tests = testList "TreeSitter" [ testMultibyteCharacters; toStringTest ]
171 changes: 154 additions & 17 deletions packages/darklang/languageTools/parser.dark
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,46 @@ module Darklang =
(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
Expand Down Expand Up @@ -334,38 +370,130 @@ module Darklang =
|> Stdlib.Result.Result.Error


let parseInt64Literal
let parseIntLiteral
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "int64_literal" then
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 int64_literal"
|> Stdlib.Option.toResult "No digits node found in int_literal"

let suffixPart =
(findNodeByFieldName node "suffix")
|> Stdlib.Option.toResult "No suffix node found in int64_literal"
|> Stdlib.Option.toResult "No suffix node found in int_literal"

match intPart, suffixPart with
| Ok intPart, Ok suffixPart ->
let intText = getText intPart

match Stdlib.Int64.parse intText with
| Ok i ->
(WrittenTypes.Expr.EInt64(
node.sourceRange,
(intPart.sourceRange, i),
suffixPart.sourceRange
))
|> Stdlib.Result.Result.Ok
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

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

else
(WrittenTypes.Unparseable { source = node })
|> Stdlib.Result.Result.Error
Expand Down Expand Up @@ -585,7 +713,16 @@ module Darklang =
| "unit" ->
(WrittenTypes.Expr.EUnit node.sourceRange) |> Stdlib.Result.Result.Ok
| "bool_literal" -> parseBoolLiteral node
| "int64_literal" -> parseInt64Literal node
| "int8_literal" -> parseIntLiteral node
| "uint8_literal" -> parseIntLiteral node
| "int16_literal" -> parseIntLiteral node
| "uint16_literal" -> parseIntLiteral node
| "int32_literal" -> parseIntLiteral node
| "uint32_literal" -> parseIntLiteral node
| "int64_literal" -> parseIntLiteral node
| "uint64_literal" -> parseIntLiteral node
| "int128_literal" -> parseIntLiteral node
| "uint128_literal" -> parseIntLiteral node
| "string_literal" -> parseStringLiteral node

// assigning and accessing variables
Expand Down
54 changes: 54 additions & 0 deletions packages/darklang/languageTools/semanticTokens.dark
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,16 @@ module Darklang =
match t with
| TUnit r -> [ makeToken r TokenType.TypeName ]
| TBool r -> [ makeToken r TokenType.TypeName ]
| TInt8 r -> [ makeToken r TokenType.TypeName ]
| TUInt8 r -> [ makeToken r TokenType.TypeName ]
| TInt16 r -> [ makeToken r TokenType.TypeName ]
| TUInt16 r -> [ makeToken r TokenType.TypeName ]
| TInt32 r -> [ makeToken r TokenType.TypeName ]
| TUInt32 r -> [ makeToken r TokenType.TypeName ]
| TInt64 r -> [ makeToken r TokenType.TypeName ]
| TUInt64 r -> [ makeToken r TokenType.TypeName ]
| TInt128 r -> [ makeToken r TokenType.TypeName ]
| TUInt128 r -> [ makeToken r TokenType.TypeName ]
| TFloat r -> [ makeToken r TokenType.TypeName ]
| TChar r -> [ makeToken r TokenType.TypeName ]
| TString r -> [ makeToken r TokenType.TypeName ]
Expand Down Expand Up @@ -156,11 +165,56 @@ module Darklang =
// true
| EBool(range, _b) -> [ makeToken range TokenType.Keyword ]

// 12y
| EInt8(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12uy
| EUInt8(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12s
| EInt16(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12us
| EUInt16(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12l
| EInt32(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12ul
| EUInt32(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12L
| EInt64(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12UL
| EUInt64(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12Q
| EInt128(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// 12Z
| EUInt128(_, (intPartRange, _), suffixPartRange) ->
[ makeToken intPartRange TokenType.Number
makeToken suffixPartRange TokenType.Symbol ]

// "hello"
| EString(range, _contentsMaybe, symbolOpenQuote, symbolCloseQuote) ->
[ // "
Expand Down
Loading