int_fiction.py 55 KB

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