Imaginary preprocessor language

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.

1. 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)

2. Control sequences

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

2.1. 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

2.2. Operator declarations

2.2.1. Substitution operator

type

“sub”

text

string for replacement

2.2.2. Switcher operator

type

“mode”

text

pair of strings, represented by array, list or tuple

2.2.3. 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

3. Templates

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

4. Issues

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

Appendix A. 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