瀏覽代碼

Merge pull request #148 from amaanq/c23-baby

feat: C23
pull/146/head
Amaan Qureshi 3 年之前
committed by GitHub
父節點
當前提交
51cd29c3d1
沒有發現已知的金鑰在資料庫的簽署中 GPG 金鑰 ID: 4AEE18F83AFDEB23
共有 6 個檔案被更改,包括 83169 行新增64903 行删除
  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 查看文件

@ -472,15 +472,18 @@ module.exports = grammar({
'auto',
'register',
'inline',
'thread_local',
),
type_qualifier: _ => choice(
'const',
'constexpr',
'volatile',
'restrict',
'__restrict__',
'_Atomic',
'_Noreturn',
'noreturn',
),
_type_specifier: $ => choice(
@ -515,9 +518,12 @@ module.exports = grammar({
'void',
'size_t',
'ssize_t',
'ptrdiff_t',
'intptr_t',
'uintptr_t',
'charptr_t',
'nullptr_t',
'max_align_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 => `char${n}_t`),
@ -1086,9 +1092,10 @@ module.exports = grammar({
true: _ => token(choice('TRUE', 'true')),
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),
_field_identifier: $ => alias($.identifier, $.field_identifier),
@ -1167,7 +1174,7 @@ function preprocIf(suffix, content) {
choice(preprocessor('ifdef'), preprocessor('ifndef')),
field('name', $.identifier),
repeat(content($)),
field('alternative', optional(elseBlock($))),
field('alternative', optional(choice(elseBlock($), $.preproc_elifdef))),
preprocessor('endif'),
),
@ -1183,6 +1190,13 @@ function preprocIf(suffix, content) {
repeat(content($)),
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 查看文件

@ -424,12 +424,21 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "preproc_else"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "preproc_else"
},
{
"type": "SYMBOL",
"name": "preproc_elif"
}
]
},
{
"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": {
"type": "SEQ",
"members": [
@ -657,22 +734,31 @@
"type": "CHOICE",
"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": {
"type": "PATTERN",
"value": "#[ \\t]*[a-zA-Z]\\w*"
@ -3050,6 +3214,10 @@
{
"type": "STRING",
"value": "inline"
},
{
"type": "STRING",
"value": "thread_local"
}
]
},
@ -3060,6 +3228,10 @@
"type": "STRING",
"value": "const"
},
{
"type": "STRING",
"value": "constexpr"
},
{
"type": "STRING",
"value": "volatile"
@ -3079,6 +3251,10 @@
{
"type": "STRING",
"value": "_Noreturn"
},
{
"type": "STRING",
"value": "noreturn"
}
]
},
@ -3210,6 +3386,10 @@
"type": "STRING",
"value": "ssize_t"
},
{
"type": "STRING",
"value": "ptrdiff_t"
},
{
"type": "STRING",
"value": "intptr_t"
@ -3222,6 +3402,14 @@
"type": "STRING",
"value": "charptr_t"
},
{
"type": "STRING",
"value": "nullptr_t"
},
{
"type": "STRING",
"value": "max_align_t"
},
{
"type": "STRING",
"value": "int8_t"
@ -7132,12 +7320,21 @@
}
},
"null": {
"type": "STRING",
"value": "NULL"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "NULL"
},
{
"type": "STRING",
"value": "nullptr"
}
]
},
"identifier": {
"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": "ALIAS",

+ 119
- 2
src/node-types.json 查看文件

@ -2247,6 +2247,11 @@
"named": true,
"fields": {}
},
{
"type": "null",
"named": true,
"fields": {}
},
{
"type": "offsetof_expression",
"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",
"named": true,
@ -2876,6 +2965,10 @@
"type": "preproc_elif",
"named": true
},
{
"type": "preproc_elifdef",
"named": true
},
{
"type": "preproc_else",
"named": true
@ -3486,6 +3579,14 @@
"type": "#elif",
"named": false
},
{
"type": "#elifdef",
"named": false
},
{
"type": "#elifndef",
"named": false
},
{
"type": "#else",
"named": false
@ -3662,6 +3763,10 @@
"type": "L'",
"named": false
},
{
"type": "NULL",
"named": false
},
{
"type": "U\"",
"named": false
@ -3786,6 +3891,10 @@
"type": "const",
"named": false
},
{
"type": "constexpr",
"named": false
},
{
"type": "continue",
"named": false
@ -3863,8 +3972,12 @@
"named": true
},
{
"type": "null",
"named": true
"type": "noreturn",
"named": false
},
{
"type": "nullptr",
"named": false
},
{
"type": "number_literal",
@ -3934,6 +4047,10 @@
"type": "system_lib_string",
"named": true
},
{
"type": "thread_local",
"named": false
},
{
"type": "true",
"named": true

+ 82754
- 64877
src/parser.c
文件差異過大導致無法顯示
查看文件


+ 16
- 1
test/corpus/declarations.txt 查看文件

@ -489,6 +489,8 @@ Type qualifiers
const _Atomic unsigned long int x = 5;
restrict int y = 6;
volatile int z = 7;
constexpr int a = 8;
noreturn void b() {}
--------------------------------------------------------------------------------
@ -512,7 +514,20 @@ volatile int z = 7;
(primitive_type)
(init_declarator
(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

+ 46
- 0
test/corpus/preprocessor.txt 查看文件

@ -164,6 +164,52 @@ int b;
name: (identifier)
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
================================================================================

Loading…
取消
儲存