int_fiction.py 56 KB

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