1
0

Add index expressions

This commit is contained in:
Jip J. Dekker 2021-01-07 17:03:49 +11:00
parent 45def29c1c
commit ff43112b5f
6 changed files with 2559 additions and 1984 deletions

View File

@ -55,6 +55,27 @@ or = x \/ y;
(assignment_item (identifier) (binary_operation (identifier) (identifier))) (assignment_item (identifier) (binary_operation (identifier) (identifier)))
(assignment_item (identifier) (binary_operation (identifier) (identifier)))) (assignment_item (identifier) (binary_operation (identifier) (identifier))))
================
Index Expression
================
named_colection = my_collection[1];
named_accessor = my_collection[i];
literal_collection = [1,2,3][i];
multiple_accessor = my_collection[i,2];
expression_accessor = my_collection[-i + 2];
slice = my_collection[1..2];
---
(source_file
(assignment_item (identifier) (index_expression (identifier) (integer_literal)))
(assignment_item (identifier) (index_expression (identifier) (identifier)))
(assignment_item (identifier) (index_expression (array_literal (integer_literal) (integer_literal) (integer_literal)) (identifier)))
(assignment_item (identifier) (index_expression (identifier) (identifier) (integer_literal)))
(assignment_item (identifier) (index_expression (identifier) (binary_operation (unary_operation (identifier)) (integer_literal))))
(assignment_item (identifier) (index_expression (identifier) (binary_operation (integer_literal) (integer_literal)))))
============== ==============
Unary Operator Unary Operator
============== ==============

View File

@ -1,5 +1,7 @@
const PREC = { const PREC = {
annotation: 13, call: 15,
annotation: 14,
unary: 13,
exponent: 12, exponent: 12,
multiplicative: 11, multiplicative: 11,
additive: 10, additive: 10,
@ -40,8 +42,10 @@ module.exports = grammar({
_expression: $ => choice( _expression: $ => choice(
$.identifier, $.identifier,
$._literal, $._literal,
$.unary_operation,
$.binary_operation, $.binary_operation,
$.index_expression,
$.unary_operation,
// TODO: Other expression types // TODO: Other expression types
), ),
@ -73,10 +77,17 @@ module.exports = grammar({
)))); ))));
}, },
unary_operation: $ => seq( index_expression: $ => prec(PREC.call, seq(
field('collection', $._expression),
'[',
field('indices', seq($._expression, repeat(seq(',', $._expression)))),
']',
)),
unary_operation: $ => prec(PREC.unary, seq(
field('operator', choice('-', 'not', '¬')), field('operator', choice('-', 'not', '¬')),
$._expression $._expression
), )),
_literal: $ => choice( _literal: $ => choice(
$.absent, $.absent,

8
src/binding.cc vendored
View File

@ -4,7 +4,7 @@
using namespace v8; using namespace v8;
extern "C" TSLanguage * tree_sitter_MiniZinc(); extern "C" TSLanguage * tree_sitter_minizinc();
namespace { namespace {
@ -17,12 +17,12 @@ void Init(Local<Object> exports, Local<Object> module) {
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked(); Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked(); Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_MiniZinc()); Nan::SetInternalFieldPointer(instance, 0, tree_sitter_minizinc());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("MiniZinc").ToLocalChecked()); Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("minizinc").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance); Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
} }
NODE_MODULE(tree_sitter_MiniZinc_binding, Init) NODE_MODULE(tree_sitter_minizinc_binding, Init)
} // namespace } // namespace

122
src/grammar.json vendored
View File

@ -99,14 +99,72 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "unary_operation" "name": "binary_operation"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "binary_operation" "name": "index_expression"
},
{
"type": "SYMBOL",
"name": "unary_operation"
} }
] ]
}, },
"index_expression": {
"type": "PREC",
"value": 15,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "collection",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": "["
},
{
"type": "FIELD",
"name": "indices",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
}
]
}
},
{
"type": "STRING",
"value": "]"
}
]
}
},
"binary_operation": { "binary_operation": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -654,7 +712,7 @@
}, },
{ {
"type": "PREC_LEFT", "type": "PREC_LEFT",
"value": 13, "value": 14,
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -688,34 +746,38 @@
] ]
}, },
"unary_operation": { "unary_operation": {
"type": "SEQ", "type": "PREC",
"members": [ "value": 13,
{ "content": {
"type": "FIELD", "type": "SEQ",
"name": "operator", "members": [
"content": { {
"type": "CHOICE", "type": "FIELD",
"members": [ "name": "operator",
{ "content": {
"type": "STRING", "type": "CHOICE",
"value": "-" "members": [
}, {
{ "type": "STRING",
"type": "STRING", "value": "-"
"value": "not" },
}, {
{ "type": "STRING",
"type": "STRING", "value": "not"
"value": "¬" },
} {
] "type": "STRING",
"value": "¬"
}
]
}
},
{
"type": "SYMBOL",
"name": "_expression"
} }
}, ]
{ }
"type": "SYMBOL",
"name": "_expression"
}
]
}, },
"_literal": { "_literal": {
"type": "CHOICE", "type": "CHOICE",

134
src/node-types.json vendored
View File

@ -31,6 +31,10 @@
"type": "identifier", "type": "identifier",
"named": true "named": true
}, },
{
"type": "index_expression",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -82,6 +86,10 @@
"type": "identifier", "type": "identifier",
"named": true "named": true
}, },
{
"type": "index_expression",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -144,6 +152,10 @@
"type": "identifier", "type": "identifier",
"named": true "named": true
}, },
{
"type": "index_expression",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -316,6 +328,10 @@
"type": "identifier", "type": "identifier",
"named": true "named": true
}, },
{
"type": "index_expression",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -341,6 +357,116 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "index_expression",
"named": true,
"fields": {
"collection": {
"multiple": false,
"required": true,
"types": [
{
"type": "absent",
"named": true
},
{
"type": "array_literal",
"named": true
},
{
"type": "binary_operation",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "float_literal",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "index_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "set_literal",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_operation",
"named": true
}
]
},
"indices": {
"multiple": true,
"required": true,
"types": [
{
"type": ",",
"named": false
},
{
"type": "absent",
"named": true
},
{
"type": "array_literal",
"named": true
},
{
"type": "binary_operation",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "float_literal",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "index_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "set_literal",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_operation",
"named": true
}
]
}
}
},
{ {
"type": "set_literal", "type": "set_literal",
"named": true, "named": true,
@ -373,6 +499,10 @@
"type": "identifier", "type": "identifier",
"named": true "named": true
}, },
{
"type": "index_expression",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true
@ -473,6 +603,10 @@
"type": "identifier", "type": "identifier",
"named": true "named": true
}, },
{
"type": "index_expression",
"named": true
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true "named": true

4239
src/parser.c vendored

File diff suppressed because it is too large Load Diff