aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authororbitalquark <70453897+orbitalquark@users.noreply.github.com>2024-09-12 19:56:24 -0400
committerRandy Palamar <randy@rnpnr.xyz>2025-01-04 12:20:19 -0700
commitc51b9e952d4565dcca259baaf30dd035a1ae9c11 (patch)
tree03ffb233652f8a4bbd3c1031615be7ecdfd483ca /lua
parent57aacac06814dd5d72421b6e5cd969f96a7cd061 (diff)
downloadvis-c51b9e952d4565dcca259baaf30dd035a1ae9c11.tar.gz
vis-c51b9e952d4565dcca259baaf30dd035a1ae9c11.tar.xz
Add C23 attributes to C lexer
Based on contribution from Samuel Marquis.
Diffstat (limited to 'lua')
-rw-r--r--lua/lexers/ansi_c.lua20
1 files changed, 17 insertions, 3 deletions
diff --git a/lua/lexers/ansi_c.lua b/lua/lexers/ansi_c.lua
index 61ab047..b9fa6f7 100644
--- a/lua/lexers/ansi_c.lua
+++ b/lua/lexers/ansi_c.lua
@@ -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.
@@ -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('+-/*%<>~!=^&|?~:;,.()[]{}')))
@@ -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