Explorar el Código

Merge pull request #148 from amaanq/c23-baby

feat: C23
pull/146/head
Amaan Qureshi hace 3 años
cometido por GitHub
padre
commit
51cd29c3d1
No se encontró ninguna clave conocida en la base de datos para esta firma ID de clave GPG: 4AEE18F83AFDEB23
Se han modificado 6 ficheros con 83169 adiciones y 64903 borrados
  1. +17
    -3
      grammar.js
  2. +217
    -20
      src/grammar.json
  3. +119
    -2
      src/node-types.json
  4. +82754
    -64877
      src/parser.c
  5. +16
    -1
      test/corpus/declarations.txt
  6. +46
    -0
      test/corpus/preprocessor.txt

+ 17
- 3
grammar.js Ver fichero

@ -472,15 +472,18 @@ module.exports = grammar({
'auto', 'auto',
'register', 'register',
'inline', 'inline',
'thread_local',
), ),
type_qualifier: _ => choice( type_qualifier: _ => choice(
'const', 'const',
'constexpr',
'volatile', 'volatile',
'restrict', 'restrict',
'__restrict__', '__restrict__',
'_Atomic', '_Atomic',
'_Noreturn', '_Noreturn',
'noreturn',
), ),
_type_specifier: $ => choice( _type_specifier: $ => choice(
@ -515,9 +518,12 @@ module.exports = grammar({
'void', 'void',
'size_t', 'size_t',
'ssize_t', 'ssize_t',
'ptrdiff_t',
'intptr_t', 'intptr_t',
'uintptr_t', 'uintptr_t',
'charptr_t', 'charptr_t',
'nullptr_t',
'max_align_t',
...[8, 16, 32, 64].map(n => `int${n}_t`), ...[8, 16, 32, 64].map(n => `int${n}_t`),
...[8, 16, 32, 64].map(n => `uint${n}_t`), ...[8, 16, 32, 64].map(n => `uint${n}_t`),
...[8, 16, 32, 64].map(n => `char${n}_t`), ...[8, 16, 32, 64].map(n => `char${n}_t`),
@ -1086,9 +1092,10 @@ module.exports = grammar({
true: _ => token(choice('TRUE', 'true')), true: _ => token(choice('TRUE', 'true')),
false: _ => token(choice('FALSE', 'false')), false: _ => token(choice('FALSE', 'false')),
null: _ => 'NULL',
null: _ => choice('NULL', 'nullptr'),
identifier: _ => /(\p{XID_Start}|_)\p{XID_Continue}*/,
identifier: _ =>
/(\p{XID_Start}|_|\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})(\p{XID_Continue}|\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})*/,
_type_identifier: $ => alias($.identifier, $.type_identifier), _type_identifier: $ => alias($.identifier, $.type_identifier),
_field_identifier: $ => alias($.identifier, $.field_identifier), _field_identifier: $ => alias($.identifier, $.field_identifier),
@ -1167,7 +1174,7 @@ function preprocIf(suffix, content) {
choice(preprocessor('ifdef'), preprocessor('ifndef')), choice(preprocessor('ifdef'), preprocessor('ifndef')),
field('name', $.identifier), field('name', $.identifier),
repeat(content($)), repeat(content($)),
field('alternative', optional(elseBlock($))),
field('alternative', optional(choice(elseBlock($), $.preproc_elifdef))),
preprocessor('endif'), preprocessor('endif'),
), ),
@ -1183,6 +1190,13 @@ function preprocIf(suffix, content) {
repeat(content($)), repeat(content($)),
field('alternative', optional(elseBlock($))), field('alternative', optional(elseBlock($))),
), ),
['preproc_elifdef' + suffix]: $ => seq(
choice(preprocessor('elifdef'), preprocessor('elifndef')),
field('name', $.identifier),
repeat(content($)),
field('alternative', optional(elseBlock($))),
),
}; };
} }

+ 217
- 20
src/grammar.json Ver fichero

