Просмотр исходного кода

Differentiate between top-level items and those that appear in statement blocks

pull/6/head
Max Brunsfeld 9 лет назад
Родитель
Сommit
7775b575e6
3 измененных файлов: 58445 добавлений и 62504 удалений
  1. +67
    -42
      grammar.js
  2. +209
    -100
      src/grammar.json
  3. +58169
    -62362
      src/parser.c

+ 67
- 42
grammar.js Просмотреть файл

@ -29,20 +29,17 @@ module.exports = grammar({
],
inline: $ => [
$._statement
$._statement,
$._top_level_item,
$._compound_statement_item
],
conflicts: $ => [
[$._declarator, $._type_specifier, $._expression, $.macro_type_specifier],
[$._type_specifier, $._declarator],
[$._type_specifier, $._declarator, $.macro_type_specifier],
[$._type_specifier, $._expression],
[$._type_specifier, $._expression, $.macro_type_specifier],
[$._declarator, $._type_specifier, $.macro_type_specifier],
[$._field_declarator, $._type_specifier, $.macro_type_specifier],
[$._declarator, $._type_specifier, $._expression],
[$._field_declarator, $._type_specifier],
[$._declarator, $._type_specifier],
[$._type_specifier, $.macro_type_specifier],
[$._type_specifier, $._expression],
[$._declarator, $._expression],
[$.sized_type_specifier],
],
@ -50,17 +47,10 @@ module.exports = grammar({
translation_unit: $ => repeat($._top_level_item),
_top_level_item: $ => choice(
$._preproc_statement,
$.function_definition,
$.linkage_specification,
$.declaration,
$._statement,
$._empty_declaration,
$.linkage_specification
),
// Preprocesser
_preproc_statement: $ => choice(
$.preproc_if,
$.preproc_ifdef,
$.preproc_include,
@ -69,26 +59,34 @@ module.exports = grammar({
$.preproc_call
),
_compound_statement_item: $ => choice(
$._statement,
$.declaration,
$._empty_declaration,
rename($.preproc_if_in_compound_statement, 'preproc_if'),
rename($.preproc_ifdef_in_compound_statement, 'preproc_ifdef'),
$.preproc_include,
$.preproc_def,
$.preproc_function_def,
$.preproc_call
),
// Preprocesser
preproc_include: $ => seq(
/#[ \t]*include/,
choice(
$.string_literal,
$.system_lib_string
)
$._pound_include,
choice($.string_literal, $.system_lib_string)
),
preproc_def: $ => seq(
/#[ \t]*define/,
$._pound_define,
$.identifier,
optional(seq(
/[ \t]+/,
$.preproc_arg
)),
optional(seq(/[ \t]+/, $.preproc_arg)),
'\n'
),
preproc_function_def: $ => seq(
/#[ \t]*define/,
$._pound_define,
$.identifier,
$.preproc_params,
optional($.preproc_arg),
@ -104,38 +102,61 @@ module.exports = grammar({
$.preproc_arg
),
preproc_arg: $ => token(prec(-1, repeat1(choice(/./, '\\\n')))),
preproc_if: $ => seq(
/#[ \t]*if/,
$._pound_if,
$.preproc_arg,
repeat($._top_level_item),
optional($.preproc_else),
/#[ \t]*endif/
$._pound_endif
),
preproc_if_in_compound_statement: $ => seq(
$._pound_if,
$.preproc_arg,
repeat($._compound_statement_item),
optional(rename($.preproc_else_in_compound_statement, 'preproc_else')),
$._pound_endif
),
preproc_ifdef: $ => seq(
choice(
/#[ \t]*ifdef/,
/#[ \t]*ifndef/
),
$._pound_ifdef,
$.identifier,
repeat($._top_level_item),
optional($.preproc_else),
/#[ \t]*endif/
$._pound_endif
),
preproc_ifdef_in_compound_statement: $ => seq(
$._pound_ifdef,
$.identifier,
repeat($._compound_statement_item),
optional(rename($.preproc_else_in_compound_statement, 'preproc_else')),
$._pound_endif
),
preproc_else: $ => seq(
/#[ \t]*else/,
$._pound_else,
repeat($._top_level_item)
),
preproc_directive: $ => /#[ \t]*\a\w*/,
preproc_else_in_compound_statement: $ => seq(
$._pound_else,
repeat($._compound_statement_item)
),
_pound_include: $ => preprocessor('include'),
_pound_define: $ => preprocessor('define'),
_pound_if: $ => preprocessor('if'),
_pound_ifdef: $ => preprocessor('ifn?def'),
_pound_endif: $ => preprocessor('endif'),
_pound_else: $ => preprocessor('else'),
preproc_directive: $ => preprocessor('\\a\\w*'),
preproc_arg: $ => token(prec(-1, repeat1(choice(/./, '\\\n')))),
// Main Grammar
function_definition: $ => seq(
optional($._declaration_specifiers),
$._declaration_specifiers,
$._declarator,
$.compound_statement
),
@ -244,7 +265,7 @@ module.exports = grammar({
compound_statement: $ => seq(
'{',
repeat($._top_level_item),
repeat($._compound_statement_item),
'}'
),
@ -329,7 +350,7 @@ module.exports = grammar({
),
field_declaration: $ => seq(
optional($._declaration_specifiers),
$._declaration_specifiers,
commaSep($._field_declarator),
optional(seq(':', $._expression)),
';'
@ -700,6 +721,10 @@ module.exports = grammar({
module.exports.PREC = PREC
function preprocessor (pattern) {
return new RegExp('#[ \t]*' + pattern)
}
function commaSep (rule) {
return optional(commaSep1(rule))
}

+ 209
- 100
src/grammar.json Просмотреть файл

@ -13,11 +13,11 @@
"members": [
{
"type": "SYMBOL",
"name": "_preproc_statement"
"name": "function_definition"
},
{
"type": "SYMBOL",
"name": "function_definition"
"name": "linkage_specification"
},
{
"type": "SYMBOL",
@ -25,28 +25,64 @@
},
{
"type": "SYMBOL",
"name": "_statement"
"name": "_empty_declaration"
},
{
"type": "SYMBOL",
"name": "_empty_declaration"
"name": "preproc_if"
},
{
"type": "SYMBOL",
"name": "linkage_specification"
"name": "preproc_ifdef"
},
{
"type": "SYMBOL",
"name": "preproc_include"
},
{
"type": "SYMBOL",
"name": "preproc_def"
},
{
"type": "SYMBOL",
"name": "preproc_function_def"
},
{
"type": "SYMBOL",
"name": "preproc_call"
}
]
},
"_preproc_statement": {
"_compound_statement_item": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "preproc_if"
"name": "_statement"
},
{
"type": "SYMBOL",
"name": "preproc_ifdef"
"name": "declaration"
},
{
"type": "SYMBOL",
"name": "_empty_declaration"
},
{
"type": "RENAME",
"content": {
"type": "SYMBOL",
"name": "preproc_if_in_compound_statement"
},
"value": "preproc_if"
},
{
"type": "RENAME",
"content": {
"type": "SYMBOL",
"name": "preproc_ifdef_in_compound_statement"
},
"value": "preproc_ifdef"
},
{
"type": "SYMBOL",
@ -70,8 +106,8 @@
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "#[ \\t]*include"
"type": "SYMBOL",
"name": "_pound_include"
},
{
"type": "CHOICE",
@ -92,8 +128,8 @@
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "#[ \\t]*define"
"type": "SYMBOL",
"name": "_pound_define"
},
{
"type": "SYMBOL",
@ -130,8 +166,8 @@
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "#[ \\t]*define"
"type": "SYMBOL",
"name": "_pound_define"
},
{
"type": "SYMBOL",
@ -236,35 +272,12 @@
}
]
},
"preproc_arg": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": -1,
"content": {
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "."
},
{
"type": "STRING",
"value": "\\\n"
}
]
}
}
}
},
"preproc_if": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "#[ \\t]*if"
"type": "SYMBOL",
"name": "_pound_if"
},
{
"type": "SYMBOL",
@ -290,27 +303,58 @@
]
},
{
"type": "PATTERN",
"value": "#[ \\t]*endif"
"type": "SYMBOL",
"name": "_pound_endif"
}
]
},
"preproc_ifdef": {
"preproc_if_in_compound_statement": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_pound_if"
},
{
"type": "SYMBOL",
"name": "preproc_arg"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_compound_statement_item"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "#[ \\t]*ifdef"
"type": "RENAME",
"content": {
"type": "SYMBOL",
"name": "preproc_else_in_compound_statement"
},
"value": "preproc_else"
},
{
"type": "PATTERN",
"value": "#[ \\t]*ifndef"
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_pound_endif"
}
]
},
"preproc_ifdef": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_pound_ifdef"
},
{
"type": "SYMBOL",
"name": "identifier"
@ -335,8 +379,48 @@
]
},
{
"type": "PATTERN",
"value": "#[ \\t]*endif"
"type": "SYMBOL",
"name": "_pound_endif"
}
]
},
"preproc_ifdef_in_compound_statement": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_pound_ifdef"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_compound_statement_item"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "RENAME",
"content": {
"type": "SYMBOL",
"name": "preproc_else_in_compound_statement"
},
"value": "preproc_else"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_pound_endif"
}
]
},
@ -344,8 +428,8 @@
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "#[ \\t]*else"
"type": "SYMBOL",
"name": "_pound_else"
},
{
"type": "REPEAT",
@ -356,24 +440,79 @@
}
]
},
"preproc_else_in_compound_statement": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_pound_else"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_compound_statement_item"
}
}
]
},
"_pound_include": {
"type": "PATTERN",
"value": "#[ \t]*include"
},
"_pound_define": {
"type": "PATTERN",
"value": "#[ \t]*define"
},
"_pound_if": {
"type": "PATTERN",
"value": "#[ \t]*if"
},
"_pound_ifdef": {
"type": "PATTERN",
"value": "#[ \t]*ifn?def"
},
"_pound_endif": {
"type": "PATTERN",
"value": "#[ \t]*endif"
},
"_pound_else": {
"type": "PATTERN",
"value": "#[ \t]*else"
},
"preproc_directive": {
"type": "PATTERN",
"value": "#[ \\t]*\\a\\w*"
"value": "#[ \t]*\\a\\w*"
},
"preproc_arg": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": -1,
"content": {
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "."
},
{
"type": "STRING",
"value": "\\\n"
}
]
}
}
}
},
"function_definition": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_declaration_specifiers"
},
{
"type": "BLANK"
}
]
"type": "SYMBOL",
"name": "_declaration_specifiers"
},
{
"type": "SYMBOL",
@ -972,7 +1111,7 @@
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_top_level_item"
"name": "_compound_statement_item"
}
},
{
@ -1312,16 +1451,8 @@
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_declaration_specifiers"
},
{
"type": "BLANK"
}
]
"type": "SYMBOL",
"name": "_declaration_specifiers"
},
{
"type": "CHOICE",
@ -3429,50 +3560,26 @@
],
"conflicts": [
[
"_declarator",
"_type_specifier",
"_expression",
"macro_type_specifier"
"_declarator"
],
[
"_type_specifier",
"_expression",
"macro_type_specifier"
],
[
"_declarator",
"_type_specifier",
"macro_type_specifier"
],
[
"_field_declarator",
"_type_specifier",
"macro_type_specifier"
],
[
"_declarator",
"_type_specifier",
"_expression"
],
[
"_field_declarator",
"_type_specifier"
],
[
"_declarator",
"_type_specifier"
],
[
"_type_specifier",
"_expression",
"macro_type_specifier"
],
[
"_type_specifier",
"_expression"
],
[
"_declarator",
"_expression"
"macro_type_specifier"
],
[
"sized_type_specifier"
@ -3480,7 +3587,9 @@
],
"externals": [],
"inline": [
"_statement"
"_statement",
"_top_level_item",
"_compound_statement_item"
],
"PREC": {
"ASSIGNMENT": -1,

+ 58169
- 62362
src/parser.c
Разница между файлами не показана из-за своего большого размера
Просмотреть файл


Загрузка…
Отмена
Сохранить