aboutsummaryrefslogtreecommitdiff
path: root/core/text.nom
blob: 840a596bffc63e156f4f93aea0770bf27b76edeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#
    This file contains some definitions of text escape sequences, including ANSI console
    color codes.

use "core/metaprogramming.nom"

# Text functions
action [%texts joined with %glue]
    lua> ".."
        local text_bits = {}
        for i,bit in ipairs(\%texts) do text_bits[i] = stringify(bit) end
        return table.concat(text_bits, \%glue)
parse [joined %texts, %texts joined] as: %texts joined with ""

compile [capitalized %text, %text capitalized] to
    Lua value "((\(%text as lua expr)):gsub('%l', string.upper, 1))"

compile [%text with %sub instead of %patt, %text with %patt replaced by %sub, %text s/%patt / %sub] to
    Lua value "((\(%text as lua expr)):gsub(\(%patt as lua expr), \(%sub as lua expr)))"

action [lines in %text, lines of %text]
    lua> ".."
        local result = list{}
        for line in (\%text):gmatch('[^\n]+') do
            result[#result+1] = line
        end
        return result

# Text literals
lua> ".."
    do
        local escapes = {
            nl="\\\\n", newline="\\\\n", tab="\\\\t", bell="\\\\a", cr="\\\\r", ["carriage return"]="\\\\r",
            backspace="\\\\b", ["form feed"]="\\\\f", formfeed="\\\\f", ["vertical tab"]="\\\\v",
        };
        for name, e in pairs(escapes) do
            local lua = "'"..e.."'"
            nomsu.COMPILE_ACTIONS[name] = (function(nomsu, tree) return LuaCode.Value(tree.source, lua) end)
        end
    end