How compilation works

The compiler reads a .cherri file, builds the full source, turns it into tokens, turns the tokens into Shortcut actions, and writes the result as a signed Shortcut.

Table of contents

  1. Pipeline
  2. Building the full source
  3. Tokens to actions

Pipeline

flowchart TD
    source[".cherri file"] --> read["Read the file<br/>handleFile"]
    read --> pre["Build the full source<br/>preParse"]
    pre --> parse["Turn characters into tokens<br/>parse"]
    parse --> gen["Turn tokens into Shortcut actions<br/>generateShortcut"]
    gen --> write["Encode the property list<br/>createShortcut"]
    write --> skip{"--skip-sign?"}
    skip -- yes --> unsigned["NAME_unsigned.shortcut"]
    skip -- no --> sign["Sign the Shortcut"]
    sign --> signed["NAME.shortcut"]
Stage Function File What it does
Read handleFile() main.go Reads the file and sets the Shortcut name from the file name.
Build the full source preParse() parser.go Resolves packages and includes, then expands imports, copy and paste, action definitions and functions.
Parse parse() parser.go Reads the source one character at a time and collects tokens. See Parsing.
Generate generateShortcut() shortcutgen.go Fills in the Shortcut header, then generates actions from the tokens.
Write createShortcut() output.go Encodes the Shortcut as a property list and signs it. See Signing.

Building the full source

preParse() rewrites the file contents before any parsing happens, in this order:

  1. Install and include packages, if there is an info.plist in the current directory.
  2. Add #include 'actions/basic' to the top.
  3. Resolve every #include.
  4. Collect import actions.
  5. Expand copy and paste macros.
  6. Collect action definitions.
  7. Process functions.

With --debug, the result is written to NAME_processed.cherri.

Tokens to actions

generateActions() walks the tokens in order and creates the actions for each one. Variables and control flow are tokens like any other, so they produce actions too.

Token Generates
Variable, +=, -=, *=, /= Variable actions
Action call The action
repeat A repeat action group
for A repeat with each action group
menu and item A menu action group
if A conditional action group
Comment A comment action, only with --comments

The property list itself is covered in Property List Generation.


Copyright ©. Distributed under the GPL-2.0 License. Siri Shortcuts and Mac are registered trademarks of Apple Inc. Apple is not involved in this project in any way. Do not contact Apple Support unless you are having an issue with the Shortcuts app itself.