glsl.lua (7595B)
1 -- TODO: Hack: This is just a copy of the ansi_c lexer with some additions. 2 3 local l = require("lexer") 4 local lex = l.new(...) 5 local P, R, S = lpeg.P, lpeg.R, lpeg.S 6 local T = function(name, patt) return lex:tag(name, patt) end 7 local any = P(1) 8 local oct = R"07" 9 local dec = R"09" 10 local hex = R("09", "AF", "af") 11 local alpha = R("AZ", "az") 12 local letter = alpha + dec + "_" 13 local word = (alpha + P"_") * letter^0 14 local pm = S"+-" 15 local hws = S"\t " 16 local vws = S"\n\r" 17 local ws = hws + vws 18 local function I(s) -- Case-insensitive string match 19 local p = P(true) 20 for i = 1, #s do 21 local c = s:sub(i, i) 22 p = p * (P(c:lower()) + P(c:upper())) 23 end 24 return p 25 end 26 local function N(p, min, max) 27 max = max or min 28 return p^min - p^(max+1) 29 end 30 31 local whitespace = T("whitespace", ws^1) 32 33 local comment_keyword = T("comment_keyword", (I"todo" + I"xxx" + I"fixme" + I"sync" + I"fallthrough" + I"unreachable") * #(any - letter)) 34 local line_comment_text = T("comment_text", (any - comment_keyword - S"\\\n" + P"\\\n" + P"\\")^1) 35 local line_comment = T("comment_text", P"//") * (line_comment_text + comment_keyword)^0 * (T("whitespace", P"\n") + P"") 36 local block_comment_text = T("comment_text", (any - comment_keyword - P"*/")^1) 37 local block_comment = T("comment_text", P"/*") * (block_comment_text + comment_keyword)^0 * T("comment_text", P"*/")^-1 38 local comment = line_comment + block_comment 39 40 local fltlit_dec_exp = S"eE" * pm^-1 * dec^1 41 local fltlit_dec = dec^1 * (P"." * dec^0 * fltlit_dec_exp^-1 + fltlit_dec_exp) + P"." * dec^1 * fltlit_dec_exp^-1 42 local fltlit_hex_exp = S"pP" * pm^-1 * dec^1 43 local fltlit_hex = P"0" * S"xX" * (hex^1 * (P"." * hex^0)^-1 + P"." * hex^1) * fltlit_hex_exp 44 local fltlit_suffix = S"fFlL" 45 -- local fltlit = pm^-1 * (fltlit_hex + fltlit_dec) * fltlit_suffix^-1 46 local fltlit = (fltlit_hex + fltlit_dec) * fltlit_suffix^-1 47 48 local intlit_oct = P"0" * oct^1 49 local intlit_dec = dec^1 50 local intlit_hex = P"0" * S"xX" * hex^1 51 local intlit_width = P"ll" + P"l" + P"LL" + P"L" 52 local intlit_suffix = (S"uU" * intlit_width^-1) + (intlit_width * S"uU"^-1) 53 -- local intlit = pm^-1 * (intlit_hex + intlit_oct + intlit_dec) * intlit_suffix^-1 54 local intlit = (intlit_hex + intlit_oct + intlit_dec) * intlit_suffix^-1 55 56 local numlit = T("numlit", fltlit + intlit) 57 58 local escape = T("escape", 59 P"\\" * S"'\"?\\abfnrtv" 60 + P"\\" * dec * dec^-2 61 + P"\\x" * hex^1 62 + P"\\u" * N(hex, 4) 63 + P"\\U" * N(hex, 8)) 64 local bad_escape = T("bad_escape", P"\\" * any) 65 66 local charlit_text = T("charlit_text", (any - vws - S"'\\")^1) 67 local charlit_prefix = T("charlit_delim", P"u8" + P"u" + P"U" + P"L") 68 local charlit_delim = T("charlit_delim", P"'") 69 local charlit = charlit_prefix^-1 * charlit_delim 70 * (charlit_text + escape + bad_escape)^0 * charlit_delim 71 72 local strlit_format_param = dec^1 * P"$" 73 local strlit_format = T("strlit_format", P"%" 74 * strlit_format_param^-1 75 * S"-+ 0'#"^0 -- flags 76 * (R"19" * dec^0 + (P"*" * strlit_format_param^-1))^-1 -- width 77 * (P"." * (P"*" * strlit_format_param^-1 + dec^0))^-1 -- precision 78 * (S"Lhjltz" + P"hh" + P"ll")^-1 -- length 79 * S"%diuoxXeEfFgGaAcspn") -- type 80 local strlit_text = T("strlit_text", (any - vws - S"\"\\" - strlit_format)^1) 81 local strlit_prefix = T("strlit_delim", P"u8" + P"u" + P"U" + P"L") 82 local strlit_delim = T("strlit_delim", P"\"") 83 local strlit = strlit_prefix^-1 * strlit_delim 84 * (strlit_text + strlit_format + escape + bad_escape)^0 * strlit_delim 85 86 local delimiter = T("delimiter", S"(){};,\\") 87 88 local operator = T("operator", S"+-*/%!&|^~<=>?:.[]" + P"sizeof" + P"_Alignof" + P"alignof") 89 90 local preproc_cond = T("preproc_cond", P"#") * T("whitespace", hws^0) 91 * T("preproc_cond", P"ifdef" + P"ifndef" + P"if" + P"elif" + P"else" + P"endif") 92 local preproc_def = T("preproc", P"#") * T("whitespace", hws^0) 93 * T("preproc", P"define") * T("whitespace", hws^0) 94 * (T("function", word) * #P"(" + T("identifier", word)) 95 local preproc_inc = T("preproc", P"#") * T("whitespace", hws^0) 96 * T("preproc", P"include") * ( 97 T("whitespace", hws^0) 98 * T("strlit_delim", P"<") 99 * T("strlit_text", (any - vws - P">")^0) 100 * T("strlit_delim", P">") 101 )^-1 102 local preproc_other = T("preproc", P"#") * T("whitespace", hws^0) 103 * T("preproc", P"undef" + P"pragma" + P"include" + P"error" + P"warning" + P"line" + P"version") 104 local preproc = preproc_cond + preproc_def + preproc_inc + preproc_other + T("preproc", "#") 105 106 local keyword = T("keyword", l.word_match{ 107 -- Storage classes 108 "extern", "static", "auto", "register", "_Thread_local", 109 "thread_local", -- <threads.h> 110 111 -- Type qualifiers 112 "const", "restrict", "volatile", "_Atomic", 113 114 -- Function specifier 115 "inline", "_Noreturn", 116 "noreturn", -- <stdnoreturn.h> 117 118 -- Control flow 119 "return", "break", "continue", "goto", 120 "if", "else", 121 "switch", "case","default", 122 "do", "while", "for", 123 124 -- Misc 125 "typedef", "_Alignas", "_Generic", "_Static_assert", 126 "alignas", -- <stdalign.h> 127 "static_assert", -- <assert.h> 128 129 -- GNU 130 "__typeof__", "typeof", 131 "__attribute__", 132 "__asm__", "asm", 133 134 -- rcx macros 135 "likely", "unlikely", 136 "unreachable", 137 138 -- GLSL 139 "in", "out", "inout", "layout", 140 "uniform", "buffer", "shared", 141 "lowp", "mediump", "highp", "precision", 142 "coherent", "readonly", "writeonly", 143 }) 144 145 local constant = T("constant", l.word_match{ 146 -- Very special constants only. 147 "__DATE__", "__FILE__", "__LINE__", "__TIME__", "__func__", 148 "__VA_ARGS__", 149 "NULL", 150 "true", "false", -- <stdbool.h> 151 }) 152 153 local type_builtin = l.word_match{ 154 "void", 155 "_Bool", "char", "int", "float", "double", 156 "bool", -- <stdbool.h> 157 "short", "long", 158 "signed", "unsigned", 159 "_Complex", "_Imaginary", 160 "complex", "imaginary", -- <complex.h> 161 "struct", "union", "enum", 162 } 163 local type_std = l.word_match{ 164 "ptrdiff_t", "size_t", "ssize_t", "max_align_t", "wchar_t", 165 } 166 local type_stdint = P"u"^-1 * P"int" * ((P"_least" + P"_fast")^-1 * dec^1 + P"max" + P"ptr") * P"_t" * #(any - letter) 167 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{ 168 "schar", "uchar", "ushort", "uint", "ulong", "llong", "ullong", 169 } 170 local type_rcx = (S"iu" * (P"max" + P"ptr" + P"size") + P"rune" + P"maxalign") * #(any - letter) 171 local type_glsl = ( 172 S"buid"^-1 * P"vec" * R"24" + 173 S"d"^-1 * P"mat" * R"24" * (P"x" * R"24")^-1 + 174 (S"ui"^-1 * (P"sampler" + P"image") + P"image") * ( 175 P"1D" + P"2D" + P"3D" + P"Cube" + P"2DRect" + P"1DArray" + 176 P"2DArray" + P"CubeArray" + P"Buffer" + P"2DMS" + P"2DMSArray" 177 ) + 178 P"sampler" * ( 179 P"1D" + P"2D" + P"Cube" + P"2DRect" + 180 P"1DArray" + P"2DArray" + P"CubeArray" 181 ) * P"Shadow" + 182 P"atomic_uint" 183 ) * #(any - letter) 184 local type_ = T("type", type_builtin + type_std + type_stdint + type_nice + type_rcx + type_glsl) 185 186 local label = T("whitespace", hws^0) * T("label", word) * T("delimiter", P":") 187 188 --local function_ = T("function", word) * T("whitespace", ws^0) * #P"(" 189 local function_ = T("function", word) * #P"(" 190 191 local identifier = T("identifier", word) 192 193 lex:add_rule("whitespace", whitespace) 194 lex:add_rule("comment", comment) 195 lex:add_rule("numlit", numlit) 196 lex:add_rule("charlit", charlit) 197 lex:add_rule("strlit", strlit) 198 lex:add_rule("delimiter", delimiter) 199 lex:add_rule("operator", operator) 200 lex:add_rule("preproc", preproc) 201 lex:add_rule("keyword", keyword) 202 lex:add_rule("constant", constant) 203 lex:add_rule("type", type_) 204 lex:add_rule("label", label) 205 lex:add_rule("function", function_) 206 lex:add_rule("identifier", identifier) 207 208 lex:add_rule("error", T("error", any)) -- TODO: TEMP 209 210 return lex