c
The community/lang/c
directory contains files that provide support for the C programming language in Talon.
The c.talon
file defines the core grammar for C. It includes:
* Tags for identifying C code, such as user.code_imperative
, user.code_block_c_like
, user.code_comment_line
, etc.
* Settings for formatting C code, such as user.code_private_function_formatter
, user.code_public_variable_formatter
, etc.
* State commands for common C preprocessor directives such as #include
, typedef
, #define
, #ifdef
, #if
, #error
, #pragma
, etc.
* A push command to insert braces.
* Commands for declaring variables with types and names, such as <user.c_variable> <phrase>
.
* Commands for casting, types, keywords, and standard integer types like cast to <user.c_cast>
, standard cast to <user.stdint_cast>
, <user.c_types>
, <user.c_pointers>
, <user.c_keywords>
, <user.c_signed>
, standard <user.stdint_types>
.
* A command int main
to insert int main()
.
* A command include <user.code_libraries>
to insert a library include with the chosen library.
The c.py
file contains the Python code that supports the grammar defined in c.talon
. It:
* Defines a Talon Context that matches when code.language
is c
.
* Defines lists for common C elements, such as:
* self.c_pointers
: *
, **
* self.stdint_signed
: signed
, unsigned
* self.c_signed
: signed
, unsigned
* self.c_keywords
: static
, volatile
, register
* self.stdint_types
: int8_t
, int16_t
, int32_t
, int64_t
, void
, double
, etc.
* self.c_types
: char
, short
, long
, int
, void
, double
, etc.
* user.code_libraries
: assert.h
, ctype.h
, errno.h
, math.h
, stdio.h
, stdlib.h
, string.h
, etc.
* user.code_common_function
: memcpy
, memset
, strcat
, strcmp
, strlen
, printf
, malloc
, free
, etc.
* Defines captures for those lists, such as c_pointers
, c_signed
, c_keywords
, c_types
, and stdint_types
.
* Defines captures for C casts, such as c_cast
and stdint_cast
.
* Defines a capture for variables using the previously defined captures, c_variable
.
* Implements actions for inserting common C operators such as code_operator_indirection
, code_operator_address_of
, code_operator_structure_dereference
, code_operator_subscript
, code_operator_assignment
, and many more.
* Implements actions for inserting control flow statements such as code_state_if
, code_state_else
, code_state_switch
, code_state_for
, code_state_while
, code_state_return
, etc.
* Implements actions for inserting code snippets, including: code_insert_null
, code_insert_is_null
, code_insert_is_not_null
, code_comment_line_prefix
, code_insert_function
, code_private_function
, code_private_static_function
, and code_insert_library
.
Together, c.talon
and c.py
provide a comprehensive set of tools for writing C code with Talon. c.talon
defines the grammar and c.py
implements the logic and provides the necessary data.