Skip to content

Commit

Permalink
Migrated AutoIt lexer.
Browse files Browse the repository at this point in the history
Thanks to Snoopy.
  • Loading branch information
orbitalquark committed Nov 20, 2022
1 parent 6e9c610 commit ba11af4
Showing 1 changed file with 48 additions and 41 deletions.
89 changes: 48 additions & 41 deletions lexers/autoit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,60 @@
-- AutoIt LPeg lexer.
-- Contributed by Jeff Stone.

local lexer = require('lexer')
local token, word_match = lexer.token, lexer.word_match
local P, S = lpeg.P, lpeg.S
local lexer = lexer
local P, S, B = lpeg.P, lpeg.S, lpeg.B

local lex = lexer.new('autoit')

-- Whitespace.
lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
local lex = lexer.new(...)

-- Keywords.
lex:add_rule('keyword', token(lexer.KEYWORD, word_match({
lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD, true)))

-- Functions.
local builtin_func = -B('.') *
lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN, true))
local func = lex:tag(lexer.FUNCTION, lexer.word)
local method = B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
lex:add_rule('function', (builtin_func + method + func) * #(lexer.space^0 * '('))

-- Identifiers.
lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))

-- Comments.
local line_comment = lexer.to_eol(';')
local block_comment = lexer.range('#comments-start', '#comments-end') + lexer.range('#cs', '#ce')
lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))

-- Preprocessor.
lex:add_rule('preprocessor',
lex:tag(lexer.PREPROCESSOR, '#' * lex:word_match(lexer.PREPROCESSOR, true)))

-- Strings.
local dq_str = lexer.range('"', true, false)
local sq_str = lexer.range("'", true, false)
local inc = lexer.range('<', '>', true, false, true)
lex:add_rule('string', lex:tag(lexer.STRING, dq_str + sq_str + inc))

-- Macros.
lex:add_rule('macro', lex:tag(lexer.CONSTANT_BUILTIN, '@' * (lexer.alnum + '_')^1))

-- Variables.
lex:add_rule('variable', lex:tag(lexer.VARIABLE, '$' * (lexer.alnum + '_')^1))

-- Numbers.
lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number))

-- Operators.
lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-^*/&<>=?:()[]')))

lex:set_word_list(lexer.KEYWORD, {
'False', 'True', 'And', 'Or', 'Not', 'ContinueCase', 'ContinueLoop', 'Default', 'Dim', 'Global',
'Local', 'Const', 'Do', 'Until', 'Enum', 'Exit', 'ExitLoop', 'For', 'To', 'Step', 'Next', 'In',
'Func', 'Return', 'EndFunc', 'If', 'Then', 'ElseIf', 'Else', 'EndIf', 'Null', 'ReDim', 'Select',
'Case', 'EndSelect', 'Static', 'Switch', 'EndSwitch', 'Volatile', 'While', 'WEnd', 'With',
'EndWith'
}, true)))
})

-- Functions.
lex:add_rule('function', token(lexer.FUNCTION_BUILTIN, word_match({
lex:set_word_list(lexer.FUNCTION_BUILTIN, {
'Abs', 'ACos', 'AdlibRegister', 'AdlibUnRegister', 'Asc', 'AscW', 'ASin', 'Assign', 'ATan',
'AutoItSetOption', 'AutoItWinGetTitle', 'AutoItWinSetTitle', 'Beep', 'Binary', 'BinaryLen',
'BinaryMid', 'BinaryToString', 'BitAND', 'BitNOT', 'BitOR', 'BitRotate', 'BitShift', 'BitXOR',
Expand Down Expand Up @@ -91,39 +125,12 @@ lex:add_rule('function', token(lexer.FUNCTION_BUILTIN, word_match({
'WinGetState', 'WinGetText', 'WinGetTitle', 'WinKill', 'WinList', 'WinMenuSelectItem',
'WinMinimizeAll', 'WinMinimizeAllUndo', 'WinMove', 'WinSetOnTop', 'WinSetState', 'WinSetTitle',
'WinSetTrans', 'WinWait', 'WinWaitActive', 'WinWaitClose', 'WinWaitNotActive'
}, true)))

-- Identifiers.
lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))

-- Comments.
local line_comment = lexer.to_eol(';')
local block_comment = lexer.range('#comments-start', '#comments-end') + lexer.range('#cs', '#ce')
lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
})

-- Preprocessor.
lex:add_rule('preprocessor', token(lexer.PREPROCESSOR, '#' * word_match({
lex:set_word_list(lexer.PREPROCESSOR, {
'include-once', 'include', 'pragma', 'forceref', 'RequireAdmin', 'NoTrayIcon',
'OnAutoItStartRegister'
}, true)))

-- Strings.
local dq_str = lexer.range('"', true, false)
local sq_str = lexer.range("'", true, false)
local inc = lexer.range('<', '>', true, false, true)
lex:add_rule('string', token(lexer.STRING, dq_str + sq_str + inc))

-- Macros.
lex:add_rule('macro', token(lexer.CONSTANT_BUILTIN, '@' * (lexer.alnum + '_')^1))

-- Variables.
lex:add_rule('variable', token(lexer.VARIABLE, '$' * (lexer.alnum + '_')^1))

-- Numbers.
lex:add_rule('number', token(lexer.NUMBER, lexer.number))

-- Operators.
lex:add_rule('operator', token(lexer.OPERATOR, S('+-^*/&<>=?:()[]')))
})

lexer.property['scintillua.comment'] = ';'

Expand Down

0 comments on commit ba11af4

Please sign in to comment.