From 505fec2a61d2571317cc4bbc36ec0f4822a63f9d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 2 Feb 2018 15:48:28 -0800 Subject: Restructured the nomsu files to group all the essentials into core/ and all the optionals into lib/. lib/core.nom and tests/all.nom are no longer needed now. --- core/text.nom | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 core/text.nom (limited to 'core/text.nom') diff --git a/core/text.nom b/core/text.nom new file mode 100644 index 0000000..8213988 --- /dev/null +++ b/core/text.nom @@ -0,0 +1,56 @@ +#.. + 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 {..} + expr:"((\(%text as lua expr)):gsub('%l', string.upper, 1))" + +compile [%text with %sub instead of %patt, %text s/%patt/%sub] to {..} + expr:"((\(%text as lua expr)):gsub(\(%patt as lua expr), \(%sub as lua expr)))" + +# TODO: figure out whether indent/dedent should affect first line +compile [indented %text, %text indented] to {expr:"((\%text):gsub('\\n','\\n'..(' ')))"} +compile [dedented %obj, %obj dedented] to {expr:"nomsu:dedent(\(%obj as lua expr))"} +compile [%text indented %n times] to {expr:"((\%text):gsub('\\n','\\n'..(' '):rep(\%n)))"} + +# 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:define_compile_action(name, \(!! code location !!), function() return {expr=lua}; end); + end + local colors = { + ["reset color"]="\\\\27[0m", bright="\\\\27[1m", dim="\\\\27[2m", underscore="\\\\27[4m", + blink="\\\\27[5m", inverse="\\\\27[7m", hidden="\\\\27[8m", + + black="\\\\27[30m", red="\\\\27[31m", green="\\\\27[32m", yellow="\\\\27[33m", blue="\\\\27[34m", + magenta="\\\\27[35m", cyan="\\\\27[36m", white="\\\\27[37m", + + ["on black"]="\\\\27[40m", ["on red"]="\\\\27[41m", ["on green"]="\\\\27[42m", ["on yellow"]="\\\\27[43m", + ["on blue"]="\\\\27[44m", ["on magenta"]="\\\\27[45m", ["on cyan"]="\\\\27[46m", ["on white"]="\\\\27[47m", + }; + for name, c in pairs(colors) do + local color = "'"..c.."'"; + local reset = "'"..colors["reset color"].."'"; + nomsu:define_compile_action(name, \(!! code location !!), function() return {expr=color}; end); + nomsu:define_compile_action(name.." %", \(!! code location !!), function(\%) + return {expr=color..".."..nomsu:tree_to_lua(\%).expr..".."..reset}; + end); + end + end + -- cgit v1.2.3