Commits

Tim Hatch committed 11aad97

Add new Autohotkey lexer (#417)

  • Participants
  • Parent commits 76c9dc7

Comments (0)

Files changed (3)

 * Nick Efford -- Python 3 lexer
 * Artem Egorkine -- terminal256 formatter
 * James H. Fisher -- PostScript lexer
+* Naveen Garg - Autohotkey lexer
 * Laurent Gautier -- R/S lexer
 * Krzysiek Goj -- Scala lexer
 * Matt Good -- Genshi, Cheetah lexers

File pygments/lexers/other.py

             (r'([0-8]{3}|n|r|t|b|f|\\|\(|\)|)', String.Escape, '#pop'),
         ],
     }
+
+
+class AutohotkeyLexer(RegexLexer):
+    """
+    For `autohotkey <http://www.autohotkey.com/>`_ source code.
+
+    *New in Pygments 1.4.*
+    """
+    name = 'autohotkey'
+    aliases = ['ahk']
+    filenames = ['*.ahk', '*.ahkl']
+    mimetypes = ['text/x-autohotkey']
+
+    flags = re.IGNORECASE | re.DOTALL | re.MULTILINE
+
+    tokens = {
+        'root': [
+            include('whitespace'),
+            (r'^\(', String, 'continuation'),
+            include('comments'),
+            (r'(^\s*)(\w+)(\s*)(=)',
+             bygroups(Text.Whitespace, Name, Text.Whitespace, Operator),
+             'command'),
+            (r'([\w#@$?\[\]]+)(\s*)(\()',
+             bygroups(Name.Function, Text.Whitespace, Punctuation),
+             'parameters'),
+            include('directives'),
+            include('labels'),
+            include('commands'),
+            include('expressions'),
+            include('numbers'),
+            include('literals'),
+            include('keynames'),
+            include('keywords'),
+        ],
+        'command': [
+            include('comments'),
+            include('whitespace'),
+            (r'^\(', String, 'continuation'),
+            (r'[^\n]*?(?=;*|$)', String, '#pop'),
+            include('numbers'),
+            include('literals'),
+        ],
+
+        'expressions': [
+            include('comments'),
+            include('whitespace'),
+            include('numbers'),
+            include('literals'),
+            (r'([]\w#@$?[]+)(\s*)(\()',
+             bygroups(Name.Function, Text.Whitespace, Punctuation),
+             'parameters'),
+            (r'A_\w+', Name.Builtin),
+            (r'%[]\w#@$?[]+?%', Name.Variable),
+            # blocks: if, else, function definitions
+            (r'{', Punctuation, 'block'),
+            # parameters in function calls
+            ],
+        'literals': [
+            (r'"', String, 'string'),
+            (r'A_\w+', Name.Builtin),
+            (r'%[]\w#@$?[]+?%', Name.Variable),
+            (r'[-~!%^&*+|?:<>/=]=?', Operator, 'expressions'),
+            (r'==', Operator, 'expressions'),
+            ('[{()},.%#`;]', Punctuation),
+            (r'\\', Punctuation),
+            include('keywords'),
+            (r'\w+', Text),
+            ],
+        'string': [
+            (r'"', String, '#pop'),
+            (r'""|`.', String.Escape),
+            (r'[^\`"\n]+', String), # all other characters
+        ],
+        'block': [
+            include('root'),
+            ('{', Punctuation, '#push'),
+            ('}', Punctuation, '#pop'),
+        ],
+        'parameters': [
+            (r'\)', Punctuation, '#pop'),
+            (r'\(', Punctuation, '#push'),
+            include('numbers'),
+            include('literals'),
+            include('whitespace'),
+        ],
+        'keywords': [
+            (r'(static|global|local)\b', Keyword.Type),
+            (r'(if|else|and|or)\b', Keyword.Reserved),
+            ],
+        'directives': [
+            (r'#\w+?\s', Keyword),
+            ],
+        'labels': [
+            # hotkeys and labels
+            # technically, hotkey names are limited to named keys and buttons
+            (r'(^\s*)([^:\s]+?:{1,2})', bygroups(Text.Whitespace, Name.Label)),
+             # hotstrings
+            (r'(^\s*)(::[]\w#@$?[]+?::)', bygroups(Text.Whitespace, Name.Label)),
+            ],
+        'comments': [
+            (r'^;+.*?$', Comment.Single),  # beginning of line comments
+            (r'(?<=\s);+.*?$', Comment.Single),    # end of line comments
+            (r'^/\*.*?\n\*/', Comment.Multiline),
+            (r'(?<!\n)/\*.*?\n\*/', Error),  # must be at start of line
+            ],
+        'whitespace': [
+            (r'[ \t]+', Text.Whitespace),
+            ],
+        'numbers': [
+            (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
+            (r'\d+[eE][+-]?[0-9]+', Number.Float),
+            (r'0[0-7]+', Number.Oct),
+            (r'0[xX][a-fA-F0-9]+', Number.Hex),
+            (r'\d+L', Number.Integer.Long),
+            (r'\d+', Number.Integer)
+        ],
+        'continuation': [
+            (r'\n\)', Punctuation, '#pop'),
+            (r'\s[^\n\)]+', String),
+        ],
+        'keynames': [
+            (r'\[[^\]]+\]', Keyword, 'keynames')
+        ],
+        'commands': [
+            (r'(autotrim|blockinput|break|click|'
+             r'clipwait|continue|control|'
+             r'controlclick|controlfocus|controlget|'
+             r'controlgetfocus|controlgetpos|controlgettext|'
+             r'controlmove|controlsend|controlsendraw|'
+             r'controlsettext|coordmode|critical|'
+             r'detecthiddentext|detecthiddenwindows|'
+             r'dllcall|drive|'
+             r'driveget|drivespacefree|'
+             r'else|envadd|envdiv|'
+             r'envget|envmult|envset|'
+             r'envsub|envupdate|exit|'
+             r'exitapp|fileappend|filecopy|'
+             r'filecopydir|filecreatedir|filecreateshortcut|'
+             r'filedelete|filegetattrib|filegetshortcut|'
+             r'filegetsize|filegettime|filegetversion|'
+             r'fileinstall|filemove|filemovedir|'
+             r'fileread|filereadline|filerecycle|'
+             r'filerecycleempty|fileremovedir|fileselectfile|'
+             r'fileselectfolder|filesetattrib|filesettime|'
+             r'formattime|gosub|'
+             r'goto|groupactivate|groupadd|'
+             r'groupclose|groupdeactivate|gui|'
+             r'guicontrol|guicontrolget|hotkey|'
+             r'ifexist|ifgreater|ifgreaterorequal|'
+             r'ifinstring|ifless|iflessorequal|'
+             r'ifmsgbox|ifnotequal|ifnotexist|'
+             r'ifnotinstring|ifwinactive|ifwinexist|'
+             r'ifwinnotactive|ifwinnotexist|imagesearch|'
+             r'inidelete|iniread|iniwrite|'
+             r'input|inputbox|keyhistory|'
+             r'keywait|listhotkeys|listlines|'
+             r'listvars|loop|'
+             r'menu|mouseclick|mouseclickdrag|'
+             r'mousegetpos|mousemove|msgbox|'
+             r'onmessage|onexit|outputdebug|'
+             r'pixelgetcolor|pixelsearch|postmessage|'
+             r'process|progress|random|'
+             r'regexmatch|regexreplace|registercallback|'
+             r'regdelete|regread|regwrite|'
+             r'reload|repeat|return|'
+             r'run|runas|runwait|'
+             r'send|sendevent|sendinput|'
+             r'sendmessage|sendmode|sendplay|'
+             r'sendraw|setbatchlines|setcapslockstate|'
+             r'setcontroldelay|setdefaultmousespeed|setenv|'
+             r'setformat|setkeydelay|setmousedelay|'
+             r'setnumlockstate|setscrolllockstate|'
+             r'setstorecapslockmode|'
+             r'settimer|settitlematchmode|setwindelay|'
+             r'setworkingdir|shutdown|sleep|'
+             r'sort|soundbeep|soundget|'
+             r'soundgetwavevolume|soundplay|soundset|'
+             r'soundsetwavevolume|splashimage|splashtextoff|'
+             r'splashtexton|splitpath|statusbargettext|'
+             r'statusbarwait|stringcasesense|stringgetpos|'
+             r'stringleft|stringlen|stringlower|'
+             r'stringmid|stringreplace|stringright|'
+             r'stringsplit|stringtrimleft|stringtrimright|'
+             r'stringupper|suspend|sysget|'
+             r'thread|tooltip|transform|'
+             r'traytip|urldownloadtofile|while|'
+             r'varsetcapacity|'
+             r'winactivate|winactivatebottom|winclose|'
+             r'winget|wingetactivestats|wingetactivetitle|'
+             r'wingetclass|wingetpos|wingettext|'
+             r'wingettitle|winhide|winkill|'
+             r'winmaximize|winmenuselectitem|winminimize|'
+             r'winminimizeall|winminimizeallundo|winmove|'
+             r'winrestore|winset|winsettitle|'
+             r'winshow|winwait|winwaitactive|'
+             r'winwaitclose|winwaitnotactive'
+             r'true|false|NULL)\b', Keyword, 'command'),
+            ],
+
+        }
+

File tests/examplefiles/demo.ahk

+/*
+multiline comment
+msgbox(comment)
+*/
+send, key[pgdn]
+string := "hello" . x . "world!"
+string := "hello ""world""! "
+string := "hello `"world""! "
+; single line comment1
+;;;  single line comment2
+
+::stopi::viper_off()  
+
+a::send, ^a
+mylabel:send, ^{space}  ;; set mark
+e::send, ^e
+n::
+  send, ^n
+  return
+!i::     
+viper("iviper")   ; stdlib
+x = "viper"" "  ; escaped quote
+Return
+#If WinExist("iviper" )
+indexdir = %A_ScriptDir%\%dir%
+FileCreateDir, % indexdir
+fileindex = %indexdir%\_files
+FileSelectFile, file,,, Select an image:, Images (*.gif; *.jpg; *.bmp; *.png; *.tif; *.ico; *.cur; *.ani; *.exe; *.dll)
+
+; viper
+
+
+i::viper_off() 
+#If
+
+;; keybindings
+#If WinExist("iviper") and WinActive("ahk_class Emacs")
+
+p::
+k::
+send, ^p
+return
+,::send, +!,  ;; beginning of page
+.::send, +!.  ;; end of page
+[::send, !a
+]::send, !e  
+d:: ^k  ;; kill line
+x:: send ^d
+\:: ^!k  ;; kill next word or sexp
+
+
+#IfWinActive
+#Persistent
+  
+F2::     ;; hotkey
+start:   ;; label
+start2:   ; label
+  ppm := ppm_new(50, 50, 255)    
+  ppm_fill(ppm, 80, 90, 95)      
+  msgbox % getPixel(ppm, 1, 1)    
+  setPixel(90, 90, 90, ppm, 1, 1)
+  msgbox % getPixel(ppm, 1, 1)
+  ListVars  ; command
+  msgbox % ppm
+  return
+  
+  
+  ppm_read(file)
+  {
+	fileread, ppm, % file
+ return ppm
+}
+
+::hotstring::
+::hot3::
+ppm_width(ppm)
+{
+ regexmatch(ppm, "\R(\d+)\s(\d+)", dim)
+ return dim1
+}
+ppm_height(ppm)
+{
+ regexmatch(ppm, "\R(\d+)\s(\d+)", dim)
+    return dim2
+} 
+
+ppm_colors(ppm)
+{
+regexmatch(ppm, "\R(\d+)\D*\R", colors)  ; \R stands for any
+return colors1
+}
+
+ppm_data(ppm)
+{
+pos :=  regexmatch(ppm, "\R(\d+)\D*\R", colors)  ; \R stands for any newline
+stringtrimleft, data, ppm, pos + strlen(colors1)
+return data
+}
+ppm_header(ppm)
+{
+pos :=  regexmatch(ppm, "\R(\d+)\D*\R", colors)  ; \R stands for any newline
+stringleft, header, ppm, pos + strlen(colors1)
+return header
+}
+
+ppm_fill(ByRef ppm, r, g, b)
+{
+  width := ppm_width(ppm)	
+  height := ppm_height(ppm)
+  header := ppm_header(ppm)
+  headerLength := strlen(header) 
+  varsetcapacity(data, width * height, 0)
+  loop, % (width * height)
+  {
+	if r
+   numput(r, data, (A_Index - 1) * 3, "uchar")
+ if g
+   numput(g, data, (A_Index - 1) * 3 + 1, "uchar")
+ if b
+   numput(b, data, (A_Index - 1) * 3 + 2, "uchar")
+}
+VarCopy(&ppm + headerLength, &data, width * height)
+
+}
+
+ppm_new(width, height, colors)
+{
+  header = P6`n%width% %height%`n%colors%`n
+  headerLength := strlen(header)
+  varsetcapacity(ppm, width * height + headerLength, 1)
+  varsetcapacity(data, width * height, 0)
+  VarCopy(&ppm, &header, headerLength)
+  VarCopy(&ppm + headerLength, &data, width * height)
+  return ppm
+}
+
+heredoc = 
+(
+  P6
+  # lasdjkf
+  87 09
+  255
+  color data...
+)
+
+; Example: Simple image viewer:
+
+Gui, +Resize
+Gui, Add, Button, default, &Load New Image
+Gui, Add, Radio, ym+5 x+10 vRadio checked, Load &actual size
+Gui, Add, Radio, ym+5 x+10, Load to &fit screen
+Gui, Add, Pic, xm vPic
+Gui, Show
+return
+
+ButtonLoadNewImage:
+FileSelectFile, file,,, Select an image:, Images (*.gif; *.jpg; *.bmp; *.png; *.tif; *.ico; *.cur; *.ani; *.exe; *.dll)
+if file =
+    return
+Gui, Submit, NoHide ; Save the values of the radio buttons.
+if Radio = 1  ; Display image at its actual size.
+{
+    Width = 0
+    Height = 0
+}
+else ; Second radio is selected: Resize the image to fit the screen.
+{
+    Width := A_ScreenWidth - 28  ; Minus 28 to allow room for borders and margins inside.
+    Height = -1  ; "Keep aspect ratio" seems best.
+}
+GuiControl,, Pic, *w%width% *h%height% %file%  ; Load the image.
+Gui, Show, xCenter y0 AutoSize, %file%  ; Resize the window to match the picture size.
+return
+
+GuiClose:
+ExitApp
+; Example: Simple text editor with menu bar.
+
+; Create the sub-menus for the menu bar:
+Menu, FileMenu, Add, &New, FileNew
+