int_fiction.py 55 KB

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