@ -424,12 +424,21 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL",
"name": "preproc_else"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "preproc_else"
},
{
"type": "SYMBOL",
"name": "preproc_elif"
}
]
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "preproc_elif"
"name": "preproc_elifdef"
} }
] ]
}, },
@ -529,6 +538,74 @@
} }
] ]
}, },
"preproc_elifdef": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "#[ \t]*elifdef"
},
"named": false,
"value": "#elifdef"
},
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "#[ \t]*elifndef"
},
"named": false,
"value": "#elifndef"
}
]
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_top_level_item"
}
},
{
"type": "FIELD",
"name": "alternative",
"content": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "preproc_else"
},
{
"type": "SYMBOL",
"name": "preproc_elif"
}
]
},
{
"type": "BLANK"
}
]
}
}
]
},
"preproc_if_in_field_declaration_list": { "preproc_if_in_field_declaration_list": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -657,22 +734,31 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_else_in_field_declaration_list"
},
"named": true,
"value": "preproc_else"
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_else_in_field_declaration_list"
},
"named": true,
"value": "preproc_else"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_elif_in_field_declaration_list"
},
"named": true,
"value": "preproc_elif"
}
]
}, },
{ {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_elif_in_field_declaration_list"
},
"named": true,
"value": "preproc_elif"
"type": "SYMBOL",
"name": "preproc_elifdef"
} }
] ]
}, },
@ -782,6 +868,84 @@
} }
] ]
}, },
"preproc_elifdef_in_field_declaration_list": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "#[ \t]*elifdef"
},
"named": false,
"value": "#elifdef"
},
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "#[ \t]*elifndef"
},
"named": false,
"value": "#elifndef"
}
]
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_field_declaration_list_item"
}
},
{
"type": "FIELD",
"name": "alternative",
"content": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_else_in_field_declaration_list"
},
"named": true,
"value": "preproc_else"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "preproc_elif_in_field_declaration_list"
},
"named": true,
"value": "preproc_elif"
}
]
},
{
"type": "BLANK"
}
]
}
}
]
},
"preproc_directive": { "preproc_directive": {
"type": "PATTERN", "type": "PATTERN",
"value": "#[ \\t]*[a-zA-Z]\\w*" "value": "#[ \\t]*[a-zA-Z]\\w*"
@ -3050,6 +3214,10 @@
{ {
"type": "STRING", "type": "STRING",
"value": "inline" "value": "inline"
},
{
"type": "STRING",
"value": "thread_local"
} }
] ]
}, },
@ -3060,6 +3228,10 @@
"type": "STRING", "type": "STRING",
"value": "const" "value": "const"
}, },
{
"type": "STRING",
"value": "constexpr"
},
{ {
"type": "STRING", "type": "STRING",
"value": "volatile" "value": "volatile"
@ -3079,6 +3251,10 @@
{ {
"type": "STRING", "type": "STRING",
"value": "_Noreturn" "value": "_Noreturn"
},
{
"type": "STRING",
"value": "noreturn"
} }
] ]
}, },
@ -3210,6 +3386,10 @@
"type": "STRING", "type": "STRING",
"value": "ssize_t" "value": "ssize_t"
}, },
{
"type": "STRING",
"value": "ptrdiff_t"
},
{ {
"type": "STRING", "type": "STRING",
"value": "intptr_t" "value": "intptr_t"
@ -3222,6 +3402,14 @@
"type": "STRING", "type": "STRING",
"value": "charptr_t" "value": "charptr_t"
}, },
{
"type": "STRING",
"value": "nullptr_t"
},
{
"type": "STRING",
"value": "max_align_t"
},
{ {
"type": "STRING", "type": "STRING",
"value": "int8_t" "value": "int8_t"
@ -7132,12 +7320,21 @@
} }
}, },
"null": { "null": {
"type": "STRING",
"value": "NULL"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "NULL"
},
{
"type": "STRING",
"value": "nullptr"
}
]
}, },
"identifier": { "identifier": {
"type": "PATTERN", "type": "PATTERN",
"value": "(\\p{XID_Start}|_)\\p{XID_Continue}*"
"value": "(\\p{XID_Start}|_|\\\\u[0-9A-Fa-f]{4}|\\\\U[0-9A-Fa-f]{8})(\\p{XID_Continue}|\\\\u[0-9A-Fa-f]{4}|\\\\U[0-9A-Fa-f]{8})*"
}, },
"_type_identifier": { "_type_identifier": {
"type": "ALIAS", "type": "ALIAS",

+ 119
- 2
src/node-types.json Ver fichero

@ -2247,6 +2247,11 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "null",
"named": true,
"fields": {}
},
{ {
"type": "offsetof_expression", "type": "offsetof_expression",
"named": true, "named": true,
@ -2649,6 +2654,90 @@
] ]
} }
}, },
{
"type": "preproc_elifdef",
"named": true,
"fields": {
"alternative": {
"multiple": false,
"required": false,
"types": [
{
"type": "preproc_elif",
"named": true
},
{
"type": "preproc_else",
"named": true
}
]
},
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "_statement",
"named": true
},
{
"type": "_type_specifier",
"named": true
},
{
"type": "declaration",
"named": true
},
{
"type": "function_definition",
"named": true
},
{
"type": "linkage_specification",
"named": true
},
{
"type": "preproc_call",
"named": true
},
{
"type": "preproc_def",
"named": true
},
{
"type": "preproc_function_def",
"named": true
},
{
"type": "preproc_if",
"named": true
},
{
"type": "preproc_ifdef",
"named": true
},
{
"type": "preproc_include",
"named": true
},
{
"type": "type_definition",
"named": true
}
]
}
},
{ {
"type": "preproc_else", "type": "preproc_else",
"named": true, "named": true,
@ -2876,6 +2965,10 @@
"type": "preproc_elif", "type": "preproc_elif",
"named": true "named": true
}, },
{
"type": "preproc_elifdef",
"named": true
},
{ {
"type": "preproc_else", "type": "preproc_else",
"named": true "named": true
@ -3486,6 +3579,14 @@
"type": "#elif", "type": "#elif",
"named": false "named": false
}, },
{
"type": "#elifdef",
"named": false
},
{
"type": "#elifndef",
"named": false
},
{ {
"type": "#else", "type": "#else",
"named": false "named": false
@ -3662,6 +3763,10 @@
"type": "L'", "type": "L'",
"named": false "named": false
}, },
{
"type": "NULL",
"named": false
},
{ {
"type": "U\"", "type": "U\"",
"named": false "named": false
@ -3786,6 +3891,10 @@
"type": "const", "type": "const",
"named": false "named": false
}, },
{
"type": "constexpr",
"named": false
},
{ {
"type": "continue", "type": "continue",
"named": false "named": false
@ -3863,8 +3972,12 @@
"named": true "named": true
}, },
{ {
"type": "null",
"named": true
"type": "noreturn",
"named": false
},
{
"type": "nullptr",
"named": false
}, },
{ {
"type": "number_literal", "type": "number_literal",
@ -3934,6 +4047,10 @@
"type": "system_lib_string", "type": "system_lib_string",
"named": true "named": true
}, },
{
"type": "thread_local",
"named": false
},
{ {
"type": "true", "type": "true",
"named": true "named": true

+ 82754
- 64877
src/parser.c
La diferencia del archivo ha sido suprimido porque es demasiado grande
Ver fichero


+ 16
- 1
test/corpus/declarations.txt Ver fichero

@ -489,6 +489,8 @@ Type qualifiers
const _Atomic unsigned long int x = 5; const _Atomic unsigned long int x = 5;
restrict int y = 6; restrict int y = 6;
volatile int z = 7; volatile int z = 7;
constexpr int a = 8;
noreturn void b() {}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -512,7 +514,20 @@ volatile int z = 7;
(primitive_type) (primitive_type)
(init_declarator (init_declarator
(identifier) (identifier)
(number_literal))))
(number_literal)))
(declaration
(type_qualifier)
(primitive_type)
(init_declarator
(identifier)
(number_literal)))
(function_definition
(type_qualifier)
(primitive_type)
(function_declarator
(identifier)
(parameter_list))
(compound_statement)))
================================================================================ ================================================================================
Local array declarations Local array declarations

+ 46
- 0
test/corpus/preprocessor.txt Ver fichero

@ -164,6 +164,52 @@ int b;
name: (identifier) name: (identifier)
alternative: (preproc_else))))) alternative: (preproc_else)))))
================================================================================
Elifdefs
================================================================================
#ifndef DEFINE1
int j;
#elifndef DEFINE2
int k;
#endif
#ifdef DEFINE2
ssize_t b;
#elifdef DEFINE3
ssize_t c;
#else
int b;
#endif
--------------------------------------------------------------------------------
(translation_unit
(preproc_ifdef
(identifier)
(declaration
(primitive_type)
(identifier))
(preproc_elifdef
(identifier)
(declaration
(primitive_type)
(identifier))))
(preproc_ifdef
(identifier)
(declaration
(primitive_type)
(identifier))
(preproc_elifdef
(identifier)
(declaration
(primitive_type)
(identifier))
(preproc_else
(declaration
(primitive_type)
(identifier))))))
================================================================================ ================================================================================
General if blocks General if blocks
================================================================================ ================================================================================

Cargando…
Cancelar
Guardar