Skip to content

Commit 74bfc22

Browse files
jneenjneen
andauthored
Rewrite the GLSL lexer as not a subclass of C (#2249)
* rewrite the GLSL lexer as not a subclass of C * remove duplicate comment from gherkin.rake * highlight builtin functions when they're called * add float format spec from shader-minifier * add another spec for int formats --------- Co-authored-by: jneen <jneen@jneen.net>
1 parent 3eaf116 commit 74bfc22

File tree

6 files changed

+302
-144
lines changed

6 files changed

+302
-144
lines changed

lib/rouge/lexers/gherkin/keywords.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# DO NOT EDIT
55
# This file is automatically generated by `rake builtins:gherkin`.
66
# See tasks/builtins/gherkin.rake for more info.
7-
# See tasks/builtins/gherkin.rake for more info.
87

98
module Rouge
109
module Lexers

lib/rouge/lexers/glsl.rb

Lines changed: 45 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -5,162 +5,65 @@
55

66
module Rouge
77
module Lexers
8-
# This file defines the GLSL language lexer to the Rouge
9-
# syntax highlighter.
10-
#
11-
# Author: Sri Harsha Chilakapati
12-
class Glsl < C
8+
class GLSL < RegexLexer
139
tag 'glsl'
1410
filenames '*.glsl', '*.frag', '*.vert', '*.geom', '*.vs', '*.gs', '*.shader'
1511
mimetypes 'x-shader/x-vertex', 'x-shader/x-fragment', 'x-shader/x-geometry'
1612

1713
title "GLSL"
1814
desc "The GLSL shader language"
1915

20-
def self.keywords
21-
@keywords ||= Set.new %w(
22-
const uniform buffer shared attribute varying
23-
coherent volatile restrict readonly writeonly
24-
atomic_uint
25-
layout
26-
centroid flat smooth noperspective
27-
patch sample
28-
invariant precise
29-
break continue do for while switch case default
30-
if else
31-
subroutine
32-
in out inout
33-
true false
34-
discard return
35-
lowp mediump highp precision
36-
struct
16+
lazy { require_relative "glsl/builtins" }
3717

38-
row_major column_major packed std140 std43 binding offset align location
39-
early_fragment_tests
40-
)
41-
end
18+
state :root do
19+
rule %r/\s+/, Text
20+
rule %r(/[*].*?[*]/)m, Comment::Multiline
21+
rule %r(//.*$), Comment
4222

43-
def self.keywords_type
44-
@keywords_type ||= Set.new %w(
45-
int void bool float double
23+
rule %r/^#(?:[^\n]|\\\r?\n)*/, Comment::Preproc
24+
rule %r/defined\b/, Comment::Preproc
25+
rule %r/__(?:LINE|FILE|VERSION)__\b/, Keyword::Constant
26+
rule %r/(?:true|false)\b/, Keyword::Pseudo
4627

47-
vec2 vec3 vec4 ivec2 ivec3 ivec4 bvec2 bvec3 bvec4
48-
uint uvec2 uvec3 uvec4
49-
dvec2 dvec3 dvec4
50-
mat2 mat3 mat4
51-
mat2x2 mat2x3 mat2x4
52-
mat3x2 mat3x3 mat3x4
53-
mat4x2 mat4x3 mat4x4
54-
dmat2 dmat3 dmat4
55-
dmat2x2 dmat2x3 dmat2x4
56-
dmat3x2 dmat3x3 dmat3x4
57-
dmat4x2 dmat4x3 dmat4x4
58-
sampler1D sampler1DShadow sampler1DArray sampler1DArrayShadow
59-
isampler1D isampler1DArray usampler1D usampler1DArray
60-
sampler2D sampler2DShadow sampler2DArray sampler2DArrayShadow
61-
isampler2D isampler2DArray usampler2D usampler2DArray
62-
sampler2DRect sampler2DRectShadow isampler2DRect usampler2DRect
63-
sampler2DMS isampler2DMS usampler2DMS
64-
sampler2DMSArray isampler2DMSArray usampler2DMSArray
65-
sampler3D isampler3D usampler3D
66-
samplerCube samplerCubeShadow isamplerCube usamplerCube
67-
samplerCubeArray samplerCubeArrayShadow
68-
isamplerCubeArray usamplerCubeArray
69-
samplerBuffer isamplerBuffer usamplerBuffer
70-
image1D iimage1D uimage1D
71-
image1DArray iimage1DArray uimage1DArray
72-
image2D iimage2D uimage2D
73-
image2DArray iimage2DArray uimage2DArray
74-
image2DRect iimage2DRect uimage2DRect
75-
image2DMS iimage2DMS uimage2DMS
76-
image2DMSArray iimage2DMSArray uimage2DMSArray
77-
image3D iimage3D uimage3D
78-
imageCube iimageCube uimageCube
79-
imageCubeArray iimageCubeArray uimageCubeArray
80-
imageBuffer iimageBuffer uimageBuffer
81-
atomic_uint
28+
rule %r/0x\h+u?/i, Num::Hex
29+
rule %r/(?:\d+[.]\d*|[.]\d+)(?:e[+-]?\d+)?(?:l?f)?/i, Num::Float
30+
rule %r/\d+(?:e[+-]?\d+)(?:l?f)?/i, Num::Float
31+
rule %r/[1-9]\d*u?/i, Num::Integer
32+
rule %r/0[0-7]+/, Num::Oct
33+
rule %r/0/, Num::Integer
34+
rule %r([-.+/*%<>\[\](){}^|&~=!:;,?]), Punctuation
8235

83-
texture1D texture1DArray
84-
itexture1D itexture1DArray utexture1D utexture1DArray
85-
texture2D texture2DArray
86-
itexture2D itexture2DArray utexture2D utexture2DArray
87-
texture2DRect itexture2DRect utexture2DRect
88-
texture2DMS itexture2DMS utexture2DMS
89-
texture2DMSArray itexture2DMSArray utexture2DMSArray
90-
texture3D itexture3D utexture3D
91-
textureCube itextureCube utextureCube
92-
textureCubeArray itextureCubeArray utextureCubeArray
93-
textureBuffer itextureBuffer utextureBuffer
94-
sampler samplerShadow
95-
subpassInput isubpassInput usubpassInput
96-
subpassInputMS isubpassInputMS usubpassInputMS
97-
)
98-
end
36+
rule %r/\w+/ do |m|
37+
if KEYWORDS.include?(m[0])
38+
token Keyword
39+
elsif TYPES.include?(m[0])
40+
token Keyword::Type
41+
else
42+
fallthrough!
43+
end
44+
end
9945

100-
def self.reserved
101-
@reserved ||= Set.new %w(
102-
common partition active
103-
asm
104-
class union enum typedef template this
105-
resource
106-
goto
107-
inline noinline public static extern external interface
108-
long short half fixed unsigned superp
109-
input output
110-
hvec2 hvec3 hvec4 fvec2 fvec3 fvec4
111-
filter
112-
sizeof cast
113-
namespace using
114-
sampler3DRect
115-
)
116-
end
46+
rule %r/\w+(?=\s*[(])/ do |m|
47+
if BUILTINS.include?(m[0])
48+
token Name::Builtin
49+
else
50+
token Name::Function
51+
end
52+
end
11753

118-
def self.builtins
119-
@builtins ||= Set.new %w(
120-
gl_VertexID gl_VertexIndex gl_InstanceID gl_InstanceIndex gl_PerVertex gl_Position gl_PointSize gl_ClipDistance gl_CullDistance
121-
gl_PrimitiveIDIn gl_InvocationID gl_PrimitiveID gl_Layer gl_ViewportIndex
122-
gl_MaxPatchVertices gl_PatchVerticesIn gl_TessLevelOuter gl_TessLevelInner
123-
gl_TessCoord gl_FragCoord gl_FrontFacing gl_PointCoord gl_SampleID gl_SamplePosition
124-
gl_FragColor gl_FragData gl_MaxDrawBuffers gl_FragDepth gl_SampleMask
125-
gl_ClipVertex gl_FrontColor gl_BackColor gl_FrontSecondaryColor gl_BackSecondaryColor
126-
gl_TexCoord gl_FogFragCoord gl_Color gl_SecondaryColor gl_Normal
127-
gl_MultiTexCord0 gl_MultiTexCord1 gl_MultiTexCord2 gl_MultiTexCord3
128-
gl_MultiTexCord4 gl_MultiTexCord5 gl_MultiTexCord6 gl_MultiTexCord7
129-
gl_FogCoord gl_MaxVertexAttribs gl_MaxVertexUniformComponents
130-
gl_MaxVaryingFloats gl_MaxVaryingComponents gl_MaxVertexOutputComponents
131-
gl_MaxGeometryInputComponents gl_MaxGeometryOutputComponents
132-
gl_MaxFragmentInputComponents gl_MaxVertexTextureImageUnits
133-
gl_MaxCombinedTextureImageUnits gl_MaxTextureImageUnits
134-
gl_MaxFragmentUniformComponents gl_MaxClipDistances
135-
gl_MaxGeometryTextureImageUnits gl_MaxGeometryUniformComponents
136-
gl_MaxGeometryVaryingComponents gl_MaxTessControlInputComponents
137-
gl_MaxTessControlOutputComponents gl_MaxTessControlTextureImageUnits
138-
gl_MaxTessControlUniformComponents gl_MaxTessControlTotalOutputComponents
139-
gl_MaxTessEvaluationInputComponents gl_MaxTessEvaluationOutputComponents
140-
gl_MaxTessEvaluationTextureImageUnits gl_MaxTessEvaluationUniformComponents
141-
gl_MaxTessPatchComponents gl_MaxTessGenLevel gl_MaxViewports
142-
gl_MaxVertexUniformVectors gl_MaxFragmentUniformVectors gl_MaxVaryingVectors
143-
gl_MaxTextureUnits gl_MaxTextureCoords gl_MaxClipPlanes gl_DepthRange
144-
gl_DepthRangeParameters gl_ModelViewMatrix gl_ProjectionMatrix
145-
gl_ModelViewProjectionMatrix gl_TextureMatrix gl_NormalMatrix
146-
gl_ModelViewMatrixInverse gl_ProjectionMatrixInverse gl_ModelViewProjectionMatrixInverse
147-
gl_TextureMatrixInverse gl_ModelViewMatrixTranspose
148-
gl_ModelViewProjectionMatrixTranspose gl_TextureMatrixTranspose
149-
gl_ModelViewMatrixInverseTranspose gl_ProjectionMatrixInverseTranspose
150-
gl_ModelViewProjectionMatrixInverseTranspose
151-
gl_TextureMatrixInverseTranspose gl_NormalScale gl_ClipPlane gl_PointParameters
152-
gl_Point gl_MaterialParameters gl_FrontMaterial gl_BackMaterial
153-
gl_LightSourceParameters gl_LightSource gl_MaxLights gl_LightModelParameters
154-
gl_LightModel gl_LightModelProducts gl_FrontLightModelProduct
155-
gl_BackLightModelProduct gl_LightProducts gl_FrontLightProduct
156-
gl_BackLightProduct gl_TextureEnvColor gl_EyePlaneS gl_EyePlaneT gl_EyePlaneR
157-
gl_EyePlaneQ gl_ObjectPlaneS gl_ObjectPlaneT gl_ObjectPlaneR gl_ObjectPlaneQ
158-
gl_FogParameters gl_Fog
159-
gl_DrawID gl_BaseVertex gl_BaseInstance
160-
gl_NumWorkGroups gl_WorkGroupID gl_LocalInvocationID gl_GlobalInvocationID gl_LocalInvocationIndex
161-
gl_WorkGroupSize gl_HelperInvocation
162-
)
54+
rule %r/\w+/ do |m|
55+
if GL_VARS.include?(m[0])
56+
token Name::Constant
57+
elsif BUILTINS.include?(m[0])
58+
token Name::Builtin
59+
else
60+
token Name
61+
end
62+
end
16363
end
16464
end
65+
66+
# backcompat
67+
Glsl = GLSL
16568
end
16669
end

0 commit comments

Comments
 (0)