Skip to content

Commit

Permalink
Merge pull request #3 from tree-sitter/structured-bindings
Browse files Browse the repository at this point in the history
Add c++17 structured binding declarations
  • Loading branch information
Max Brunsfeld authored Apr 23, 2018
2 parents 6bce5c8 + fa4cfdf commit 03af9e0
Show file tree
Hide file tree
Showing 7 changed files with 109,098 additions and 105,742 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
language: node_js

sudo: false

node_js:
- "node"

compiler: clang-3.6

env:
- CXX=clang-3.6

addons:
apt:
sources:
- llvm-toolchain-precise-3.6
- ubuntu-toolchain-r-test
packages:
- clang-3.6

branches:
only:
- master
22 changes: 22 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
image: Visual Studio 2015

environment:
nodejs_version: "8"

platform:
- x64

install:
- ps: Install-Product node $env:nodejs_version
- node --version
- npm --version
- npm install

test_script:
- npm run test-windows

build: off

branches:
only:
- master
31 changes: 31 additions & 0 deletions corpus/declarations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,34 @@ template A<int, bool>::A(char *, size_t);
(parameter_list
(parameter_declaration (primitive_type) (abstract_pointer_declarator))
(parameter_declaration (primitive_type))))))

=====================================
Structured binding declarations
=====================================

auto [a] = B{};

int main() {
auto &&[b, c] = std::make_tuple(c);
const auto [x, y] {1, 2};
}

---

(translation_unit
(structured_binding_declaration (auto) (structured_binding_declarator (identifier)) (compound_literal_expression (type_identifier) (initializer_list)))
(function_definition
(primitive_type)
(function_declarator (identifier) (parameter_list))
(compound_statement
(structured_binding_declaration
(auto)
(reference_declarator (structured_binding_declarator (identifier) (identifier)))
(call_expression
(scoped_identifier (namespace_identifier) (identifier))
(argument_list (identifier))))
(structured_binding_declaration
(type_qualifier)
(auto)
(structured_binding_declarator (identifier) (identifier))
(initializer_list (number_literal) (number_literal))))))
23 changes: 23 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ module.exports = grammar(C, {
$.alias_declaration,
$.template_declaration,
$.template_instantiation,
$.structured_binding_declaration,
alias($.constructor_or_destructor_definition, $.function_definition)
),

_compound_statement_item: ($, original) => choice(
original,
$.structured_binding_declaration
),

// Types

_type_specifier: ($, original) => choice(
Expand Down Expand Up @@ -117,6 +123,20 @@ module.exports = grammar(C, {

// Declarations

structured_binding_declaration: $ => seq(
$._declaration_specifiers,
choice(
alias($.structured_binding_reference_declarator, $.reference_declarator),
$.structured_binding_declarator,
),
choice(
seq('=', choice($.initializer_list, $._expression)),
$.initializer_list,
$.argument_list
),
';'
),

template_declaration: $ => seq(
'template',
$.template_parameter_list,
Expand Down Expand Up @@ -259,6 +279,9 @@ module.exports = grammar(C, {
reference_field_declarator: $ => prec.right(seq(choice('&', '&&'), $._field_declarator)),
abstract_reference_declarator: $ => prec.right(seq(choice('&', '&&'), optional($._abstract_declarator))),

structured_binding_reference_declarator: $ => seq(choice('&', '&&'), $.structured_binding_declarator),
structured_binding_declarator: $ => seq('[', commaSep1($.identifier), ']'),

function_declarator: ($, original) => seq(
original,
repeat(choice(
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"scripts": {
"build": "tree-sitter generate && node-gyp build",
"test": "tree-sitter test && tree-sitter parse examples/* --quiet --time"
"test": "tree-sitter test && tree-sitter parse examples/* --quiet --time",
"test-windows": "tree-sitter test"
}
}
229 changes: 184 additions & 45 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
"type": "SYMBOL",
"name": "template_instantiation"
},
{
"type": "SYMBOL",
"name": "structured_binding_declaration"
},
{
"type": "ALIAS",
"content": {
Expand All @@ -95,54 +99,63 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_statement"
},
{
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "SYMBOL",
"name": "type_definition"
},
{
"type": "SYMBOL",
"name": "_empty_declaration"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_if_in_compound_statement"
},
"named": true,
"value": "preproc_if"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_ifdef_in_compound_statement"
},
"named": true,
"value": "preproc_ifdef"
},
{
"type": "SYMBOL",
"name": "preproc_include"
},
{
"type": "SYMBOL",
"name": "preproc_def"
},
{
"type": "SYMBOL",
"name": "preproc_function_def"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_statement"
},
{
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "SYMBOL",
"name": "type_definition"
},
{
"type": "SYMBOL",
"name": "_empty_declaration"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_if_in_compound_statement"
},
"named": true,
"value": "preproc_if"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_ifdef_in_compound_statement"
},
"named": true,
"value": "preproc_ifdef"
},
{
"type": "SYMBOL",
"name": "preproc_include"
},
{
"type": "SYMBOL",
"name": "preproc_def"
},
{
"type": "SYMBOL",
"name": "preproc_function_def"
},
{
"type": "SYMBOL",
"name": "preproc_call"
}
]
},
{
"type": "SYMBOL",
"name": "preproc_call"
"name": "structured_binding_declaration"
}
]
},
Expand Down Expand Up @@ -4689,6 +4702,72 @@
"type": "STRING",
"value": "auto"
},
"structured_binding_declaration": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_declaration_specifiers"
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "structured_binding_reference_declarator"
},
"named": true,
"value": "reference_declarator"
},
{
"type": "SYMBOL",
"name": "structured_binding_declarator"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "initializer_list"
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
]
},
{
"type": "SYMBOL",
"name": "initializer_list"
},
{
"type": "SYMBOL",
"name": "argument_list"
}
]
},
{
"type": "STRING",
"value": ";"
}
]
},
"template_declaration": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -5105,6 +5184,66 @@
]
}
},
"structured_binding_reference_declarator": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "&"
},
{
"type": "STRING",
"value": "&&"
}
]
},
{
"type": "SYMBOL",
"name": "structured_binding_declarator"
}
]
},
"structured_binding_declarator": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "["
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
}
}
]
},
{
"type": "STRING",
"value": "]"
}
]
},
"trailing_return_type": {
"type": "PREC_RIGHT",
"value": 0,
Expand Down
Loading

0 comments on commit 03af9e0

Please sign in to comment.