commit 67d780553957a30bb3ec6ac12aaf192932acf81e
parent 7a56fe02cce8159568a870508811723bcec66780
Author: Robert Russell <robert@rr3.xyz>
Date: Tue, 24 Dec 2024 19:59:10 -0800
Add glsl lexer to vis config
Might be incomplete.
Diffstat:
2 files changed, 242 insertions(+), 0 deletions(-)
diff --git a/vis/lexers/glsl.lua b/vis/lexers/glsl.lua
@@ -0,0 +1,241 @@
+local M = {_NAME = "ansi_c"}
+
+local l = require("lexer")
+local P, R, S = lpeg.P, lpeg.R, lpeg.S
+local T = l.token
+local any = P(1)
+local oct = R"07"
+local dec = R"09"
+local hex = R("09", "AF", "af")
+local alpha = R("AZ", "az")
+local letter = alpha + dec + "_"
+local word = (alpha + P"_") * letter^0
+local pm = S"+-"
+local hws = S"\t "
+local vws = S"\n\r"
+local ws = hws + vws
+local function I(s) -- Case-insensitive string match
+ local p = P(true)
+ for i = 1, #s do
+ local c = s:sub(i, i)
+ p = p * (P(c:lower()) + P(c:upper()))
+ end
+ return p
+end
+local function N(p, min, max)
+ max = max or min
+ return p^min - p^(max+1)
+end
+
+local whitespace = T("whitespace", ws^1)
+
+local comment_keyword = T("comment_keyword", (I"todo" + I"xxx" + I"fixme" + I"sync" + I"fallthrough" + I"unreachable") * #(any - letter))
+local line_comment_text = T("comment_text", (any - comment_keyword - S"\\\n" + P"\\\n" + P"\\")^1)
+local line_comment = T("comment_text", P"//") * (line_comment_text + comment_keyword)^0 * (T("whitespace", P"\n") + P"")
+local block_comment_text = T("comment_text", (any - comment_keyword - P"*/")^1)
+local block_comment = T("comment_text", P"/*") * (block_comment_text + comment_keyword)^0 * T("comment_text", P"*/")^-1
+local comment = line_comment + block_comment
+
+local fltlit_dec_exp = S"eE" * pm^-1 * dec^1
+local fltlit_dec = dec^1 * (P"." * dec^0 * fltlit_dec_exp^-1 + fltlit_dec_exp) + P"." * dec^1 * fltlit_dec_exp^-1
+local fltlit_hex_exp = S"pP" * pm^-1 * dec^1
+local fltlit_hex = P"0" * S"xX" * (hex^1 * (P"." * hex^0)^-1 + P"." * hex^1) * fltlit_hex_exp
+local fltlit_suffix = S"fFlL"
+-- local fltlit = pm^-1 * (fltlit_hex + fltlit_dec) * fltlit_suffix^-1
+local fltlit = (fltlit_hex + fltlit_dec) * fltlit_suffix^-1
+
+local intlit_oct = P"0" * oct^1
+local intlit_dec = dec^1
+local intlit_hex = P"0" * S"xX" * hex^1
+local intlit_width = P"ll" + P"l" + P"LL" + P"L"
+local intlit_suffix = (S"uU" * intlit_width^-1) + (intlit_width * S"uU"^-1)
+-- local intlit = pm^-1 * (intlit_hex + intlit_oct + intlit_dec) * intlit_suffix^-1
+local intlit = (intlit_hex + intlit_oct + intlit_dec) * intlit_suffix^-1
+
+local numlit = T("numlit", fltlit + intlit)
+
+local escape = T("escape",
+ P"\\" * S"'\"?\\abfnrtv"
+ + P"\\" * dec * dec^-2
+ + P"\\x" * hex^1
+ + P"\\u" * N(hex, 4)
+ + P"\\U" * N(hex, 8))
+local bad_escape = T("bad_escape", P"\\" * any)
+
+local charlit_text = T("charlit_text", (any - vws - S"'\\")^1)
+local charlit_prefix = T("charlit_delim", P"u8" + P"u" + P"U" + P"L")
+local charlit_delim = T("charlit_delim", P"'")
+local charlit = charlit_prefix^-1 * charlit_delim
+ * (charlit_text + escape + bad_escape)^0 * charlit_delim
+
+local strlit_format_param = dec^1 * P"$"
+local strlit_format = T("strlit_format", P"%"
+ * strlit_format_param^-1
+ * S"-+ 0'#"^0 -- flags
+ * (R"19" * dec^0 + (P"*" * strlit_format_param^-1))^-1 -- width
+ * (P"." * (P"*" * strlit_format_param^-1 + dec^0))^-1 -- precision
+ * (S"Lhjltz" + P"hh" + P"ll")^-1 -- length
+ * S"%diuoxXeEfFgGaAcspn") -- type
+local strlit_text = T("strlit_text", (any - vws - S"\"\\" - strlit_format)^1)
+local strlit_prefix = T("strlit_delim", P"u8" + P"u" + P"U" + P"L")
+local strlit_delim = T("strlit_delim", P"\"")
+local strlit = strlit_prefix^-1 * strlit_delim
+ * (strlit_text + strlit_format + escape + bad_escape)^0 * strlit_delim
+
+local delimiter = T("delimiter", S"(){};,\\")
+
+local operator = T("operator", S"+-*/%!&|^~<=>?:.[]" + P"sizeof" + P"_Alignof" + P"alignof")
+
+local preproc_cond = T("preproc_cond", P"#") * T("whitespace", hws^0)
+ * T("preproc_cond", P"ifdef" + P"ifndef" + P"if" + P"elif" + P"else" + P"endif")
+local preproc_def = T("preproc", P"#") * T("whitespace", hws^0)
+ * T("preproc", P"define") * T("whitespace", hws^0)
+ * (T("function", word) * #P"(" + T("identifier", word))
+local preproc_inc = T("preproc", P"#") * T("whitespace", hws^0)
+ * T("preproc", P"include") * (
+ T("whitespace", hws^0)
+ * T("strlit_delim", P"<")
+ * T("strlit_text", (any - vws - P">")^0)
+ * T("strlit_delim", P">")
+ )^-1
+local preproc_other = T("preproc", P"#") * T("whitespace", hws^0)
+ * T("preproc", P"undef" + P"pragma" + P"include" + P"error" + P"warning" + P"line" + P"version")
+local preproc = preproc_cond + preproc_def + preproc_inc + preproc_other + T("preproc", "#")
+
+local keyword = T("keyword", l.word_match{
+ -- Storage classes
+ "extern", "static", "auto", "register", "_Thread_local",
+ "thread_local", -- <threads.h>
+
+ -- Type qualifiers
+ "const", "restrict", "volatile", "_Atomic",
+
+ -- Function specifier
+ "inline", "_Noreturn",
+ "noreturn", -- <stdnoreturn.h>
+
+ -- Control flow
+ "return", "break", "continue", "goto",
+ "if", "else",
+ "switch", "case","default",
+ "do", "while", "for",
+
+ -- Misc
+ "typedef", "_Alignas", "_Generic", "_Static_assert",
+ "alignas", -- <stdalign.h>
+ "static_assert", -- <assert.h>
+
+ -- GNU
+ "__typeof__", "typeof",
+ "__attribute__",
+ "__asm__", "asm",
+
+ -- rcx macros
+ "likely", "unlikely",
+ "unreachable",
+
+ -- GLSL
+ "layout", "in", "out", "inout",
+ "uniform", "shared", "buffer",
+ "coherent","readonly", "writeonly",
+})
+
+local constant = T("constant", l.word_match{
+ -- Very special constants only.
+ "__DATE__", "__FILE__", "__LINE__", "__TIME__", "__func__",
+ "__VA_ARGS__",
+ "NULL",
+ "true", "false", -- <stdbool.h>
+})
+
+local type_builtin = l.word_match{
+ "void",
+ "_Bool", "char", "int", "float", "double",
+ "bool", -- <stdbool.h>
+ "short", "long",
+ "signed", "unsigned",
+ "_Complex", "_Imaginary",
+ "complex", "imaginary", -- <complex.h>
+ "struct", "union", "enum",
+}
+local type_std = l.word_match{
+ "ptrdiff_t", "size_t", "ssize_t", "max_align_t", "wchar_t",
+}
+local type_stdint = P"u"^-1 * P"int" * ((P"_least" + P"_fast")^-1 * dec^1 + P"max" + P"ptr") * P"_t" * #(any - letter)
+local type_nice = P"__"^-1 * (P"v" * dec^1)^-1 * S"usifc" * dec^1 * (P"a" * dec^1)^-1 * P"_t"^-1 * #(any - letter) + l.word_match{
+ "schar", "uchar", "ushort", "uint", "ulong", "llong", "ullong",
+}
+local type_rcx = (S"iu" * (P"max" + P"ptr" + P"size") + P"rune" + P"maxalign") * #(any - letter)
+local type_glsl = (
+ S"biud"^-1 * P"vec" * S"234" +
+ S"d"^-1 * P"mat" * S"234" * (P"x" * S"243")^-1 +
+ S"iu"^-1 * (P"image" + P"sampler") * (
+ P"1D" + P"2D" + P"3D" + P"Cube" + P"2DRect" + P"1DArray" +
+ P"2DArray" + P"CubeArray" + P"Buffer" + P"2DMS" + P"2DMSArray"
+ ) +
+ P"sampler" * (
+ P"1D" + P"2D" + P"Cube" + P"2DRect" + P"1DArray" +
+ P"2DArray" + P"CubeArray"
+ ) * P"Shadow"
+ ) * #(any - letter)
+local type_ = T("type", type_builtin + type_std + type_stdint + type_nice + type_rcx + type_glsl)
+
+local label = T("whitespace", hws^0) * T("label", word) * T("delimiter", P":")
+
+--local function_ = T("function", word) * T("whitespace", ws^0) * #P"("
+local function_ = T("function", word) * #P"("
+
+local identifier = T("identifier", word)
+
+M._rules = {
+ {"whitespace", whitespace},
+ {"comment", comment},
+ {"numlit", numlit},
+ {"charlit", charlit},
+ {"strlit", strlit},
+ {"delimiter", delimiter},
+ {"operator", operator},
+ {"preproc", preproc},
+ {"keyword", keyword},
+ {"constant", constant},
+ {"type", type_},
+ {"label", label},
+ {"function", function_},
+ {"identifier", identifier},
+
+ {"error", T("error", any)}, -- TODO: TEMP
+}
+
+M._tokenstyles = {
+ whitespace = l.STYLE_WHITESPACE,
+
+ comment_text = l.STYLE_COMMENT,
+ comment_keyword = l.STYLE_COMMENT_KEYWORD,
+
+ numlit = l.STYLE_NUMBER,
+
+ escape = l.STYLE_ESCAPE,
+ bad_escape = l.STYLE_ERROR,
+
+ charlit_delim = l.STYLE_NUMBER,
+ charlit_text = l.STYLE_NUMBER,
+
+ strlit_delim = l.STYLE_STRING,
+ strlit_text = l.STYLE_STRING,
+ strlit_format = l.STYLE_STRING_FORMAT,
+
+ delimiter = l.STYLE_DELIMITER,
+ operator = l.STYLE_OPERATOR,
+
+ preproc = l.STYLE_PREPROCESSOR,
+ preproc_cond = l.STYLE_PREPROCESSOR_CONDITIONAL,
+
+ keyword = l.STYLE_KEYWORD,
+ constant = l.STYLE_CONSTANT,
+ ["type"] = l.STYLE_TYPE,
+ label = l.STYLE_LABEL,
+ ["function"] = l.STYLE_FUNCTION,
+ identifier = l.STYLE_IDENTIFIER,
+}
+
+return M
diff --git a/vis/visrc.lua b/vis/visrc.lua
@@ -13,6 +13,7 @@ events.subscribe(events.INIT, function()
vis.ftdetect.filetypes.mp = {ext = {"%.mp$"}}
vis.ftdetect.filetypes.suq = {ext = {"%.suq$"}}
vis.ftdetect.filetypes.lean = {ext = {"%.lean$"}}
+ vis.ftdetect.filetypes.glsl = {ext = {"%.glsl$"}}
vis:command("set theme custom")
end)