-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_hla.lua
402 lines (367 loc) · 14.3 KB
/
script_hla.lua
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
--[[
Started: 25th February 2014
Completed: 28th February 2014
A SciTE script lexer for the HLA programming language
http://en.wikipedia.org/wiki/High_Level_Assembly
Some good references that'll help when scripting SciTE with Lua:
http://www.scintilla.org/ScintillaDoc.html
http://www.scintilla.org/ScriptLexer.html
http://lua-users.org/wiki/UsingLuaWithScite
]]
HLA_DEFAULT = 0
HLA_BOOLEAN = 1
HLA_CONSTANT = 2
HLA_NUMBER = 3
HLA_ERROR = 4
HLA_CHARACTER = 5
HLA_DOUBLE_STRING = 6
HLA_LINE_COMMENT = 7
HLA_MULTILINE_COMMENT = 8
HLA_REGISTER = 9
HLA_DATA_DEFINITION = 10
HLA_PROGRAM_ID = 11
HLA_PROCEDURE_NAME = 12
HLA_TASK_MARKER = 13
HLA_IDENTIFIER = 14
HLA_OPERATOR = 15
HLA_DIRECTIVE = 16
HLA_CONDITIONAL = 17
HLA_STATEMENT = 18
HLA_MACRO = 19
HLA_ATTRIBUTE = 20
HLA_BINARY_NUMBER = 21
HLA_ASCII_CODE = 22
HLA_HEX_NUMBER = 23
attributes = {}
booleans = {}
conditionals = {}
constants = {}
directives = {}
macros = {}
registers = {}
statements = {}
taskMarkers = {}
types = {}
programId = {}
procedureName = {}
function AddToTable(tbl)
-- This function will be called by gsub with the matched substring as the
-- first argument. That capture is added to the table with true as it's value
-- so that testing for the existence of a key will be as easy as retrieving
-- the key
return function (capture)
tbl[capture] = true
end
end
function loadProperties()
-- Add a space at the end so that the last entry in the list of options will not
-- be left out when gsubed below
hlaAttributes = props['hla.attributes'] .. " "
hlaBooleans = props['hla.booleans'] .. " "
hlaConditionals = props['hla.conditionals'] .. " "
hlaConstants = props['hla.constants'] .. " "
hlaDirectives = props['hla.directives'] .. " "
hlaMacros = props['hla.macros'] .. " "
hlaRegisters = props['hla.registers'] .. " "
hlaStatements = props['hla.statements'] .. " "
hlaTaskMarker = props['hla.task.markers'] .. " "
hlaTypes = props['hla.data.declaration'] .. " "
digits = props['hla.digits']
foldCompact = props["fold.compact"]
identifierEnds = props['hla.valid.identifier.terminators']
identifierStarts = props['hla.valid.identifier.starters']
operators = props['hla.operators']
-- Populate the tables with the words set in the .properties files
hlaAttributes:gsub("(.-) " , AddToTable(attributes))
hlaBooleans:gsub("(.-) " , AddToTable(booleans))
hlaConditionals:gsub("(.-) ", AddToTable(conditionals))
hlaConstants:gsub("(.-) " , AddToTable(constants))
hlaDirectives:gsub("(.-) " , AddToTable(directives))
hlaMacros:gsub("(.-) " , AddToTable(macros))
hlaRegisters:gsub("(.-) " , AddToTable(registers))
hlaStatements:gsub("(.-) " , AddToTable(statements))
hlaTaskMarker:gsub("(.-) " , AddToTable(taskMarkers))
hlaTypes:gsub("(.-) " , AddToTable(types))
end
-- Load settings
-- FIXME: Placing this function inside OnStyle causes the program ID
-- not to be coloured after modification of the .properties file.
loadProperties()
function OnStyle(styler)
styler:StartStyling(styler.startPos, styler.lengthDoc, styler.initStyle)
-- Used to detect the programID. If the previous token is 'program', then the
-- current token is the programID
local previousState = nil
local previousToken = ""
-- Used to tell whether we've finished colouring a task marker and can
-- therefore restore the color to that of the comment type it is embedded in
local finishedWithMarker = false
-- Used to flag an error in a character string with more than one character
local charactersLeft = 1
-- Used to validate ascii characters in binary form, e.g #%0010_0000
local asciiInBinaryForm = false
local asciiInDecimalForm = false
while styler:More() do
--~~~~~~~~~~~~~~~~~~~~~~~~~~~[ Switch OFF states ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if styler:AtLineEnd() and styler:State() == HLA_LINE_COMMENT then
-- Line comments stop colouring at the end of the line
styler:SetState(HLA_DEFAULT)
end
if (styler:State() == HLA_IDENTIFIER) and (previousState ~= nil) and
-- Placed here so that the state won't be switched off in the lines before
-- we restore the comment's colour.
styler:Current():find("[ \n\r:]") then
finishedWithMarker = true
styler:ChangeState(HLA_TASK_MARKER)
end
if finishedWithMarker then
-- Finished colouring task markers, restore the colouring of the original
-- comment type(line or multiline)
finishedWithMarker = false
styler:SetState(previousState)
previousState = nil
end
if (styler:State() == HLA_CHARACTER) and (styler:Current() ~= "'") then
-- Keep track of the number of characters in the character literal.
charactersLeft = charactersLeft - 1
end
if not identifierEnds:find(styler:Current(), 1, true) and (styler:State() == HLA_IDENTIFIER) then
local _identifier = styler:Token()
local identifier = _identifier:gsub("[?#]", "")
-- keywords like 'while' and 'begin' are case insensitive while statements
-- like stdout.flushInput are case sensitive
-- styler:Token() will fetch you all the characters before the current
-- position that have the same style. It doesn't always fetch words
-- separated by a space or operator.
identifier = string.lower(identifier)
-- Save the program ID and procedure name so that any subsequent
-- occurrences will be highlighted.
if previousToken == "program" then
programId = {[identifier] = true}
elseif previousToken == "procedure" then
procedureName[identifier] = true
end
if programId[identifier] then
-- Colour program ID
styler:ChangeState(HLA_PROGRAM_ID)
elseif procedureName[identifier] then
-- Colour procedure name
styler:ChangeState(HLA_PROCEDURE_NAME)
elseif types[identifier] then
-- Colour data definitions, int28, int32
styler:ChangeState(HLA_DATA_DEFINITION)
elseif constants[identifier] then
-- Constant
styler:ChangeState(HLA_CONSTANT)
elseif booleans[identifier] then
-- Boolean
styler:ChangeState(HLA_BOOLEAN)
elseif conditionals[identifier] then
-- Conditional
styler:ChangeState(HLA_CONDITIONAL)
elseif directives[identifier] then
-- Directive, emms
styler:ChangeState(HLA_DIRECTIVE)
elseif statements[_identifier] then
-- Statement, e.g. stdout.put
styler:ChangeState(HLA_STATEMENT)
elseif macros[identifier] then
-- Macro, e.g. #include
styler:ChangeState(HLA_MACRO)
elseif attributes[identifier] then
-- Attribute/function, e.g. @Size
styler:ChangeState(HLA_ATTRIBUTE)
elseif registers[identifier] then
-- Registers
styler:ChangeState(HLA_REGISTER)
end
styler:SetState(HLA_DEFAULT)
previousToken = identifier
end
if styler:State() == HLA_CHARACTER then
-- End of single quoted string
if styler:Match("'") then
-- Use ForwardSetState so that any state set in this branch will not be
-- undone when control reaches the lines below.
styler:ForwardSetState(HLA_DEFAULT)
end
elseif styler:State() == HLA_DOUBLE_STRING then
if styler:Match('"') then
-- End of double quoted string
styler:ForwardSetState(HLA_DEFAULT)
elseif styler:Line(styler:Position()) == editor.LineCount - 1 then
-- Highlighting an unclosed string as an error if it extends to EOF
styler:ChangeState(HLA_ERROR)
end
elseif (styler:Current():find('[^0-9.eE_+-]') or (styler:Current() == '.' and
styler:Next() == '.') or (styler:Current():find('[+-]') and
(styler:Previous():find('[^eE]') or styler:Next():find('[^0-9]'))))
and (styler:State() == HLA_NUMBER) then
-- Stop colouring numbers
styler:SetState(HLA_DEFAULT)
elseif styler:State() == HLA_OPERATOR then
-- Stop colouring operators
styler:SetState(HLA_DEFAULT)
elseif styler:Current() == "/" and styler:Previous() == "*" then
-- Stop colouring a multiline comment
styler:ForwardSetState(HLA_DEFAULT)
elseif styler:Current():find('[^01_]') and styler:State() == HLA_BINARY_NUMBER then
-- Stop colouring binary numbers
styler:SetState(HLA_DEFAULT)
elseif ((asciiInBinaryForm and styler:Current():find('[^_01%%]')) or
(asciiInDecimalForm and styler:Current():find('[^0-9]')) or
styler:Current():find('[^0-9_$%%a-fA-f]')) and styler:State() == HLA_ASCII_CODE then
-- Colour an ascii character literal as a character and restore state to
-- default.
styler:ChangeState(HLA_CHARACTER)
styler:SetState(HLA_DEFAULT)
asciiInBinaryForm = false
asciiInDecimalForm = false
elseif styler:State() == HLA_HEX_NUMBER and styler:Current():find('[^0-9A-Fa-f_]') then
-- Colour a hexadecimal number and restore state to default
styler:ChangeState(HLA_NUMBER)
styler:SetState(HLA_DEFAULT)
end
if (charactersLeft < 0) and (styler:State() == HLA_CHARACTER) then
-- Colour the character string as an error because it has more than one
-- character
charactersLeft = 1
styler:ChangeState(HLA_ERROR)
end
if (styler:State() == HLA_ERROR) and (styler:Current() == "'") then
-- Restore the colour after highlighting the error
styler:ForwardSetState(HLA_DEFAULT)
end
--~~~~~~~~~~~~~~~~~~~~~~~~~~~[ Switch ON states ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if styler:State() == HLA_DEFAULT then
if styler:Match("'") then
-- Found character
charactersLeft = 1
styler:SetState(HLA_CHARACTER)
elseif styler:Match('"') then
-- Start of double quoted string
styler:SetState(HLA_DOUBLE_STRING)
elseif styler:Match("//") then
-- Start of a line comment
styler:SetState(HLA_LINE_COMMENT)
elseif styler:Match("/*") then
-- Start of a Multiline comment
styler:SetState(HLA_MULTILINE_COMMENT)
elseif styler:Current() == '%' then
-- Start of a binary number
styler:SetState(HLA_BINARY_NUMBER)
elseif identifierStarts:find(styler:Current(), 1, true) and
styler:Next():find('[^%%$0-9]') then
-- Start of an identifier
styler:SetState(HLA_IDENTIFIER)
elseif digits:find(styler:Current(), 1, true) then
-- Start of a Number
styler:SetState(HLA_NUMBER)
elseif operators:find(styler:Current(), 1, true) then
-- Start of an operator
styler:SetState(HLA_OPERATOR)
elseif styler:Current() == '#' and styler:Next():find('[$%%0-9]') then
-- Start of a character literal in numerical form
if styler:Next() == '%' then
asciiInBinaryForm = true
elseif styler:Next():find('[0-9]') then
asciiInDecimalForm = true
end
styler:SetState(HLA_ASCII_CODE)
elseif styler:Current() == '$' then
styler:SetState(HLA_HEX_NUMBER)
end
end
if IsTaskMarker(styler) and (styler:State() == HLA_LINE_COMMENT or
styler:State() == HLA_MULTILINE_COMMENT) then
-- Found a task marker, save the current state(comment type)
previousState = styler:State()
styler:SetState(HLA_IDENTIFIER)
end
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Move forward one character
FoldDocument(styler)
styler:Forward()
end
-- Call it a night
styler:EndStyling()
end
function IsTaskMarker(styler)
for i, v in pairs(taskMarkers) do
-- The table looks like this, {['MARKER'] = true, ....}
if styler:Match(i) then
return true
end
end
return false
end
--[[
SC_FOLDLEVELHEADERFLAG = 0x2000 | 8192 | 0b10000000000000
SC_FOLDLEVELWHITEFLAG = 0x1000 | 4096 | 0b1000000000000
SC_FOLDLEVELNUMBERMASK = 0xFFF | 4095 | 0b111111111111
SC_FOLDLEVELBASE = 0x400 | 1024 | 0b10000000000
]]
function IsNotWhitespace(char)
local ord = string.byte(char)
return not ((ord == 32) or ((ord >= 9) and (ord <= 13)))
end
visibleChars = 0
currentLevel = 0 -- A global variable to track fold levels
prevFoldLevel = currentLevel
function FoldDocument(styler)
-- Called every time the styler loop moves forward one character
-- Folding is done at the end of the line
local charOffset = styler:Position()
local lineNumber = styler:Line(charOffset)
local currStyle = styler:State()
local currChar = styler:Current()
--~~~~~~~~~~~~~~~~~~~~~~~~[ Track foldpoints ]~~~~~~~~~~~~~~~
if currStyle == HLA_IDENTIFIER then
if styler:Match("begin") then
currentLevel = currentLevel + 1
elseif styler:Match("end") then
currentLevel = currentLevel - 1
end
end
if currStyle == HLA_OPERATOR then
-- Fold brackets
if currChar:find("[{(]") then
currentLevel = currentLevel + 1
elseif currChar:find("[})]") then
currentLevel = currentLevel - 1
end
end
if currStyle == HLA_MULTILINE_COMMENT then
-- Fold multiline comments
if styler:Match("/*") then
currentLevel = currentLevel + 1
elseif styler:Match("*/") then
currentLevel = currentLevel - 1
end
end
if currentLevel < 0 then
-- Prevent the fold level from dropping below zero. It won't work as
-- expected without this
currentLevel = 0
end
--~~~~~~~~~~~~~~~~~~~~~~~~~[ FOLD ]~~~~~~~~~~~~~~~~~~~~~~~~~~
lev = prevFoldLevel
if styler:AtLineEnd() then
-- All folding is done at the end of the line
if (visibleChars == 0) and (foldCompact == '1') then
-- Don't fold this line because it's all whitespace
lev = lev + SC_FOLDLEVELWHITEFLAG
end
if (currentLevel > prevFoldLevel) then
lev = lev + SC_FOLDLEVELHEADERFLAG
end
if lev ~= styler:LevelAt(lineNumber) then
styler:SetLevelAt(lineNumber, lev)
end
prevFoldLevel = currentLevel
visibleChars = 0
end
if IsNotWhitespace(currChar) then
visibleChars = visibleChars + 1
end
end