ui: make primary cursor blink even if no lua theme has been loaded
[vis.git] / lexers / glsl.lua
blob6d59da80601714944a24b8440295ba988c307a6a
1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- GLSL LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
7 local table = _G.table
9 local M = {_NAME = 'glsl'}
11 -- Whitespace.
12 local ws = token(l.WHITESPACE, l.space^1)
14 -- Keywords.
15 local keyword = token(l.KEYWORD, word_match{
16 'attribute', 'const', 'in', 'inout', 'out', 'uniform', 'varying', 'invariant',
17 'centroid', 'flat', 'smooth', 'noperspective', 'layout', 'patch', 'sample',
18 'subroutine', 'lowp', 'mediump', 'highp', 'precision',
19 -- Macros.
20 '__VERSION__', '__LINE__', '__FILE__',
23 -- Functions.
24 local func = token(l.FUNCTION, word_match{
25 'radians', 'degrees', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'sinh',
26 'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'pow', 'exp', 'log', 'exp2',
27 'log2', 'sqrt', 'inversesqrt', 'abs', 'sign', 'floor', 'trunc', 'round',
28 'roundEven', 'ceil', 'fract', 'mod', 'modf', 'min', 'max', 'clamp', 'mix',
29 'step', 'smoothstep', 'isnan', 'isinf', 'floatBitsToInt', 'floatBitsToUint',
30 'intBitsToFloat', 'uintBitsToFloat', 'fma', 'frexp', 'ldexp', 'packUnorm2x16',
31 'packUnorm4x8', 'packSnorm4x8', 'unpackUnorm2x16', 'unpackUnorm4x8',
32 'unpackSnorm4x8', 'packDouble2x32', 'unpackDouble2x32', 'length', 'distance',
33 'dot', 'cross', 'normalize', 'ftransform', 'faceforward', 'reflect',
34 'refract', 'matrixCompMult', 'outerProduct', 'transpose', 'determinant',
35 'inverse', 'lessThan', 'lessThanEqual', 'greaterThan', 'greaterThanEqual',
36 'equal', 'notEqual', 'any', 'all', 'not', 'uaddCarry', 'usubBorrow',
37 'umulExtended', 'imulExtended', 'bitfieldExtract', 'bitfildInsert',
38 'bitfieldReverse', 'bitCount', 'findLSB', 'findMSB', 'textureSize',
39 'textureQueryLOD', 'texture', 'textureProj', 'textureLod', 'textureOffset',
40 'texelFetch', 'texelFetchOffset', 'textureProjOffset', 'textureLodOffset',
41 'textureProjLod', 'textureProjLodOffset', 'textureGrad', 'textureGradOffset',
42 'textureProjGrad', 'textureProjGradOffset', 'textureGather',
43 'textureGatherOffset', 'texture1D', 'texture2D', 'texture3D', 'texture1DProj',
44 'texture2DProj', 'texture3DProj', 'texture1DLod', 'texture2DLod',
45 'texture3DLod', 'texture1DProjLod', 'texture2DProjLod', 'texture3DProjLod',
46 'textureCube', 'textureCubeLod', 'shadow1D', 'shadow2D', 'shadow1DProj',
47 'shadow2DProj', 'shadow1DLod', 'shadow2DLod', 'shadow1DProjLod',
48 'shadow2DProjLod', 'dFdx', 'dFdy', 'fwidth', 'interpolateAtCentroid',
49 'interpolateAtSample', 'interpolateAtOffset', 'noise1', 'noise2', 'noise3',
50 'noise4', 'EmitStreamVertex', 'EndStreamPrimitive', 'EmitVertex',
51 'EndPrimitive', 'barrier'
54 -- Types.
55 local type = token(l.TYPE,
56 S('bdiu')^-1 * 'vec' * R('24') +
57 P('d')^-1 * 'mat' * R('24') * ('x' * R('24')^-1) +
58 S('iu')^-1 * 'sampler' * R('13') * 'D' +
59 'sampler' * R('12') * 'D' * P('Array')^-1 * 'Shadow' +
60 S('iu')^-1 * 'sampler' * (R('12') * 'DArray' + word_match{
61 'Cube', '2DRect', 'Buffer', '2DMS', '2DMSArray',
62 '2DMSCubeArray'
63 }) +
64 word_match{
65 'samplerCubeShadow', 'sampler2DRectShadow',
66 'samplerCubeArrayShadow'
69 -- Variables.
70 local variable = token(l.VARIABLE, word_match{
71 'gl_VertexID', 'gl_InstanceID', 'gl_Position', 'gl_PointSize',
72 'gl_ClipDistance', 'gl_PrimitiveIDIn', 'gl_InvocationID', 'gl_PrimitiveID',
73 'gl_Layer', 'gl_PatchVerticesIn', 'gl_TessLevelOuter', 'gl_TessLevelInner',
74 'gl_TessCoord', 'gl_FragCoord', 'gl_FrontFacing', 'gl_PointCoord',
75 'gl_SampleID', 'gl_SamplePosition', 'gl_FragColor', 'gl_FragData',
76 'gl_FragDepth', 'gl_SampleMask', 'gl_ClipVertex', 'gl_FrontColor',
77 'gl_BackColor', 'gl_FrontSecondaryColor', 'gl_BackSecondaryColor',
78 'gl_TexCoord', 'gl_FogFragCoord', 'gl_Color', 'gl_SecondaryColor',
79 'gl_Normal', 'gl_Vertex', 'gl_MultiTexCoord0', 'gl_MultiTexCoord1',
80 'gl_MultiTexCoord2', 'gl_MultiTexCoord3', 'gl_MultiTexCoord4',
81 'gl_MultiTexCoord5', 'gl_MultiTexCoord6', 'gl_MultiTexCoord7', 'gl_FogCoord'
84 -- Constants.
85 local constant = token(l.CONSTANT, word_match{
86 'gl_MaxVertexAttribs', 'gl_MaxVertexUniformComponents', 'gl_MaxVaryingFloats',
87 'gl_MaxVaryingComponents', 'gl_MaxVertexOutputComponents',
88 'gl_MaxGeometryInputComponents', 'gl_MaxGeometryOutputComponents',
89 'gl_MaxFragmentInputComponents', 'gl_MaxVertexTextureImageUnits',
90 'gl_MaxCombinedTextureImageUnits', 'gl_MaxTextureImageUnits',
91 'gl_MaxFragmentUniformComponents', 'gl_MaxDrawBuffers', 'gl_MaxClipDistances',
92 'gl_MaxGeometryTextureImageUnits', 'gl_MaxGeometryOutputVertices',
93 'gl_MaxGeometryTotalOutputComponents', 'gl_MaxGeometryUniformComponents',
94 'gl_MaxGeometryVaryingComponents', 'gl_MaxTessControlInputComponents',
95 'gl_MaxTessControlOutputComponents', 'gl_MaxTessControlTextureImageUnits',
96 'gl_MaxTessControlUniformComponents',
97 'gl_MaxTessControlTotalOutputComponents',
98 'gl_MaxTessEvaluationInputComponents', 'gl_MaxTessEvaluationOutputComponents',
99 'gl_MaxTessEvaluationTextureImageUnits',
100 'gl_MaxTessEvaluationUniformComponents', 'gl_MaxTessPatchComponents',
101 'gl_MaxPatchVertices', 'gl_MaxTessGenLevel', 'gl_MaxTextureUnits',
102 'gl_MaxTextureCoords', 'gl_MaxClipPlanes',
104 'gl_DepthRange', 'gl_ModelViewMatrix', 'gl_ProjectionMatrix',
105 'gl_ModelViewProjectionMatrix', 'gl_TextureMatrix', 'gl_NormalMatrix',
106 'gl_ModelViewMatrixInverse', 'gl_ProjectionMatrixInverse',
107 'gl_ModelViewProjectionMatrixInverse', 'gl_TextureMatrixInverse',
108 'gl_ModelViewMatrixTranspose', 'gl_ProjectionMatrixTranspose',
109 'gl_ModelViewProjectionMatrixTranspose', 'gl_TextureMatrixTranspose',
110 'gl_ModelViewMatrixInverseTranspose', 'gl_ProjectionMatrixInverseTranspose',
111 'gl_ModelViewProjectionMatrixInverseTranspose',
112 'gl_TextureMatrixInverseTranspose', 'gl_NormalScale', 'gl_ClipPlane',
113 'gl_Point', 'gl_FrontMaterial', 'gl_BackMaterial', 'gl_LightSource',
114 'gl_LightModel', 'gl_FrontLightModelProduct', 'gl_BackLightModelProduct',
115 'gl_FrontLightProduct', 'gl_BackLightProduct', 'gl_TextureEnvColor',
116 'gl_EyePlaneS', 'gl_EyePlaneT', 'gl_EyePlaneR', 'gl_EyePlaneQ',
117 'gl_ObjectPlaneS', 'gl_ObjectPlaneT', 'gl_ObjectPlaneR', 'gl_ObjectPlaneQ',
118 'gl_Fog'
121 -- Extend cpp lexer to include GLSL elements.
122 local cpp = l.load('cpp')
123 local _rules = cpp._rules
124 _rules[1] = {'whitespace', ws}
125 table.insert(_rules, 2, {'glsl_keyword', keyword})
126 table.insert(_rules, 3, {'glsl_function', func})
127 table.insert(_rules, 4, {'glsl_type', type})
128 table.insert(_rules, 5, {'glsl_variable', variable})
129 M._rules = _rules
130 M._foldsymbols = cpp._foldsymbols
132 return M