Skip to content
 
 

Repository files navigation

tree-sitter-processing-cpp

A tree-sitter grammar for Processing (C++ mode).pde sketches that target processing.cpp.

Extends tree-sitter-cpp with Processing-specific syntax.


What this adds over tree-sitter-cpp

Feature Details
color pseudo-type First-class type keyword, also usable as a variable name
Processing types PVector, PImage, PGraphics, PFont, PShape, PShader, ArrayList, HashMap, and more — tagged as processing_type nodes
Processing built-in variables width, height, mouseX, mouseY, frameCount, etc. — tagged as processing_builtin_variable
Processing constants PI, TWO_PI, HALF_PI, P2D, P3D, LEFT, RIGHT, CENTER, etc. — tagged as processing_constant
Optional class trailing ; class Ball { ... } without a semicolon is valid (matches real .pde files)
.pde file type Grammar scope is source.pde, injection regex matches pde and processing
Lifecycle function highlighting setup, draw, mousePressed, etc. highlighted as @function.builtin
Full query suite highlights.scm, locals.scm, tags.scm tuned for Processing sketches

Setup (fork on GitHub)

  1. Fork this repo on GitHub under your account.
  2. Clone your fork locally:
    git clone https://github.com/YOUR_USERNAME/tree-sitter-processing-cpp.git
    cd tree-sitter-processing-cpp
  3. Install dependencies:
    npm install
  4. Generate the parser from the grammar:
    npx tree-sitter generate
  5. Run the corpus tests:
    npx tree-sitter test

Testing a sketch file

npx tree-sitter parse path/to/your/sketch.pde

Play with the grammar in the browser:

npm start   # opens tree-sitter playground at localhost:port

Integrating with the Processing IDE (CppMode)

The Processing IDE (CppMode) uses this grammar for:

  • Syntax highlighting — via queries/highlights.scm
  • Go-to-definition / symbol outline — via queries/tags.scm
  • Variable scope tracking — via queries/locals.scm

In CppMode.java, load the grammar via the tree-sitter Java bindings:

// Using the tree-sitter Java bindings (ch.usi.si.seart:java-tree-sitter or similar)
Language processingCpp = Language.load("tree_sitter_processing_cpp");
Parser parser = new Parser();
parser.setLanguage(processingCpp);
Tree tree = parser.parseString(sketchSource);

The parser is built as a .wasm for the IDE's web-based playground:

npx tree-sitter build --wasm

Grammar design notes

color as a type and a name

color is added to primitive_type, matching the Java parser's PSEUDO_TYPE_KEYWORDS_USABLE_AS_NAMES set. This means color c = red; and Vec3 color; (using color as a field name) both parse correctly.

Optional trailing semicolon after class/struct body

Real .pde files regularly write class Ball { ... } without the trailing ; that strict C++ requires. This grammar overrides _empty_declaration to accept a class/struct/union specifier with a body and no terminating semicolon. Forward declarations (struct Foo;) still require the semicolon.

Processing built-ins as named nodes

processing_builtin_variable, processing_constant, and processing_type are distinct node types (not just identifier aliases) so query authors can match them precisely without fragile string-matching against identifier text.


Upstream

Based on tree-sitter-cpp — MIT licensed. Processing extensions by Jose Gonzalez Llamas.

About

Processing for C++ grammar for tree-sitter

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages