Add support for ternary expression (#81)
Fixes https://github.com/tree-sitter/tree-sitter-bash/issues/64
This commit is contained in:
parent
28e76c616d
commit
0477cc4460
|
@ -268,6 +268,26 @@ fi
|
|||
(command_substitution (command (command_name (word)) (raw_string)))))
|
||||
(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
|
||||
=============================
|
||||
|
|
11
grammar.js
11
grammar.js
|
@ -353,6 +353,7 @@ module.exports = grammar({
|
|||
_expression: $ => choice(
|
||||
$._literal,
|
||||
$.unary_expression,
|
||||
$.ternary_expression,
|
||||
$.binary_expression,
|
||||
$.postfix_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(
|
||||
choice('!', $.test_operator),
|
||||
$._expression
|
||||
|
|
|
@ -1481,6 +1481,10 @@
|
|||
"type": "SYMBOL",
|
||||
"name": "unary_expression"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "ternary_expression"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"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": {
|
||||
"type": "PREC_RIGHT",
|
||||
"value": 0,
|
||||
|
|
|
@ -23,6 +23,10 @@
|
|||
"type": "postfix_expression",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "ternary_expression",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "unary_expression",
|
||||
"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",
|
||||
"named": true,
|
||||
|
@ -1423,6 +1463,10 @@
|
|||
"type": ">|",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "?",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "[",
|
||||
"named": false
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue