Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Rewrite functions, again #214

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 46 additions & 5 deletions grammars/c++.cson
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@
]
'firstLineMatch': '(?i)-\\*-[^*]*(Mode:\\s*)?C\\+\\+(\\s*;.*?)?\\s*-\\*-'
'name': 'C++'
'injections':
'source.cpp meta.function.c meta.function.return-type.c':
'patterns': [
{
'include': '#storage_types'
}
]
'source.cpp meta.function.c - meta.function.body.c':
'patterns': [
{
'match': '(operator([-*&<>=+!]+|\\(\\)|\\[\\]))(?=\\s*\\()'
'captures':
'1':
'name': 'entity.name.function.cpp'
'2':
'name': 'keyword.operator.c'
}
{
'begin': '(?=[A-Za-z_]\\w*\\s+(operator([-*&<>=+!]+|\\(\\)|\\[\\]))\\s*\\()'
'end': '(?!\\G)'
'name': 'meta.function.return-type.c'
'patterns': [
{
'include': '#storage_types'
}
]
}
]
'patterns': [
{
'include': '#special_block'
Expand Down Expand Up @@ -65,15 +93,15 @@
'name': 'keyword.operator.cast.cpp'
}
{
'match': '\\b(and|and_eq|bitand|bitor|compl|not|not_eq|or|or_eq|typeid|xor|xor_eq|alignof|alignas)\\b'
'name': 'keyword.operator.cpp'
'match': '\\bdecltype\\b'
'name': 'keyword.operator.decltype.cpp'
}
{
'match': '\\b(class|decltype|wchar_t|char16_t|char32_t)\\b'
'name': 'storage.type.cpp'
'match': '\\b(and|and_eq|bitand|bitor|compl|not|not_eq|or|or_eq|typeid|xor|xor_eq|alignof|alignas)\\b'
'name': 'keyword.operator.cpp'
}
{
'match': '\\b(constexpr|export|mutable|typename|thread_local)\\b'
'match': '\\b(class|constexpr|export|mutable|typename|thread_local)\\b'
'name': 'storage.modifier.cpp'
}
{
Expand Down Expand Up @@ -129,6 +157,9 @@
{
'include': 'source.c'
}
{
'include': '#storage_types'
}
]
'repository':
'angle_brackets':
Expand Down Expand Up @@ -364,6 +395,16 @@
]
}
]
'storage_types':
'patterns': [
{
'match': '\\b(wchar_t|char16_t|char32_t)\\b'
'name': 'storage.type.cpp'
}
{
'include': 'source.c#storage_types'
}
]
'strings':
'patterns': [
{
Expand Down
211 changes: 127 additions & 84 deletions grammars/c.cson
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
'match': '\\b(break|case|continue|default|do|else|for|goto|if|_Pragma|return|switch|while)\\b'
'name': 'keyword.control.c'
}
{
'include': '#storage_types'
}
{
'match': '\\b(const|extern|register|restrict|static|volatile|inline)\\b'
'name': 'storage.modifier.c'
Expand Down Expand Up @@ -218,6 +215,50 @@
}
]
}
{
'include': '#block'
}
{
'include': '#parens'
}
{
# FIRST CAPTURE meta.function.c scope (provides an injectable scope, balanced parentheses and prevents unnecessary scope nesting)
# TODO: Try to make this C-only
'begin': '''(?x)
(?=
[A-Za-z_]\\w* # Type modifier
\\s+
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do not require a return type for functions.
That will break:

  • main() (with no int) convention (before C99)
    (which works at the moment as below)
main() {
  /* code */
  return 0;
}
A_Macro()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. Then I think this will become trickier, because I've mostly been relying on the return type to figure out if it's a function or a function call.

Copy link
Contributor

@alpyre alpyre Mar 8, 2017

Choose a reason for hiding this comment

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

Here are some tips for you:

  • function patterns at $base are functions (which means they are either func.like macros or function definitions or forward declarations).
  • function patterns inside a block are function-calls
  • function patterns inside a block with return types are function initializations.

You should go according to these.

(
([A-Za-z_]\\w*+|::)++ # Actual name
|
(operator([-*&<>=+!]+|\\(\\)|\\[\\])) # Operator overloading (for C++)
)
\\s*\\(
)
'''
'end': '(?<=})|(?=;)'
'name': 'meta.function.c'
'patterns': [
{
'include': '#function-body'
}
{
'begin': '\\G'
'end': '(?<=\\))'
'patterns': [
{
'include': '#function-innards'
}
]
}
{
'include': '#comments'
}
]
}
{
'include': '#function-call'
}
Copy link
Contributor

Choose a reason for hiding this comment

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

There can be no function calls here at $base (only func. declarations or definitions can).
Shouldn't this be moved back in to block_innards?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was mostly for testing purposes so that I didn't have to constantly create the necessary scaffolding.

{
'match': '\\b(u_char|u_short|u_int|u_long|ushort|uint|u_quad_t|quad_t|qaddr_t|caddr_t|daddr_t|div_t|dev_t|fixpt_t|blkcnt_t|blksize_t|gid_t|in_addr_t|in_port_t|ino_t|key_t|mode_t|nlink_t|id_t|pid_t|off_t|segsz_t|swblk_t|uid_t|id_t|clock_t|size_t|ssize_t|time_t|useconds_t|suseconds_t)\\b'
'name': 'support.type.sys-types.c'
Expand Down Expand Up @@ -258,28 +299,7 @@
'name': 'support.type.posix-reserved.c'
}
{
'include': '#block'
}
{
'include': '#parens'
}
{
# FIRST CAPTURE meta.function.c scope (provides an injectable scope, balanced parentheses and prevents unnecessary scope nesting)
'begin': '''(?x)
(?!(?:while|for|do|if|else|switch|catch|enumerate|return|sizeof|[cr]?iterate|asm|__asm__|auto|bool|_Bool|char|_Complex|double|enum|float|_Imaginary|int|long|short|signed|struct|typedef|union|unsigned|void)\\s*\\()
(?=
(?:[A-Za-z_][A-Za-z0-9_]*+|::)++\\s*\\( # actual name
|
(?:(?<=operator)(?:[-*&<>=+!]+|\\(\\)|\\[\\]))\\s*\\(
)
'''
'end': '(?<=\\))(?!\\w)'
'name': 'meta.function.c'
'patterns': [
{
'include': '#function-innards'
}
]
'include': '#storage_types'
}
{
'include': '#line_continuation_character'
Expand Down Expand Up @@ -311,6 +331,21 @@
'4':
'name': 'variable.other.member.c'
'match': '((\\.)|(->))\\s*(([a-zA-Z_][a-zA-Z_0-9]*)\\b(?!\\s*\\())?'
'arguments':
'begin': '\\('
'beginCaptures':
'0':
'name': 'punctuation.definition.arguments.begin.bracket.round.c'
'end': '\\)'
'endCaptures':
'0':
'name': 'punctuation.definition.arguments.end.bracket.round.c'
'name': 'meta.arguments.c'
'patterns': [
{
'include': '$base'
}
]
'block':
'patterns': [
{
Expand Down Expand Up @@ -347,9 +382,6 @@
{
'include': '#libc'
}
{
'include': '#c_function_call'
}
{
'captures':
'1':
Expand Down Expand Up @@ -395,21 +427,17 @@
'include': '$base'
}
]
'c_function_call':
'function-call':
# FIRST CAPTURE meta.function-call.c scope (provides an injectable scope, balanced parentheses and prevents unnecessary scope nesting)
'begin': '''(?x)
(?!(?:while|for|do|if|else|switch|catch|enumerate|return|sizeof|[cr]?iterate)\\s*\\()
(?=
(?:[A-Za-z_][A-Za-z0-9_]*+|::)++\\s*\\( # actual name
|
(?:(?<=operator)(?:[-*&<>=+!]+|\\(\\)|\\[\\]))\\s*\\(
)
'''
'end': '(?<=\\))(?!\\w)'
'begin': '([A-Za-z_]\\w*)(?=\\s*\\()'
'beginCaptures':
'1':
'name': 'entity.name.function.c'
'end': '(?<=\\))'
'name': 'meta.function-call.c'
'patterns': [
{
'include': '#function-call-innards'
'include': '#arguments'
}
]
'comments':
Expand Down Expand Up @@ -526,6 +554,40 @@
'name': 'constant.numeric.c'
}
]
'parameters':
'patterns': [
{
'begin': '\\('
'beginCaptures':
'0':
'name': 'punctuation.definition.parameters.begin.bracket.round.c'
'end': '\\)'
'endCaptures':
'0':
'name': 'punctuation.definition.parameters.end.bracket.round.c'
'name': 'meta.parameters.c'
'patterns': [
{
'include': '#vararg_ellipses'
}
{
'match': '\\b(const)\\b'
'name': 'storage.modifier.c'
}
{
'include': '#storage_types'
}
{
'match': '[A-Za-z_]\\w*'
'name': 'variable.parameter.c'
}
{
'match': ','
'name': 'punctuation.separator.delimiter.c'
}
]
}
]
'parens':
'begin': '\\('
'beginCaptures':
Expand Down Expand Up @@ -629,10 +691,7 @@
'include': '#libc'
}
{
'include': '#c_function_call'
}
{
'include': '$self'
'include': '$base'
}
]
}
Expand Down Expand Up @@ -1679,63 +1738,47 @@
'include': '#preprocessor-rule-define-line-contents'
}
]
'function-innards':
'function-body':
'patterns': [
{
'include': '#comments'
}
{
'include': '#storage_types'
}
{
'include': '#operators'
}
{
'include': '#vararg_ellipses'
}
{
'begin': '''(?x)
(?!(?:while|for|do|if|else|switch|catch|enumerate|return|sizeof|[cr]?iterate)\\s*\\()
(
(?:[A-Za-z_][A-Za-z0-9_]*+|::)++ # actual name
|
(?:(?<=operator)(?:[-*&<>=+!]+|\\(\\)|\\[\\]))
)
\\s*(\\()
'''
'begin': '{'
'beginCaptures':
'1':
'name': 'entity.name.function.c'
'2':
'name': 'punctuation.section.parameters.begin.bracket.round.c'
'end': '\\)'
'0':
'name': 'punctuation.definition.function.begin.bracket.curly.c'
'end': '}'
'endCaptures':
'0':
'name': 'punctuation.section.parameters.end.bracket.round.c'
'name': 'punctuation.definition.function.end.bracket.curly.c'
'contentName': 'meta.function.body.c'
'patterns': [
{
'include': '#function-innards'
'include': '#block_innards'
}
]
}
]
'function-innards':
'patterns': [
{
'begin': '\\('
'beginCaptures':
'0':
'name': 'punctuation.section.parens.begin.bracket.round.c'
'end': '\\)'
'endCaptures':
'0':
'name': 'punctuation.section.parens.end.bracket.round.c'
'include': '#comments'
}
{
'match': '[A-Za-z_]\\w*(?=\\s*\\()'
'name': 'entity.name.function.c'
}
{
'include': '#parameters'
}
{
'begin': '(?=[A-Za-z_]\\w*\\s+[A-Za-z_]\\w*\\s*\\()'
'end': '(?!\\G)'
'name': 'meta.function.return-type.c'
'patterns': [
{
'include': '#function-innards'
'include': '#storage_types'
}
]
}
{
'include': '$base'
}
]
'function-call-innards':
'patterns': [
Expand Down
Loading