aboutsummaryrefslogtreecommitdiff
path: root/core/text.nom
blob: da9d550b5ffd54d41f65200043f23c3916eadd88 (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
#!/usr/bin/env nomsu -V3.8.7.6
#
    This file contains some definitions of text escape sequences, including ANSI console
    color codes.

use "core/metaprogramming.nom"

test:
    assume "\[1, 2, 3]" == "[1, 2, 3]"
    assume "foo = \(1 + 2)!" == "foo = 3!"
    assume (..)
        ".."
            one
            two
        ..== "one\ntwo"
    assume (..)
        ".."
            no\
            ..gap
        ..== "nogap"
    assume (["x", "y"]::joined with ",") == "x,y"
    assume (["x", "y"]::joined) == "xy"
    assume ("BAR"::byte 2) == 65
    assume ("BAR"::bytes 1 to 2) == [66, 65]
    assume ("asdf"::capitalized) == "Asdf"
    assume ("asdf"::uppercase) == "ASDF"
    assume ("asdf"::with "s" -> "X") == "aXdf"
    assume ("one\ntwo\n"::lines) == ["one", "two", ""]
    parse [アクション %spec %body] as (action %spec %body)
test:
    %こんにちは = "こんにちは"
    アクション [% と言う] "\(%)世界"
    assume (%こんにちは と言う) == "こんにちは世界"

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

test:
    assume "\n" == (newline)

# 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.."'"
            COMPILE_ACTIONS[name] = function(nomsu, tree)
                return LuaCode.Value(tree.source, lua)
            end
        end
    end