vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

nsis.lua

(7997B)


      1 -- Copyright 2006-2025 Robert Gieseke. See LICENSE.
      2 -- NSIS LPeg lexer
      3 -- Based on NSIS 2.46 docs: http://nsis.sourceforge.net/Docs/.
      4 
      5 local lexer = require('lexer')
      6 local token, word_match = lexer.token, lexer.word_match
      7 local P, S = lpeg.P, lpeg.S
      8 
      9 local lex = lexer.new('nsis')
     10 
     11 -- Whitespace.
     12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     13 
     14 -- Comments (4.1).
     15 local line_comment = lexer.to_eol(S(';#'))
     16 local block_comment = lexer.range('/*', '*/')
     17 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     18 
     19 -- Strings.
     20 local sq_str = lexer.range("'")
     21 local dq_str = lexer.range('"')
     22 local bq_str = lexer.range('`')
     23 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str + bq_str))
     24 
     25 -- Constants (4.2.3).
     26 lex:add_rule('constant', token(lexer.CONSTANT, word_match{
     27 	'$PROGRAMFILES', '$PROGRAMFILES32', '$PROGRAMFILES64', '$COMMONFILES', '$COMMONFILES32',
     28 	'$COMMONFILES64', '$DESKTOP', '$EXEDIR', '$EXEFILE', '$EXEPATH', '${NSISDIR}', '$WINDIR',
     29 	'$SYSDIR', '$TEMP', '$STARTMENU', '$SMPROGRAMS', '$SMSTARTUP', '$QUICKLAUNCH$DOCUMENTS',
     30 	'$SENDTO', '$RECENT', '$FAVORITES', '$MUSIC', '$PICTURES', '$VIDEOS', '$NETHOOD', '$FONTS',
     31 	'$TEMPLATES', '$APPDATA', '$LOCALAPPDATA', '$PRINTHOOD', '$INTERNET_CACHE', '$COOKIES',
     32 	'$HISTORY', '$PROFILE', '$ADMINTOOLS', '$RESOURCES', '$RESOURCES_LOCALIZED', '$CDBURN_AREA',
     33 	'$HWNDPARENT', '$PLUGINSDIR'
     34 }))
     35 -- TODO? Constants used in strings: $$ $\r $\n $\t
     36 
     37 -- Variables (4.2).
     38 lex:add_rule('variable', token(lexer.VARIABLE, word_match{
     39 	'$0', '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$R0', '$R1', '$R2', '$R3', '$R4',
     40 	'$R5', '$R6', '$R7', '$R8', '$R9', '$INSTDIR', '$OUTDIR', '$CMDLINE', '$LANGUAGE', 'Var',
     41 	'/GLOBAL'
     42 } + '$' * lexer.word))
     43 
     44 -- Keywords.
     45 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
     46 	-- Pages (4.5).
     47 	'Page', 'UninstPage', 'PageEx', 'PageEnd', 'PageExEnd',
     48 	-- Section commands (4.6).
     49 	'AddSize', 'Section', 'SectionEnd', 'SectionIn', 'SectionGroup', 'SectionGroupEnd',
     50 	-- Functions (4.7).
     51 	'Function', 'FunctionEnd',
     52 	-- Callbacks (4.7.2).
     53 	'.onGUIInit', '.onInit', '.onInstFailed', '.onInstSuccess', '.onGUIEnd', '.onMouseOverSection',
     54 	'.onRebootFailed', '.onSelChange', '.onUserAbort', '.onVerifyInstDir', 'un.onGUIInit',
     55 	'un.onInit', 'un.onUninstFailed', 'un.onUninstSuccess', 'un.onGUIEnd', 'un.onRebootFailed',
     56 	'un.onSelChange', 'un.onUserAbort',
     57 	-- General Attributes (4.8.1).
     58 	'AddBrandingImage', 'AllowRootDirInstall', 'AutoCloseWindow', 'BGFont', 'BGFont', 'BrandingText',
     59 	'/TRIMLEFT', '/TRIMRIGHT', '/TRIMCENTER', 'Caption', 'ChangeUI', 'CheckBitmap', 'CompletedText',
     60 	'ComponentText', 'CRCCheck', 'DetailsButtonText', 'DirText', 'DirVar', 'DirVerify',
     61 	'FileErrorText', 'Icon', 'InstallButtonText', 'InstallColors', 'InstallDir', 'InstallDirRegKey',
     62 	'InstProgressFlags', 'InstType', 'LicenseBkColor', 'LicenseData', 'LicenseForceSelection',
     63 	'LicenseText', 'MiscButtonText', 'Name', 'OutFile', 'RequestExecutionLevel', 'SetFont',
     64 	'ShowInstDetails', 'ShowUninstDetails', 'SilentInstall', 'SilentUnInstall', 'SpaceTexts',
     65 	'SubCaption', 'UninstallButtonText', 'UninstallCaption', 'UninstallIcon', 'UninstallSubCaption',
     66 	'UninstallText', 'WindowIcon', 'XPStyle', 'admin', 'auto', 'bottom', 'checkbox', 'false', 'force',
     67 	'height', 'hide', 'highest', 'leave', 'left', 'nevershow', 'none', 'normal', 'off', 'on',
     68 	'radiobuttons', 'right', 'show', 'silent', 'silentlog', 'top', 'true', 'user', 'width',
     69 	-- Compiler Flags (4.8.2).
     70 	'AllowSkipFiles', 'FileBufSize', 'SetCompress', 'SetCompressor', '/SOLID', '/FINAL', 'zlib',
     71 	'bzip2', 'lzma', 'SetCompressorDictSize', 'SetDatablockOptimize', 'SetDateSave', 'SetOverwrite',
     72 	'ifnewer', 'ifdiff', 'lastused', 'try',
     73 	-- Version Information (4.8.3).
     74 	'VIAddVersionKey', 'VIProductVersion', '/LANG', 'ProductName', 'Comments', 'CompanyName',
     75 	'LegalCopyright', 'FileDescription', 'FileVersion', 'ProductVersion', 'InternalName',
     76 	'LegalTrademarks', 'OriginalFilename', 'PrivateBuild', 'SpecialBuild',
     77 	-- Basic Instructions (4.9.1).
     78 	'Delete', '/REBOOTOK', 'Exec', 'ExecShell', 'ExecShell', 'File', '/nonfatal', 'Rename',
     79 	'ReserveFile', 'RMDir', 'SetOutPath',
     80 	-- Registry INI File Instructions (4.9.2).
     81 	'DeleteINISec', 'DeleteINIStr', 'DeleteRegKey', '/ifempty', 'DeleteRegValue', 'EnumRegKey',
     82 	'EnumRegValue', 'ExpandEnvStrings', 'FlushINI', 'ReadEnvStr', 'ReadINIStr', 'ReadRegDWORD',
     83 	'ReadRegStr', 'WriteINIStr', 'WriteRegBin', 'WriteRegDWORD', 'WriteRegStr', 'WriteRegExpandStr',
     84 	'HKCR', 'HKEY_CLASSES_ROOT', 'HKLM', 'HKEY_LOCAL_MACHINE', 'HKCU', 'HKEY_CURRENT_USER', 'HKU',
     85 	'HKEY_USERS', 'HKCC', 'HKEY_CURRENT_CONFIG', 'HKDD', 'HKEY_DYN_DATA', 'HKPD',
     86 	'HKEY_PERFORMANCE_DATA', 'SHCTX', 'SHELL_CONTEXT',
     87 	-- General Purpose Instructions (4.9.3).
     88 	'CallInstDLL', 'CopyFiles', '/SILENT', '/FILESONLY', 'CreateDirectory', 'CreateShortCut',
     89 	'GetDLLVersion', 'GetDLLVersionLocal', 'GetFileTime', 'GetFileTimeLocal', 'GetFullPathName',
     90 	'/SHORT', 'GetTempFileName', 'SearchPath', 'SetFileAttributes', 'RegDLL', 'UnRegDLL',
     91 	-- Flow Control Instructions (4.9.4).
     92 	'Abort', 'Call', 'ClearErrors', 'GetCurrentAddress', 'GetFunctionAddress', 'GetLabelAddress',
     93 	'Goto', 'IfAbort', 'IfErrors', 'IfFileExists', 'IfRebootFlag', 'IfSilent', 'IntCmp', 'IntCmpU',
     94 	'MessageBox', 'MB_OK', 'MB_OKCANCEL', 'MB_ABORTRETRYIGNORE', 'MB_RETRYCANCEL', 'MB_YESNO',
     95 	'MB_YESNOCANCEL', 'MB_ICONEXCLAMATION', 'MB_ICONINFORMATION', 'MB_ICONQUESTION', 'MB_ICONSTOP',
     96 	'MB_USERICON', 'MB_TOPMOST', 'MB_SETFOREGROUND', 'MB_RIGHT', 'MB_RTLREADING', 'MB_DEFBUTTON1',
     97 	'MB_DEFBUTTON2', 'MB_DEFBUTTON3', 'MB_DEFBUTTON4', 'IDABORT', 'IDCANCEL', 'IDIGNORE', 'IDNO',
     98 	'IDOK', 'IDRETRY', 'IDYES', 'Return', 'Quit', 'SetErrors', 'StrCmp', 'StrCmpS',
     99 	-- File Instructions (4.9.5).
    100 	'FileClose', 'FileOpen', 'FileRead', 'FileReadByte', 'FileSeek', 'FileWrite', 'FileWriteByte',
    101 	'FindClose', 'FindFirst', 'FindNext',
    102 	-- Uninstaller Instructions (4.9.6).
    103 	'WriteUninstaller',
    104 	-- Miscellaneous Instructions (4.9.7).
    105 	'GetErrorLevel', 'GetInstDirError', 'InitPluginsDir', 'Nop', 'SetErrorLevel', 'SetRegView',
    106 	'SetShellVarContext', 'all', 'current', 'Sleep',
    107 	-- String Manipulation Instructions (4.9.8).
    108 	'StrCpy', 'StrLen',
    109 	-- Stack Support (4.9.9).
    110 	'Exch', 'Pop', 'Push',
    111 	-- Integer Support (4.9.10).
    112 	'IntFmt', 'IntOp',
    113 	-- Reboot Instructions (4.9.11).
    114 	'Reboot', 'SetRebootFlag',
    115 	-- Install Logging Instructions (4.9.12).
    116 	'LogSet', 'LogText',
    117 	-- Section Management (4.9.13).
    118 	'SectionSetFlags', 'SectionGetFlags', 'SectionGetFlags', 'SectionSetText', 'SectionGetText',
    119 	'SectionSetInstTypes', 'SectionGetInstTypes', 'SectionSetSize', 'SectionGetSize',
    120 	'SetCurInstType', 'GetCurInstType', 'InstTypeSetText', 'InstTypeGetText',
    121 	-- User Interface Instructions (4.9.14).
    122 	'BringToFront', 'CreateFont', 'DetailPrint', 'EnableWindow', 'FindWindow', 'GetDlgItem',
    123 	'HideWindow', 'IsWindow', 'LockWindow', 'SendMessage', 'SetAutoClose', 'SetBrandingImage',
    124 	'SetDetailsView', 'SetDetailsPrint', 'listonlytextonly', 'both', 'SetCtlColors', '/BRANDING',
    125 	'SetSilent', 'ShowWindow',
    126 	-- Multiple Languages Instructions (4.9.15).
    127 	'LoadLanguageFile', 'LangString', 'LicenseLangString',
    128 	-- Compile time commands (5).
    129 	'!include', '!addincludedir', '!addplugindir', '!appendfile', '!cd', '!delfile', '!echo',
    130 	'!error', '!execute', '!packhdr', '!system', '!tempfile', '!warning', '!verbose', '{__FILE__}',
    131 	'{__LINE__}', '{__DATE__}', '{__TIME__}', '{__TIMESTAMP__}', '{NSIS_VERSION}', '!define',
    132 	'!undef', '!ifdef', '!ifndef', '!if', '!ifmacrodef', '!ifmacrondef', '!else', '!endif',
    133 	'!insertmacro', '!macro', '!macroend', '!searchparse', '!searchreplace'
    134 }))
    135 
    136 -- Numbers.
    137 lex:add_rule('number', token(lexer.NUMBER, lexer.integer))
    138 
    139 -- Operators.
    140 lex:add_rule('operator', token(lexer.OPERATOR, S('+-*/%|&^~!<>')))
    141 
    142 -- Labels (4.3).
    143 lex:add_rule('label', token(lexer.LABEL, lexer.word * ':'))
    144 
    145 -- Identifiers.
    146 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
    147 
    148 lexer.property['scintillua.comment'] = '#'
    149 
    150 return lex