Fix errors with operators overload in c++

This commit is contained in:
Calixte Denizet 2019-12-19 10:51:09 +01:00
Родитель 56c0f678ea
Коммит e80db56c40
9 изменённых файлов: 319172 добавлений и 294123 удалений

Просмотреть файл

@ -49,9 +49,9 @@ fn dump_span(
stdout: &mut StandardStreamLock,
last: bool,
) -> std::io::Result<()> {
/*if !span.error {
if !span.error {
return Ok(());
}*/
}
let pref = if last { " `- " } else { " |- " };

Просмотреть файл

@ -245,6 +245,10 @@ impl Getter for CppCode {
let typ = node.kind_id();
match typ.into() {
Cpp::FunctionDefinition | Cpp::FunctionDefinition2 | Cpp::FunctionDefinition3 => {
if let Some(op_cast) = node.first_child(|id| Cpp::OperatorCast == id) {
let code = &code[op_cast.start_byte()..op_cast.end_byte()];
return std::str::from_utf8(code).ok();
}
// we're in a function_definition so need to get the declarator
if let Some(declarator) = node.child_by_field_name("declarator") {
if let Some(fd) = declarator.first_occurence(|id| {

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -53,4 +53,14 @@ impl<'a> Search<'a> for Node<'a> {
}
}
}
fn first_child(&self, pred: fn(u16) -> bool) -> Option<Node<'a>> {
let mut cursor = self.walk();
for child in self.children(&mut cursor) {
if pred(child.kind_id()) {
return Some(child);
}
}
None
}
}

Просмотреть файл

@ -44,4 +44,5 @@ pub trait Callback {
pub trait Search<'a> {
fn first_occurence(&self, pred: fn(u16) -> bool) -> Option<Node<'a>>;
fn act_on_node(&self, pred: &mut dyn FnMut(&Node<'a>));
fn first_child(&self, pred: fn(u16) -> bool) -> Option<Node<'a>>;
}

Просмотреть файл

@ -5,6 +5,90 @@ module.exports = grammar(CPP, {
rules: {
_top_level_item: ($, original) => choice(
$.alone_macro,
$.alone_macro_call,
alias($.operator_cast_definition, $.function_definition),
alias($.operator_cast_declaration, $._declaration),
original,
),
operator_name: $ => token(seq(
'operator',
/\s*/,
choice(
'+', '-', '*', '/', '%',
'^', '&', '|', '~',
'!', '=', '<', '>',
'+=', '-=', '*=', '/=', '%=', '^=', '&=', '|=',
'<<', '>>', '>>=', '<<=',
'==', '!=', '<=', '>=',
'&&', '||',
'++', '--',
',',
'->*',
'->',
'()', '[]',
'new', 'delete',
'new[]', 'delete[]',
)
)),
_field_declaration_list_item: ($, original) => choice(
original,
alias($.operator_cast_definition, $.function_definition),
alias($.operator_cast_declaration, $.declaration),
$.alone_macro,
$.alone_macro_call,
),
operator_cast: $ => prec(1, seq(
optional(seq(
field('namespace', optional(choice(
$._namespace_identifier,
$.template_type,
$.scoped_namespace_identifier
))),
'::',
)),
'operator',
$._declaration_specifiers,
field('declarator', $._abstract_declarator),
)),
operator_cast_definition: $ => seq(
repeat(choice(
$.storage_class_specifier,
$.type_qualifier,
$.attribute_specifier
)),
prec(1, seq(
optional(choice($.virtual_function_specifier, $.explicit_function_specifier)),
field('declarator', $.operator_cast),
)),
choice(
field('body', $.compound_statement),
$.default_method_clause,
$.delete_method_clause
)
),
operator_cast_declaration: $ => seq(
optional(choice($.virtual_function_specifier, $.explicit_function_specifier)),
field('declarator', $.operator_cast),
optional(seq('=', field('default_value', $._expression))),
';'
),
alone_macro: $ => /[_A-Z][_A-Z0-9]+\s*\n/,
alone_macro_call: $ => seq(
/[_A-Z][_A-Z0-9]+/,
'(',
optional(seq(/[_A-Z][_A-Z0-9]+/, repeat(seq(',', /[_A-Z][_A-Z0-9]+/)))),
')',
'\n',
),
class_specifier: $ => prec.right(seq(
'class',
optional($.macro_annotation),

Просмотреть файл

@ -13,90 +13,121 @@
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "function_definition"
},
{
"type": "SYMBOL",
"name": "linkage_specification"
},
{
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "SYMBOL",
"name": "_statement"
},
{
"type": "SYMBOL",
"name": "type_definition"
},
{
"type": "SYMBOL",
"name": "_empty_declaration"
},
{
"type": "SYMBOL",
"name": "preproc_if"
},
{
"type": "SYMBOL",
"name": "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": "alone_macro"
},
{
"type": "SYMBOL",
"name": "namespace_definition"
},
{
"type": "SYMBOL",
"name": "using_declaration"
},
{
"type": "SYMBOL",
"name": "alias_declaration"
},
{
"type": "SYMBOL",
"name": "static_assert_declaration"
},
{
"type": "SYMBOL",
"name": "template_declaration"
},
{
"type": "SYMBOL",
"name": "template_instantiation"
"name": "alone_macro_call"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "constructor_or_destructor_definition"
"name": "operator_cast_definition"
},
"named": true,
"value": "function_definition"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "operator_cast_declaration"
},
"named": true,
"value": "_declaration"
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "function_definition"
},
{
"type": "SYMBOL",
"name": "linkage_specification"
},
{
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "SYMBOL",
"name": "_statement"
},
{
"type": "SYMBOL",
"name": "type_definition"
},
{
"type": "SYMBOL",
"name": "_empty_declaration"
},
{
"type": "SYMBOL",
"name": "preproc_if"
},
{
"type": "SYMBOL",
"name": "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": "namespace_definition"
},
{
"type": "SYMBOL",
"name": "using_declaration"
},
{
"type": "SYMBOL",
"name": "alias_declaration"
},
{
"type": "SYMBOL",
"name": "static_assert_declaration"
},
{
"type": "SYMBOL",
"name": "template_declaration"
},
{
"type": "SYMBOL",
"name": "template_instantiation"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "constructor_or_destructor_definition"
},
"named": true,
"value": "function_definition"
}
]
}
]
},
@ -4332,50 +4363,106 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "field_declaration"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "field_declaration"
},
{
"type": "SYMBOL",
"name": "preproc_def"
},
{
"type": "SYMBOL",
"name": "preproc_function_def"
},
{
"type": "SYMBOL",
"name": "preproc_call"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_if_in_field_declaration_list"
},
"named": true,
"value": "preproc_if"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_ifdef_in_field_declaration_list"
},
"named": true,
"value": "preproc_ifdef"
}
]
},
{
"type": "SYMBOL",
"name": "preproc_def"
},
{
"type": "SYMBOL",
"name": "preproc_function_def"
},
{
"type": "SYMBOL",
"name": "preproc_call"
"name": "template_declaration"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_if_in_field_declaration_list"
"name": "inline_method_definition"
},
"named": true,
"value": "preproc_if"
"value": "function_definition"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_ifdef_in_field_declaration_list"
"name": "constructor_or_destructor_definition"
},
"named": true,
"value": "preproc_ifdef"
"value": "function_definition"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "constructor_or_destructor_declaration"
},
"named": true,
"value": "declaration"
},
{
"type": "SYMBOL",
"name": "friend_declaration"
},
{
"type": "SYMBOL",
"name": "access_specifier"
},
{
"type": "SYMBOL",
"name": "alias_declaration"
},
{
"type": "SYMBOL",
"name": "using_declaration"
},
{
"type": "SYMBOL",
"name": "type_definition"
},
{
"type": "SYMBOL",
"name": "static_assert_declaration"
}
]
},
{
"type": "SYMBOL",
"name": "template_declaration"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "inline_method_definition"
"name": "operator_cast_definition"
},
"named": true,
"value": "function_definition"
@ -4384,43 +4471,18 @@
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "constructor_or_destructor_definition"
},
"named": true,
"value": "function_definition"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "constructor_or_destructor_declaration"
"name": "operator_cast_declaration"
},
"named": true,
"value": "declaration"
},
{
"type": "SYMBOL",
"name": "friend_declaration"
"name": "alone_macro"
},
{
"type": "SYMBOL",
"name": "access_specifier"
},
{
"type": "SYMBOL",
"name": "alias_declaration"
},
{
"type": "SYMBOL",
"name": "using_declaration"
},
{
"type": "SYMBOL",
"name": "type_definition"
},
{
"type": "SYMBOL",
"name": "static_assert_declaration"
"name": "alone_macro_call"
}
]
},
@ -10478,6 +10540,10 @@
"type": "STRING",
"value": "operator"
},
{
"type": "PATTERN",
"value": "\\s*"
},
{
"type": "CHOICE",
"members": [
@ -10632,6 +10698,22 @@
{
"type": "STRING",
"value": "[]"
},
{
"type": "STRING",
"value": "new"
},
{
"type": "STRING",
"value": "delete"
},
{
"type": "STRING",
"value": "new[]"
},
{
"type": "STRING",
"value": "delete[]"
}
]
}
@ -10655,6 +10737,282 @@
"named": true,
"value": "namespace_identifier"
},
"operator_cast": {
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "namespace",
"content": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_namespace_identifier"
},
{
"type": "SYMBOL",
"name": "template_type"
},
{
"type": "SYMBOL",
"name": "scoped_namespace_identifier"
}
]
},
{
"type": "BLANK"
}
]
}
},
{
"type": "STRING",
"value": "::"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "operator"
},
{
"type": "SYMBOL",
"name": "_declaration_specifiers"
},
{
"type": "FIELD",
"name": "declarator",
"content": {
"type": "SYMBOL",
"name": "_abstract_declarator"
}
}
]
}
},
"operator_cast_definition": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "storage_class_specifier"
},
{
"type": "SYMBOL",
"name": "type_qualifier"
},
{
"type": "SYMBOL",
"name": "attribute_specifier"
}
]
}
},
{
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "virtual_function_specifier"
},
{
"type": "SYMBOL",
"name": "explicit_function_specifier"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "FIELD",
"name": "declarator",
"content": {
"type": "SYMBOL",
"name": "operator_cast"
}
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "compound_statement"
}
},
{
"type": "SYMBOL",
"name": "default_method_clause"
},
{
"type": "SYMBOL",
"name": "delete_method_clause"
}
]
}
]
},
"operator_cast_declaration": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "virtual_function_specifier"
},
{
"type": "SYMBOL",
"name": "explicit_function_specifier"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "FIELD",
"name": "declarator",
"content": {
"type": "SYMBOL",
"name": "operator_cast"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "FIELD",
"name": "default_value",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ";"
}
]
},
"alone_macro": {
"type": "PATTERN",
"value": "[_A-Z][_A-Z0-9]+\\s*\\n"
},
"alone_macro_call": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[_A-Z][_A-Z0-9]+"
},
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[_A-Z][_A-Z0-9]+"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "PATTERN",
"value": "[_A-Z][_A-Z0-9]+"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ")"
},
{
"type": "STRING",
"value": "\n"
}
]
},
"macro_annotation": {
"type": "CHOICE",
"members": [

Просмотреть файл

@ -389,6 +389,46 @@
}
]
},
{
"type": "_declaration",
"named": true,
"fields": {
"declarator": {
"multiple": false,
"required": true,
"types": [
{
"type": "operator_cast",
"named": true
}
]
},
"default_value": {
"multiple": false,
"required": false,
"types": [
{
"type": "_expression",
"named": true
}
]
}
},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "explicit_function_specifier",
"named": true
},
{
"type": "virtual_function_specifier",
"named": true
}
]
}
},
{
"type": "abstract_array_declarator",
"named": true,
@ -564,6 +604,11 @@
}
}
},
{
"type": "alone_macro_call",
"named": true,
"fields": {}
},
{
"type": "argument_list",
"named": true,
@ -1261,6 +1306,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "_declaration",
"named": true
},
{
"type": "_statement",
"named": true
@ -1273,6 +1322,14 @@
"type": "alias_declaration",
"named": true
},
{
"type": "alone_macro",
"named": true
},
{
"type": "alone_macro_call",
"named": true
},
{
"type": "declaration",
"named": true
@ -1453,6 +1510,20 @@
{
"type": "init_declarator",
"named": true
},
{
"type": "operator_cast",
"named": true
}
]
},
"default_value": {
"multiple": false,
"required": false,
"types": [
{
"type": "_expression",
"named": true
}
]
},
@ -1520,6 +1591,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "_declaration",
"named": true
},
{
"type": "_statement",
"named": true
@ -1532,6 +1607,14 @@
"type": "alias_declaration",
"named": true
},
{
"type": "alone_macro",
"named": true
},
{
"type": "alone_macro_call",
"named": true
},
{
"type": "declaration",
"named": true
@ -1904,6 +1987,14 @@
"type": "alias_declaration",
"named": true
},
{
"type": "alone_macro",
"named": true
},
{
"type": "alone_macro_call",
"named": true
},
{
"type": "declaration",
"named": true
@ -2292,6 +2383,10 @@
{
"type": "function_declarator",
"named": true
},
{
"type": "operator_cast",
"named": true
}
]
},
@ -2734,6 +2829,68 @@
]
}
},
{
"type": "operator_cast",
"named": true,
"fields": {
"declarator": {
"multiple": false,
"required": true,
"types": [
{
"type": "_abstract_declarator",
"named": true
}
]
},
"namespace": {
"multiple": false,
"required": false,
"types": [
{
"type": "namespace_identifier",
"named": true
},
{
"type": "scoped_namespace_identifier",
"named": true
},
{
"type": "template_type",
"named": true
}
]
},
"type": {
"multiple": false,
"required": true,
"types": [
{
"type": "_type_specifier",
"named": true
}
]
}
},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "attribute_specifier",
"named": true
},
{
"type": "storage_class_specifier",
"named": true
},
{
"type": "type_qualifier",
"named": true
}
]
}
},
{
"type": "optional_parameter_declaration",
"named": true,
@ -3179,6 +3336,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "_declaration",
"named": true
},
{
"type": "_statement",
"named": true
@ -3195,6 +3356,14 @@
"type": "alias_declaration",
"named": true
},
{
"type": "alone_macro",
"named": true
},
{
"type": "alone_macro_call",
"named": true
},
{
"type": "declaration",
"named": true
@ -3274,6 +3443,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "_declaration",
"named": true
},
{
"type": "_statement",
"named": true
@ -3290,6 +3463,14 @@
"type": "alias_declaration",
"named": true
},
{
"type": "alone_macro",
"named": true
},
{
"type": "alone_macro_call",
"named": true
},
{
"type": "declaration",
"named": true
@ -3458,6 +3639,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "_declaration",
"named": true
},
{
"type": "_statement",
"named": true
@ -3474,6 +3659,14 @@
"type": "alias_declaration",
"named": true
},
{
"type": "alone_macro",
"named": true
},
{
"type": "alone_macro_call",
"named": true
},
{
"type": "declaration",
"named": true
@ -3578,6 +3771,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "_declaration",
"named": true
},
{
"type": "_statement",
"named": true
@ -3594,6 +3791,14 @@
"type": "alias_declaration",
"named": true
},
{
"type": "alone_macro",
"named": true
},
{
"type": "alone_macro_call",
"named": true
},
{
"type": "declaration",
"named": true
@ -4483,6 +4688,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "_declaration",
"named": true
},
{
"type": "_statement",
"named": true
@ -4495,6 +4704,14 @@
"type": "alias_declaration",
"named": true
},
{
"type": "alone_macro",
"named": true
},
{
"type": "alone_macro_call",
"named": true
},
{
"type": "declaration",
"named": true
@ -5437,6 +5654,10 @@
"type": "__attribute__",
"named": false
},
{
"type": "alone_macro",
"named": true
},
{
"type": "auto",
"named": true
@ -5581,6 +5802,10 @@
"type": "number_literal",
"named": true
},
{
"type": "operator",
"named": false
},
{
"type": "operator_name",
"named": true

Разница между файлами не показана из-за своего большого размера Загрузить разницу