Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C Family] Full rewrite of C family syntaxes #4147

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d46975e
Initial Commit for rewrite
camila314 Feb 7, 2025
8546556
syntax test file wip
camila314 Feb 7, 2025
c7b239a
\9 shouldn't be a valid escape
camila314 Feb 7, 2025
ab7b691
Fix some issues pointed out
camila314 Feb 7, 2025
0748783
- Add _Static_assert, static_assert, _Pragma word operators
camila314 Feb 8, 2025
e09a040
Fix line escape in string
camila314 Feb 8, 2025
85a129e
Update Syntax Test
camila314 Feb 8, 2025
0f0ac07
Add typedef entity (with fn pointer) and make function params highlig…
camila314 Feb 10, 2025
3e2d92a
Remove consume-pop
camila314 Feb 10, 2025
f1abee3
Add std funcs
camila314 Feb 10, 2025
5497d83
Syntax Test
camila314 Feb 10, 2025
a8887ab
Add actual tests to the syntax test file
camila314 Feb 10, 2025
a60ca70
Fix name in syntax test
camila314 Feb 10, 2025
332929e
Reformat file to be top-down instead of bottom-up
camila314 Feb 10, 2025
9dcc40d
fix scopes in test file (oops)
camila314 Feb 10, 2025
8e6534c
Start ObjC
camila314 Feb 11, 2025
38438e0
Fix declspec being silly
camila314 Feb 11, 2025
568b426
huge mistake
camila314 Feb 11, 2025
c69b396
Add fallback for messed up parenthesis
camila314 Feb 11, 2025
23f087c
Macros should only have parameters if there's no space between the na…
camila314 Feb 11, 2025
f703731
Add some more objc storage types
camila314 Feb 11, 2025
d4a595a
Add self access to ObjC, remove look-behinds from both syntaxes
camila314 Feb 11, 2025
f00c315
Don't include prototype for single and angle strings
camila314 Feb 12, 2025
be03dd4
Update Syntax Tests
camila314 Feb 12, 2025
a1e1478
Add many to ObjC
camila314 Feb 12, 2025
d7ba999
Minor fixes
camila314 Feb 12, 2025
acf156b
Avoid treating `return` and other control keywords as function return…
camila314 Feb 12, 2025
b5b5bcd
Function calls can span a newline (#2815)
camila314 Feb 12, 2025
5315b9d
Update tests for previous commit
camila314 Feb 12, 2025
5b4e0aa
Improve multi-line function call
camila314 Feb 12, 2025
048a17d
Fix major multiline bug, move directive beginning into variable
camila314 Feb 12, 2025
7d0a6a4
Modify ObjC tests to match correct scope
camila314 Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 143 additions & 85 deletions C++/C.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ contexts:
scope: constant.other.placeholder.c
string.double:
- meta_scope: string.quoted.double.c
- meta_include_prototype: false
- include: preprocessor.line-escape
- match: \"
scope: punctuation.definition.string.end.c
pop: 1
Expand All @@ -46,6 +48,11 @@ contexts:
scope: punctuation.definition.string.end.c
pop: 1
- include: string.escape
string.angled:
- meta_scope: string.quoted.other.c
- match: \>
scope: punctuation.definition.string.end.c
pop: 1
string:
- match: (L|u8?|U)?(\")
captures:
Expand Down Expand Up @@ -75,7 +82,7 @@ contexts:
- match: ==|!=|<=|>=|<|>
scope: keyword.operator.comparison.c
operator.word:
- match: \b(?:sizeof|_Alignof|alignof|offsetof)\b
- match: \b(?:sizeof|_Alignof|alignof|offsetof|_Static_assert|static_assert|_Pragma)\b
scope: keyword.operator.word.c
operator.ternary:
- match: '\?|\:'
Expand Down Expand Up @@ -118,7 +125,7 @@ contexts:
# decimal floats
- match: |-
(?x:
\b(\d+)
\b(\d(?:\d|'(?=[^']))*)
(?:
( (\.)
(?:
Expand Down Expand Up @@ -160,46 +167,46 @@ contexts:
4: constant.numeric.suffix.c
5: invalid.illegal.numeric.suffix.c
# binary integer (C23)
- match: \b(0[bB])([0-1]*)(?:({{integer_suffix}})|({{bin_suffix}}))?
- match: \b(0[bB])([01](?:[01]|'(?=[^']))*)?(?:({{integer_suffix}})|({{bin_suffix}}))?
camila314 marked this conversation as resolved.
Show resolved Hide resolved
scope: meta.number.integer.binary.c
captures:
1: constant.numeric.base.c
2: constant.numeric.value.c
3: constant.numeric.suffix.c
4: invalid.illegal.numeric.suffix.c
# hexadecimal integer
- match: \b(0[xX])(\h*)(?:({{integer_suffix}})|({{hex_suffix}}))?
- match: \b(0[xX])(\h(?:\h|'(?=[^']))*)?(?:({{integer_suffix}})|({{hex_suffix}}))?
scope: meta.number.integer.hexadecimal.c
captures:
1: constant.numeric.base.c
2: constant.numeric.value.c
3: constant.numeric.suffix.c
4: invalid.illegal.numeric.suffix.c
# octal integer
- match: \b(0)([0-7]+)(?:({{integer_suffix}})|({{oct_suffix}}))?
- match: \b(0)([0-7](?:[0-7]|'(?=[^']))*)(?:({{integer_suffix}})|({{oct_suffix}}))?
scope: meta.number.integer.octal.c
captures:
1: constant.numeric.base.c
2: constant.numeric.value.c
3: constant.numeric.suffix.c
4: invalid.illegal.numeric.suffix.c
# decimal integer
- match: \b(\d+)(?:({{integer_suffix}})|({{dec_suffix}}))?
- match: \b(\d(?:\d|'(?=[^']))*)(?:({{integer_suffix}})|({{dec_suffix}}))?
scope: meta.number.integer.decimal.c
captures:
1: constant.numeric.value.c
2: constant.numeric.suffix.c
3: invalid.illegal.numeric.suffix.c

constant.malformed:
- match: \b(?i:true|false|null|__func__|__cplusplus)\b
- match: \b(?i:true|false|null|nullptr|__cplusplus)\b
scope: invalid.illegal.malformed-constant-language.c
constant:
- match: \b(true|TRUE)\b
camila314 marked this conversation as resolved.
Show resolved Hide resolved
scope: constant.language.boolean.true.c
- match: \b(false|FALSE)\b
scope: constant.language.boolean.false.c
- match: \bNULL\b
- match: \b(NULL|nullptr)\b
scope: constant.language.null.c
- match: \b__func__\b
scope: constant.language.c
Expand All @@ -213,7 +220,7 @@ contexts:
- meta_scope: meta.group.c
- match: \b{{declspec_item}}\b
scope: constant.other.c
- match: \b{{declspec_func}}\s*(\()
- match: \b({{declspec_func}})\s*(\()
captures:
1: storage.modifier.c
2: punctuation.section.group.begin.c
Expand All @@ -239,14 +246,14 @@ contexts:
- include: punctuation.comma
- match: \b{{attribute_item}}\b
scope: constant.other.c
- match: \b{{attribute_func}}\s*(\()
- match: \b({{attribute_func}})\s*(\()
captures:
1: storage.modifier.c
2: punctuation.section.group.begin.c
push: enclosure.parenthesis
- match: \)\)
scope: punctuation.section.group.end.c
pop: 2
pop: 1

keyword.storage:
- match: \bauto\b
Expand Down Expand Up @@ -334,7 +341,6 @@ contexts:

type.enum-body:
- meta_content_scope: meta.block.c
- meta_include_prototype: false
- match: \b{{identifier}}\b
scope: entity.name.constant.c
push:
Expand Down Expand Up @@ -472,6 +478,7 @@ contexts:
- meta_scope: meta.function.c
- match: ;
pop: 1
- include: gnu-attribute
- match: \{
scope: punctuation.section.block.begin
set: function.body
Expand All @@ -490,7 +497,7 @@ contexts:
pop: 1
- include: enclosure

- match: ({{identifier}})\b(?=[,\)]|$)
- match: ({{identifier}})\s*(?=[,\)]|$)
captures:
1: variable.parameter.c
- include: expression
Expand All @@ -505,103 +512,151 @@ contexts:
- match: \b{{identifier}}\b

statement:
- match: (?=\S)
push:
- match: ;
scope: punctuation.terminator.c
pop: 1
- include: expression
- match: ;
scope: punctuation.terminator.c
pop: 1
- include: expression

main:
- include: preprocessor
- include: function
- include: statement
- match: (?=\S)
push: statement

prototype:
- include: preprocessor.comment
- match: \\$
- include: comment
- include: preprocessor

preprocessor.line-escape:
- match: \\$\n
scope: punctuation.separator.continuation.c
push:
- match: ^
pop: 1
preprocessor.include:
- meta_scope: meta.preprocessor.c
- match: \"
scope: punctuation.definition.string.begin.c
push: string.double
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strings are commonly used in various contexts and thus may deserve a dedicated context.

Maybe something like:

Suggested change
- match: \"
scope: punctuation.definition.string.begin.c
push: string.double
- include: string.quoted.double

And in "literal" or "constants" section:

  string.quoted.double:
    - match: \"
      scope: punctuation.definition.string.begin.c
      push: string.quoted.double.body

  string.quoted.double.body:
    - match: \"
      scope: punctuation.definition.string.end.c
      pop: 1
  

- match: \<
scope: punctuation.definition.string.begin.c
push: string.angled
- include: eol-pop
preprocessor.diagnostic:
- meta_scope: meta.preprocessor.diagnostic.c
- meta_content_scope: string.unquoted.c
- include: preprocessor.line-escape
- include: eol-pop

preprocessor.define-body:
- match: ''
set: main
with_prototype:
- match: \b__VA_ARGS__\b
scope: constant.other.c
- match: \b__VA_OPT__\b
scope: keyword.operator.word.c
- match: \#
scope: keyword.operator.c
- include: preprocessor.line-escape
preprocessor.define-params:
- include: eol-pop
- match: \b{{identifier}}\b
scope: variable.parameter.c
- include: punctuation.comma
- include: operator.variadic
- match: \)
scope: punctuation.section.group.end.c
embed: preprocessor.define-body
escape: (?<!\\)\n
- include: else-pop
preprocessor.define:
- include: eol-pop
- match: \s*({{identifier}})\s*(\()
captures:
1: entity.name.macro.c support.macro.c
2: punctuation.section.group.begin.c
set: preprocessor.define-params
- match: \b{{identifier}}\b
scope: entity.name.macro.c support.macro.c
embed: preprocessor.define-body
escape: (?<!\\)\n
preprocessor.embed-enclosure:
- match: \)
scope: punctuation.section.group.end.c
pop: 1
- include: number
- include: operator
- include: punctuation
- include: string
preprocessor.embed:
- match: \"
scope: punctuation.definition.string.begin.c
push: string.double
- match: \<
scope: punctuation.definition.string.begin.c
push: string.angled
- match: \b(?:limit|prefix|suffix|if_empty)\b
scope: keyword.other.c
- match: \(
scope: punctuation.section.group.begin.c
push: preprocessor.embed-enclosure
- include: eol-pop
preprocessor:
- include: preprocessor.line-escape

# Everything after this comment needs to be rewritten so don't pay too much mind
- match: ^\s*#\s*((?:el)?if(?:n?def)?|undef|pragma)\b
scope: keyword.control.import.c
push:
- meta_scope: meta.preprocessor.c
- include: eol-pop

preprocessor:
- include: preprocessor.comment
- match: ^\s*#\s*(?=(if|elif|endif|else|ifndef|ifdef|undef|include(_next)?|define|error|warning|pragma)\b)
- match: ^\s*#\s*(else|endif)\b
scope: keyword.control.import.c
push: preprocessor.directive
preprocessor.directive:
- meta_scope: meta.preprocessor.c

- match: \bifndef|ifdef|if|elif\b
- match: ^\s*#\s*(include|include_next)\b
scope: keyword.control.import.c
set:
- match: ^
pop: 1
push: preprocessor.include

- match: \belse|endif\b
- match: ^\s*#\s*(embed)\b
scope: keyword.control.import.c
pop: 1
push: preprocessor.embed

- match: \b(warning|error)\b
scope: meta.preprocessor.diagnostic.c keyword.control.import.error.c
set:
- meta_content_scope: meta.preprocessor.diagnostic.c string.quoted.c
- include: string
- match: ^\s*#\s*(define)\b
scope: keyword.control.import.c
push: preprocessor.define

- match: ^\s*#\s*(undef)\b
scope: keyword.control.import.c
push:
- meta_scope: meta.preprocessor.c
- include: eol-pop

- match: \binclude(_next)?\b
scope: meta.preprocessor.include.c keyword.control.import.include.c
set:
- meta_content_scope: meta.preprocessor.include.c
- match: <
push:
- meta_scope: string.quoted.c
- match: ">"
pop: 1
- match: \"
push:
- meta_scope: string.quoted.c
- match: \"
pop: 1
- match: ^\s*#\s*(error)\b
scope: keyword.control.import.error.c
push: preprocessor.diagnostic

- match: ^
pop: 1
- match: ^\s*#\s*(warning)\b
scope: keyword.control.import.warning.c
push: preprocessor.diagnostic

- match: \bundef|pragma\b
scope: keyword.control.import.c
set:
- meta_content_scope: meta.preprocessor.c
- match: ^
pop: 1
comment:
- match: //
scope: punctuation.definition.comment.c
push:
- meta_scope: comment.line.double-slash.c
- include: preprocessor.line-escape
- include: eol-pop

- match: \bdefine\b
scope: keyword.control.import.c
set:
- match: ^
pop: 1
preprocessor.comment:
- match: /\*
scope: punctuation.definition.comment.c
push:
- meta_scope: comment.block.c
- match: '\\$\n'
scope: punctuation.separator.continuation.c
- include: preprocessor.line-escape
- match: \*/
scope: punctuation.definition.comment.c
pop: 1
- match: //
scope: punctuation.definition.comment.c
push:
- meta_scope: comment.line.double-slash.c
- match: '(\\)$\n'
captures:
1: punctuation.separator.continuation.c
pop: 1
- match: (?=\n)
pop: 1
- match: \*/
scope: invalid.illegal.stray-comment-end.c

# Helpers

Expand All @@ -614,6 +669,9 @@ contexts:
eol-pop:
- match: $\n?
pop: 1
consume-pop:
camila314 marked this conversation as resolved.
Show resolved Hide resolved
- match: .
pop: 1

variables:
identifier: \b[[:alpha:]_][[:alnum:]_]*\b
Expand All @@ -629,7 +687,7 @@ variables:
dec_exponent: '(?:[eE][-+]?\d*)'
hex_exponent: '(?:[pP][-+]?\d*)'

declspec_item: '(allocator|appdomain|deprecated|dllimport|dllexport|empty_bases|hybrid_patchable|jitintrinsic|naked|noalias|noinline|noreturn|nothrow|novtable|no_sanitize_address|process|restrict|safebuffers|selectany|thread)'
declspec_func: '(align|allocate|code_seg|spectre|uuid|deprecated)'
attribute_item: '(noreturn|pure|const|malloc|cold|hot|deprecated|always_inline|noinline|packed|unused|used|leaf|artificial|flatten|noclone|returns_twice|weak|no_reorder|minsize|externally_visible|constructor|destructor)'
attribute_func: '(aligned|format|nonnull|section|visibility|alias|ifunc|target|alloc_size|cleanup|warning|error|cold_hot_partitioning|no_sanitize|optimize)'
declspec_item: '(?:allocator|appdomain|deprecated|dllimport|dllexport|empty_bases|hybrid_patchable|jitintrinsic|naked|noalias|noinline|noreturn|nothrow|novtable|no_sanitize_address|process|restrict|safebuffers|selectany|thread)'
declspec_func: '(?:align|allocate|code_seg|spectre|uuid|deprecated)'
attribute_item: '(?:noreturn|pure|const|malloc|cold|hot|deprecated|always_inline|noinline|packed|unused|used|leaf|artificial|flatten|noclone|returns_twice|weak|no_reorder|minsize|externally_visible|constructor|destructor)'

Loading
Loading