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 (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
=============================

View File

@ -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

45
src/grammar.json vendored
View File

@ -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,

44
src/node-types.json vendored
View File

@ -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

161886
src/parser.c vendored

File diff suppressed because it is too large Load Diff