summaryrefslogtreecommitdiff
path: root/slang-mode.el
blob: 2e08371cff3335206ca7096e14bd14708fea346d (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
;;; -*- lexical-binding: t; -*-
;;; slang-lsp.el --- Major mode for Slang shader files

;; Author: Michael Ghaben <michaelghaben@gmail.com>
;; Version: 0.0.1
;; Package-Requires: ((emacs "28.1") (eglot "1.19"))
;; Keywords: slang shaders vulkan opengl spir-v

;;; Code:
(require 'cc-mode)
(require 'font-lock)

(defgroup slang nil
  "Major mode for Slang shader files."
  :group 'language
  :prefix "slang-")

(defcustom slang-indent-offset 4
  "Indentation offset for Slang code."
  :type 'integer
  :group 'slang)

;; Syntax table
(defvar slang-mode-syntax-table
  (let ((table (make-syntax-table)))
    ;; C-style comments
    (modify-syntax-entry ?/ ". 124b" table)
    (modify-syntax-entry ?* ". 23" table)
    (modify-syntax-entry ?\n "> b" table)
    
    ;; Strings
    (modify-syntax-entry ?\" "\"" table)
    (modify-syntax-entry ?\' "\"" table)
    
    ;; Operators and punctuation
    (modify-syntax-entry ?+ "." table)
    (modify-syntax-entry ?- "." table)
    (modify-syntax-entry ?% "." table)
    (modify-syntax-entry ?< "." table)
    (modify-syntax-entry ?> "." table)
    (modify-syntax-entry ?& "." table)
    (modify-syntax-entry ?| "." table)
    (modify-syntax-entry ?= "." table)
    (modify-syntax-entry ?! "." table)
    
    ;; Underscore as word constituent
    (modify-syntax-entry ?_ "w" table)
    
    table)
  "Syntax table for Slang mode.")

;; Font lock keywords - comprehensive Slang language support
(defconst slang-font-lock-keywords
  (list
   ;; Preprocessor directives
   '("^[ \t]*#.*$" . 'font-lock-preprocessor-face)
   
   ;; Attributes (shader annotations)
   '("\\[\\([^]]+\\)\\]" . 'font-lock-preprocessor-face)
   
   ;; Keywords - core language
   `(,(regexp-opt '("break" "case" "continue" "default" "do" "else" "for" 
                    "if" "return" "switch" "while" "goto"
                    "const" "static" "uniform" "varying" "in" "out" "inout"
                    "public" "private" "internal" "extern"
                    "true" "false" "null" "none")
                  'words)
     . 'font-lock-keyword-face)
   
   ;; Keywords - Slang-specific
   `(,(regexp-opt '("interface" "struct" "enum" "class" "namespace" "module"
                    "import" "export" "using" "typealias" "associatedtype"
                    "extension" "where" "let" "var" "property" "subscript"
                    "init" "__init" "deinit" "__deinit" "this" "This" "Self"
                    "super" "throws" "try" "catch" "finally"
                    "is" "as" "sizeof" "alignof" "offsetof"
                    "expand" "each" "repeat" "unroll" "loop"
                    "target" "version" "profile" "stage" "binding"
                    "set" "get" "implements" "extends")
                  'words)
     . 'font-lock-keyword-face)
   
   ;; Storage class keywords  
   `(,(regexp-opt '("typedef" "register" "auto" "volatile" "inline"
                    "virtual" "abstract" "override" "final" "sealed")
                  'words)
     . 'font-lock-keyword-face)
   
   ;; Built-in scalar types
   `(,(regexp-opt '("void" "bool" "int" "uint" "float" "double" "half"
                    "int8_t" "uint8_t" "int16_t" "uint16_t" 
                    "int32_t" "uint32_t" "int64_t" "uint64_t"
                    "float16_t" "float32_t" "float64_t"
                    "string" "StringLiteral")
                  'words)
     . 'font-lock-type-face)
   
   ;; Vector and matrix types
   '("\\b\\(bool\\|int\\|uint\\|float\\|double\\|half\\)\\([1-4]\\)\\b" . 'font-lock-type-face)
   '("\\b\\(bool\\|int\\|uint\\|float\\|double\\|half\\)\\([1-4]\\)x\\([1-4]\\)\\b" . 'font-lock-type-face)
   
   ;; Texture and buffer types
   `(,(regexp-opt '("Texture1D" "Texture2D" "Texture3D" "TextureCube"
                    "Texture1DArray" "Texture2DArray" "TextureCubeArray"
                    "Texture2DMS" "Texture2DMSArray"
                    "RWTexture1D" "RWTexture2D" "RWTexture3D"
                    "RWTexture1DArray" "RWTexture2DArray"
                    "Buffer" "RWBuffer" "StructuredBuffer" "RWStructuredBuffer"
                    "ByteAddressBuffer" "RWByteAddressBuffer"
                    "ConstantBuffer" "TextureBuffer" "RWTextureBuffer"
                    "AppendStructuredBuffer" "ConsumeStructuredBuffer"
                    "SamplerState" "SamplerComparisonState"
                    "RasterizerOrderedBuffer" "RasterizerOrderedStructuredBuffer"
                    "RasterizerOrderedTexture1D" "RasterizerOrderedTexture2D"
                    "RasterizerOrderedTexture3D")
                  'words)
     . 'font-lock-type-face)
   
   ;; Built-in interfaces
   `(,(regexp-opt '("IArithmetic" "ILogical" "IComparable" "IFloat"
                    "IInteger" "ISigned" "IUnsigned" "INumeric"
                    "IDifferentiable" "ITexture" "IResource")
                  'words)
     . 'font-lock-type-face)
   
   ;; HLSL semantics and system values
   `(,(regexp-opt '("SV_Position" "SV_Target" "SV_Depth" "SV_Coverage"
                    "SV_VertexID" "SV_InstanceID" "SV_PrimitiveID"
                    "SV_DispatchThreadID" "SV_GroupID" "SV_GroupThreadID"
                    "SV_GroupIndex" "SV_DomainLocation" "SV_InsideTessFactor"
                    "SV_TessFactor" "SV_OutputControlPointID" "SV_GSInstanceID"
                    "POSITION" "NORMAL" "TANGENT" "BINORMAL" "COLOR"
                    "TEXCOORD0" "TEXCOORD1" "TEXCOORD2" "TEXCOORD3"
                    "TEXCOORD4" "TEXCOORD5" "TEXCOORD6" "TEXCOORD7"
                    "BLENDWEIGHT" "BLENDINDICES" "PSIZE" "FOG")
                  'words)
     . 'font-lock-constant-face)
   
   ;; Boolean constants
   `(,(regexp-opt '("true" "false") 'words) . 'font-lock-constant-face)
   
   ;; Built-in mathematical functions
   `(,(regexp-opt '("abs" "acos" "all" "any" "asin" "atan" "atan2"
                    "ceil" "clamp" "cos" "cosh" "cross" "degrees"
                    "determinant" "distance" "dot" "exp" "exp2"
                    "faceforward" "floor" "fmod" "frac" "frexp"
                    "fwidth" "ldexp" "length" "lerp" "log" "log2"
                    "log10" "max" "min" "modf" "mul" "normalize"
                    "pow" "radians" "reflect" "refract" "round"
                    "rsqrt" "saturate" "sign" "sin" "sincos" "sinh"
                    "smoothstep" "sqrt" "step" "tan" "tanh" "transpose"
                    "trunc" "ddx" "ddy" "ddx_coarse" "ddy_coarse"
                    "ddx_fine" "ddy_fine" "tex1D" "tex2D" "tex3D"
                    "texCUBE" "noise" "clip" "discard" "lit")
                  'words)
     . 'font-lock-builtin-face)
   
   ;; Shader function intrinsics
   `(,(regexp-opt '("Sample" "SampleLevel" "SampleBias" "SampleCmp"
                    "SampleCmpLevelZero" "SampleGrad" "Load" "Store"
                    "GetDimensions" "CalculateLevelOfDetail"
                    "CalculateLevelOfDetailUnclamped"
                    "Gather" "GatherRed" "GatherGreen" "GatherBlue"
                    "GatherAlpha" "GatherCmp" "GatherCmpRed"
                    "GatherCmpGreen" "GatherCmpBlue" "GatherCmpAlpha"
                    "GroupMemoryBarrier" "GroupMemoryBarrierWithGroupSync"
                    "DeviceMemoryBarrier" "DeviceMemoryBarrierWithGroupSync"
                    "AllMemoryBarrier" "AllMemoryBarrierWithGroupSync"
                    "InterlockedAdd" "InterlockedAnd" "InterlockedCompareExchange"
                    "InterlockedCompareStore" "InterlockedExchange"
                    "InterlockedMax" "InterlockedMin" "InterlockedOr"
                    "InterlockedXor" "WaveActiveAllEqual" "WaveActiveBallot"
                    "WaveActiveCountBits" "WaveGetLaneCount" "WaveGetLaneIndex"
                    "WaveIsFirstLane" "WaveActiveAnyTrue" "WaveActiveAllTrue"
                    "WaveActiveSum" "WaveActiveProduct" "WaveActiveMin"
                    "WaveActiveMax" "WaveActiveBitAnd" "WaveActiveBitOr"
                    "WaveActiveBitXor" "WavePrefixCountBits" "WavePrefixSum"
                    "WavePrefixProduct" "WaveQuadReadAcrossX" "WaveQuadReadAcrossY"
                    "WaveQuadReadAcrossDiagonal" "WaveQuadSwapX" "WaveQuadSwapY"
                    "WaveReadLaneAt" "WaveReadLaneFirst")
                  'words)
     . 'font-lock-builtin-face)
   
   ;; Number literals
   '("\\b[0-9]+\\(\\.[0-9]+\\)?\\([eE][-+]?[0-9]+\\)?[fFhHlLuU]*\\b" . 'font-lock-constant-face)
   '("\\b0[xX][0-9a-fA-F]+[uUlL]*\\b" . 'font-lock-constant-face)
   
   ;; String literals
   '("\"\\([^\"\\\\]\\|\\\\.\\)*\"" . 'font-lock-string-face)
   '("'\\([^'\\\\]\\|\\\\.\\)*'" . 'font-lock-string-face)
   
   ;; Function definitions
   '("\\b\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s-*(" (1 'font-lock-function-name-face))
   
   ;; Type names (capitalized identifiers)
   '("\\b[A-Z][a-zA-Z0-9_]*\\b" . 'font-lock-type-face)
   )
  "Font lock keywords for Slang mode.")

;; Imenu support
(defvar slang-imenu-generic-expression
  '(("Functions" "^\\s-*\\([a-zA-Z_][a-zA-Z0-9_<>]*\\)\\s-+\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s-*(" 2)
    ("Structs" "^\\s-*struct\\s-+\\([a-zA-Z_][a-zA-Z0-9_]*\\)" 1)
    ("Interfaces" "^\\s-*interface\\s-+\\([a-zA-Z_][a-zA-Z0-9_]*\\)" 1)
    ("Enums" "^\\s-*enum\\s-+\\([a-zA-Z_][a-zA-Z0-9_]*\\)" 1)
    ("Classes" "^\\s-*class\\s-+\\([a-zA-Z_][a-zA-Z0-9_]*\\)" 1))
  "Imenu generic expression for Slang mode.")

;; Indentation function
(defun slang-indent-line ()
  "Indent current line as Slang code."
  (interactive)
  (c-indent-line))

;; Electric brace function
(defun slang-electric-brace (arg)
  "Insert a brace and indent properly."
  (interactive "P")
  (self-insert-command (prefix-numeric-value arg))
  (when (and (not arg)
             (eolp)
             (save-excursion
               (skip-chars-backward " \t")
               (bolp)))
    (c-indent-line)))

;; Keymap
(defvar slang-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "{" 'slang-electric-brace)
    (define-key map "}" 'slang-electric-brace)
    (define-key map (kbd "C-c C-c") 'comment-region)
    (define-key map (kbd "C-c C-u") 'uncomment-region)
    map)
  "Keymap for Slang mode.")

;; Mode menu
(easy-menu-define slang-mode-menu slang-mode-map
  "Menu for Slang mode."
  '("Slang"
    ["Comment Region" comment-region (mark)]
    ["Uncomment Region" uncomment-region (mark)]
    "---"
    ["Indent Line" slang-indent-line t]
    ["Indent Region" indent-region (mark)]
    "---"
    ["Customize Slang" (customize-group 'slang) t]))

;;;###autoload
(define-derived-mode slang-mode c-mode "Slang"
  "Major mode for editing Slang shader files."
  :syntax-table slang-mode-syntax-table
  :group 'slang
  
  ;; Font lock
  (setq font-lock-defaults '(slang-font-lock-keywords nil nil nil nil))
  
  ;; Comments
  (setq comment-start "// "
        comment-end ""
        comment-start-skip "\\(//+\\|/\\*+\\)\\s *"
        comment-end-skip "\\s *\\(\\s>\\|\\*+/\\)")
  
  ;; Indentation
  (setq c-basic-offset slang-indent-offset
        tab-width slang-indent-offset
        indent-tabs-mode nil)
  
  ;; Case sensitivity
  (setq case-fold-search nil)
  
  ;; Imenu
  (setq imenu-generic-expression slang-imenu-generic-expression)
  
  ;; Compilation support
  (when (boundp 'compilation-error-regexp-alist)
    (set (make-local-variable 'compilation-error-regexp-alist)
         '(("^\\([^:]+\\):\\([0-9]+\\):" 1 2))))
  
  ;; Run mode hooks
  (run-mode-hooks 'slang-mode-hook))

;; File associations
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.slang\\'" . slang-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.sl\\'" . slang-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.slangh\\'" . slang-mode))

(provide 'slang-mode)

;;; slang-mode.el ends here