@wooorm/starry-night
Version:
Syntax highlighting, like GitHub
454 lines (451 loc) • 17.1 kB
JavaScript
// This is a TextMate grammar distributed by `starry-night`.
// This grammar is developed at
// <https://github.com/mikomikotaishi/c.tmbundle>
// and licensed permissive.
// See <https://github.com/wooorm/starry-night> for more info.
/**
* @import {Grammar} from '@wooorm/starry-night'
*/
/** @type {Grammar} */
const grammar = {
extensions: [
'.c',
'.cats',
'.h',
'.h.in',
'.idc',
'.opencl',
'.upc',
'.xbm',
'.xpm',
'.xs'
],
names: [
'c',
'dtrace',
'dtrace-script',
'oncrpc',
'opencl',
'rpc',
'rpcgen',
'unified-parallel-c',
'x-bitmap',
'x-pixmap',
'xbm',
'xdr',
'xpm',
'xs'
],
patterns: [
{include: '#preprocessor-rule-enabled'},
{include: '#preprocessor-rule-disabled'},
{include: '#preprocessor-rule-other'},
{include: '#comments'},
{
match:
'\\b(assert|break|case|continue|countof|_Countof|default|defined|do|else|for|goto|if|_Pragma|return|sizeof|static_assert|_Static_assert|switch|typeof|typeof_unqual|while)\\b',
name: 'keyword.control.c'
},
{
match:
'\\b(_Atomic|asm|__asm__|auto|_BitInt|bool|_Bool|char|complex|_Complex|_Decimal32|_Decimal64|_Decimal128|double|enum|float|fortran|__fortran__|_Generic|imaginary|_Imaginary|int|long|_Noreturn|short|signed|struct|typedef|union|unsigned|void)\\b',
name: 'storage.type.c'
},
{
match:
'\\b(?:(?:u?int|char(?:8|16|32)|wchar|float(?:16|32|64|128)|bfloat16)_t|[uif](?:8|16|32|64|128)|bf16)\\b',
name: 'storage.typealias.c'
},
{
match:
'\\b(alignas|_Alignas|alignof|_Alignof|const|constexpr|extern|register|restrict|static|thread_local|_Thread_local|volatile|inline)\\b',
name: 'storage.modifier.c'
},
{match: '\\bk[A-Z]\\w*\\b', name: 'constant.other.variable.mac-classic.c'},
{
match: '\\bg[A-Z]\\w*\\b',
name: 'variable.other.readwrite.global.mac-classic.c'
},
{
match: '\\bs[A-Z]\\w*\\b',
name: 'variable.other.readwrite.static.mac-classic.c'
},
{
match: '\\b(NULL|nullptr|true|false|TRUE|FALSE)\\b',
name: 'constant.language.c'
},
{include: '#sizeof'},
{
captures: {
inc: {name: 'invalid.illegal.digit-separator-should-not-be-last.c++'}
},
match:
"(?x)\\b\n\t\t\t( (?i:\n\t\t\t 0x ( [0-9A-Fa-f]+ ( ' [0-9A-Fa-f]+ )* )? # Hexadecimal\n\t\t\t | 0b ( [0-1]+ ( ' [0-1]+ )* )? # Binary\n\t\t\t | 0 ( [0-7]+ ( ' [0-7]+ )* ) # Octal\n\t\t\t | ( [0-9]+ ( ' [0-9]+ )* ) # Decimal\n\t\t\t )\n\t\t\t ( ([uUfF] | u?ll? | U?LL?)\\b | (?<inc>') | \\b )\n\t\t\t| ( [0-9]+ ( ' [0-9]+ )* )?\n\t\t\t (?i:\n\t\t\t \\. ( [0-9]+ ( ' [0-9]+ )* ) E(\\+|-)? ( [0-9]+ ( ' [0-9]+ )* )\n\t\t\t | \\. ( [0-9]+ ( ' [0-9]+ )* )\n\t\t\t | E(\\+|-)? ( [0-9]+ ( ' [0-9]+ )* )\n\t\t\t )\n\t\t\t ( (?<inc>') | \\b )\n\t\t\t)",
name: 'constant.numeric.c'
},
{
begin: '"',
beginCaptures: {0: {name: 'punctuation.definition.string.begin.c'}},
end: '"',
endCaptures: {0: {name: 'punctuation.definition.string.end.c'}},
name: 'string.quoted.double.c',
patterns: [
{include: '#string_escaped_char'},
{include: '#string_placeholder'}
]
},
{
begin: "'",
beginCaptures: {0: {name: 'punctuation.definition.string.begin.c'}},
end: "'",
endCaptures: {0: {name: 'punctuation.definition.string.end.c'}},
name: 'string.quoted.single.c',
patterns: [{include: '#string_escaped_char'}]
},
{
begin:
'(?x)\n \t\t^\\s*\\#\\s*(define)\\s+ # define\n \t\t((?<id>[a-zA-Z_][a-zA-Z0-9_]*)) # macro name\n \t\t(?: # and optionally:\n \t\t (\\() # an open parenthesis\n \t\t (\n \t\t \\s* \\g<id> \\s* # first argument\n \t\t ((,) \\s* \\g<id> \\s*)* # additional arguments\n \t\t (?:\\.\\.\\.)? # varargs ellipsis?\n \t\t )\n \t\t (\\)) # a close parenthesis\n \t\t)?\n \t',
beginCaptures: {
1: {name: 'keyword.control.import.define.c'},
2: {name: 'entity.name.function.preprocessor.c'},
4: {name: 'punctuation.definition.parameters.begin.c'},
5: {name: 'variable.parameter.preprocessor.c'},
7: {name: 'punctuation.separator.parameters.c'},
8: {name: 'punctuation.definition.parameters.end.c'}
},
end: '(?=(?://|/\\*))|$',
name: 'meta.preprocessor.macro.c',
patterns: [
{
match: '(?>\\\\\\s*\\n)',
name: 'punctuation.separator.continuation.c'
},
{include: '$base'}
]
},
{
begin: '^\\s*#\\s*(error|warning)\\b',
captures: {1: {name: 'keyword.control.import.error.c'}},
end: '$',
name: 'meta.preprocessor.diagnostic.c',
patterns: [
{match: '(?>\\\\\\s*\\n)', name: 'punctuation.separator.continuation.c'}
]
},
{
begin: '^\\s*#\\s*(embed|include|import)\\b',
captures: {1: {name: 'keyword.control.import.include.c'}},
end: '(?=(?://|/\\*))|$',
name: 'meta.preprocessor.c.include',
patterns: [
{
match: '(?>\\\\\\s*\\n)',
name: 'punctuation.separator.continuation.c'
},
{
begin: '"',
beginCaptures: {0: {name: 'punctuation.definition.string.begin.c'}},
end: '"',
endCaptures: {0: {name: 'punctuation.definition.string.end.c'}},
name: 'string.quoted.double.include.c'
},
{
begin: '<',
beginCaptures: {0: {name: 'punctuation.definition.string.begin.c'}},
end: '>',
endCaptures: {0: {name: 'punctuation.definition.string.end.c'}},
name: 'string.quoted.other.lt-gt.include.c'
}
]
},
{include: '#pragma-mark'},
{
begin:
'^\\s*#\\s*(define|defined|elif|elifdef|elifndef|else|if|ifdef|ifndef|line|pragma|undef)\\b',
captures: {1: {name: 'keyword.control.import.c'}},
end: '(?=(?://|/\\*))|$',
name: 'meta.preprocessor.c',
patterns: [
{match: '(?>\\\\\\s*\\n)', name: 'punctuation.separator.continuation.c'}
]
},
{match: '\\b([a-z0-9_]+_t)\\b', name: 'support.type.posix-reserved.c'},
{include: '#block'},
{
begin:
'(?x)\n \t\t(?: ^ # begin-of-line\n \t\t | \n \t\t (?: (?= \\s ) (?