aboutsummaryrefslogtreecommitdiff
path: root/core/text.nom
blob: 37b80823ea4e2cf393a97efbca4c1331256a51a3 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#
    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

compile [for %match where %text matches %patt %body] to
    Lua ".."
        for \(%match as lua expr) in \(%text as lua expr):gmatch(\(%patt as lua expr)) do
            \(%body as lua statements)
            \(compile as: === next %match ===)
        end
        \(compile as: === stop %match ===)

compile [%expr for %match where %text matches %patt] to
    Lua value ".."
        (function()
            local ret = list{}
            for \(%match as lua expr) in \(%text as lua expr):gmatch(\(%patt as lua expr)) do
                ret[#ret+1] = \(%expr as lua statements)
            end
            return ret
        end)()

# 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