Skip to main content

GRAMMAR

Constant GRAMMAR 

Source
pub const GRAMMAR: &str = "module.exports = grammar({\n  name: \'preproc\',\n\n  externals: $ => [\n    $.raw_string_literal,\n  ],\n\n  rules: {\n    translation_unit: $ => repeat($._top_level_item),\n\n    _top_level_item: $ => choice(\n      $.string_literal,\n      $.char_literal,\n      $.raw_string_literal,\n      $.comment,\n      $.nothing,\n      $.define,\n      $.undef,\n      $.preproc_if,\n      $.preproc_include,\n      $.preproc_nothing,\n      $.integer_literal,\n    ),\n\n    identifier: $ => /[a-zA-Z_]\\w*/,\n\n    nothing: $ => token(\n      choice(\n        /[^R\"\'\\/#0-9]+/,\n        \'R\',\n        \'#\',\n        \'/\',\n        \'\\n\',\n      )\n    ),\n\n    preproc_continuation_line: $ => token.immediate(\n      /.*\\\\\\r?\\n/\n    ),\n\n    preproc_line: $ => token.immediate(\n      /.*/,\n    ),\n\n    preproc_include: $ => seq(\n      /[ \\t]*#[ \\t]*include(_next)?[ \\t]*/,\n      choice(\n        $.string_literal,\n        seq(\n          \'<\',\n          $.path,\n          \'>\',\n        ),\n        $.identifier,\n      ),\n    ),\n\n    path: $ => token(\n      /[^>]*/,\n    ),\n\n    define: $ => seq(\n      /[ \\t]*#[ \\t]*define[ \\t]+/,\n      $.identifier,\n      repeat($.preproc_continuation_line),\n      $.preproc_line,\n      \'\\n\',\n    ),\n\n    preproc_if: $ => seq(\n      /[ \\t]*#[ \\t]*(ifdef|ifndef|if).*\\n/,\n      repeat($._top_level_item),\n      repeat($.preproc_elif),\n      optional($.preproc_else),\n      /[ \\t]*#[ \\t]*endif.*\\n/,\n    ),\n\n    preproc_elif: $ => seq(\n      /[ \\t]*#[ \\t]*elif.*\\n/,\n      repeat($._top_level_item),\n    ),\n\n    preproc_else: $ => seq(\n      /[ \\t]*#[ \\t]*else.*\\n/,\n      repeat($._top_level_item),\n    ),\n\n    undef: $ => seq(\n      /[ \\t]*#[ \\t]*undef[ \\t]+/,\n      $.identifier,\n      repeat($.preproc_continuation_line),\n      $.preproc_line,\n      \'\\n\',\n    ),\n\n    preproc_nothing: $ => seq(\n      token.immediate(/[ \\t]*#[ \\t]*(error|pragma|line)/),\n      repeat($.preproc_continuation_line),\n      $.preproc_line,\n    ),\n\n    string_literal: $ => seq(\n      /\"([^\\\\\"]|\\\\(.|\\n))*\"/,\n    ),\n\n    char_literal: $ => seq(\n      /\'([^\\\\\']|\\\\(.|\\n))*\'/,\n    ),\n\n    integer_literal: $ => token.immediate(\n      /[0-9]+[0-9\']*/\n    ),\n\n    // http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890\n    comment: $ => token(choice(\n      seq(\'//\', /(\\\\(.|\\r?\\n)|[^\\\\\\n])*/),\n      seq(\n        \'/*\',\n        /[^*]*\\*+([^/*][^*]*\\*+)*/,\n      \'/\'\n    )\n    )),\n  },\n\n});\n";
Expand description

The source of the Preproc tree-sitter grammar description.