Skip to content

Commit

Permalink
Added C23 attributes to C lexer.
Browse files Browse the repository at this point in the history
Based on contribution from Samuel Marquis.
  • Loading branch information
orbitalquark committed Sep 12, 2024
1 parent c929e9d commit 799e8ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/thanks.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ and better over the years.
* Robert Gieseke
* Roberto Ierusalimschy
* S\. Gilles
* Samuel Marquis
* Simeon Maryasin
* Snoopy
* Stéphane Rivière
Expand Down
20 changes: 17 additions & 3 deletions lexers/ansi_c.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))

-- Comments.
local line_comment = lexer.to_eol('//', true)
local ws = S(' \t')^0
local block_comment = lexer.range('/*', '*/') +
lexer.range('#if' * S(' \t')^0 * '0' * lexer.space, '#endif')
lexer.range('#if' * ws * '0' * lexer.space, '#endif')
lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))

-- Numbers.
Expand All @@ -46,11 +47,18 @@ local float = lexer.float * P('f')^-1
lex:add_rule('number', lex:tag(lexer.NUMBER, float + integer))

-- Preprocessor.
local include = lex:tag(lexer.PREPROCESSOR, '#' * S('\t ')^0 * 'include') *
local include = lex:tag(lexer.PREPROCESSOR, '#' * ws * 'include') *
(lex:get_rule('whitespace') * lex:tag(lexer.STRING, lexer.range('<', '>', true)))^-1
local preproc = lex:tag(lexer.PREPROCESSOR, '#' * S('\t ')^0 * lex:word_match(lexer.PREPROCESSOR))
local preproc = lex:tag(lexer.PREPROCESSOR, '#' * ws * lex:word_match(lexer.PREPROCESSOR))
lex:add_rule('preprocessor', include + preproc)

-- Attributes.
local standard_attr = lex:word_match(lexer.ATTRIBUTE)
local non_standard_attr = lexer.word * '::' * lexer.word
local attr_args = lexer.range('(', ')')
local attr = (non_standard_attr + standard_attr) * attr_args^-1
lex:add_rule('attribute', lex:tag(lexer.ATTRIBUTE, '[[' * attr * (ws * ',' * ws * attr)^0 * ']]'))

-- Operators.
lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%<>~!=^&|?~:;,.()[]{}')))

Expand Down Expand Up @@ -194,6 +202,12 @@ lex:set_word_list(lexer.PREPROCESSOR, {
'undef'
})

lex:set_word_list(lexer.ATTRIBUTE, {
-- C23
'deprecated', 'fallthrough', 'nodiscard', 'maybe_unused', 'noreturn', '_Noreturn', 'unsequenced',
'reproducible'
})

lexer.property['scintillua.comment'] = '//'

return lex

0 comments on commit 799e8ae

Please sign in to comment.