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 field access #5365

Merged
merged 4 commits into from
May 6, 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
7 changes: 7 additions & 0 deletions backend/testfiles/execution/stdlib/parser.dark
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ module TextToTextRoundtripping =
// TODO: this is ugly
("let x = 1L\n x" |> roundtripCliScript) = "let x =\n 1L\nx"

// field access
("person.name" |> roundtripCliScript) = "person.name"
("(Person { name =\"Janice\" }).name" |> roundtripCliScript) = "(Person { name = \"Janice\" }).name"
("record.someField.anotherFieldInsideThat" |> roundtripCliScript) = "record.someField.anotherFieldInsideThat"
("person.age + 1L" |> roundtripCliScript) = "(person.age) + (1L)"


// if expressions
("if true then 1L" |> roundtripCliScript) = "if true then\n 1L"
("if true then 1L else 2L" |> roundtripCliScript) = "if true then\n 1L\nelse\n 2L"
Expand Down
24 changes: 24 additions & 0 deletions packages/darklang/languageTools/parser/expr.dark
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,28 @@ module Darklang =
else
createUnparseableError node

let parseFieldAccess
(node: ParsedNode)
: Stdlib.Result.Result<WrittenTypes.Expr, WrittenTypes.Unparseable> =
if node.typ == "field_access" then
let expr = findAndParseRequired node "expr" Expr.parse
let symbolDotNode = findField node "symbol_dot"
let fieldName = findField node "field_name"

match expr, symbolDotNode, fieldName with
| Ok expr, Ok symbolDot, Ok field ->
(WrittenTypes.Expr.EFieldAccess(
node.range,
expr,
(field.range, field.text),
symbolDot.range
))
|> Stdlib.Result.Result.Ok

| _ -> createUnparseableError node

else
createUnparseableError node

let parseInfixOperation
(node: ParsedNode)
Expand Down Expand Up @@ -693,6 +715,8 @@ module Darklang =
(WrittenTypes.Expr.EVariable(node.range, getText node))
|> Stdlib.Result.Result.Ok

| "field_access" -> parseFieldAccess node

| "if_expression" -> parseIfExpression node

// fn calls
Expand Down
10 changes: 10 additions & 0 deletions packages/darklang/languageTools/semanticTokens.dark
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ module Darklang =
// x
| EVariable(range, _varName) -> [ makeToken range TokenType.VariableName ]

// person.name
| EFieldAccess(range, expr, (r, fieldName), symbolDot) ->
[ // person
Expr.tokenize expr
// .
[ makeToken symbolDot TokenType.Symbol ]
// name
[ makeToken r TokenType.Property ] ]
|> Stdlib.List.flatten

// if true then 1 else 2
| EIf(range, cond, thenExpr, elseExpr, keywordIf, keywordThen, keywordElse) ->
let elseKeyword =
Expand Down
6 changes: 6 additions & 0 deletions packages/darklang/languageTools/writtenTypes.dark
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ module Darklang =

| EVariable of Range * String

| EFieldAccess of
Range *
Expr *
fieldName: (Range * String) *
symbolDot: Range

| EIf of
Range *
cond: Expr *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ module Darklang =

| EVariable(_, var) -> ProgramTypes.Expr.EVariable (gid ()) var

| EFieldAccess(_, expr, (_, fieldName), _) ->
ProgramTypes.Expr.EFieldAccess(gid (), toPT onMissing expr, fieldName)

| EIf(_, cond, thenExpr, elseExpr, _, _, _) ->
let elseExpr =
elseExpr |> Stdlib.Option.map (fun es -> Expr.toPT onMissing es)
Expand Down
6 changes: 4 additions & 2 deletions packages/darklang/prettyPrinter/programTypes.dark
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,10 @@ module Darklang =
let exprPart = PrettyPrinter.ProgramTypes.expr expr

// TODO: only sometimes need to wrap exprPart in parens

$"({exprPart}).{fieldName}"
match expr with
| EVariable(_, _) -> $"{exprPart}.{fieldName}"
| EFieldAccess(_, _, _) -> $"{exprPart}.{fieldName}"
| _ -> $"({exprPart}).{fieldName}"

| EVariable(_id, name) -> name

Expand Down
15 changes: 15 additions & 0 deletions tree-sitter-darklang/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const PREC = {
SUM: 3,
PRODUCT: 4,
EXPONENT: 5,
FIELDACCESS: 7,
};

const logicalOperators = choice("&&", "||");
Expand Down Expand Up @@ -184,6 +185,8 @@ module.exports = grammar({

$.infix_operation,
$.function_call,

$.field_access,
),
paren_expression: $ =>
seq(
Expand Down Expand Up @@ -530,6 +533,18 @@ module.exports = grammar({
),
),

// field access
// e.g. `person.name`
field_access: $ =>
prec(
PREC.FIELDACCESS,
seq(
field("expr", $.expression),
field("symbol_dot", alias(".", $.symbol)),
field("field_name", $.variable_identifier),
),
),

//
// Common
type_reference: $ => choice($.builtin_type, $.qualified_type_name),
Expand Down
42 changes: 42 additions & 0 deletions tree-sitter-darklang/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@
{
"type": "SYMBOL",
"name": "function_call"
},
{
"type": "SYMBOL",
"name": "field_access"
}
]
},
Expand Down Expand Up @@ -2299,6 +2303,44 @@
]
}
},
"field_access": {
"type": "PREC",
"value": 7,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "expr",
"content": {
"type": "SYMBOL",
"name": "expression"
}
},
{
"type": "FIELD",
"name": "symbol_dot",
"content": {
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "."
},
"named": true,
"value": "symbol"
}
},
{
"type": "FIELD",
"name": "field_name",
"content": {
"type": "SYMBOL",
"name": "variable_identifier"
}
}
]
}
},
"type_reference": {
"type": "CHOICE",
"members": [
Expand Down
40 changes: 40 additions & 0 deletions tree-sitter-darklang/src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@
"type": "enum_literal",
"named": true
},
{
"type": "field_access",
"named": true
},
{
"type": "float_literal",
"named": true
Expand Down Expand Up @@ -458,6 +462,42 @@
]
}
},
{
"type": "field_access",
"named": true,
"fields": {
"expr": {
"multiple": false,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
},
"field_name": {
"multiple": false,
"required": true,
"types": [
{
"type": "variable_identifier",
"named": true
}
]
},
"symbol_dot": {
"multiple": false,
"required": true,
"types": [
{
"type": "symbol",
"named": true
}
]
}
}
},
{
"type": "fn_decl",
"named": true,
Expand Down
79 changes: 79 additions & 0 deletions tree-sitter-darklang/test/corpus/exhaustive/exprs/field_access.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
==================
field access
==================

person.name

---

(source_file
(expression
(field_access
(expression (variable_identifier))
(symbol)
(variable_identifier)
)
)
)

==================
field access - paren defined Record
==================

(Person { name = "Janice" }).name

---

(source_file
(expression
(field_access
(expression
(paren_expression
(symbol)
(expression
(record_literal
(qualified_type_name (type_identifier))
(symbol)
(record_content
(record_pair
(variable_identifier)
(symbol)
(expression (string_literal (symbol) (string_content) (symbol)))
)
)
(symbol)
)
)
(symbol)
)
)
(symbol)
(variable_identifier)
)
)
)


==================
field access - nested
==================

person.address.street

---

(source_file
(expression
(field_access
(expression
(field_access
(expression (variable_identifier))
(symbol)
(variable_identifier)
)
)
(symbol)
(variable_identifier)
)
)
)