Skip to content

cpp

This directory provides robust support for editing C++ code using voice commands. It defines structured vocabularies for standard library components, native C++ keywords, namespace resolution, and complex type formatting rules.

These components coordinate with Talon's programming language features to enable efficient typing of variables, nested namespaces, templates, pointers, references, and system header inclusions.

Core Architecture and Syntax Resolution

The behavior is driven by two main files that implement C++ grammar parsing and command mappings:

  • cpp.py: The underlying Python logic. It establishes the code.language: cpp context, declares Talon lists and captures, and overrides default user actions.
  • cpp.talon: The user-facing voice command mappings. It activates when editing C++ files, enabling specific syntax structures like headers, variables, namespaces, and C++-specific keywords.

Advanced Type and Pointer Formatting

One of the primary challenges of writing C++ by voice is managing pointers (*), references (&), arrays ([]), and their associated whitespace. In cpp.py, the format_type() helper handles these distinct rules depending on how they are used:

  • Stand-alone Types: Using <user.code_type> outputs types with pointer annotations flush against the type (e.g., int*).
  • Variable Declarations: The command var <user.variable_type> <user.text> in cpp.talon parses spacing specifically for declarations (e.g., "var short pointer reference high score" formats to short* &high_score).

This logic uses pointer and reference keywords registered in cpp_pointers.talon-list.

C++ Standard Library Prefixing

Talon captures phrases combining a prefix (such as "stood" or "standard") with standard types, functions, or constants to automatically output them in their scoped (std::) form.

For instance, cpp.py implements three captures to handle standard library mappings: * cpp_standard_type: Combines cpp_standard_prefix.talon-list and cpp_standard_type.talon-list (e.g., "stood vector" becomes std::vector). * cpp_standard_function: Combines prefixes with cpp_standard_function.talon-list (e.g., "stood make unique" inserts std::make_unique). * cpp_standard_constant: Combines prefixes with cpp_standard_constant.talon-list (e.g., "stood see out" yields std::cout).

Vocabularies and Lists

A large set of .talon-list files provides targeted vocabularies for language structures, avoiding collision with other active languages:

Action Implementations

Inside cpp.py, standard programming actions are overridden in the UserActions class to emit valid C++ syntax:

  • Booleans & Null: Maps true/false to true/false, and null actions to nullptr.
  • Object Accessors: Maps the generic object accessor to . and self accessors to this->.
  • Operators: Incorporates the base operators defined under the common C programming interface.