Add support for ternary expression (#81)

Fixes https://github.com/tree-sitter/tree-sitter-bash/issues/64
This commit is contained in:
Kenneth Skovhus 2020-05-15 19:50:29 +02:00 committed by GitHub
parent 28e76c616d
commit 0477cc4460
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81593 additions and 80413 deletions

View File

@ -268,6 +268,26 @@ fi
(command_substitution (command (command_name (word)) (raw_string))))) (command_substitution (command (command_name (word)) (raw_string)))))
(command (command_name (word)) (word)))) (command (command_name (word)) (word))))
=============================
Test commands with ternary
=============================
if (( 1 < 2 ? 1 : 2 )); then
return 1
fi
---
(program
(if_statement
(test_command
(ternary_expression
(binary_expression (word) (word))
(word) (word)))
(command
(command_name (word)) (word))))
============================= =============================
Test commands with regexes Test commands with regexes
============================= =============================

View File

@ -353,6 +353,7 @@ module.exports = grammar({
_expression: $ => choice( _expression: $ => choice(
$._literal, $._literal,
$.unary_expression, $.unary_expression,
$.ternary_expression,
$.binary_expression, $.binary_expression,
$.postfix_expression, $.postfix_expression,
$.parenthesized_expression $.parenthesized_expression
@ -377,6 +378,16 @@ module.exports = grammar({
) )
)), )),
ternary_expression: $ => prec.left(
seq(
field('condition', $._expression),
'?',
field('consequence', $._expression),
':',
field('alternative', $._expression),
)
),
unary_expression: $ => prec.right(seq( unary_expression: $ => prec.right(seq(
choice('!', $.test_operator), choice('!', $.test_operator),
$._expression $._expression

45
src/grammar.json vendored
View File

@ -1481,6 +1481,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "unary_expression" "name": "unary_expression"
}, },
{
"type": "SYMBOL",
"name": "ternary_expression"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "binary_expression" "name": "binary_expression"
@ -1632,6 +1636,47 @@
] ]
} }
}, },
"ternary_expression": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "condition",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": "?"
},
{
"type": "FIELD",
"name": "consequence",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
"unary_expression": { "unary_expression": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
"value": 0, "value": 0,

44
src/node-types.json vendored
View File

@ -23,6 +23,10 @@
"type": "postfix_expression", "type": "postfix_expression",
"named": true "named": true
}, },
{
"type": "ternary_expression",
"named": true
},
{ {
"type": "unary_expression", "type": "unary_expression",
"named": true "named": true
@ -1101,6 +1105,42 @@
] ]
} }
}, },
{
"type": "ternary_expression",
"named": true,
"fields": {
"alternative": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
},
"condition": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
},
"consequence": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
}
}
},
{ {
"type": "test_command", "type": "test_command",
"named": true, "named": true,
@ -1423,6 +1463,10 @@
"type": ">|", "type": ">|",
"named": false "named": false
}, },
{
"type": "?",
"named": false
},
{ {
"type": "[", "type": "[",
"named": false "named": false

161886
src/parser.c vendored

File diff suppressed because it is too large Load Diff