Procházet zdrojové kódy

Allow simple abstract function declarators as in `sizeof(void * ())`

pull/6/head
Max Brunsfeld před 9 roky
rodič
revize
42436c29ae
4 změnil soubory, kde provedl 25081 přidání a 24752 odebrání
  1. +4
    -1
      corpus/expressions.txt
  2. +34
    -5
      grammar.js
  3. +56
    -34
      src/grammar.json
  4. +24987
    -24712
      src/parser.c

+ 4
- 1
corpus/expressions.txt Zobrazit soubor

@ -280,6 +280,7 @@ int main() {
sizeof x.a;
sizeof(x.a);
sizeof(const char **);
sizeof(char * ());
}
---
@ -292,7 +293,9 @@ int main() {
(expression_statement (sizeof_expression (field_expression (identifier) (identifier))))
(expression_statement (sizeof_expression (parenthesized_expression (field_expression (identifier) (identifier)))))
(expression_statement (sizeof_expression
(type_name (type_qualifier) (identifier) (abstract_pointer_declarator (abstract_pointer_declarator))))))))
(type_name (type_qualifier) (identifier) (abstract_pointer_declarator (abstract_pointer_declarator)))))
(expression_statement (sizeof_expression
(type_name (identifier) (abstract_pointer_declarator (abstract_function_declarator))))))))
============================================
Compound literals

+ 34
- 5
grammar.js Zobrazit soubor

@ -29,11 +29,37 @@ module.exports = grammar({
],
conflicts: $ => [
// Thing (
// ^
// This could be:
// * a type name in a declaration with a parenthesized variable name:
// int (b) = 0;
// * a function name in a function call:
// puts("hi");
// * the name of a macro that defines a type:
// Array(int) x;
[$._type_specifier, $._expression],
[$.sizeof_expression, $.cast_expression],
[$._type_specifier, $._expression, $.macro_type_specifier],
[$._type_specifier, $.macro_type_specifier],
// unsigned x
// ^
// This could be:
// * a modifier for the type that follows:
// unsigned int x = 5;
// * a type in itself:
// unsigned x = 5;
[$.sized_type_specifier],
// void foo(int (bar)
// ^
// This could be:
// * the type of the first parameter to a callback function:
// void foo(int (int), bool);
// * a parenthesized name for the first parameter:
// void foo(int (some_int), bool);
[$._declarator, $._type_specifier],
[$._declarator, $._type_specifier, $.macro_type_specifier],
],
rules: {
@ -193,7 +219,7 @@ module.exports = grammar({
)),
abstract_function_declarator: $ => prec(1, seq(
$._abstract_declarator,
optional($._abstract_declarator),
'(',
optional($.parameter_type_list),
')'
@ -552,9 +578,12 @@ module.exports = grammar({
argument_list: $ => seq('(', commaSep($._expression), ')'),
field_expression: $ => choice(
prec.left(PREC.FIELD, seq($._expression, '.', $.identifier)),
prec.left(PREC.FIELD, seq($._expression, '->', $.identifier))
field_expression: $ => seq(
prec(PREC.FIELD, seq(
$._expression,
choice('.', '->')
)),
$.identifier
),
compound_literal_expression: $ => seq(

+ 56
- 34
src/grammar.json Zobrazit soubor

@ -689,8 +689,16 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_abstract_declarator"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_abstract_declarator"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
@ -2704,10 +2712,10 @@
]
},
"field_expression": {
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "PREC_LEFT",
"type": "PREC",
"value": 15,
"content": {
"type": "SEQ",
@ -2717,36 +2725,24 @@
"name": "_expression"
},
{
"type": "STRING",
"value": "."
},
{
"type": "SYMBOL",
"name": "identifier"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "STRING",
"value": "->"
}
]
}
]
}
},
{
"type": "PREC_LEFT",
"value": 15,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "STRING",
"value": "->"
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
}
"type": "SYMBOL",
"name": "identifier"
}
]
},
@ -3348,10 +3344,6 @@
"_type_specifier",
"_expression"
],
[
"sizeof_expression",
"cast_expression"
],
[
"_type_specifier",
"_expression",
@ -3363,7 +3355,37 @@
],
[
"sized_type_specifier"
],
[
"_declarator",
"_type_specifier"
],
[
"_declarator",
"_type_specifier",
"macro_type_specifier"
]
],
"externals": []
"externals": [],
"PREC": {
"ASSIGNMENT": -1,
"CONDITIONAL": -2,
"DEFAULT": 0,
"LOGICAL_OR": 1,
"LOGICAL_AND": 2,
"INCLUSIVE_OR": 3,
"EXCLUSIVE_OR": 4,
"BITWISE_AND": 5,
"EQUAL": 6,
"RELATIONAL": 7,
"SIZEOF": 8,
"SHIFT": 9,
"ADD": 10,
"MULTIPLY": 11,
"CALL": 12,
"CAST": 13,
"UNARY": 14,
"FIELD": 15,
"SUBSCRIPT": 16
}
}

+ 24987
- 24712
src/parser.c
Diff nebyl zobrazen, protože je příliš veliký
Zobrazit soubor


Načítá se…
Zrušit
Uložit