int_fiction.py 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.int_fiction
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for interactive fiction languages.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, include, bygroups, using, \
  11. this, default, words
  12. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  13. Number, Punctuation, Error, Generic
  14. __all__ = ['Inform6Lexer', 'Inform6TemplateLexer', 'Inform7Lexer',
  15. 'Tads3Lexer']
  16. class Inform6Lexer(RegexLexer):
  17. """
  18. For `Inform 6 <http://inform-fiction.org/>`_ source code.
  19. .. versionadded:: 2.0
  20. """
  21. name = 'Inform 6'
  22. aliases = ['inform6', 'i6']
  23. filenames = ['*.inf']
  24. flags = re.MULTILINE | re.DOTALL | re.UNICODE
  25. _name = r'[a-zA-Z_]\w*'
  26. # Inform 7 maps these four character classes to their ASCII
  27. # equivalents. To support Inform 6 inclusions within Inform 7,
  28. # Inform6Lexer maps them too.
  29. _dash = u'\\-\u2010-\u2014'
  30. _dquote = u'"\u201c\u201d'
  31. _squote = u"'\u2018\u2019"
  32. _newline = u'\\n\u0085\u2028\u2029'
  33. tokens = {
  34. 'root': [
  35. (r'\A(!%%[^%s]*[%s])+' % (_newline, _newline), Comment.Preproc,
  36. 'directive'),
  37. default('directive')
  38. ],
  39. '_whitespace': [
  40. (r'\s+', Text),
  41. (r'![^%s]*' % _newline, Comment.Single)
  42. ],
  43. 'default': [
  44. include('_whitespace'),
  45. (r'\[', Punctuation, 'many-values'), # Array initialization
  46. (r':|(?=;)', Punctuation, '#pop'),
  47. (r'<', Punctuation), # Second angle bracket in an action statement
  48. default(('expression', '_expression'))
  49. ],
  50. # Expressions
  51. '_expression': [
  52. include('_whitespace'),
  53. (r'(?=sp\b)', Text, '#pop'),
  54. (r'(?=[%s%s$0-9#a-zA-Z_])' % (_dquote, _squote), Text,
  55. ('#pop', 'value')),
  56. (r'\+\+|[%s]{1,2}(?!>)|~~?' % _dash, Operator),
  57. (r'(?=[()\[%s,?@{:;])' % _dash, Text, '#pop')
  58. ],
  59. 'expression': [
  60. include('_whitespace'),
  61. (r'\(', Punctuation, ('expression', '_expression')),
  62. (r'\)', Punctuation, '#pop'),
  63. (r'\[', Punctuation, ('#pop', 'statements', 'locals')),
  64. (r'>(?=(\s+|(![^%s]*))*[>;])' % _newline, Punctuation),
  65. (r'\+\+|[%s]{2}(?!>)' % _dash, Operator),
  66. (r',', Punctuation, '_expression'),
  67. (r'&&?|\|\|?|[=~><]?=|[%s]{1,2}>?|\.\.?[&#]?|::|[<>+*/%%]' % _dash,
  68. Operator, '_expression'),
  69. (r'(has|hasnt|in|notin|ofclass|or|provides)\b', Operator.Word,
  70. '_expression'),
  71. (r'sp\b', Name),
  72. (r'\?~?', Name.Label, 'label?'),
  73. (r'[@{]', Error),
  74. default('#pop')
  75. ],
  76. '_assembly-expression': [
  77. (r'\(', Punctuation, ('#push', '_expression')),
  78. (r'[\[\]]', Punctuation),
  79. (r'[%s]>' % _dash, Punctuation, '_expression'),
  80. (r'sp\b', Keyword.Pseudo),
  81. (r';', Punctuation, '#pop:3'),
  82. include('expression')
  83. ],
  84. '_for-expression': [
  85. (r'\)', Punctuation, '#pop:2'),
  86. (r':', Punctuation, '#pop'),
  87. include('expression')
  88. ],
  89. '_keyword-expression': [
  90. (r'(from|near|to)\b', Keyword, '_expression'),
  91. include('expression')
  92. ],
  93. '_list-expression': [
  94. (r',', Punctuation, '#pop'),
  95. include('expression')
  96. ],
  97. '_object-expression': [
  98. (r'has\b', Keyword.Declaration, '#pop'),
  99. include('_list-expression')
  100. ],
  101. # Values
  102. 'value': [
  103. include('_whitespace'),
  104. # Strings
  105. (r'[%s][^@][%s]' % (_squote, _squote), String.Char, '#pop'),
  106. (r'([%s])(@\{[0-9a-fA-F]{1,4}\})([%s])' % (_squote, _squote),
  107. bygroups(String.Char, String.Escape, String.Char), '#pop'),
  108. (r'([%s])(@.{2})([%s])' % (_squote, _squote),
  109. bygroups(String.Char, String.Escape, String.Char), '#pop'),
  110. (r'[%s]' % _squote, String.Single, ('#pop', 'dictionary-word')),
  111. (r'[%s]' % _dquote, String.Double, ('#pop', 'string')),
  112. # Numbers
  113. (r'\$[+%s][0-9]*\.?[0-9]*([eE][+%s]?[0-9]+)?' % (_dash, _dash),
  114. Number.Float, '#pop'),
  115. (r'\$[0-9a-fA-F]+', Number.Hex, '#pop'),
  116. (r'\$\$[01]+', Number.Bin, '#pop'),
  117. (r'[0-9]+', Number.Integer, '#pop'),
  118. # Values prefixed by hashes
  119. (r'(##|#a\$)(%s)' % _name, bygroups(Operator, Name), '#pop'),
  120. (r'(#g\$)(%s)' % _name,
  121. bygroups(Operator, Name.Variable.Global), '#pop'),
  122. (r'#[nw]\$', Operator, ('#pop', 'obsolete-dictionary-word')),
  123. (r'(#r\$)(%s)' % _name, bygroups(Operator, Name.Function), '#pop'),
  124. (r'#', Name.Builtin, ('#pop', 'system-constant')),
  125. # System functions
  126. (words((
  127. 'child', 'children', 'elder', 'eldest', 'glk', 'indirect', 'metaclass',
  128. 'parent', 'random', 'sibling', 'younger', 'youngest'), suffix=r'\b'),
  129. Name.Builtin, '#pop'),
  130. # Metaclasses
  131. (r'(?i)(Class|Object|Routine|String)\b', Name.Builtin, '#pop'),
  132. # Veneer routines
  133. (words((
  134. 'Box__Routine', 'CA__Pr', 'CDefArt', 'CInDefArt', 'Cl__Ms',
  135. 'Copy__Primitive', 'CP__Tab', 'DA__Pr', 'DB__Pr', 'DefArt', 'Dynam__String',
  136. 'EnglishNumber', 'Glk__Wrap', 'IA__Pr', 'IB__Pr', 'InDefArt', 'Main__',
  137. 'Meta__class', 'OB__Move', 'OB__Remove', 'OC__Cl', 'OP__Pr', 'Print__Addr',
  138. 'Print__PName', 'PrintShortName', 'RA__Pr', 'RA__Sc', 'RL__Pr', 'R_Process',
  139. 'RT__ChG', 'RT__ChGt', 'RT__ChLDB', 'RT__ChLDW', 'RT__ChPR', 'RT__ChPrintA',
  140. 'RT__ChPrintC', 'RT__ChPrintO', 'RT__ChPrintS', 'RT__ChPS', 'RT__ChR',
  141. 'RT__ChSTB', 'RT__ChSTW', 'RT__ChT', 'RT__Err', 'RT__TrPS', 'RV__Pr',
  142. 'Symb__Tab', 'Unsigned__Compare', 'WV__Pr', 'Z__Region'),
  143. prefix='(?i)', suffix=r'\b'),
  144. Name.Builtin, '#pop'),
  145. # Other built-in symbols
  146. (words((
  147. 'call', 'copy', 'create', 'DEBUG', 'destroy', 'DICT_CHAR_SIZE',
  148. 'DICT_ENTRY_BYTES', 'DICT_IS_UNICODE', 'DICT_WORD_SIZE', 'false',
  149. 'FLOAT_INFINITY', 'FLOAT_NAN', 'FLOAT_NINFINITY', 'GOBJFIELD_CHAIN',
  150. 'GOBJFIELD_CHILD', 'GOBJFIELD_NAME', 'GOBJFIELD_PARENT',
  151. 'GOBJFIELD_PROPTAB', 'GOBJFIELD_SIBLING', 'GOBJ_EXT_START',
  152. 'GOBJ_TOTAL_LENGTH', 'Grammar__Version', 'INDIV_PROP_START', 'INFIX',
  153. 'infix__watching', 'MODULE_MODE', 'name', 'nothing', 'NUM_ATTR_BYTES', 'print',
  154. 'print_to_array', 'recreate', 'remaining', 'self', 'sender', 'STRICT_MODE',
  155. 'sw__var', 'sys__glob0', 'sys__glob1', 'sys__glob2', 'sys_statusline_flag',
  156. 'TARGET_GLULX', 'TARGET_ZCODE', 'temp__global2', 'temp__global3',
  157. 'temp__global4', 'temp_global', 'true', 'USE_MODULES', 'WORDSIZE'),
  158. prefix='(?i)', suffix=r'\b'),
  159. Name.Builtin, '#pop'),
  160. # Other values
  161. (_name, Name, '#pop')
  162. ],
  163. # Strings
  164. 'dictionary-word': [
  165. (r'[~^]+', String.Escape),
  166. (r'[^~^\\@({%s]+' % _squote, String.Single),
  167. (r'[({]', String.Single),
  168. (r'@\{[0-9a-fA-F]{,4}\}', String.Escape),
  169. (r'@.{2}', String.Escape),
  170. (r'[%s]' % _squote, String.Single, '#pop')
  171. ],
  172. 'string': [
  173. (r'[~^]+', String.Escape),
  174. (r'[^~^\\@({%s]+' % _dquote, String.Double),
  175. (r'[({]', String.Double),
  176. (r'\\', String.Escape),
  177. (r'@(\\\s*[%s]\s*)*@((\\\s*[%s]\s*)*[0-9])*' %
  178. (_newline, _newline), String.Escape),
  179. (r'@(\\\s*[%s]\s*)*\{((\\\s*[%s]\s*)*[0-9a-fA-F]){,4}'
  180. r'(\\\s*[%s]\s*)*\}' % (_newline, _newline, _newline),
  181. String.Escape),
  182. (r'@(\\\s*[%s]\s*)*.(\\\s*[%s]\s*)*.' % (_newline, _newline),
  183. String.Escape),
  184. (r'[%s]' % _dquote, String.Double, '#pop')
  185. ],
  186. 'plain-string': [
  187. (r'[^~^\\({\[\]%s]+' % _dquote, String.Double),
  188. (r'[~^({\[\]]', String.Double),
  189. (r'\\', String.Escape),
  190. (r'[%s]' % _dquote, String.Double, '#pop')
  191. ],
  192. # Names
  193. '_constant': [
  194. include('_whitespace'),
  195. (_name, Name.Constant, '#pop'),
  196. include('value')
  197. ],
  198. '_global': [
  199. include('_whitespace'),
  200. (_name, Name.Variable.Global, '#pop'),
  201. include('value')
  202. ],
  203. 'label?': [
  204. include('_whitespace'),
  205. (_name, Name.Label, '#pop'),
  206. default('#pop')
  207. ],
  208. 'variable?': [
  209. include('_whitespace'),
  210. (_name, Name.Variable, '#pop'),
  211. default('#pop')
  212. ],
  213. # Values after hashes
  214. 'obsolete-dictionary-word': [
  215. (r'\S\w*', String.Other, '#pop')
  216. ],
  217. 'system-constant': [
  218. include('_whitespace'),
  219. (_name, Name.Builtin, '#pop')
  220. ],
  221. # Directives
  222. 'directive': [
  223. include('_whitespace'),
  224. (r'#', Punctuation),
  225. (r';', Punctuation, '#pop'),
  226. (r'\[', Punctuation,
  227. ('default', 'statements', 'locals', 'routine-name?')),
  228. (words((
  229. 'abbreviate', 'endif', 'dictionary', 'ifdef', 'iffalse', 'ifndef', 'ifnot',
  230. 'iftrue', 'ifv3', 'ifv5', 'release', 'serial', 'switches', 'system_file',
  231. 'version'), prefix='(?i)', suffix=r'\b'),
  232. Keyword, 'default'),
  233. (r'(?i)(array|global)\b', Keyword,
  234. ('default', 'directive-keyword?', '_global')),
  235. (r'(?i)attribute\b', Keyword, ('default', 'alias?', '_constant')),
  236. (r'(?i)class\b', Keyword,
  237. ('object-body', 'duplicates', 'class-name')),
  238. (r'(?i)(constant|default)\b', Keyword,
  239. ('default', 'expression', '_constant')),
  240. (r'(?i)(end\b)(.*)', bygroups(Keyword, Text)),
  241. (r'(?i)(extend|verb)\b', Keyword, 'grammar'),
  242. (r'(?i)fake_action\b', Keyword, ('default', '_constant')),
  243. (r'(?i)import\b', Keyword, 'manifest'),
  244. (r'(?i)(include|link)\b', Keyword,
  245. ('default', 'before-plain-string')),
  246. (r'(?i)(lowstring|undef)\b', Keyword, ('default', '_constant')),
  247. (r'(?i)message\b', Keyword, ('default', 'diagnostic')),
  248. (r'(?i)(nearby|object)\b', Keyword,
  249. ('object-body', '_object-head')),
  250. (r'(?i)property\b', Keyword,
  251. ('default', 'alias?', '_constant', 'property-keyword*')),
  252. (r'(?i)replace\b', Keyword,
  253. ('default', 'routine-name?', 'routine-name?')),
  254. (r'(?i)statusline\b', Keyword, ('default', 'directive-keyword?')),
  255. (r'(?i)stub\b', Keyword, ('default', 'routine-name?')),
  256. (r'(?i)trace\b', Keyword,
  257. ('default', 'trace-keyword?', 'trace-keyword?')),
  258. (r'(?i)zcharacter\b', Keyword,
  259. ('default', 'directive-keyword?', 'directive-keyword?')),
  260. (_name, Name.Class, ('object-body', '_object-head'))
  261. ],
  262. # [, Replace, Stub
  263. 'routine-name?': [
  264. include('_whitespace'),
  265. (_name, Name.Function, '#pop'),
  266. default('#pop')
  267. ],
  268. 'locals': [
  269. include('_whitespace'),
  270. (r';', Punctuation, '#pop'),
  271. (r'\*', Punctuation),
  272. (r'"', String.Double, 'plain-string'),
  273. (_name, Name.Variable)
  274. ],
  275. # Array
  276. 'many-values': [
  277. include('_whitespace'),
  278. (r';', Punctuation),
  279. (r'\]', Punctuation, '#pop'),
  280. (r':', Error),
  281. default(('expression', '_expression'))
  282. ],
  283. # Attribute, Property
  284. 'alias?': [
  285. include('_whitespace'),
  286. (r'alias\b', Keyword, ('#pop', '_constant')),
  287. default('#pop')
  288. ],
  289. # Class, Object, Nearby
  290. 'class-name': [
  291. include('_whitespace'),
  292. (r'(?=[,;]|(class|has|private|with)\b)', Text, '#pop'),
  293. (_name, Name.Class, '#pop')
  294. ],
  295. 'duplicates': [
  296. include('_whitespace'),
  297. (r'\(', Punctuation, ('#pop', 'expression', '_expression')),
  298. default('#pop')
  299. ],
  300. '_object-head': [
  301. (r'[%s]>' % _dash, Punctuation),
  302. (r'(class|has|private|with)\b', Keyword.Declaration, '#pop'),
  303. include('_global')
  304. ],
  305. 'object-body': [
  306. include('_whitespace'),
  307. (r';', Punctuation, '#pop:2'),
  308. (r',', Punctuation),
  309. (r'class\b', Keyword.Declaration, 'class-segment'),
  310. (r'(has|private|with)\b', Keyword.Declaration),
  311. (r':', Error),
  312. default(('_object-expression', '_expression'))
  313. ],
  314. 'class-segment': [
  315. include('_whitespace'),
  316. (r'(?=[,;]|(class|has|private|with)\b)', Text, '#pop'),
  317. (_name, Name.Class),
  318. default('value')
  319. ],
  320. # Extend, Verb
  321. 'grammar': [
  322. include('_whitespace'),
  323. (r'=', Punctuation, ('#pop', 'default')),
  324. (r'\*', Punctuation, ('#pop', 'grammar-line')),
  325. default('_directive-keyword')
  326. ],
  327. 'grammar-line': [
  328. include('_whitespace'),
  329. (r';', Punctuation, '#pop'),
  330. (r'[/*]', Punctuation),
  331. (r'[%s]>' % _dash, Punctuation, 'value'),
  332. (r'(noun|scope)\b', Keyword, '=routine'),
  333. default('_directive-keyword')
  334. ],
  335. '=routine': [
  336. include('_whitespace'),
  337. (r'=', Punctuation, 'routine-name?'),
  338. default('#pop')
  339. ],
  340. # Import
  341. 'manifest': [
  342. include('_whitespace'),
  343. (r';', Punctuation, '#pop'),
  344. (r',', Punctuation),
  345. (r'(?i)global\b', Keyword, '_global'),
  346. default('_global')
  347. ],
  348. # Include, Link, Message
  349. 'diagnostic': [
  350. include('_whitespace'),
  351. (r'[%s]' % _dquote, String.Double, ('#pop', 'message-string')),
  352. default(('#pop', 'before-plain-string', 'directive-keyword?'))
  353. ],
  354. 'before-plain-string': [
  355. include('_whitespace'),
  356. (r'[%s]' % _dquote, String.Double, ('#pop', 'plain-string'))
  357. ],
  358. 'message-string': [
  359. (r'[~^]+', String.Escape),
  360. include('plain-string')
  361. ],
  362. # Keywords used in directives
  363. '_directive-keyword!': [
  364. include('_whitespace'),
  365. (words((
  366. 'additive', 'alias', 'buffer', 'class', 'creature', 'data', 'error', 'fatalerror',
  367. 'first', 'has', 'held', 'initial', 'initstr', 'last', 'long', 'meta', 'multi',
  368. 'multiexcept', 'multiheld', 'multiinside', 'noun', 'number', 'only', 'private',
  369. 'replace', 'reverse', 'scope', 'score', 'special', 'string', 'table', 'terminating',
  370. 'time', 'topic', 'warning', 'with'), suffix=r'\b'),
  371. Keyword, '#pop'),
  372. (r'[%s]{1,2}>|[+=]' % _dash, Punctuation, '#pop')
  373. ],
  374. '_directive-keyword': [
  375. include('_directive-keyword!'),
  376. include('value')
  377. ],
  378. 'directive-keyword?': [
  379. include('_directive-keyword!'),
  380. default('#pop')
  381. ],
  382. 'property-keyword*': [
  383. include('_whitespace'),
  384. (r'(additive|long)\b', Keyword),
  385. default('#pop')
  386. ],
  387. 'trace-keyword?': [
  388. include('_whitespace'),
  389. (words((
  390. 'assembly', 'dictionary', 'expressions', 'lines', 'linker',
  391. 'objects', 'off', 'on', 'symbols', 'tokens', 'verbs'), suffix=r'\b'),
  392. Keyword, '#pop'),
  393. default('#pop')
  394. ],
  395. # Statements
  396. 'statements': [
  397. include('_whitespace'),
  398. (r'\]', Punctuation, '#pop'),
  399. (r'[;{}]', Punctuation),
  400. (words((
  401. 'box', 'break', 'continue', 'default', 'give', 'inversion',
  402. 'new_line', 'quit', 'read', 'remove', 'return', 'rfalse', 'rtrue',
  403. 'spaces', 'string', 'until'), suffix=r'\b'),
  404. Keyword, 'default'),
  405. (r'(do|else)\b', Keyword),
  406. (r'(font|style)\b', Keyword,
  407. ('default', 'miscellaneous-keyword?')),
  408. (r'for\b', Keyword, ('for', '(?')),
  409. (r'(if|switch|while)', Keyword,
  410. ('expression', '_expression', '(?')),
  411. (r'(jump|save|restore)\b', Keyword, ('default', 'label?')),
  412. (r'objectloop\b', Keyword,
  413. ('_keyword-expression', 'variable?', '(?')),
  414. (r'print(_ret)?\b|(?=[%s])' % _dquote, Keyword, 'print-list'),
  415. (r'\.', Name.Label, 'label?'),
  416. (r'@', Keyword, 'opcode'),
  417. (r'#(?![agrnw]\$|#)', Punctuation, 'directive'),
  418. (r'<', Punctuation, 'default'),
  419. (r'move\b', Keyword,
  420. ('default', '_keyword-expression', '_expression')),
  421. default(('default', '_keyword-expression', '_expression'))
  422. ],
  423. 'miscellaneous-keyword?': [
  424. include('_whitespace'),
  425. (r'(bold|fixed|from|near|off|on|reverse|roman|to|underline)\b',
  426. Keyword, '#pop'),
  427. (r'(a|A|an|address|char|name|number|object|property|string|the|'
  428. r'The)\b(?=(\s+|(![^%s]*))*\))' % _newline, Keyword.Pseudo,
  429. '#pop'),
  430. (r'%s(?=(\s+|(![^%s]*))*\))' % (_name, _newline), Name.Function,
  431. '#pop'),
  432. default('#pop')
  433. ],
  434. '(?': [
  435. include('_whitespace'),
  436. (r'\(', Punctuation, '#pop'),
  437. default('#pop')
  438. ],
  439. 'for': [
  440. include('_whitespace'),
  441. (r';', Punctuation, ('_for-expression', '_expression')),
  442. default(('_for-expression', '_expression'))
  443. ],
  444. 'print-list': [
  445. include('_whitespace'),
  446. (r';', Punctuation, '#pop'),
  447. (r':', Error),
  448. default(('_list-expression', '_expression', '_list-expression', 'form'))
  449. ],
  450. 'form': [
  451. include('_whitespace'),
  452. (r'\(', Punctuation, ('#pop', 'miscellaneous-keyword?')),
  453. default('#pop')
  454. ],
  455. # Assembly
  456. 'opcode': [
  457. include('_whitespace'),
  458. (r'[%s]' % _dquote, String.Double, ('operands', 'plain-string')),
  459. (_name, Keyword, 'operands')
  460. ],
  461. 'operands': [
  462. (r':', Error),
  463. default(('_assembly-expression', '_expression'))
  464. ]
  465. }
  466. def get_tokens_unprocessed(self, text):
  467. # 'in' is either a keyword or an operator.
  468. # If the token two tokens after 'in' is ')', 'in' is a keyword:
  469. # objectloop(a in b)
  470. # Otherwise, it is an operator:
  471. # objectloop(a in b && true)
  472. objectloop_queue = []
  473. objectloop_token_count = -1
  474. previous_token = None
  475. for index, token, value in RegexLexer.get_tokens_unprocessed(self,
  476. text):
  477. if previous_token is Name.Variable and value == 'in':
  478. objectloop_queue = [[index, token, value]]
  479. objectloop_token_count = 2
  480. elif objectloop_token_count > 0:
  481. if token not in Comment and token not in Text:
  482. objectloop_token_count -= 1
  483. objectloop_queue.append((index, token, value))
  484. else:
  485. if objectloop_token_count == 0:
  486. if objectloop_queue[-1][2] == ')':
  487. objectloop_queue[0][1] = Keyword
  488. while objectloop_queue:
  489. yield objectloop_queue.pop(0)
  490. objectloop_token_count = -1
  491. yield index, token, value
  492. if token not in Comment and token not in Text:
  493. previous_token = token
  494. while objectloop_queue:
  495. yield objectloop_queue.pop(0)
  496. class Inform7Lexer(RegexLexer):
  497. """
  498. For `Inform 7 <http://inform7.com/>`_ source code.
  499. .. versionadded:: 2.0
  500. """
  501. name = 'Inform 7'
  502. aliases = ['inform7', 'i7']
  503. filenames = ['*.ni', '*.i7x']
  504. flags = re.MULTILINE | re.DOTALL | re.UNICODE
  505. _dash = Inform6Lexer._dash
  506. _dquote = Inform6Lexer._dquote
  507. _newline = Inform6Lexer._newline
  508. _start = r'\A|(?<=[%s])' % _newline
  509. # There are three variants of Inform 7, differing in how to
  510. # interpret at signs and braces in I6T. In top-level inclusions, at
  511. # signs in the first column are inweb syntax. In phrase definitions
  512. # and use options, tokens in braces are treated as I7. Use options
  513. # also interpret "{N}".
  514. tokens = {}
  515. token_variants = ['+i6t-not-inline', '+i6t-inline', '+i6t-use-option']
  516. for level in token_variants:
  517. tokens[level] = {
  518. '+i6-root': list(Inform6Lexer.tokens['root']),
  519. '+i6t-root': [ # For Inform6TemplateLexer
  520. (r'[^%s]*' % Inform6Lexer._newline, Comment.Preproc,
  521. ('directive', '+p'))
  522. ],
  523. 'root': [
  524. (r'(\|?\s)+', Text),
  525. (r'\[', Comment.Multiline, '+comment'),
  526. (r'[%s]' % _dquote, Generic.Heading,
  527. ('+main', '+titling', '+titling-string')),
  528. default(('+main', '+heading?'))
  529. ],
  530. '+titling-string': [
  531. (r'[^%s]+' % _dquote, Generic.Heading),
  532. (r'[%s]' % _dquote, Generic.Heading, '#pop')
  533. ],
  534. '+titling': [
  535. (r'\[', Comment.Multiline, '+comment'),
  536. (r'[^%s.;:|%s]+' % (_dquote, _newline), Generic.Heading),
  537. (r'[%s]' % _dquote, Generic.Heading, '+titling-string'),
  538. (r'[%s]{2}|(?<=[\s%s])\|[\s%s]' % (_newline, _dquote, _dquote),
  539. Text, ('#pop', '+heading?')),
  540. (r'[.;:]|(?<=[\s%s])\|' % _dquote, Text, '#pop'),
  541. (r'[|%s]' % _newline, Generic.Heading)
  542. ],
  543. '+main': [
  544. (r'(?i)[^%s:a\[(|%s]+' % (_dquote, _newline), Text),
  545. (r'[%s]' % _dquote, String.Double, '+text'),
  546. (r':', Text, '+phrase-definition'),
  547. (r'(?i)\bas\b', Text, '+use-option'),
  548. (r'\[', Comment.Multiline, '+comment'),
  549. (r'(\([%s])(.*?)([%s]\))' % (_dash, _dash),
  550. bygroups(Punctuation,
  551. using(this, state=('+i6-root', 'directive'),
  552. i6t='+i6t-not-inline'), Punctuation)),
  553. (r'(%s|(?<=[\s;:.%s]))\|\s|[%s]{2,}' %
  554. (_start, _dquote, _newline), Text, '+heading?'),
  555. (r'(?i)[a(|%s]' % _newline, Text)
  556. ],
  557. '+phrase-definition': [
  558. (r'\s+', Text),
  559. (r'\[', Comment.Multiline, '+comment'),
  560. (r'(\([%s])(.*?)([%s]\))' % (_dash, _dash),
  561. bygroups(Punctuation,
  562. using(this, state=('+i6-root', 'directive',
  563. 'default', 'statements'),
  564. i6t='+i6t-inline'), Punctuation), '#pop'),
  565. default('#pop')
  566. ],
  567. '+use-option': [
  568. (r'\s+', Text),
  569. (r'\[', Comment.Multiline, '+comment'),
  570. (r'(\([%s])(.*?)([%s]\))' % (_dash, _dash),
  571. bygroups(Punctuation,
  572. using(this, state=('+i6-root', 'directive'),
  573. i6t='+i6t-use-option'), Punctuation), '#pop'),
  574. default('#pop')
  575. ],
  576. '+comment': [
  577. (r'[^\[\]]+', Comment.Multiline),
  578. (r'\[', Comment.Multiline, '#push'),
  579. (r'\]', Comment.Multiline, '#pop')
  580. ],
  581. '+text': [
  582. (r'[^\[%s]+' % _dquote, String.Double),
  583. (r'\[.*?\]', String.Interpol),
  584. (r'[%s]' % _dquote, String.Double, '#pop')
  585. ],
  586. '+heading?': [
  587. (r'(\|?\s)+', Text),
  588. (r'\[', Comment.Multiline, '+comment'),
  589. (r'[%s]{4}\s+' % _dash, Text, '+documentation-heading'),
  590. (r'[%s]{1,3}' % _dash, Text),
  591. (r'(?i)(volume|book|part|chapter|section)\b[^%s]*' % _newline,
  592. Generic.Heading, '#pop'),
  593. default('#pop')
  594. ],
  595. '+documentation-heading': [
  596. (r'\s+', Text),
  597. (r'\[', Comment.Multiline, '+comment'),
  598. (r'(?i)documentation\s+', Text, '+documentation-heading2'),
  599. default('#pop')
  600. ],
  601. '+documentation-heading2': [
  602. (r'\s+', Text),
  603. (r'\[', Comment.Multiline, '+comment'),
  604. (r'[%s]{4}\s' % _dash, Text, '+documentation'),
  605. default('#pop:2')
  606. ],
  607. '+documentation': [
  608. (r'(?i)(%s)\s*(chapter|example)\s*:[^%s]*' %
  609. (_start, _newline), Generic.Heading),
  610. (r'(?i)(%s)\s*section\s*:[^%s]*' % (_start, _newline),
  611. Generic.Subheading),
  612. (r'((%s)\t.*?[%s])+' % (_start, _newline),
  613. using(this, state='+main')),
  614. (r'[^%s\[]+|[%s\[]' % (_newline, _newline), Text),
  615. (r'\[', Comment.Multiline, '+comment'),
  616. ],
  617. '+i6t-not-inline': [
  618. (r'(%s)@c( .*?)?([%s]|\Z)' % (_start, _newline),
  619. Comment.Preproc),
  620. (r'(%s)@([%s]+|Purpose:)[^%s]*' % (_start, _dash, _newline),
  621. Comment.Preproc),
  622. (r'(%s)@p( .*?)?([%s]|\Z)' % (_start, _newline),
  623. Generic.Heading, '+p')
  624. ],
  625. '+i6t-use-option': [
  626. include('+i6t-not-inline'),
  627. (r'(\{)(N)(\})', bygroups(Punctuation, Text, Punctuation))
  628. ],
  629. '+i6t-inline': [
  630. (r'(\{)(\S[^}]*)?(\})',
  631. bygroups(Punctuation, using(this, state='+main'),
  632. Punctuation))
  633. ],
  634. '+i6t': [
  635. (r'(\{[%s])(![^}]*)(\}?)' % _dash,
  636. bygroups(Punctuation, Comment.Single, Punctuation)),
  637. (r'(\{[%s])(lines)(:)([^}]*)(\}?)' % _dash,
  638. bygroups(Punctuation, Keyword, Punctuation, Text,
  639. Punctuation), '+lines'),
  640. (r'(\{[%s])([^:}]*)(:?)([^}]*)(\}?)' % _dash,
  641. bygroups(Punctuation, Keyword, Punctuation, Text,
  642. Punctuation)),
  643. (r'(\(\+)(.*?)(\+\)|\Z)',
  644. bygroups(Punctuation, using(this, state='+main'),
  645. Punctuation))
  646. ],
  647. '+p': [
  648. (r'[^@]+', Comment.Preproc),
  649. (r'(%s)@c( .*?)?([%s]|\Z)' % (_start, _newline),
  650. Comment.Preproc, '#pop'),
  651. (r'(%s)@([%s]|Purpose:)' % (_start, _dash), Comment.Preproc),
  652. (r'(%s)@p( .*?)?([%s]|\Z)' % (_start, _newline),
  653. Generic.Heading),
  654. (r'@', Comment.Preproc)
  655. ],
  656. '+lines': [
  657. (r'(%s)@c( .*?)?([%s]|\Z)' % (_start, _newline),
  658. Comment.Preproc),
  659. (r'(%s)@([%s]|Purpose:)[^%s]*' % (_start, _dash, _newline),
  660. Comment.Preproc),
  661. (r'(%s)@p( .*?)?([%s]|\Z)' % (_start, _newline),
  662. Generic.Heading, '+p'),
  663. (r'(%s)@\w*[ %s]' % (_start, _newline), Keyword),
  664. (r'![^%s]*' % _newline, Comment.Single),
  665. (r'(\{)([%s]endlines)(\})' % _dash,
  666. bygroups(Punctuation, Keyword, Punctuation), '#pop'),
  667. (r'[^@!{]+?([%s]|\Z)|.' % _newline, Text)
  668. ]
  669. }
  670. # Inform 7 can include snippets of Inform 6 template language,
  671. # so all of Inform6Lexer's states are copied here, with
  672. # modifications to account for template syntax. Inform7Lexer's
  673. # own states begin with '+' to avoid name conflicts. Some of
  674. # Inform6Lexer's states begin with '_': these are not modified.
  675. # They deal with template syntax either by including modified
  676. # states, or by matching r'' then pushing to modified states.
  677. for token in Inform6Lexer.tokens:
  678. if token == 'root':
  679. continue
  680. tokens[level][token] = list(Inform6Lexer.tokens[token])
  681. if not token.startswith('_'):
  682. tokens[level][token][:0] = [include('+i6t'), include(level)]
  683. def __init__(self, **options):
  684. level = options.get('i6t', '+i6t-not-inline')
  685. if level not in self._all_tokens:
  686. self._tokens = self.__class__.process_tokendef(level)
  687. else:
  688. self._tokens = self._all_tokens[level]
  689. RegexLexer.__init__(self, **options)
  690. class Inform6TemplateLexer(Inform7Lexer):
  691. """
  692. For `Inform 6 template
  693. <http://inform7.com/sources/src/i6template/Woven/index.html>`_ code.
  694. .. versionadded:: 2.0
  695. """
  696. name = 'Inform 6 template'
  697. aliases = ['i6t']
  698. filenames = ['*.i6t']
  699. def get_tokens_unprocessed(self, text, stack=('+i6t-root',)):
  700. return Inform7Lexer.get_tokens_unprocessed(self, text, stack)
  701. class Tads3Lexer(RegexLexer):
  702. """
  703. For `TADS 3 <http://www.tads.org/>`_ source code.
  704. """
  705. name = 'TADS 3'
  706. aliases = ['tads3']
  707. filenames = ['*.t']
  708. flags = re.DOTALL | re.MULTILINE
  709. _comment_single = r'(?://(?:[^\\\n]|\\+[\w\W])*$)'
  710. _comment_multiline = r'(?:/\*(?:[^*]|\*(?!/))*\*/)'
  711. _escape = (r'(?:\\(?:[\n\\<>"\'^v bnrt]|u[\da-fA-F]{,4}|x[\da-fA-F]{,2}|'
  712. r'[0-3]?[0-7]{1,2}))')
  713. _name = r'(?:[_a-zA-Z]\w*)'
  714. _no_quote = r'(?=\s|\\?>)'
  715. _operator = (r'(?:&&|\|\||\+\+|--|\?\?|::|[.,@\[\]~]|'
  716. r'(?:[=+\-*/%!&|^]|<<?|>>?>?)=?)')
  717. _ws = r'(?:\\|\s|%s|%s)' % (_comment_single, _comment_multiline)
  718. _ws_pp = r'(?:\\\n|[^\S\n]|%s|%s)' % (_comment_single, _comment_multiline)
  719. def _make_string_state(triple, double, verbatim=None, _escape=_escape):
  720. if verbatim:
  721. verbatim = ''.join(['(?:%s|%s)' % (re.escape(c.lower()),
  722. re.escape(c.upper()))
  723. for c in verbatim])
  724. char = r'"' if double else r"'"
  725. token = String.Double if double else String.Single
  726. escaped_quotes = r'+|%s(?!%s{2})' % (char, char) if triple else r''
  727. prefix = '%s%s' % ('t' if triple else '', 'd' if double else 's')
  728. tag_state_name = '%sqt' % prefix
  729. state = []
  730. if triple:
  731. state += [
  732. (r'%s{3,}' % char, token, '#pop'),
  733. (r'\\%s+' % char, String.Escape),
  734. (char, token)
  735. ]
  736. else:
  737. state.append((char, token, '#pop'))
  738. state += [
  739. include('s/verbatim'),
  740. (r'[^\\<&{}%s]+' % char, token)
  741. ]
  742. if verbatim:
  743. # This regex can't use `(?i)` because escape sequences are
  744. # case-sensitive. `<\XMP>` works; `<\xmp>` doesn't.
  745. state.append((r'\\?<(/|\\\\|(?!%s)\\)%s(?=[\s=>])' %
  746. (_escape, verbatim),
  747. Name.Tag, ('#pop', '%sqs' % prefix, tag_state_name)))
  748. else:
  749. state += [
  750. (r'\\?<!([^><\\%s]|<(?!<)|\\%s%s|%s|\\.)*>?' %
  751. (char, char, escaped_quotes, _escape), Comment.Multiline),
  752. (r'(?i)\\?<listing(?=[\s=>]|\\>)', Name.Tag,
  753. ('#pop', '%sqs/listing' % prefix, tag_state_name)),
  754. (r'(?i)\\?<xmp(?=[\s=>]|\\>)', Name.Tag,
  755. ('#pop', '%sqs/xmp' % prefix, tag_state_name)),
  756. (r'\\?<([^\s=><\\%s]|<(?!<)|\\%s%s|%s|\\.)*' %
  757. (char, char, escaped_quotes, _escape), Name.Tag,
  758. tag_state_name),
  759. include('s/entity')
  760. ]
  761. state += [
  762. include('s/escape'),
  763. (r'\{([^}<\\%s]|<(?!<)|\\%s%s|%s|\\.)*\}' %
  764. (char, char, escaped_quotes, _escape), String.Interpol),
  765. (r'[\\&{}<]', token)
  766. ]
  767. return state
  768. def _make_tag_state(triple, double, _escape=_escape):
  769. char = r'"' if double else r"'"
  770. quantifier = r'{3,}' if triple else r''
  771. state_name = '%s%sqt' % ('t' if triple else '', 'd' if double else 's')
  772. token = String.Double if double else String.Single
  773. escaped_quotes = r'+|%s(?!%s{2})' % (char, char) if triple else r''
  774. return [
  775. (r'%s%s' % (char, quantifier), token, '#pop:2'),
  776. (r'(\s|\\\n)+', Text),
  777. (r'(=)(\\?")', bygroups(Punctuation, String.Double),
  778. 'dqs/%s' % state_name),
  779. (r"(=)(\\?')", bygroups(Punctuation, String.Single),
  780. 'sqs/%s' % state_name),
  781. (r'=', Punctuation, 'uqs/%s' % state_name),
  782. (r'\\?>', Name.Tag, '#pop'),
  783. (r'\{([^}<\\%s]|<(?!<)|\\%s%s|%s|\\.)*\}' %
  784. (char, char, escaped_quotes, _escape), String.Interpol),
  785. (r'([^\s=><\\%s]|<(?!<)|\\%s%s|%s|\\.)+' %
  786. (char, char, escaped_quotes, _escape), Name.Attribute),
  787. include('s/escape'),
  788. include('s/verbatim'),
  789. include('s/entity'),
  790. (r'[\\{}&]', Name.Attribute)
  791. ]
  792. def _make_attribute_value_state(terminator, host_triple, host_double,
  793. _escape=_escape):
  794. token = (String.Double if terminator == r'"' else
  795. String.Single if terminator == r"'" else String.Other)
  796. host_char = r'"' if host_double else r"'"
  797. host_quantifier = r'{3,}' if host_triple else r''
  798. host_token = String.Double if host_double else String.Single
  799. escaped_quotes = (r'+|%s(?!%s{2})' % (host_char, host_char)
  800. if host_triple else r'')
  801. return [
  802. (r'%s%s' % (host_char, host_quantifier), host_token, '#pop:3'),
  803. (r'%s%s' % (r'' if token is String.Other else r'\\?', terminator),
  804. token, '#pop'),
  805. include('s/verbatim'),
  806. include('s/entity'),
  807. (r'\{([^}<\\%s]|<(?!<)|\\%s%s|%s|\\.)*\}' %
  808. (host_char, host_char, escaped_quotes, _escape), String.Interpol),
  809. (r'([^\s"\'<%s{}\\&])+' % (r'>' if token is String.Other else r''),
  810. token),
  811. include('s/escape'),
  812. (r'["\'\s&{<}\\]', token)
  813. ]
  814. tokens = {
  815. 'root': [
  816. (u'\ufeff', Text),
  817. (r'\{', Punctuation, 'object-body'),
  818. (r';+', Punctuation),
  819. (r'(?=(argcount|break|case|catch|continue|default|definingobj|'
  820. r'delegated|do|else|for|foreach|finally|goto|if|inherited|'
  821. r'invokee|local|nil|new|operator|replaced|return|self|switch|'
  822. r'targetobj|targetprop|throw|true|try|while)\b)', Text, 'block'),
  823. (r'(%s)(%s*)(\()' % (_name, _ws),
  824. bygroups(Name.Function, using(this, state='whitespace'),
  825. Punctuation),
  826. ('block?/root', 'more/parameters', 'main/parameters')),
  827. include('whitespace'),
  828. (r'\++', Punctuation),
  829. (r'[^\s!"%-(*->@-_a-z{-~]+', Error), # Averts an infinite loop
  830. (r'(?!\Z)', Text, 'main/root')
  831. ],
  832. 'main/root': [
  833. include('main/basic'),
  834. default(('#pop', 'object-body/no-braces', 'classes', 'class'))
  835. ],
  836. 'object-body/no-braces': [
  837. (r';', Punctuation, '#pop'),
  838. (r'\{', Punctuation, ('#pop', 'object-body')),
  839. include('object-body')
  840. ],
  841. 'object-body': [
  842. (r';', Punctuation),
  843. (r'\{', Punctuation, '#push'),
  844. (r'\}', Punctuation, '#pop'),
  845. (r':', Punctuation, ('classes', 'class')),
  846. (r'(%s?)(%s*)(\()' % (_name, _ws),
  847. bygroups(Name.Function, using(this, state='whitespace'),
  848. Punctuation),
  849. ('block?', 'more/parameters', 'main/parameters')),
  850. (r'(%s)(%s*)(\{)' % (_name, _ws),
  851. bygroups(Name.Function, using(this, state='whitespace'),
  852. Punctuation), 'block'),
  853. (r'(%s)(%s*)(:)' % (_name, _ws),
  854. bygroups(Name.Variable, using(this, state='whitespace'),
  855. Punctuation),
  856. ('object-body/no-braces', 'classes', 'class')),
  857. include('whitespace'),
  858. (r'->|%s' % _operator, Punctuation, 'main'),
  859. default('main/object-body')
  860. ],
  861. 'main/object-body': [
  862. include('main/basic'),
  863. (r'(%s)(%s*)(=?)' % (_name, _ws),
  864. bygroups(Name.Variable, using(this, state='whitespace'),
  865. Punctuation), ('#pop', 'more', 'main')),
  866. default('#pop:2')
  867. ],
  868. 'block?/root': [
  869. (r'\{', Punctuation, ('#pop', 'block')),
  870. include('whitespace'),
  871. (r'(?=[\[\'"<(:])', Text, # It might be a VerbRule macro.
  872. ('#pop', 'object-body/no-braces', 'grammar', 'grammar-rules')),
  873. # It might be a macro like DefineAction.
  874. default(('#pop', 'object-body/no-braces'))
  875. ],
  876. 'block?': [
  877. (r'\{', Punctuation, ('#pop', 'block')),
  878. include('whitespace'),
  879. default('#pop')
  880. ],
  881. 'block/basic': [
  882. (r'[;:]+', Punctuation),
  883. (r'\{', Punctuation, '#push'),
  884. (r'\}', Punctuation, '#pop'),
  885. (r'default\b', Keyword.Reserved),
  886. (r'(%s)(%s*)(:)' % (_name, _ws),
  887. bygroups(Name.Label, using(this, state='whitespace'),
  888. Punctuation)),
  889. include('whitespace')
  890. ],
  891. 'block': [
  892. include('block/basic'),
  893. (r'(?!\Z)', Text, ('more', 'main'))
  894. ],
  895. 'block/embed': [
  896. (r'>>', String.Interpol, '#pop'),
  897. include('block/basic'),
  898. (r'(?!\Z)', Text, ('more/embed', 'main'))
  899. ],
  900. 'main/basic': [
  901. include('whitespace'),
  902. (r'\(', Punctuation, ('#pop', 'more', 'main')),
  903. (r'\[', Punctuation, ('#pop', 'more/list', 'main')),
  904. (r'\{', Punctuation, ('#pop', 'more/inner', 'main/inner',
  905. 'more/parameters', 'main/parameters')),
  906. (r'\*|\.{3}', Punctuation, '#pop'),
  907. (r'(?i)0x[\da-f]+', Number.Hex, '#pop'),
  908. (r'(\d+\.(?!\.)\d*|\.\d+)([eE][-+]?\d+)?|\d+[eE][-+]?\d+',
  909. Number.Float, '#pop'),
  910. (r'0[0-7]+', Number.Oct, '#pop'),
  911. (r'\d+', Number.Integer, '#pop'),
  912. (r'"""', String.Double, ('#pop', 'tdqs')),
  913. (r"'''", String.Single, ('#pop', 'tsqs')),
  914. (r'"', String.Double, ('#pop', 'dqs')),
  915. (r"'", String.Single, ('#pop', 'sqs')),
  916. (r'R"""', String.Regex, ('#pop', 'tdqr')),
  917. (r"R'''", String.Regex, ('#pop', 'tsqr')),
  918. (r'R"', String.Regex, ('#pop', 'dqr')),
  919. (r"R'", String.Regex, ('#pop', 'sqr')),
  920. # Two-token keywords
  921. (r'(extern)(%s+)(object\b)' % _ws,
  922. bygroups(Keyword.Reserved, using(this, state='whitespace'),
  923. Keyword.Reserved)),
  924. (r'(function|method)(%s*)(\()' % _ws,
  925. bygroups(Keyword.Reserved, using(this, state='whitespace'),
  926. Punctuation),
  927. ('#pop', 'block?', 'more/parameters', 'main/parameters')),
  928. (r'(modify)(%s+)(grammar\b)' % _ws,
  929. bygroups(Keyword.Reserved, using(this, state='whitespace'),
  930. Keyword.Reserved),
  931. ('#pop', 'object-body/no-braces', ':', 'grammar')),
  932. (r'(new)(%s+(?=(?:function|method)\b))' % _ws,
  933. bygroups(Keyword.Reserved, using(this, state='whitespace'))),
  934. (r'(object)(%s+)(template\b)' % _ws,
  935. bygroups(Keyword.Reserved, using(this, state='whitespace'),
  936. Keyword.Reserved), ('#pop', 'template')),
  937. (r'(string)(%s+)(template\b)' % _ws,
  938. bygroups(Keyword, using(this, state='whitespace'),
  939. Keyword.Reserved), ('#pop', 'function-name')),
  940. # Keywords
  941. (r'(argcount|definingobj|invokee|replaced|targetobj|targetprop)\b',
  942. Name.Builtin, '#pop'),
  943. (r'(break|continue|goto)\b', Keyword.Reserved, ('#pop', 'label')),
  944. (r'(case|extern|if|intrinsic|return|static|while)\b',
  945. Keyword.Reserved),
  946. (r'catch\b', Keyword.Reserved, ('#pop', 'catch')),
  947. (r'class\b', Keyword.Reserved,
  948. ('#pop', 'object-body/no-braces', 'class')),
  949. (r'(default|do|else|finally|try)\b', Keyword.Reserved, '#pop'),
  950. (r'(dictionary|property)\b', Keyword.Reserved,
  951. ('#pop', 'constants')),
  952. (r'enum\b', Keyword.Reserved, ('#pop', 'enum')),
  953. (r'export\b', Keyword.Reserved, ('#pop', 'main')),
  954. (r'(for|foreach)\b', Keyword.Reserved,
  955. ('#pop', 'more/inner', 'main/inner')),
  956. (r'(function|method)\b', Keyword.Reserved,
  957. ('#pop', 'block?', 'function-name')),
  958. (r'grammar\b', Keyword.Reserved,
  959. ('#pop', 'object-body/no-braces', 'grammar')),
  960. (r'inherited\b', Keyword.Reserved, ('#pop', 'inherited')),
  961. (r'local\b', Keyword.Reserved,
  962. ('#pop', 'more/local', 'main/local')),
  963. (r'(modify|replace|switch|throw|transient)\b', Keyword.Reserved,
  964. '#pop'),
  965. (r'new\b', Keyword.Reserved, ('#pop', 'class')),
  966. (r'(nil|true)\b', Keyword.Constant, '#pop'),
  967. (r'object\b', Keyword.Reserved, ('#pop', 'object-body/no-braces')),
  968. (r'operator\b', Keyword.Reserved, ('#pop', 'operator')),
  969. (r'propertyset\b', Keyword.Reserved,
  970. ('#pop', 'propertyset', 'main')),
  971. (r'self\b', Name.Builtin.Pseudo, '#pop'),
  972. (r'template\b', Keyword.Reserved, ('#pop', 'template')),
  973. # Operators
  974. (r'(__objref|defined)(%s*)(\()' % _ws,
  975. bygroups(Operator.Word, using(this, state='whitespace'),
  976. Operator), ('#pop', 'more/__objref', 'main')),
  977. (r'delegated\b', Operator.Word),
  978. # Compiler-defined macros and built-in properties
  979. (r'(__DATE__|__DEBUG|__LINE__|__FILE__|'
  980. r'__TADS_MACRO_FORMAT_VERSION|__TADS_SYS_\w*|__TADS_SYSTEM_NAME|'
  981. r'__TADS_VERSION_MAJOR|__TADS_VERSION_MINOR|__TADS3|__TIME__|'
  982. r'construct|finalize|grammarInfo|grammarTag|lexicalParent|'
  983. r'miscVocab|sourceTextGroup|sourceTextGroupName|'
  984. r'sourceTextGroupOrder|sourceTextOrder)\b', Name.Builtin, '#pop')
  985. ],
  986. 'main': [
  987. include('main/basic'),
  988. (_name, Name, '#pop'),
  989. default('#pop')
  990. ],
  991. 'more/basic': [
  992. (r'\(', Punctuation, ('more/list', 'main')),
  993. (r'\[', Punctuation, ('more', 'main')),
  994. (r'\.{3}', Punctuation),
  995. (r'->|\.\.', Punctuation, 'main'),
  996. (r'(?=;)|[:)\]]', Punctuation, '#pop'),
  997. include('whitespace'),
  998. (_operator, Operator, 'main'),
  999. (r'\?', Operator, ('main', 'more/conditional', 'main')),
  1000. (r'(is|not)(%s+)(in\b)' % _ws,
  1001. bygroups(Operator.Word, using(this, state='whitespace'),
  1002. Operator.Word)),
  1003. (r'[^\s!"%-_a-z{-~]+', Error) # Averts an infinite loop
  1004. ],
  1005. 'more': [
  1006. include('more/basic'),
  1007. default('#pop')
  1008. ],
  1009. # Then expression (conditional operator)
  1010. 'more/conditional': [
  1011. (r':(?!:)', Operator, '#pop'),
  1012. include('more')
  1013. ],
  1014. # Embedded expressions
  1015. 'more/embed': [
  1016. (r'>>', String.Interpol, '#pop:2'),
  1017. include('more')
  1018. ],
  1019. # For/foreach loop initializer or short-form anonymous function
  1020. 'main/inner': [
  1021. (r'\(', Punctuation, ('#pop', 'more/inner', 'main/inner')),
  1022. (r'local\b', Keyword.Reserved, ('#pop', 'main/local')),
  1023. include('main')
  1024. ],
  1025. 'more/inner': [
  1026. (r'\}', Punctuation, '#pop'),
  1027. (r',', Punctuation, 'main/inner'),
  1028. (r'(in|step)\b', Keyword, 'main/inner'),
  1029. include('more')
  1030. ],
  1031. # Local
  1032. 'main/local': [
  1033. (_name, Name.Variable, '#pop'),
  1034. include('whitespace')
  1035. ],
  1036. 'more/local': [
  1037. (r',', Punctuation, 'main/local'),
  1038. include('more')
  1039. ],
  1040. # List
  1041. 'more/list': [
  1042. (r'[,:]', Punctuation, 'main'),
  1043. include('more')
  1044. ],
  1045. # Parameter list
  1046. 'main/parameters': [
  1047. (r'(%s)(%s*)(?=:)' % (_name, _ws),
  1048. bygroups(Name.Variable, using(this, state='whitespace')), '#pop'),
  1049. (r'(%s)(%s+)(%s)' % (_name, _ws, _name),
  1050. bygroups(Name.Class, using(this, state='whitespace'),
  1051. Name.Variable), '#pop'),
  1052. (r'\[+', Punctuation),
  1053. include('main/basic'),
  1054. (_name, Name.Variable, '#pop'),
  1055. default('#pop')
  1056. ],
  1057. 'more/parameters': [
  1058. (r'(:)(%s*(?=[?=,:)]))' % _ws,
  1059. bygroups(Punctuation, using(this, state='whitespace'))),
  1060. (r'[?\]]+', Punctuation),
  1061. (r'[:)]', Punctuation, ('#pop', 'multimethod?')),
  1062. (r',', Punctuation, 'main/parameters'),
  1063. (r'=', Punctuation, ('more/parameter', 'main')),
  1064. include('more')
  1065. ],
  1066. 'more/parameter': [
  1067. (r'(?=[,)])', Text, '#pop'),
  1068. include('more')
  1069. ],
  1070. 'multimethod?': [
  1071. (r'multimethod\b', Keyword, '#pop'),
  1072. include('whitespace'),
  1073. default('#pop')
  1074. ],
  1075. # Statements and expressions
  1076. 'more/__objref': [
  1077. (r',', Punctuation, 'mode'),
  1078. (r'\)', Operator, '#pop'),
  1079. include('more')
  1080. ],
  1081. 'mode': [
  1082. (r'(error|warn)\b', Keyword, '#pop'),
  1083. include('whitespace')
  1084. ],
  1085. 'catch': [
  1086. (r'\(+', Punctuation),
  1087. (_name, Name.Exception, ('#pop', 'variables')),
  1088. include('whitespace')
  1089. ],
  1090. 'enum': [
  1091. include('whitespace'),
  1092. (r'token\b', Keyword, ('#pop', 'constants')),
  1093. default(('#pop', 'constants'))
  1094. ],
  1095. 'grammar': [
  1096. (r'\)+', Punctuation),
  1097. (r'\(', Punctuation, 'grammar-tag'),
  1098. (r':', Punctuation, 'grammar-rules'),
  1099. (_name, Name.Class),
  1100. include('whitespace')
  1101. ],
  1102. 'grammar-tag': [
  1103. include('whitespace'),
  1104. (r'"""([^\\"<]|""?(?!")|\\"+|\\.|<(?!<))+("{3,}|<<)|'
  1105. r'R"""([^\\"]|""?(?!")|\\"+|\\.)+"{3,}|'
  1106. r"'''([^\\'<]|''?(?!')|\\'+|\\.|<(?!<))+('{3,}|<<)|"
  1107. r"R'''([^\\']|''?(?!')|\\'+|\\.)+'{3,}|"
  1108. r'"([^\\"<]|\\.|<(?!<))+("|<<)|R"([^\\"]|\\.)+"|'
  1109. r"'([^\\'<]|\\.|<(?!<))+('|<<)|R'([^\\']|\\.)+'|"
  1110. r"([^)\s\\/]|/(?![/*]))+|\)", String.Other, '#pop')
  1111. ],
  1112. 'grammar-rules': [
  1113. include('string'),
  1114. include('whitespace'),
  1115. (r'(\[)(%s*)(badness)' % _ws,
  1116. bygroups(Punctuation, using(this, state='whitespace'), Keyword),
  1117. 'main'),
  1118. (r'->|%s|[()]' % _operator, Punctuation),
  1119. (_name, Name.Constant),
  1120. default('#pop:2')
  1121. ],
  1122. ':': [
  1123. (r':', Punctuation, '#pop')
  1124. ],
  1125. 'function-name': [
  1126. (r'(<<([^>]|>>>|>(?!>))*>>)+', String.Interpol),
  1127. (r'(?=%s?%s*[({])' % (_name, _ws), Text, '#pop'),
  1128. (_name, Name.Function, '#pop'),
  1129. include('whitespace')
  1130. ],
  1131. 'inherited': [
  1132. (r'<', Punctuation, ('#pop', 'classes', 'class')),
  1133. include('whitespace'),
  1134. (_name, Name.Class, '#pop'),
  1135. default('#pop')
  1136. ],
  1137. 'operator': [
  1138. (r'negate\b', Operator.Word, '#pop'),
  1139. include('whitespace'),
  1140. (_operator, Operator),
  1141. default('#pop')
  1142. ],
  1143. 'propertyset': [
  1144. (r'\(', Punctuation, ('more/parameters', 'main/parameters')),
  1145. (r'\{', Punctuation, ('#pop', 'object-body')),
  1146. include('whitespace')
  1147. ],
  1148. 'template': [
  1149. (r'(?=;)', Text, '#pop'),
  1150. include('string'),
  1151. (r'inherited\b', Keyword.Reserved),
  1152. include('whitespace'),
  1153. (r'->|\?|%s' % _operator, Punctuation),
  1154. (_name, Name.Variable)
  1155. ],
  1156. # Identifiers
  1157. 'class': [
  1158. (r'\*|\.{3}', Punctuation, '#pop'),
  1159. (r'object\b', Keyword.Reserved, '#pop'),
  1160. (r'transient\b', Keyword.Reserved),
  1161. (_name, Name.Class, '#pop'),
  1162. include('whitespace'),
  1163. default('#pop')
  1164. ],
  1165. 'classes': [
  1166. (r'[:,]', Punctuation, 'class'),
  1167. include('whitespace'),
  1168. (r'>', Punctuation, '#pop'),
  1169. default('#pop')
  1170. ],
  1171. 'constants': [
  1172. (r',+', Punctuation),
  1173. (r';', Punctuation, '#pop'),
  1174. (r'property\b', Keyword.Reserved),
  1175. (_name, Name.Constant),
  1176. include('whitespace')
  1177. ],
  1178. 'label': [
  1179. (_name, Name.Label, '#pop'),
  1180. include('whitespace'),
  1181. default('#pop')
  1182. ],
  1183. 'variables': [
  1184. (r',+', Punctuation),
  1185. (r'\)', Punctuation, '#pop'),
  1186. include('whitespace'),
  1187. (_name, Name.Variable)
  1188. ],
  1189. # Whitespace and comments
  1190. 'whitespace': [
  1191. (r'^%s*#(%s|[^\n]|(?<=\\)\n)*\n?' % (_ws_pp, _comment_multiline),
  1192. Comment.Preproc),
  1193. (_comment_single, Comment.Single),
  1194. (_comment_multiline, Comment.Multiline),
  1195. (r'\\+\n+%s*#?|\n+|([^\S\n]|\\)+' % _ws_pp, Text)
  1196. ],
  1197. # Strings
  1198. 'string': [
  1199. (r'"""', String.Double, 'tdqs'),
  1200. (r"'''", String.Single, 'tsqs'),
  1201. (r'"', String.Double, 'dqs'),
  1202. (r"'", String.Single, 'sqs')
  1203. ],
  1204. 's/escape': [
  1205. (r'\{\{|\}\}|%s' % _escape, String.Escape)
  1206. ],
  1207. 's/verbatim': [
  1208. (r'<<\s*(as\s+decreasingly\s+likely\s+outcomes|cycling|else|end|'
  1209. r'first\s+time|one\s+of|only|or|otherwise|'
  1210. r'(sticky|(then\s+)?(purely\s+)?at)\s+random|stopping|'
  1211. r'(then\s+)?(half\s+)?shuffled|\|\|)\s*>>', String.Interpol),
  1212. (r'<<(%%(_(%s|\\?.)|[\-+ ,#]|\[\d*\]?)*\d*\.?\d*(%s|\\?.)|'
  1213. r'\s*((else|otherwise)\s+)?(if|unless)\b)?' % (_escape, _escape),
  1214. String.Interpol, ('block/embed', 'more/embed', 'main'))
  1215. ],
  1216. 's/entity': [
  1217. (r'(?i)&(#(x[\da-f]+|\d+)|[a-z][\da-z]*);?', Name.Entity)
  1218. ],
  1219. 'tdqs': _make_string_state(True, True),
  1220. 'tsqs': _make_string_state(True, False),
  1221. 'dqs': _make_string_state(False, True),
  1222. 'sqs': _make_string_state(False, False),
  1223. 'tdqs/listing': _make_string_state(True, True, 'listing'),
  1224. 'tsqs/listing': _make_string_state(True, False, 'listing'),
  1225. 'dqs/listing': _make_string_state(False, True, 'listing'),
  1226. 'sqs/listing': _make_string_state(False, False, 'listing'),
  1227. 'tdqs/xmp': _make_string_state(True, True, 'xmp'),
  1228. 'tsqs/xmp': _make_string_state(True, False, 'xmp'),
  1229. 'dqs/xmp': _make_string_state(False, True, 'xmp'),
  1230. 'sqs/xmp': _make_string_state(False, False, 'xmp'),
  1231. # Tags
  1232. 'tdqt': _make_tag_state(True, True),
  1233. 'tsqt': _make_tag_state(True, False),
  1234. 'dqt': _make_tag_state(False, True),
  1235. 'sqt': _make_tag_state(False, False),
  1236. 'dqs/tdqt': _make_attribute_value_state(r'"', True, True),
  1237. 'dqs/tsqt': _make_attribute_value_state(r'"', True, False),
  1238. 'dqs/dqt': _make_attribute_value_state(r'"', False, True),
  1239. 'dqs/sqt': _make_attribute_value_state(r'"', False, False),
  1240. 'sqs/tdqt': _make_attribute_value_state(r"'", True, True),
  1241. 'sqs/tsqt': _make_attribute_value_state(r"'", True, False),
  1242. 'sqs/dqt': _make_attribute_value_state(r"'", False, True),
  1243. 'sqs/sqt': _make_attribute_value_state(r"'", False, False),
  1244. 'uqs/tdqt': _make_attribute_value_state(_no_quote, True, True),
  1245. 'uqs/tsqt': _make_attribute_value_state(_no_quote, True, False),
  1246. 'uqs/dqt': _make_attribute_value_state(_no_quote, False, True),
  1247. 'uqs/sqt': _make_attribute_value_state(_no_quote, False, False),
  1248. # Regular expressions
  1249. 'tdqr': [
  1250. (r'[^\\"]+', String.Regex),
  1251. (r'\\"*', String.Regex),
  1252. (r'"{3,}', String.Regex, '#pop'),
  1253. (r'"', String.Regex)
  1254. ],
  1255. 'tsqr': [
  1256. (r"[^\\']+", String.Regex),
  1257. (r"\\'*", String.Regex),
  1258. (r"'{3,}", String.Regex, '#pop'),
  1259. (r"'", String.Regex)
  1260. ],
  1261. 'dqr': [
  1262. (r'[^\\"]+', String.Regex),
  1263. (r'\\"?', String.Regex),
  1264. (r'"', String.Regex, '#pop')
  1265. ],
  1266. 'sqr': [
  1267. (r"[^\\']+", String.Regex),
  1268. (r"\\'?", String.Regex),
  1269. (r"'", String.Regex, '#pop')
  1270. ]
  1271. }
  1272. def get_tokens_unprocessed(self, text, **kwargs):
  1273. pp = r'^%s*#%s*' % (self._ws_pp, self._ws_pp)
  1274. if_false_level = 0
  1275. for index, token, value in (
  1276. RegexLexer.get_tokens_unprocessed(self, text, **kwargs)):
  1277. if if_false_level == 0: # Not in a false #if
  1278. if (token is Comment.Preproc and
  1279. re.match(r'%sif%s+(0|nil)%s*$\n?' %
  1280. (pp, self._ws_pp, self._ws_pp), value)):
  1281. if_false_level = 1
  1282. else: # In a false #if
  1283. if token is Comment.Preproc:
  1284. if (if_false_level == 1 and
  1285. re.match(r'%sel(if|se)\b' % pp, value)):
  1286. if_false_level = 0
  1287. elif re.match(r'%sif' % pp, value):
  1288. if_false_level += 1
  1289. elif re.match(r'%sendif\b' % pp, value):
  1290. if_false_level -= 1
  1291. else:
  1292. token = Comment
  1293. yield index, token, value