aboutsummaryrefslogtreecommitdiff
path: root/core/text.nom
blob: 43739828bc7d7b53a3dd03fa3e9c8da7dfc4ab2a (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env nomsu -V2.5.5.4
#
    This file contains some definitions of text escape sequences, including ANSI console
    color codes.

use "core/metaprogramming.nom"

test:
    assume (("x" + "y") == "xy")
    %s = "list:\[1, 2, 3]"
    assume (%s == "list:[1, 2, 3]")
    assume ("foo = \(1 + 2)!" == "foo = 3!")
    assume (..)
        ".."
            one
            two
        ..== "one\ntwo"
    assume (..)
        ".."
            no\
            ..gap
        ..== "nogap"
    parse [アクション %spec %body] as (action %spec %body)

test:
    %こんにちは = "こんにちは"
    アクション [% と言う] "\(%)世界"
    assume ((%こんにちは と言う) == "こんにちは世界") or barf ".."
        Unicode doesn't work

# Text functions
test:
    assume ((["x", "y"] joined with ",") == "x,y") or barf "joined with failed"
    assume ((["x", "y"] joined) == "xy") or barf "joined failed"

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 "")
test:
    assume (("asdf" capitalized) == "Asdf") or barf "capitalized failed"

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

test:
    assume (("asdf" with "X" instead of "s") == "aXdf") or barf ".."
        substitution failed

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

test:
    assume (..)
        (..)
            lines in ".."
                one
                two
        ..== ["one", "two"]

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 in %text matching %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 in %text matching %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)()

compile [%text matches %pattern] to (..)
    Lua value ".."
        (\(%text as lua expr):match(\(%pattern as lua expr)) and true or false)

compile [%text matching %pattern] to (..)
    Lua value "\(%text as lua expr):match(\(%pattern as lua expr))"

test:
    assume ("\n" == (newline)) or barf "Text literals failed."

# 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