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

feat: allow to put type arguments in calling expressions #59

Merged
merged 5 commits into from
Jan 6, 2022
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
32 changes: 32 additions & 0 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,38 @@ func main() {
(call_expression (identifier) (argument_list
(identifier))))))))))

============================================
Generic call expressions
============================================

package main

func main() {
a[b](c)
a[b, c](d)
a[b[c], d](e[f])
}

---

(source_file
(package_clause (package_identifier))
(function_declaration (identifier) (parameter_list) (block
(call_expression
(identifier)
(type_arguments (type_identifier))
(argument_list (identifier)))
(call_expression
(identifier)
(type_arguments (type_identifier) (type_identifier))
(argument_list (identifier)))
(call_expression
(identifier)
(type_arguments
(generic_type (type_identifier) (type_arguments (type_identifier)))
(type_identifier))
(argument_list (index_expression (identifier) (identifier)))))))

============================================
Calls to 'make' and 'new'
============================================
Expand Down
9 changes: 5 additions & 4 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,16 @@ module.exports = grammar({
),

generic_type: $ => seq(
field("type", $._type_identifier),
field("type_arguments", $.type_arguments),
field('type', $._type_identifier),
field('type_arguments', $.type_arguments),
),

type_arguments: $ => seq(
type_arguments: $ => prec.dynamic(2, seq(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a comment would be useful here to explain the need for prec.dynamic, that is to prefer call_expression to index_expression, and why this works with the value 2.

Copy link
Contributor Author

@kawaemon kawaemon Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is because _type_identifier rule in _simple_type rule has its dynamic precedence -1. For example, if type_arguments rule had 1 dynamic precedence typical syntax tree like (type_arguments (_simple_type (type_identifier))) will have 0 dynamic precedence so it makes no difference between index_expression.
Can I open a pull request to write that comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those comments are useful for the next person that sees those prec.dynamic(2)
and wonder what it is.
Conflicts are hard to understand, so the more we have comments like this the better I think.

'[',
commaSep1($._type),
optional(','),
']'
),
)),

pointer_type: $ => prec(PREC.unary, seq('*', $._type)),

Expand Down Expand Up @@ -668,6 +668,7 @@ module.exports = grammar({
),
seq(
field('function', $._expression),
field('type_arguments', optional($.type_arguments)),
field('arguments', $.argument_list)
)
)),
Expand Down
114 changes: 67 additions & 47 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1180,54 +1180,58 @@
]
},
"type_arguments": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "["
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_type"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_type"
}
]
"type": "PREC_DYNAMIC",
"value": 2,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "["
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_type"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_type"
}
]
}
}
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "]"
}
]
]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "]"
}
]
}
},
"pointer_type": {
"type": "PREC",
Expand Down Expand Up @@ -3253,6 +3257,22 @@
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "type_arguments",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "type_arguments"
},
{
"type": "BLANK"
}
]
}
},
{
"type": "FIELD",
"name": "arguments",
Expand Down
10 changes: 10 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,16 @@
"named": true
}
]
},
"type_arguments": {
"multiple": false,
"required": false,
"types": [
{
"type": "type_arguments",
"named": true
}
]
}
}
},
Expand Down
Loading