pascal.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. """
  2. pygments.lexers.pascal
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Pascal family languages.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import Lexer, RegexLexer, include, bygroups, words, \
  10. using, this, default
  11. from pygments.util import get_bool_opt, get_list_opt
  12. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  13. Number, Punctuation, Error
  14. from pygments.scanner import Scanner
  15. # compatibility import
  16. from pygments.lexers.modula2 import Modula2Lexer
  17. __all__ = ['DelphiLexer', 'AdaLexer']
  18. class DelphiLexer(Lexer):
  19. """
  20. For `Delphi <http://www.borland.com/delphi/>`_ (Borland Object Pascal),
  21. Turbo Pascal and Free Pascal source code.
  22. Additional options accepted:
  23. `turbopascal`
  24. Highlight Turbo Pascal specific keywords (default: ``True``).
  25. `delphi`
  26. Highlight Borland Delphi specific keywords (default: ``True``).
  27. `freepascal`
  28. Highlight Free Pascal specific keywords (default: ``True``).
  29. `units`
  30. A list of units that should be considered builtin, supported are
  31. ``System``, ``SysUtils``, ``Classes`` and ``Math``.
  32. Default is to consider all of them builtin.
  33. """
  34. name = 'Delphi'
  35. aliases = ['delphi', 'pas', 'pascal', 'objectpascal']
  36. filenames = ['*.pas', '*.dpr']
  37. mimetypes = ['text/x-pascal']
  38. TURBO_PASCAL_KEYWORDS = (
  39. 'absolute', 'and', 'array', 'asm', 'begin', 'break', 'case',
  40. 'const', 'constructor', 'continue', 'destructor', 'div', 'do',
  41. 'downto', 'else', 'end', 'file', 'for', 'function', 'goto',
  42. 'if', 'implementation', 'in', 'inherited', 'inline', 'interface',
  43. 'label', 'mod', 'nil', 'not', 'object', 'of', 'on', 'operator',
  44. 'or', 'packed', 'procedure', 'program', 'record', 'reintroduce',
  45. 'repeat', 'self', 'set', 'shl', 'shr', 'string', 'then', 'to',
  46. 'type', 'unit', 'until', 'uses', 'var', 'while', 'with', 'xor'
  47. )
  48. DELPHI_KEYWORDS = (
  49. 'as', 'class', 'except', 'exports', 'finalization', 'finally',
  50. 'initialization', 'is', 'library', 'on', 'property', 'raise',
  51. 'threadvar', 'try'
  52. )
  53. FREE_PASCAL_KEYWORDS = (
  54. 'dispose', 'exit', 'false', 'new', 'true'
  55. )
  56. BLOCK_KEYWORDS = {
  57. 'begin', 'class', 'const', 'constructor', 'destructor', 'end',
  58. 'finalization', 'function', 'implementation', 'initialization',
  59. 'label', 'library', 'operator', 'procedure', 'program', 'property',
  60. 'record', 'threadvar', 'type', 'unit', 'uses', 'var'
  61. }
  62. FUNCTION_MODIFIERS = {
  63. 'alias', 'cdecl', 'export', 'inline', 'interrupt', 'nostackframe',
  64. 'pascal', 'register', 'safecall', 'softfloat', 'stdcall',
  65. 'varargs', 'name', 'dynamic', 'near', 'virtual', 'external',
  66. 'override', 'assembler'
  67. }
  68. # XXX: those aren't global. but currently we know no way for defining
  69. # them just for the type context.
  70. DIRECTIVES = {
  71. 'absolute', 'abstract', 'assembler', 'cppdecl', 'default', 'far',
  72. 'far16', 'forward', 'index', 'oldfpccall', 'private', 'protected',
  73. 'published', 'public'
  74. }
  75. BUILTIN_TYPES = {
  76. 'ansichar', 'ansistring', 'bool', 'boolean', 'byte', 'bytebool',
  77. 'cardinal', 'char', 'comp', 'currency', 'double', 'dword',
  78. 'extended', 'int64', 'integer', 'iunknown', 'longbool', 'longint',
  79. 'longword', 'pansichar', 'pansistring', 'pbool', 'pboolean',
  80. 'pbyte', 'pbytearray', 'pcardinal', 'pchar', 'pcomp', 'pcurrency',
  81. 'pdate', 'pdatetime', 'pdouble', 'pdword', 'pextended', 'phandle',
  82. 'pint64', 'pinteger', 'plongint', 'plongword', 'pointer',
  83. 'ppointer', 'pshortint', 'pshortstring', 'psingle', 'psmallint',
  84. 'pstring', 'pvariant', 'pwidechar', 'pwidestring', 'pword',
  85. 'pwordarray', 'pwordbool', 'real', 'real48', 'shortint',
  86. 'shortstring', 'single', 'smallint', 'string', 'tclass', 'tdate',
  87. 'tdatetime', 'textfile', 'thandle', 'tobject', 'ttime', 'variant',
  88. 'widechar', 'widestring', 'word', 'wordbool'
  89. }
  90. BUILTIN_UNITS = {
  91. 'System': (
  92. 'abs', 'acquireexceptionobject', 'addr', 'ansitoutf8',
  93. 'append', 'arctan', 'assert', 'assigned', 'assignfile',
  94. 'beginthread', 'blockread', 'blockwrite', 'break', 'chdir',
  95. 'chr', 'close', 'closefile', 'comptocurrency', 'comptodouble',
  96. 'concat', 'continue', 'copy', 'cos', 'dec', 'delete',
  97. 'dispose', 'doubletocomp', 'endthread', 'enummodules',
  98. 'enumresourcemodules', 'eof', 'eoln', 'erase', 'exceptaddr',
  99. 'exceptobject', 'exclude', 'exit', 'exp', 'filepos', 'filesize',
  100. 'fillchar', 'finalize', 'findclasshinstance', 'findhinstance',
  101. 'findresourcehinstance', 'flush', 'frac', 'freemem',
  102. 'get8087cw', 'getdir', 'getlasterror', 'getmem',
  103. 'getmemorymanager', 'getmodulefilename', 'getvariantmanager',
  104. 'halt', 'hi', 'high', 'inc', 'include', 'initialize', 'insert',
  105. 'int', 'ioresult', 'ismemorymanagerset', 'isvariantmanagerset',
  106. 'length', 'ln', 'lo', 'low', 'mkdir', 'move', 'new', 'odd',
  107. 'olestrtostring', 'olestrtostrvar', 'ord', 'paramcount',
  108. 'paramstr', 'pi', 'pos', 'pred', 'ptr', 'pucs4chars', 'random',
  109. 'randomize', 'read', 'readln', 'reallocmem',
  110. 'releaseexceptionobject', 'rename', 'reset', 'rewrite', 'rmdir',
  111. 'round', 'runerror', 'seek', 'seekeof', 'seekeoln',
  112. 'set8087cw', 'setlength', 'setlinebreakstyle',
  113. 'setmemorymanager', 'setstring', 'settextbuf',
  114. 'setvariantmanager', 'sin', 'sizeof', 'slice', 'sqr', 'sqrt',
  115. 'str', 'stringofchar', 'stringtoolestr', 'stringtowidechar',
  116. 'succ', 'swap', 'trunc', 'truncate', 'typeinfo',
  117. 'ucs4stringtowidestring', 'unicodetoutf8', 'uniquestring',
  118. 'upcase', 'utf8decode', 'utf8encode', 'utf8toansi',
  119. 'utf8tounicode', 'val', 'vararrayredim', 'varclear',
  120. 'widecharlentostring', 'widecharlentostrvar',
  121. 'widechartostring', 'widechartostrvar',
  122. 'widestringtoucs4string', 'write', 'writeln'
  123. ),
  124. 'SysUtils': (
  125. 'abort', 'addexitproc', 'addterminateproc', 'adjustlinebreaks',
  126. 'allocmem', 'ansicomparefilename', 'ansicomparestr',
  127. 'ansicomparetext', 'ansidequotedstr', 'ansiextractquotedstr',
  128. 'ansilastchar', 'ansilowercase', 'ansilowercasefilename',
  129. 'ansipos', 'ansiquotedstr', 'ansisamestr', 'ansisametext',
  130. 'ansistrcomp', 'ansistricomp', 'ansistrlastchar', 'ansistrlcomp',
  131. 'ansistrlicomp', 'ansistrlower', 'ansistrpos', 'ansistrrscan',
  132. 'ansistrscan', 'ansistrupper', 'ansiuppercase',
  133. 'ansiuppercasefilename', 'appendstr', 'assignstr', 'beep',
  134. 'booltostr', 'bytetocharindex', 'bytetocharlen', 'bytetype',
  135. 'callterminateprocs', 'changefileext', 'charlength',
  136. 'chartobyteindex', 'chartobytelen', 'comparemem', 'comparestr',
  137. 'comparetext', 'createdir', 'createguid', 'currentyear',
  138. 'currtostr', 'currtostrf', 'date', 'datetimetofiledate',
  139. 'datetimetostr', 'datetimetostring', 'datetimetosystemtime',
  140. 'datetimetotimestamp', 'datetostr', 'dayofweek', 'decodedate',
  141. 'decodedatefully', 'decodetime', 'deletefile', 'directoryexists',
  142. 'diskfree', 'disksize', 'disposestr', 'encodedate', 'encodetime',
  143. 'exceptionerrormessage', 'excludetrailingbackslash',
  144. 'excludetrailingpathdelimiter', 'expandfilename',
  145. 'expandfilenamecase', 'expanduncfilename', 'extractfiledir',
  146. 'extractfiledrive', 'extractfileext', 'extractfilename',
  147. 'extractfilepath', 'extractrelativepath', 'extractshortpathname',
  148. 'fileage', 'fileclose', 'filecreate', 'filedatetodatetime',
  149. 'fileexists', 'filegetattr', 'filegetdate', 'fileisreadonly',
  150. 'fileopen', 'fileread', 'filesearch', 'fileseek', 'filesetattr',
  151. 'filesetdate', 'filesetreadonly', 'filewrite', 'finalizepackage',
  152. 'findclose', 'findcmdlineswitch', 'findfirst', 'findnext',
  153. 'floattocurr', 'floattodatetime', 'floattodecimal', 'floattostr',
  154. 'floattostrf', 'floattotext', 'floattotextfmt', 'fmtloadstr',
  155. 'fmtstr', 'forcedirectories', 'format', 'formatbuf', 'formatcurr',
  156. 'formatdatetime', 'formatfloat', 'freeandnil', 'getcurrentdir',
  157. 'getenvironmentvariable', 'getfileversion', 'getformatsettings',
  158. 'getlocaleformatsettings', 'getmodulename', 'getpackagedescription',
  159. 'getpackageinfo', 'gettime', 'guidtostring', 'incamonth',
  160. 'includetrailingbackslash', 'includetrailingpathdelimiter',
  161. 'incmonth', 'initializepackage', 'interlockeddecrement',
  162. 'interlockedexchange', 'interlockedexchangeadd',
  163. 'interlockedincrement', 'inttohex', 'inttostr', 'isdelimiter',
  164. 'isequalguid', 'isleapyear', 'ispathdelimiter', 'isvalidident',
  165. 'languages', 'lastdelimiter', 'loadpackage', 'loadstr',
  166. 'lowercase', 'msecstotimestamp', 'newstr', 'nextcharindex', 'now',
  167. 'outofmemoryerror', 'quotedstr', 'raiselastoserror',
  168. 'raiselastwin32error', 'removedir', 'renamefile', 'replacedate',
  169. 'replacetime', 'safeloadlibrary', 'samefilename', 'sametext',
  170. 'setcurrentdir', 'showexception', 'sleep', 'stralloc', 'strbufsize',
  171. 'strbytetype', 'strcat', 'strcharlength', 'strcomp', 'strcopy',
  172. 'strdispose', 'strecopy', 'strend', 'strfmt', 'stricomp',
  173. 'stringreplace', 'stringtoguid', 'strlcat', 'strlcomp', 'strlcopy',
  174. 'strlen', 'strlfmt', 'strlicomp', 'strlower', 'strmove', 'strnew',
  175. 'strnextchar', 'strpas', 'strpcopy', 'strplcopy', 'strpos',
  176. 'strrscan', 'strscan', 'strtobool', 'strtobooldef', 'strtocurr',
  177. 'strtocurrdef', 'strtodate', 'strtodatedef', 'strtodatetime',
  178. 'strtodatetimedef', 'strtofloat', 'strtofloatdef', 'strtoint',
  179. 'strtoint64', 'strtoint64def', 'strtointdef', 'strtotime',
  180. 'strtotimedef', 'strupper', 'supports', 'syserrormessage',
  181. 'systemtimetodatetime', 'texttofloat', 'time', 'timestamptodatetime',
  182. 'timestamptomsecs', 'timetostr', 'trim', 'trimleft', 'trimright',
  183. 'tryencodedate', 'tryencodetime', 'tryfloattocurr', 'tryfloattodatetime',
  184. 'trystrtobool', 'trystrtocurr', 'trystrtodate', 'trystrtodatetime',
  185. 'trystrtofloat', 'trystrtoint', 'trystrtoint64', 'trystrtotime',
  186. 'unloadpackage', 'uppercase', 'widecomparestr', 'widecomparetext',
  187. 'widefmtstr', 'wideformat', 'wideformatbuf', 'widelowercase',
  188. 'widesamestr', 'widesametext', 'wideuppercase', 'win32check',
  189. 'wraptext'
  190. ),
  191. 'Classes': (
  192. 'activateclassgroup', 'allocatehwnd', 'bintohex', 'checksynchronize',
  193. 'collectionsequal', 'countgenerations', 'deallocatehwnd', 'equalrect',
  194. 'extractstrings', 'findclass', 'findglobalcomponent', 'getclass',
  195. 'groupdescendantswith', 'hextobin', 'identtoint',
  196. 'initinheritedcomponent', 'inttoident', 'invalidpoint',
  197. 'isuniqueglobalcomponentname', 'linestart', 'objectbinarytotext',
  198. 'objectresourcetotext', 'objecttexttobinary', 'objecttexttoresource',
  199. 'pointsequal', 'readcomponentres', 'readcomponentresex',
  200. 'readcomponentresfile', 'rect', 'registerclass', 'registerclassalias',
  201. 'registerclasses', 'registercomponents', 'registerintegerconsts',
  202. 'registernoicon', 'registernonactivex', 'smallpoint', 'startclassgroup',
  203. 'teststreamformat', 'unregisterclass', 'unregisterclasses',
  204. 'unregisterintegerconsts', 'unregistermoduleclasses',
  205. 'writecomponentresfile'
  206. ),
  207. 'Math': (
  208. 'arccos', 'arccosh', 'arccot', 'arccoth', 'arccsc', 'arccsch', 'arcsec',
  209. 'arcsech', 'arcsin', 'arcsinh', 'arctan2', 'arctanh', 'ceil',
  210. 'comparevalue', 'cosecant', 'cosh', 'cot', 'cotan', 'coth', 'csc',
  211. 'csch', 'cycletodeg', 'cycletograd', 'cycletorad', 'degtocycle',
  212. 'degtograd', 'degtorad', 'divmod', 'doubledecliningbalance',
  213. 'ensurerange', 'floor', 'frexp', 'futurevalue', 'getexceptionmask',
  214. 'getprecisionmode', 'getroundmode', 'gradtocycle', 'gradtodeg',
  215. 'gradtorad', 'hypot', 'inrange', 'interestpayment', 'interestrate',
  216. 'internalrateofreturn', 'intpower', 'isinfinite', 'isnan', 'iszero',
  217. 'ldexp', 'lnxp1', 'log10', 'log2', 'logn', 'max', 'maxintvalue',
  218. 'maxvalue', 'mean', 'meanandstddev', 'min', 'minintvalue', 'minvalue',
  219. 'momentskewkurtosis', 'netpresentvalue', 'norm', 'numberofperiods',
  220. 'payment', 'periodpayment', 'poly', 'popnstddev', 'popnvariance',
  221. 'power', 'presentvalue', 'radtocycle', 'radtodeg', 'radtograd',
  222. 'randg', 'randomrange', 'roundto', 'samevalue', 'sec', 'secant',
  223. 'sech', 'setexceptionmask', 'setprecisionmode', 'setroundmode',
  224. 'sign', 'simpleroundto', 'sincos', 'sinh', 'slndepreciation', 'stddev',
  225. 'sum', 'sumint', 'sumofsquares', 'sumsandsquares', 'syddepreciation',
  226. 'tan', 'tanh', 'totalvariance', 'variance'
  227. )
  228. }
  229. ASM_REGISTERS = {
  230. 'ah', 'al', 'ax', 'bh', 'bl', 'bp', 'bx', 'ch', 'cl', 'cr0',
  231. 'cr1', 'cr2', 'cr3', 'cr4', 'cs', 'cx', 'dh', 'di', 'dl', 'dr0',
  232. 'dr1', 'dr2', 'dr3', 'dr4', 'dr5', 'dr6', 'dr7', 'ds', 'dx',
  233. 'eax', 'ebp', 'ebx', 'ecx', 'edi', 'edx', 'es', 'esi', 'esp',
  234. 'fs', 'gs', 'mm0', 'mm1', 'mm2', 'mm3', 'mm4', 'mm5', 'mm6',
  235. 'mm7', 'si', 'sp', 'ss', 'st0', 'st1', 'st2', 'st3', 'st4', 'st5',
  236. 'st6', 'st7', 'xmm0', 'xmm1', 'xmm2', 'xmm3', 'xmm4', 'xmm5',
  237. 'xmm6', 'xmm7'
  238. }
  239. ASM_INSTRUCTIONS = {
  240. 'aaa', 'aad', 'aam', 'aas', 'adc', 'add', 'and', 'arpl', 'bound',
  241. 'bsf', 'bsr', 'bswap', 'bt', 'btc', 'btr', 'bts', 'call', 'cbw',
  242. 'cdq', 'clc', 'cld', 'cli', 'clts', 'cmc', 'cmova', 'cmovae',
  243. 'cmovb', 'cmovbe', 'cmovc', 'cmovcxz', 'cmove', 'cmovg',
  244. 'cmovge', 'cmovl', 'cmovle', 'cmovna', 'cmovnae', 'cmovnb',
  245. 'cmovnbe', 'cmovnc', 'cmovne', 'cmovng', 'cmovnge', 'cmovnl',
  246. 'cmovnle', 'cmovno', 'cmovnp', 'cmovns', 'cmovnz', 'cmovo',
  247. 'cmovp', 'cmovpe', 'cmovpo', 'cmovs', 'cmovz', 'cmp', 'cmpsb',
  248. 'cmpsd', 'cmpsw', 'cmpxchg', 'cmpxchg486', 'cmpxchg8b', 'cpuid',
  249. 'cwd', 'cwde', 'daa', 'das', 'dec', 'div', 'emms', 'enter', 'hlt',
  250. 'ibts', 'icebp', 'idiv', 'imul', 'in', 'inc', 'insb', 'insd',
  251. 'insw', 'int', 'int01', 'int03', 'int1', 'int3', 'into', 'invd',
  252. 'invlpg', 'iret', 'iretd', 'iretw', 'ja', 'jae', 'jb', 'jbe',
  253. 'jc', 'jcxz', 'jcxz', 'je', 'jecxz', 'jg', 'jge', 'jl', 'jle',
  254. 'jmp', 'jna', 'jnae', 'jnb', 'jnbe', 'jnc', 'jne', 'jng', 'jnge',
  255. 'jnl', 'jnle', 'jno', 'jnp', 'jns', 'jnz', 'jo', 'jp', 'jpe',
  256. 'jpo', 'js', 'jz', 'lahf', 'lar', 'lcall', 'lds', 'lea', 'leave',
  257. 'les', 'lfs', 'lgdt', 'lgs', 'lidt', 'ljmp', 'lldt', 'lmsw',
  258. 'loadall', 'loadall286', 'lock', 'lodsb', 'lodsd', 'lodsw',
  259. 'loop', 'loope', 'loopne', 'loopnz', 'loopz', 'lsl', 'lss', 'ltr',
  260. 'mov', 'movd', 'movq', 'movsb', 'movsd', 'movsw', 'movsx',
  261. 'movzx', 'mul', 'neg', 'nop', 'not', 'or', 'out', 'outsb', 'outsd',
  262. 'outsw', 'pop', 'popa', 'popad', 'popaw', 'popf', 'popfd', 'popfw',
  263. 'push', 'pusha', 'pushad', 'pushaw', 'pushf', 'pushfd', 'pushfw',
  264. 'rcl', 'rcr', 'rdmsr', 'rdpmc', 'rdshr', 'rdtsc', 'rep', 'repe',
  265. 'repne', 'repnz', 'repz', 'ret', 'retf', 'retn', 'rol', 'ror',
  266. 'rsdc', 'rsldt', 'rsm', 'sahf', 'sal', 'salc', 'sar', 'sbb',
  267. 'scasb', 'scasd', 'scasw', 'seta', 'setae', 'setb', 'setbe',
  268. 'setc', 'setcxz', 'sete', 'setg', 'setge', 'setl', 'setle',
  269. 'setna', 'setnae', 'setnb', 'setnbe', 'setnc', 'setne', 'setng',
  270. 'setnge', 'setnl', 'setnle', 'setno', 'setnp', 'setns', 'setnz',
  271. 'seto', 'setp', 'setpe', 'setpo', 'sets', 'setz', 'sgdt', 'shl',
  272. 'shld', 'shr', 'shrd', 'sidt', 'sldt', 'smi', 'smint', 'smintold',
  273. 'smsw', 'stc', 'std', 'sti', 'stosb', 'stosd', 'stosw', 'str',
  274. 'sub', 'svdc', 'svldt', 'svts', 'syscall', 'sysenter', 'sysexit',
  275. 'sysret', 'test', 'ud1', 'ud2', 'umov', 'verr', 'verw', 'wait',
  276. 'wbinvd', 'wrmsr', 'wrshr', 'xadd', 'xbts', 'xchg', 'xlat',
  277. 'xlatb', 'xor'
  278. }
  279. def __init__(self, **options):
  280. Lexer.__init__(self, **options)
  281. self.keywords = set()
  282. if get_bool_opt(options, 'turbopascal', True):
  283. self.keywords.update(self.TURBO_PASCAL_KEYWORDS)
  284. if get_bool_opt(options, 'delphi', True):
  285. self.keywords.update(self.DELPHI_KEYWORDS)
  286. if get_bool_opt(options, 'freepascal', True):
  287. self.keywords.update(self.FREE_PASCAL_KEYWORDS)
  288. self.builtins = set()
  289. for unit in get_list_opt(options, 'units', list(self.BUILTIN_UNITS)):
  290. self.builtins.update(self.BUILTIN_UNITS[unit])
  291. def get_tokens_unprocessed(self, text):
  292. scanner = Scanner(text, re.DOTALL | re.MULTILINE | re.IGNORECASE)
  293. stack = ['initial']
  294. in_function_block = False
  295. in_property_block = False
  296. was_dot = False
  297. next_token_is_function = False
  298. next_token_is_property = False
  299. collect_labels = False
  300. block_labels = set()
  301. brace_balance = [0, 0]
  302. while not scanner.eos:
  303. token = Error
  304. if stack[-1] == 'initial':
  305. if scanner.scan(r'\s+'):
  306. token = Text
  307. elif scanner.scan(r'\{.*?\}|\(\*.*?\*\)'):
  308. if scanner.match.startswith('$'):
  309. token = Comment.Preproc
  310. else:
  311. token = Comment.Multiline
  312. elif scanner.scan(r'//.*?$'):
  313. token = Comment.Single
  314. elif scanner.scan(r'[-+*\/=<>:;,.@\^]'):
  315. token = Operator
  316. # stop label highlighting on next ";"
  317. if collect_labels and scanner.match == ';':
  318. collect_labels = False
  319. elif scanner.scan(r'[\(\)\[\]]+'):
  320. token = Punctuation
  321. # abort function naming ``foo = Function(...)``
  322. next_token_is_function = False
  323. # if we are in a function block we count the open
  324. # braces because ootherwise it's impossible to
  325. # determine the end of the modifier context
  326. if in_function_block or in_property_block:
  327. if scanner.match == '(':
  328. brace_balance[0] += 1
  329. elif scanner.match == ')':
  330. brace_balance[0] -= 1
  331. elif scanner.match == '[':
  332. brace_balance[1] += 1
  333. elif scanner.match == ']':
  334. brace_balance[1] -= 1
  335. elif scanner.scan(r'[A-Za-z_][A-Za-z_0-9]*'):
  336. lowercase_name = scanner.match.lower()
  337. if lowercase_name == 'result':
  338. token = Name.Builtin.Pseudo
  339. elif lowercase_name in self.keywords:
  340. token = Keyword
  341. # if we are in a special block and a
  342. # block ending keyword occours (and the parenthesis
  343. # is balanced) we end the current block context
  344. if (in_function_block or in_property_block) and \
  345. lowercase_name in self.BLOCK_KEYWORDS and \
  346. brace_balance[0] <= 0 and \
  347. brace_balance[1] <= 0:
  348. in_function_block = False
  349. in_property_block = False
  350. brace_balance = [0, 0]
  351. block_labels = set()
  352. if lowercase_name in ('label', 'goto'):
  353. collect_labels = True
  354. elif lowercase_name == 'asm':
  355. stack.append('asm')
  356. elif lowercase_name == 'property':
  357. in_property_block = True
  358. next_token_is_property = True
  359. elif lowercase_name in ('procedure', 'operator',
  360. 'function', 'constructor',
  361. 'destructor'):
  362. in_function_block = True
  363. next_token_is_function = True
  364. # we are in a function block and the current name
  365. # is in the set of registered modifiers. highlight
  366. # it as pseudo keyword
  367. elif in_function_block and \
  368. lowercase_name in self.FUNCTION_MODIFIERS:
  369. token = Keyword.Pseudo
  370. # if we are in a property highlight some more
  371. # modifiers
  372. elif in_property_block and \
  373. lowercase_name in ('read', 'write'):
  374. token = Keyword.Pseudo
  375. next_token_is_function = True
  376. # if the last iteration set next_token_is_function
  377. # to true we now want this name highlighted as
  378. # function. so do that and reset the state
  379. elif next_token_is_function:
  380. # Look if the next token is a dot. If yes it's
  381. # not a function, but a class name and the
  382. # part after the dot a function name
  383. if scanner.test(r'\s*\.\s*'):
  384. token = Name.Class
  385. # it's not a dot, our job is done
  386. else:
  387. token = Name.Function
  388. next_token_is_function = False
  389. # same for properties
  390. elif next_token_is_property:
  391. token = Name.Property
  392. next_token_is_property = False
  393. # Highlight this token as label and add it
  394. # to the list of known labels
  395. elif collect_labels:
  396. token = Name.Label
  397. block_labels.add(scanner.match.lower())
  398. # name is in list of known labels
  399. elif lowercase_name in block_labels:
  400. token = Name.Label
  401. elif lowercase_name in self.BUILTIN_TYPES:
  402. token = Keyword.Type
  403. elif lowercase_name in self.DIRECTIVES:
  404. token = Keyword.Pseudo
  405. # builtins are just builtins if the token
  406. # before isn't a dot
  407. elif not was_dot and lowercase_name in self.builtins:
  408. token = Name.Builtin
  409. else:
  410. token = Name
  411. elif scanner.scan(r"'"):
  412. token = String
  413. stack.append('string')
  414. elif scanner.scan(r'\#(\d+|\$[0-9A-Fa-f]+)'):
  415. token = String.Char
  416. elif scanner.scan(r'\$[0-9A-Fa-f]+'):
  417. token = Number.Hex
  418. elif scanner.scan(r'\d+(?![eE]|\.[^.])'):
  419. token = Number.Integer
  420. elif scanner.scan(r'\d+(\.\d+([eE][+-]?\d+)?|[eE][+-]?\d+)'):
  421. token = Number.Float
  422. else:
  423. # if the stack depth is deeper than once, pop
  424. if len(stack) > 1:
  425. stack.pop()
  426. scanner.get_char()
  427. elif stack[-1] == 'string':
  428. if scanner.scan(r"''"):
  429. token = String.Escape
  430. elif scanner.scan(r"'"):
  431. token = String
  432. stack.pop()
  433. elif scanner.scan(r"[^']*"):
  434. token = String
  435. else:
  436. scanner.get_char()
  437. stack.pop()
  438. elif stack[-1] == 'asm':
  439. if scanner.scan(r'\s+'):
  440. token = Text
  441. elif scanner.scan(r'end'):
  442. token = Keyword
  443. stack.pop()
  444. elif scanner.scan(r'\{.*?\}|\(\*.*?\*\)'):
  445. if scanner.match.startswith('$'):
  446. token = Comment.Preproc
  447. else:
  448. token = Comment.Multiline
  449. elif scanner.scan(r'//.*?$'):
  450. token = Comment.Single
  451. elif scanner.scan(r"'"):
  452. token = String
  453. stack.append('string')
  454. elif scanner.scan(r'@@[A-Za-z_][A-Za-z_0-9]*'):
  455. token = Name.Label
  456. elif scanner.scan(r'[A-Za-z_][A-Za-z_0-9]*'):
  457. lowercase_name = scanner.match.lower()
  458. if lowercase_name in self.ASM_INSTRUCTIONS:
  459. token = Keyword
  460. elif lowercase_name in self.ASM_REGISTERS:
  461. token = Name.Builtin
  462. else:
  463. token = Name
  464. elif scanner.scan(r'[-+*\/=<>:;,.@\^]+'):
  465. token = Operator
  466. elif scanner.scan(r'[\(\)\[\]]+'):
  467. token = Punctuation
  468. elif scanner.scan(r'\$[0-9A-Fa-f]+'):
  469. token = Number.Hex
  470. elif scanner.scan(r'\d+(?![eE]|\.[^.])'):
  471. token = Number.Integer
  472. elif scanner.scan(r'\d+(\.\d+([eE][+-]?\d+)?|[eE][+-]?\d+)'):
  473. token = Number.Float
  474. else:
  475. scanner.get_char()
  476. stack.pop()
  477. # save the dot!!!11
  478. if scanner.match.strip():
  479. was_dot = scanner.match == '.'
  480. yield scanner.start_pos, token, scanner.match or ''
  481. class AdaLexer(RegexLexer):
  482. """
  483. For Ada source code.
  484. .. versionadded:: 1.3
  485. """
  486. name = 'Ada'
  487. aliases = ['ada', 'ada95', 'ada2005']
  488. filenames = ['*.adb', '*.ads', '*.ada']
  489. mimetypes = ['text/x-ada']
  490. flags = re.MULTILINE | re.IGNORECASE
  491. tokens = {
  492. 'root': [
  493. (r'[^\S\n]+', Text),
  494. (r'--.*?\n', Comment.Single),
  495. (r'[^\S\n]+', Text),
  496. (r'function|procedure|entry', Keyword.Declaration, 'subprogram'),
  497. (r'(subtype|type)(\s+)(\w+)',
  498. bygroups(Keyword.Declaration, Text, Keyword.Type), 'type_def'),
  499. (r'task|protected', Keyword.Declaration),
  500. (r'(subtype)(\s+)', bygroups(Keyword.Declaration, Text)),
  501. (r'(end)(\s+)', bygroups(Keyword.Reserved, Text), 'end'),
  502. (r'(pragma)(\s+)(\w+)', bygroups(Keyword.Reserved, Text,
  503. Comment.Preproc)),
  504. (r'(true|false|null)\b', Keyword.Constant),
  505. (words((
  506. 'Address', 'Byte', 'Boolean', 'Character', 'Controlled', 'Count',
  507. 'Cursor', 'Duration', 'File_Mode', 'File_Type', 'Float', 'Generator',
  508. 'Integer', 'Long_Float', 'Long_Integer', 'Long_Long_Float',
  509. 'Long_Long_Integer', 'Natural', 'Positive', 'Reference_Type',
  510. 'Short_Float', 'Short_Integer', 'Short_Short_Float',
  511. 'Short_Short_Integer', 'String', 'Wide_Character', 'Wide_String'),
  512. suffix=r'\b'),
  513. Keyword.Type),
  514. (r'(and(\s+then)?|in|mod|not|or(\s+else)|rem)\b', Operator.Word),
  515. (r'generic|private', Keyword.Declaration),
  516. (r'package', Keyword.Declaration, 'package'),
  517. (r'array\b', Keyword.Reserved, 'array_def'),
  518. (r'(with|use)(\s+)', bygroups(Keyword.Namespace, Text), 'import'),
  519. (r'(\w+)(\s*)(:)(\s*)(constant)',
  520. bygroups(Name.Constant, Text, Punctuation, Text,
  521. Keyword.Reserved)),
  522. (r'<<\w+>>', Name.Label),
  523. (r'(\w+)(\s*)(:)(\s*)(declare|begin|loop|for|while)',
  524. bygroups(Name.Label, Text, Punctuation, Text, Keyword.Reserved)),
  525. (words((
  526. 'abort', 'abs', 'abstract', 'accept', 'access', 'aliased', 'all',
  527. 'array', 'at', 'begin', 'body', 'case', 'constant', 'declare',
  528. 'delay', 'delta', 'digits', 'do', 'else', 'elsif', 'end', 'entry',
  529. 'exception', 'exit', 'interface', 'for', 'goto', 'if', 'is', 'limited',
  530. 'loop', 'new', 'null', 'of', 'or', 'others', 'out', 'overriding',
  531. 'pragma', 'protected', 'raise', 'range', 'record', 'renames', 'requeue',
  532. 'return', 'reverse', 'select', 'separate', 'some', 'subtype',
  533. 'synchronized', 'task', 'tagged', 'terminate', 'then', 'type', 'until',
  534. 'when', 'while', 'xor'), prefix=r'\b', suffix=r'\b'),
  535. Keyword.Reserved),
  536. (r'"[^"]*"', String),
  537. include('attribute'),
  538. include('numbers'),
  539. (r"'[^']'", String.Character),
  540. (r'(\w+)(\s*|[(,])', bygroups(Name, using(this))),
  541. (r"(<>|=>|:=|[()|:;,.'])", Punctuation),
  542. (r'[*<>+=/&-]', Operator),
  543. (r'\n+', Text),
  544. ],
  545. 'numbers': [
  546. (r'[0-9_]+#[0-9a-f_\.]+#', Number.Hex),
  547. (r'[0-9_]+\.[0-9_]*', Number.Float),
  548. (r'[0-9_]+', Number.Integer),
  549. ],
  550. 'attribute': [
  551. (r"(')(\w+)", bygroups(Punctuation, Name.Attribute)),
  552. ],
  553. 'subprogram': [
  554. (r'\(', Punctuation, ('#pop', 'formal_part')),
  555. (r';', Punctuation, '#pop'),
  556. (r'is\b', Keyword.Reserved, '#pop'),
  557. (r'"[^"]+"|\w+', Name.Function),
  558. include('root'),
  559. ],
  560. 'end': [
  561. ('(if|case|record|loop|select)', Keyword.Reserved),
  562. (r'"[^"]+"|[\w.]+', Name.Function),
  563. (r'\s+', Text),
  564. (';', Punctuation, '#pop'),
  565. ],
  566. 'type_def': [
  567. (r';', Punctuation, '#pop'),
  568. (r'\(', Punctuation, 'formal_part'),
  569. (r'with|and|use', Keyword.Reserved),
  570. (r'array\b', Keyword.Reserved, ('#pop', 'array_def')),
  571. (r'record\b', Keyword.Reserved, ('record_def')),
  572. (r'(null record)(;)', bygroups(Keyword.Reserved, Punctuation), '#pop'),
  573. include('root'),
  574. ],
  575. 'array_def': [
  576. (r';', Punctuation, '#pop'),
  577. (r'(\w+)(\s+)(range)', bygroups(Keyword.Type, Text, Keyword.Reserved)),
  578. include('root'),
  579. ],
  580. 'record_def': [
  581. (r'end record', Keyword.Reserved, '#pop'),
  582. include('root'),
  583. ],
  584. 'import': [
  585. (r'[\w.]+', Name.Namespace, '#pop'),
  586. default('#pop'),
  587. ],
  588. 'formal_part': [
  589. (r'\)', Punctuation, '#pop'),
  590. (r'\w+', Name.Variable),
  591. (r',|:[^=]', Punctuation),
  592. (r'(in|not|null|out|access)\b', Keyword.Reserved),
  593. include('root'),
  594. ],
  595. 'package': [
  596. ('body', Keyword.Declaration),
  597. (r'is\s+new|renames', Keyword.Reserved),
  598. ('is', Keyword.Reserved, '#pop'),
  599. (';', Punctuation, '#pop'),
  600. (r'\(', Punctuation, 'package_instantiation'),
  601. (r'([\w.]+)', Name.Class),
  602. include('root'),
  603. ],
  604. 'package_instantiation': [
  605. (r'("[^"]+"|\w+)(\s+)(=>)', bygroups(Name.Variable, Text, Punctuation)),
  606. (r'[\w.\'"]', Text),
  607. (r'\)', Punctuation, '#pop'),
  608. include('root'),
  609. ],
  610. }