Imaginary Preprocessor Language 0.1

This document describes imaginary preprocessor/markup language inspired by ANSI escape sequences. The language implementation should contain two parts: basic syntax parser (defined by this specification) and rules set, declaring operators that define exact behaviour.

Special characters

\ - escape control characters and "stop character"s when used with templated substitution operator (see 2.1)
^ - control sequence prefix
* - used as substitution character in templates (see 3)

Control sequences

Control sequence is a caret(^) and single character representing operator's identifier

Operator types

Substitution
control sequence will be replaced with some string
Switcher
paired occurences of control sequences will be replaced by corresponding pair of strings
Templated substitution
text fragment cut from control sequence to first "stop character" occurence will be substituted using template
Filtered substitution
text fragment cut from control sequence to first "stop character" occurence will be replaced by result of function

Operator declarations

Substitution operator

type
"sub"
text
string for replacement

Switcher operator

type
"mode"
text
pair of strings, represented by array, list or tuple

Templated/filtered substitution

type
"template"
stop
stop character or string containing all of them
text
(only for templated substitution) template text
filter
(only for filtered substitution) filter function

Templates

Template is string used for templated substitution where substitution character will be replaced by text fragment

Issues

This language implies one parsing iteration therefore there is no ability to nest markup in templated substitutions

Examples

 Original text: this is ^S
 Rule:          "S": { "type": "sub", "text": "Sample text" }
 Result:        this is Sample text
 
 Original text: this is ^bemphasized^b HTML text
 Rule:          "b": { "type": "mode", "text": ["<b>", "</b>"] }
 Result:        this is <b>emphasized</b> HTML text
 
 Original text: go to ^ahttps://neocities.org
 Rule:          "a": { "type": "template", "stop": " \n", "text": "<a href='*'>*</a>" }
 Result:        go to <a href='https://neocities.org'>https://neocities.org</a>
 
 Original text: 1234d is 0x^H1234
 Rule:          "H": { "type": "template", "stop": " \n", "filter": function(x){ return parseInt(x).toString(16).toUpperCase(); } }
 Result:        1234d is 0x4D2