Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Parseltongue Syntax

Casey Brooks edited this page Sep 13, 2016 · 5 revisions

This is a clog:

#{ $1 | repeat(3) | uppercase }

Isn't it cute? It matches the following regex:

(#\{\s*(\$\d+|@\d+|'.*'|\d+|-?\d+(.\d+)|true|false)(\s*\|\s*\w+(\(\s*(\$\d+|@\d+|'.*'|\d+|-?\d+(.\d+)|true|false)?(\s*,\s*(\$\d+|@\d+|'.*'|\d+|-?\d+(.\d+)|true|false)\s*)*\s*\))?)*\s*\})

It is called from your code like this:

Parseltongue pt = new Parseltongue();
pt.format("#{ $1 | repeat(3) | uppercase }", "Yo");
// prints "YO YO YO"

It looks complicated, but it's pretty simple: A clog has two basic components wrapped inside #{ }:

  1. Parameters - One of the following (matches regex \$\d+|@\d+|'.*'|\d+|-?\d+(.\d+)|true|false)
  2. A reference to one of the parameters passed to the string formatter as varargs. This is a 1-indexed number preceded by a dollarsign - $1
  3. A reference to the result of one of the previous clogs. This is a 1-indexed number preceded by an 'at' symbol - @1
  4. A literal string, enclosed in single quotes 'hello world'
  5. A literal integer - 12
  6. A literal double - -0.3
  7. A literal boolean - true or false`
  8. Formatters - Like a function call - repeat(3)
  9. Formatter names are case sensitive
  10. Formatters accept a primary object and an optional list of parameters separated by commas. The formatter takes the primary object and manipulates it in some way, outputting a new object.
  11. Formatters can be chained in a pipeline. The output of one formatter becomes the input of the next formatter in the pipeline.

The entire clog works like this:

  1. First, take an initial object. In the example above, that is $1.
  2. If there are no formatters, call .toString() and replace the clog with the result.
  3. If there are formatters, pass the object to the first, along with the parameters listed. The result of that formatter becomes the input of the next, until all formatters have run. Then call .toString() and replace the clog with the result.
Clone this wiki locally