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: add optional expr #13

Merged
merged 1 commit into from
May 22, 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
15 changes: 15 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ module.exports = grammar({
$.lambda_expr,
$.schema_expr,
$.paren_expression,
$.optional_attribute,
$.optional_item,
),

paren_expression: $ => seq(
Expand Down Expand Up @@ -529,6 +531,19 @@ module.exports = grammar({
field('attribute', $.identifier),
)),

optional_attribute: $ => prec(PREC.call, seq(
field('object', $.primary_expression),
'?.',
field('attribute', $.identifier),
)),

optional_item: $ => prec(PREC.call, seq(
field('object', $.primary_expression),
'?[',
field('index', $.expression),
']',
)),

subscript: $ => prec(PREC.call, seq(
field('value', $.primary_expression),
'[',
Expand Down
70 changes: 70 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,14 @@
{
"type": "SYMBOL",
"name": "paren_expression"
},
{
"type": "SYMBOL",
"name": "optional_attribute"
},
{
"type": "SYMBOL",
"name": "optional_item"
}
]
},
Expand Down Expand Up @@ -1863,6 +1871,68 @@
]
}
},
"optional_attribute": {
"type": "PREC",
"value": 22,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "object",
"content": {
"type": "SYMBOL",
"name": "primary_expression"
}
},
{
"type": "STRING",
"value": "?."
},
{
"type": "FIELD",
"name": "attribute",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
]
}
},
"optional_item": {
"type": "PREC",
"value": 22,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "object",
"content": {
"type": "SYMBOL",
"name": "primary_expression"
}
},
{
"type": "STRING",
"value": "?["
},
{
"type": "FIELD",
"name": "index",
"content": {
"type": "SYMBOL",
"name": "expression"
}
},
{
"type": "STRING",
"value": "]"
}
]
}
},
"subscript": {
"type": "PREC",
"value": 22,
Expand Down
62 changes: 61 additions & 1 deletion src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,15 @@
{
"type": "undefined",
"named": true
}
},
{
"type": "optional_attribute",
"named": true
},
{
"type": "optional_item",
"named": true
}
]
},
{
Expand Down Expand Up @@ -341,6 +349,58 @@
}
}
},
{
"type": "optional_attribute",
"named": true,
"fields": {
"object": {
"multiple": false,
"required": true,
"types": [
{
"type": "primary_expression",
"named": true
}
]
},
"attribute": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "optional_item",
"named": true,
"fields": {
"object": {
"multiple": false,
"required": true,
"types": [
{
"type": "primary_expression",
"named": true
}
]
},
"index": {
"multiple": false,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
}
},
{
"type": "augmented_assignment",
"named": true,
Expand Down
115 changes: 114 additions & 1 deletion test/corpus/expr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,117 @@ d = filter _, v in {a = "foo", b = "bar"} {v == "foo"}
(string
(string_start)
(string_content)
(string_end))))))
(string_end))))))

================================================================================
attribute expression
================================================================================

schema Person:
name: str

person = Person {name: "Alice"}
name1 = person.name

--------------------------------------------------------------------------------

(module
(schema_statement
(identifier)
(ERROR
(dotted_name
(identifier))
(type
(basic_type)))
(block))
(assignment
(dotted_name
(identifier))
(schema_expr
(dotted_name
(identifier))
(dictionary
(pair
(identifier)
(string
(string_start)
(string_content)
(string_end))))))
(ERROR
(dotted_name
(identifier))
(dotted_name
(identifier)
(identifier))))

================================================================================
optional attribute expression
================================================================================

schema Person:
name: str

person = Person {name: "Alice"}
name2 = person?.name

--------------------------------------------------------------------------------

(module
(schema_statement
(identifier)
(ERROR
(dotted_name
(identifier))
(type
(basic_type)))
(block))
(assignment
(dotted_name
(identifier))
(schema_expr
(dotted_name
(identifier))
(dictionary
(pair
(identifier)
(string
(string_start)
(string_content)
(string_end))))))
(ERROR
(dotted_name
(identifier))
(dotted_name
(identifier)
(ERROR
(UNEXPECTED '?'))
(identifier))))

================================================================================
optional item expression
================================================================================

datalist = [[1], 2, 3]
item = datalist?[0]?

--------------------------------------------------------------------------------

(module
(assignment
(dotted_name
(identifier))
(list
(list
(integer))
(integer)
(integer)))
(ERROR
(assignment
(dotted_name
(identifier))
(subscript
(identifier)
(ERROR
(UNEXPECTED '?'))
(integer)))
(UNEXPECTED '?')))
Loading