sql.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. """
  2. pygments.lexers.sql
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for various SQL dialects and related interactive sessions.
  5. Postgres specific lexers:
  6. `PostgresLexer`
  7. A SQL lexer for the PostgreSQL dialect. Differences w.r.t. the SQL
  8. lexer are:
  9. - keywords and data types list parsed from the PG docs (run the
  10. `_postgres_builtins` module to update them);
  11. - Content of $-strings parsed using a specific lexer, e.g. the content
  12. of a PL/Python function is parsed using the Python lexer;
  13. - parse PG specific constructs: E-strings, $-strings, U&-strings,
  14. different operators and punctuation.
  15. `PlPgsqlLexer`
  16. A lexer for the PL/pgSQL language. Adds a few specific construct on
  17. top of the PG SQL lexer (such as <<label>>).
  18. `PostgresConsoleLexer`
  19. A lexer to highlight an interactive psql session:
  20. - identifies the prompt and does its best to detect the end of command
  21. in multiline statement where not all the lines are prefixed by a
  22. prompt, telling them apart from the output;
  23. - highlights errors in the output and notification levels;
  24. - handles psql backslash commands.
  25. `PostgresExplainLexer`
  26. A lexer to highlight Postgres execution plan.
  27. The ``tests/examplefiles`` contains a few test files with data to be
  28. parsed by these lexers.
  29. :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
  30. :license: BSD, see LICENSE for details.
  31. """
  32. import collections
  33. import re
  34. from pygments.lexer import Lexer, RegexLexer, do_insertions, bygroups, words
  35. from pygments.lexers import _googlesql_builtins
  36. from pygments.lexers import _mysql_builtins
  37. from pygments.lexers import _postgres_builtins
  38. from pygments.lexers import _sql_builtins
  39. from pygments.lexers import _tsql_builtins
  40. from pygments.lexers import get_lexer_by_name, ClassNotFound
  41. from pygments.token import Punctuation, Whitespace, Text, Comment, Operator, \
  42. Keyword, Name, String, Number, Generic, Literal
  43. __all__ = ['GoogleSqlLexer', 'PostgresLexer', 'PlPgsqlLexer',
  44. 'PostgresConsoleLexer', 'PostgresExplainLexer', 'SqlLexer',
  45. 'TransactSqlLexer', 'MySqlLexer', 'SqliteConsoleLexer', 'RqlLexer']
  46. line_re = re.compile('.*?\n')
  47. sqlite_prompt_re = re.compile(r'^(?:sqlite| ...)>(?= )')
  48. language_re = re.compile(r"\s+LANGUAGE\s+'?(\w+)'?", re.IGNORECASE)
  49. do_re = re.compile(r'\bDO\b', re.IGNORECASE)
  50. # Regular expressions for analyse_text()
  51. name_between_bracket_re = re.compile(r'\[[a-zA-Z_]\w*\]')
  52. name_between_backtick_re = re.compile(r'`[a-zA-Z_]\w*`')
  53. tsql_go_re = re.compile(r'\bgo\b', re.IGNORECASE)
  54. tsql_declare_re = re.compile(r'\bdeclare\s+@', re.IGNORECASE)
  55. tsql_variable_re = re.compile(r'@[a-zA-Z_]\w*\b')
  56. # Identifiers for analyse_text()
  57. googlesql_identifiers = (
  58. _googlesql_builtins.functionnames
  59. + _googlesql_builtins.keywords
  60. + _googlesql_builtins.types)
  61. def language_callback(lexer, match):
  62. """Parse the content of a $-string using a lexer
  63. The lexer is chosen looking for a nearby LANGUAGE or assumed as
  64. plpgsql if inside a DO statement and no LANGUAGE has been found.
  65. """
  66. lx = None
  67. m = language_re.match(lexer.text[match.end():match.end()+100])
  68. if m is not None:
  69. lx = lexer._get_lexer(m.group(1))
  70. else:
  71. m = list(language_re.finditer(
  72. lexer.text[max(0, match.start()-100):match.start()]))
  73. if m:
  74. lx = lexer._get_lexer(m[-1].group(1))
  75. else:
  76. m = list(do_re.finditer(
  77. lexer.text[max(0, match.start()-25):match.start()]))
  78. if m:
  79. lx = lexer._get_lexer('plpgsql')
  80. # 1 = $, 2 = delimiter, 3 = $
  81. yield (match.start(1), String, match.group(1))
  82. yield (match.start(2), String.Delimiter, match.group(2))
  83. yield (match.start(3), String, match.group(3))
  84. # 4 = string contents
  85. if lx:
  86. yield from lx.get_tokens_unprocessed(match.group(4))
  87. else:
  88. yield (match.start(4), String, match.group(4))
  89. # 5 = $, 6 = delimiter, 7 = $
  90. yield (match.start(5), String, match.group(5))
  91. yield (match.start(6), String.Delimiter, match.group(6))
  92. yield (match.start(7), String, match.group(7))
  93. class PostgresBase:
  94. """Base class for Postgres-related lexers.
  95. This is implemented as a mixin to avoid the Lexer metaclass kicking in.
  96. this way the different lexer don't have a common Lexer ancestor. If they
  97. had, _tokens could be created on this ancestor and not updated for the
  98. other classes, resulting e.g. in PL/pgSQL parsed as SQL. This shortcoming
  99. seem to suggest that regexp lexers are not really subclassable.
  100. """
  101. def get_tokens_unprocessed(self, text, *args):
  102. # Have a copy of the entire text to be used by `language_callback`.
  103. self.text = text
  104. yield from super().get_tokens_unprocessed(text, *args)
  105. def _get_lexer(self, lang):
  106. if lang.lower() == 'sql':
  107. return get_lexer_by_name('postgresql', **self.options)
  108. tries = [lang]
  109. if lang.startswith('pl'):
  110. tries.append(lang[2:])
  111. if lang.endswith('u'):
  112. tries.append(lang[:-1])
  113. if lang.startswith('pl') and lang.endswith('u'):
  114. tries.append(lang[2:-1])
  115. for lx in tries:
  116. try:
  117. return get_lexer_by_name(lx, **self.options)
  118. except ClassNotFound:
  119. pass
  120. else:
  121. # TODO: better logging
  122. # print >>sys.stderr, "language not found:", lang
  123. return None
  124. class PostgresLexer(PostgresBase, RegexLexer):
  125. """
  126. Lexer for the PostgreSQL dialect of SQL.
  127. """
  128. name = 'PostgreSQL SQL dialect'
  129. aliases = ['postgresql', 'postgres']
  130. mimetypes = ['text/x-postgresql']
  131. url = 'https://www.postgresql.org'
  132. version_added = '1.5'
  133. flags = re.IGNORECASE
  134. tokens = {
  135. 'root': [
  136. (r'\s+', Whitespace),
  137. (r'--.*\n?', Comment.Single),
  138. (r'/\*', Comment.Multiline, 'multiline-comments'),
  139. (r'(' + '|'.join(s.replace(" ", r"\s+")
  140. for s in _postgres_builtins.DATATYPES +
  141. _postgres_builtins.PSEUDO_TYPES) + r')\b',
  142. Name.Builtin),
  143. (words(_postgres_builtins.KEYWORDS, suffix=r'\b'), Keyword),
  144. (r'[+*/<>=~!@#%^&|`?-]+', Operator),
  145. (r'::', Operator), # cast
  146. (r'\$\d+', Name.Variable),
  147. (r'([0-9]*\.[0-9]*|[0-9]+)(e[+-]?[0-9]+)?', Number.Float),
  148. (r'[0-9]+', Number.Integer),
  149. (r"((?:E|U&)?)(')", bygroups(String.Affix, String.Single), 'string'),
  150. # quoted identifier
  151. (r'((?:U&)?)(")', bygroups(String.Affix, String.Name), 'quoted-ident'),
  152. (r'(?s)(\$)([^$]*)(\$)(.*?)(\$)(\2)(\$)', language_callback),
  153. (r'[a-z_]\w*', Name),
  154. # psql variable in SQL
  155. (r""":(['"]?)[a-z]\w*\b\1""", Name.Variable),
  156. (r'[;:()\[\]{},.]', Punctuation),
  157. ],
  158. 'multiline-comments': [
  159. (r'/\*', Comment.Multiline, 'multiline-comments'),
  160. (r'\*/', Comment.Multiline, '#pop'),
  161. (r'[^/*]+', Comment.Multiline),
  162. (r'[/*]', Comment.Multiline)
  163. ],
  164. 'string': [
  165. (r"[^']+", String.Single),
  166. (r"''", String.Single),
  167. (r"'", String.Single, '#pop'),
  168. ],
  169. 'quoted-ident': [
  170. (r'[^"]+', String.Name),
  171. (r'""', String.Name),
  172. (r'"', String.Name, '#pop'),
  173. ],
  174. }
  175. class PlPgsqlLexer(PostgresBase, RegexLexer):
  176. """
  177. Handle the extra syntax in Pl/pgSQL language.
  178. """
  179. name = 'PL/pgSQL'
  180. aliases = ['plpgsql']
  181. mimetypes = ['text/x-plpgsql']
  182. url = 'https://www.postgresql.org/docs/current/plpgsql.html'
  183. version_added = '1.5'
  184. flags = re.IGNORECASE
  185. # FIXME: use inheritance
  186. tokens = {name: state[:] for (name, state) in PostgresLexer.tokens.items()}
  187. # extend the keywords list
  188. for i, pattern in enumerate(tokens['root']):
  189. if pattern[1] == Keyword:
  190. tokens['root'][i] = (
  191. words(_postgres_builtins.KEYWORDS +
  192. _postgres_builtins.PLPGSQL_KEYWORDS, suffix=r'\b'),
  193. Keyword)
  194. del i
  195. break
  196. else:
  197. assert 0, "SQL keywords not found"
  198. # Add specific PL/pgSQL rules (before the SQL ones)
  199. tokens['root'][:0] = [
  200. (r'\%[a-z]\w*\b', Name.Builtin), # actually, a datatype
  201. (r':=', Operator),
  202. (r'\<\<[a-z]\w*\>\>', Name.Label),
  203. (r'\#[a-z]\w*\b', Keyword.Pseudo), # #variable_conflict
  204. ]
  205. class PsqlRegexLexer(PostgresBase, RegexLexer):
  206. """
  207. Extend the PostgresLexer adding support specific for psql commands.
  208. This is not a complete psql lexer yet as it lacks prompt support
  209. and output rendering.
  210. """
  211. name = 'PostgreSQL console - regexp based lexer'
  212. aliases = [] # not public
  213. flags = re.IGNORECASE
  214. tokens = {name: state[:] for (name, state) in PostgresLexer.tokens.items()}
  215. tokens['root'].append(
  216. (r'\\[^\s]+', Keyword.Pseudo, 'psql-command'))
  217. tokens['psql-command'] = [
  218. (r'\n', Text, 'root'),
  219. (r'\s+', Whitespace),
  220. (r'\\[^\s]+', Keyword.Pseudo),
  221. (r""":(['"]?)[a-z]\w*\b\1""", Name.Variable),
  222. (r"'(''|[^'])*'", String.Single),
  223. (r"`([^`])*`", String.Backtick),
  224. (r"[^\s]+", String.Symbol),
  225. ]
  226. re_prompt = re.compile(r'^(\S.*?)??[=\-\(\$\'\"][#>]')
  227. re_psql_command = re.compile(r'\s*\\')
  228. re_end_command = re.compile(r';\s*(--.*?)?$')
  229. re_psql_command = re.compile(r'(\s*)(\\.+?)(\s+)$')
  230. re_error = re.compile(r'(ERROR|FATAL):')
  231. re_message = re.compile(
  232. r'((?:DEBUG|INFO|NOTICE|WARNING|ERROR|'
  233. r'FATAL|HINT|DETAIL|CONTEXT|LINE [0-9]+):)(.*?\n)')
  234. class lookahead:
  235. """Wrap an iterator and allow pushing back an item."""
  236. def __init__(self, x):
  237. self.iter = iter(x)
  238. self._nextitem = None
  239. def __iter__(self):
  240. return self
  241. def send(self, i):
  242. self._nextitem = i
  243. return i
  244. def __next__(self):
  245. if self._nextitem is not None:
  246. ni = self._nextitem
  247. self._nextitem = None
  248. return ni
  249. return next(self.iter)
  250. next = __next__
  251. class PostgresConsoleLexer(Lexer):
  252. """
  253. Lexer for psql sessions.
  254. """
  255. name = 'PostgreSQL console (psql)'
  256. aliases = ['psql', 'postgresql-console', 'postgres-console']
  257. mimetypes = ['text/x-postgresql-psql']
  258. url = 'https://www.postgresql.org'
  259. version_added = '1.5'
  260. _example = "psql/psql_session.txt"
  261. def get_tokens_unprocessed(self, data):
  262. sql = PsqlRegexLexer(**self.options)
  263. lines = lookahead(line_re.findall(data))
  264. # prompt-output cycle
  265. while 1:
  266. # consume the lines of the command: start with an optional prompt
  267. # and continue until the end of command is detected
  268. curcode = ''
  269. insertions = []
  270. for line in lines:
  271. # Identify a shell prompt in case of psql commandline example
  272. if line.startswith('$') and not curcode:
  273. lexer = get_lexer_by_name('console', **self.options)
  274. yield from lexer.get_tokens_unprocessed(line)
  275. break
  276. # Identify a psql prompt
  277. mprompt = re_prompt.match(line)
  278. if mprompt is not None:
  279. insertions.append((len(curcode),
  280. [(0, Generic.Prompt, mprompt.group())]))
  281. curcode += line[len(mprompt.group()):]
  282. else:
  283. curcode += line
  284. # Check if this is the end of the command
  285. # TODO: better handle multiline comments at the end with
  286. # a lexer with an external state?
  287. if re_psql_command.match(curcode) \
  288. or re_end_command.search(curcode):
  289. break
  290. # Emit the combined stream of command and prompt(s)
  291. yield from do_insertions(insertions,
  292. sql.get_tokens_unprocessed(curcode))
  293. # Emit the output lines
  294. out_token = Generic.Output
  295. for line in lines:
  296. mprompt = re_prompt.match(line)
  297. if mprompt is not None:
  298. # push the line back to have it processed by the prompt
  299. lines.send(line)
  300. break
  301. mmsg = re_message.match(line)
  302. if mmsg is not None:
  303. if mmsg.group(1).startswith("ERROR") \
  304. or mmsg.group(1).startswith("FATAL"):
  305. out_token = Generic.Error
  306. yield (mmsg.start(1), Generic.Strong, mmsg.group(1))
  307. yield (mmsg.start(2), out_token, mmsg.group(2))
  308. else:
  309. yield (0, out_token, line)
  310. else:
  311. return
  312. class PostgresExplainLexer(RegexLexer):
  313. """
  314. Handle PostgreSQL EXPLAIN output
  315. """
  316. name = 'PostgreSQL EXPLAIN dialect'
  317. aliases = ['postgres-explain']
  318. filenames = ['*.explain']
  319. mimetypes = ['text/x-postgresql-explain']
  320. url = 'https://www.postgresql.org/docs/current/using-explain.html'
  321. version_added = '2.15'
  322. tokens = {
  323. 'root': [
  324. (r':|\(|\)|ms|kB|->|\.\.|\,|\/|=|%|text', Punctuation),
  325. (r'\s+', Whitespace),
  326. # This match estimated cost and effectively measured counters with ANALYZE
  327. # Then, we move to instrumentation state
  328. (r'(cost)(=?)', bygroups(Name.Class, Punctuation), 'instrumentation'),
  329. (r'(actual)( )(=?)', bygroups(Name.Class, Whitespace, Punctuation), 'instrumentation'),
  330. # Misc keywords
  331. (words(('actual', 'Memory Usage', 'Disk Usage', 'Memory', 'Buckets',
  332. 'Batches', 'originally', 'row', 'rows', 'Hits', 'Misses',
  333. 'Evictions', 'Overflows', 'Planned Partitions', 'Estimates',
  334. 'capacity', 'distinct keys', 'lookups', 'hit percent',
  335. 'Index Searches', 'Storage', 'Disk Maximum Storage'), suffix=r'\b'),
  336. Comment.Single),
  337. (r'(hit|read|dirtied|written|write|time|calls|records|bytes|allocated|used|output|format)(=)', bygroups(Comment.Single, Operator)),
  338. (r'shared|temp|local', Keyword.Pseudo),
  339. # We move to sort state in order to emphasize specific keywords (especially disk access)
  340. (r'(Sort Method)(: )', bygroups(Comment.Preproc, Punctuation), 'sort'),
  341. # These keywords can be followed by an object, like a table
  342. (r'(Sort Key|Group Key|Presorted Key|Hash Key)(:)( )',
  343. bygroups(Comment.Preproc, Punctuation, Whitespace), 'object_name'),
  344. (r'(Cache Key|Cache Mode)(:)( )', bygroups(Comment, Punctuation, Whitespace), 'object_name'),
  345. # These keywords can be followed by a predicate
  346. (words(('Join Filter', 'Subplans Removed', 'Filter', 'Merge Cond',
  347. 'Hash Cond', 'Index Cond', 'Recheck Cond', 'Heap Blocks',
  348. 'TID Cond', 'Run Condition', 'Order By', 'Function Call',
  349. 'Table Function Call', 'Inner Unique', 'Params Evaluated',
  350. 'Single Copy', 'Sampling', 'One-Time Filter', 'Output',
  351. 'Relations', 'Remote SQL', 'Disabled'), suffix=r'\b'),
  352. Comment.Preproc, 'predicate'),
  353. # Special keyword to handle ON CONFLICT
  354. (r'Conflict(ing)? ', Comment.Preproc, 'conflict'),
  355. (r'(Tuples Inserted: )', Comment.Preproc, 'predicate'),
  356. # Special keyword for InitPlan or SubPlan
  357. (r'(InitPlan|SubPlan)( )(\d+)( )',
  358. bygroups(Keyword, Whitespace, Number.Integer, Whitespace),
  359. 'init_plan'),
  360. (words(('Sort Method', 'Join Filter', 'Planning time',
  361. 'Planning Time', 'Execution time', 'Execution Time',
  362. 'Workers Planned', 'Workers Launched', 'Buffers',
  363. 'Planning', 'Worker', 'Query Identifier', 'Time',
  364. 'Full-sort Groups', 'Pre-sorted Groups', 'Serialization'), suffix=r'\b'), Comment.Preproc),
  365. # Emphasize these keywords
  366. (words(('Rows Removed by Join Filter', 'Rows Removed by Filter',
  367. 'Rows Removed by Index Recheck',
  368. 'Heap Fetches', 'never executed'),
  369. suffix=r'\b'), Name.Exception),
  370. (r'(I/O Timings)(:)( )', bygroups(Name.Exception, Punctuation, Whitespace)),
  371. (r'(WAL)(:)( )', bygroups(Name.Exception, Punctuation, Whitespace)),
  372. (words(_postgres_builtins.EXPLAIN_KEYWORDS, suffix=r'\b'), Keyword),
  373. # join keywords
  374. (r'(Right|Left|Full|Semi|Anti) Join', Keyword.Type),
  375. (r'Parallel |Async |Finalize |Partial ', Comment.Preproc),
  376. (r'Backward', Comment.Preproc),
  377. (r'Intersect|Except|Hash', Comment.Preproc),
  378. (r'(CTE)( )(\w*)?', bygroups(Comment, Whitespace, Name.Variable)),
  379. # Treat "on" and "using" as a punctuation
  380. (r'on|using', Punctuation, 'object_name'),
  381. # strings
  382. (r"'(''|[^'])*'", String.Single),
  383. # numbers
  384. (r'-?\d+\.\d+', Number.Float),
  385. (r'-?\d+', Number.Integer),
  386. # boolean
  387. (r'true|false', Name.Constant),
  388. # explain header
  389. (r'\s*QUERY PLAN\s*\n\s*-+', Comment.Single),
  390. # Settings
  391. (r'(Settings)(:)( )', bygroups(Comment.Preproc, Punctuation, Whitespace), 'setting'),
  392. # Handle JIT counters
  393. (r'(JIT|Functions|Options|Timing)(:)', bygroups(Comment.Preproc, Punctuation)),
  394. (r'(Inlining|Optimization|Expressions|Deforming|Generation|Emission|Total)', Keyword.Pseudo),
  395. # Handle Triggers counters
  396. (r'(Trigger)( )(\S*)(:)( )',
  397. bygroups(Comment.Preproc, Whitespace, Name.Variable, Punctuation, Whitespace)),
  398. ],
  399. 'expression': [
  400. # matches any kind of parenthesized expression
  401. # the first opening paren is matched by the 'caller'
  402. (r'\(', Punctuation, '#push'),
  403. (r'\)', Punctuation, '#pop'),
  404. (r'never executed', Name.Exception),
  405. (r'[^)(]+', Comment),
  406. ],
  407. 'object_name': [
  408. # This is a cost or analyze measure
  409. (r'(\(cost)(=?)', bygroups(Name.Class, Punctuation), 'instrumentation'),
  410. (r'(\(actual)( )(=?)', bygroups(Name.Class, Whitespace, Punctuation), 'instrumentation'),
  411. # if object_name is parenthesized, mark opening paren as
  412. # punctuation, call 'expression', and exit state
  413. (r'\(', Punctuation, 'expression'),
  414. (r'on', Punctuation),
  415. # matches possibly schema-qualified table and column names
  416. (r'\w+(\.\w+)*( USING \S+| \w+ USING \S+)', Name.Variable),
  417. (r'\"?\w+\"?(?:\.\"?\w+\"?)?', Name.Variable),
  418. (r'\'\S*\'', Name.Variable),
  419. # if we encounter a comma, another object is listed
  420. (r',\n', Punctuation, 'object_name'),
  421. (r',', Punctuation, 'object_name'),
  422. # special case: "*SELECT*"
  423. (r'"\*SELECT\*( \d+)?"(.\w+)?', Name.Variable),
  424. (r'"\*VALUES\*(_\d+)?"(.\w+)?', Name.Variable),
  425. (r'"ANY_subquery"', Name.Variable),
  426. # Variable $1 ...
  427. (r'\$\d+', Name.Variable),
  428. # cast
  429. (r'::\w+', Name.Variable),
  430. (r' +', Whitespace),
  431. (r'"', Punctuation),
  432. (r'\[\.\.\.\]', Punctuation),
  433. (r'\)', Punctuation, '#pop'),
  434. ],
  435. 'predicate': [
  436. # if predicate is parenthesized, mark paren as punctuation
  437. (r'(\()([^\n]*)(\))', bygroups(Punctuation, Name.Variable, Punctuation), '#pop'),
  438. # otherwise color until newline
  439. (r'[^\n]*', Name.Variable, '#pop'),
  440. ],
  441. 'instrumentation': [
  442. (r'=|\.\.', Punctuation),
  443. (r' +', Whitespace),
  444. (r'rows|width|time|loops', Name.Class),
  445. (r'\d+\.\d+', Number.Float),
  446. (r'\d+', Number.Integer),
  447. (r'\)', Punctuation, '#pop'),
  448. ],
  449. 'conflict': [
  450. (r'(Resolution: )(\w+)', bygroups(Comment.Preproc, Name.Variable)),
  451. (r'Arbiter \w+:', Comment.Preproc, 'object_name'),
  452. (r'Filter: ', Comment.Preproc, 'predicate'),
  453. (r'Tuples: ', Comment.Preproc, 'predicate'),
  454. ],
  455. 'setting': [
  456. (r'([a-z_]*?)(\s*)(=)(\s*)(\'.*?\')', bygroups(Name.Attribute, Whitespace, Operator, Whitespace, String)),
  457. (r'\, ', Punctuation),
  458. ],
  459. 'init_plan': [
  460. (r'\(', Punctuation),
  461. (r'returns \$\d+(,\$\d+)?', Name.Variable),
  462. (r'\)', Punctuation, '#pop'),
  463. ],
  464. 'sort': [
  465. (r':|kB', Punctuation),
  466. (r'quicksort|top-N|heapsort|Average|Memory|Peak', Comment.Prepoc),
  467. (r'external|merge|Disk|sort', Name.Exception),
  468. (r'\d+', Number.Integer),
  469. (r' +', Whitespace),
  470. ],
  471. }
  472. class SqlLexer(RegexLexer):
  473. """
  474. Lexer for Structured Query Language. Currently, this lexer does
  475. not recognize any special syntax except ANSI SQL.
  476. """
  477. name = 'SQL'
  478. aliases = ['sql']
  479. filenames = ['*.sql']
  480. mimetypes = ['text/x-sql']
  481. url = 'https://en.wikipedia.org/wiki/SQL'
  482. version_added = ''
  483. flags = re.IGNORECASE
  484. tokens = {
  485. 'root': [
  486. (r'\s+', Whitespace),
  487. (r'--.*\n?', Comment.Single),
  488. (r'/\*', Comment.Multiline, 'multiline-comments'),
  489. (words(_sql_builtins.KEYWORDS, suffix=r'\b'), Keyword),
  490. (words(_sql_builtins.DATATYPES, suffix=r'\b'), Name.Builtin),
  491. (r'[+*/<>=~!@#%^&|`?-]', Operator),
  492. (r'[0-9]+', Number.Integer),
  493. # TODO: Backslash escapes?
  494. (r"'(''|[^'])*'", String.Single),
  495. (r'"(""|[^"])*"', String.Symbol), # not a real string literal in ANSI SQL
  496. (r'[a-z_][\w$]*', Name), # allow $s in strings for Oracle
  497. (r'[;:()\[\],.]', Punctuation)
  498. ],
  499. 'multiline-comments': [
  500. (r'/\*', Comment.Multiline, 'multiline-comments'),
  501. (r'\*/', Comment.Multiline, '#pop'),
  502. (r'[^/*]+', Comment.Multiline),
  503. (r'[/*]', Comment.Multiline)
  504. ]
  505. }
  506. def analyse_text(self, text):
  507. return
  508. class TransactSqlLexer(RegexLexer):
  509. """
  510. Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary extension to
  511. SQL.
  512. The list of keywords includes ODBC and keywords reserved for future use.
  513. """
  514. name = 'Transact-SQL'
  515. aliases = ['tsql', 't-sql']
  516. filenames = ['*.sql']
  517. mimetypes = ['text/x-tsql']
  518. url = 'https://www.tsql.info'
  519. version_added = ''
  520. flags = re.IGNORECASE
  521. tokens = {
  522. 'root': [
  523. (r'\s+', Whitespace),
  524. (r'--.*[$|\n]?', Comment.Single),
  525. (r'/\*', Comment.Multiline, 'multiline-comments'),
  526. (words(_tsql_builtins.OPERATORS), Operator),
  527. (words(_tsql_builtins.OPERATOR_WORDS, suffix=r'\b'), Operator.Word),
  528. (words(_tsql_builtins.TYPES, suffix=r'\b'), Name.Class),
  529. (words(_tsql_builtins.FUNCTIONS, suffix=r'\b'), Name.Function),
  530. (r'(goto)(\s+)(\w+\b)', bygroups(Keyword, Whitespace, Name.Label)),
  531. (words(_tsql_builtins.KEYWORDS, suffix=r'\b'), Keyword),
  532. (r'(\[)([^]]+)(\])', bygroups(Operator, Name, Operator)),
  533. (r'0x[0-9a-f]+', Number.Hex),
  534. # Float variant 1, for example: 1., 1.e2, 1.2e3
  535. (r'[0-9]+\.[0-9]*(e[+-]?[0-9]+)?', Number.Float),
  536. # Float variant 2, for example: .1, .1e2
  537. (r'\.[0-9]+(e[+-]?[0-9]+)?', Number.Float),
  538. # Float variant 3, for example: 123e45
  539. (r'[0-9]+e[+-]?[0-9]+', Number.Float),
  540. (r'[0-9]+', Number.Integer),
  541. (r"'(''|[^'])*'", String.Single),
  542. (r'"(""|[^"])*"', String.Symbol),
  543. (r'[;(),.]', Punctuation),
  544. # Below we use \w even for the first "real" character because
  545. # tokens starting with a digit have already been recognized
  546. # as Number above.
  547. (r'@@\w+', Name.Builtin),
  548. (r'@\w+', Name.Variable),
  549. (r'(\w+)(:)', bygroups(Name.Label, Punctuation)),
  550. (r'#?#?\w+', Name), # names for temp tables and anything else
  551. (r'\?', Name.Variable.Magic), # parameter for prepared statements
  552. ],
  553. 'multiline-comments': [
  554. (r'/\*', Comment.Multiline, 'multiline-comments'),
  555. (r'\*/', Comment.Multiline, '#pop'),
  556. (r'[^/*]+', Comment.Multiline),
  557. (r'[/*]', Comment.Multiline)
  558. ]
  559. }
  560. def analyse_text(text):
  561. rating = 0
  562. if tsql_declare_re.search(text):
  563. # Found T-SQL variable declaration.
  564. rating = 1.0
  565. else:
  566. name_between_backtick_count = len(
  567. name_between_backtick_re.findall(text))
  568. name_between_bracket_count = len(
  569. name_between_bracket_re.findall(text))
  570. # We need to check if there are any names using
  571. # backticks or brackets, as otherwise both are 0
  572. # and 0 >= 2 * 0, so we would always assume it's true
  573. dialect_name_count = name_between_backtick_count + name_between_bracket_count
  574. if dialect_name_count >= 1 and \
  575. name_between_bracket_count >= 2 * name_between_backtick_count:
  576. # Found at least twice as many [name] as `name`.
  577. rating += 0.5
  578. elif name_between_bracket_count > name_between_backtick_count:
  579. rating += 0.2
  580. elif name_between_bracket_count > 0:
  581. rating += 0.1
  582. if tsql_variable_re.search(text) is not None:
  583. rating += 0.1
  584. if tsql_go_re.search(text) is not None:
  585. rating += 0.1
  586. return rating
  587. class MySqlLexer(RegexLexer):
  588. """The Oracle MySQL lexer.
  589. This lexer does not attempt to maintain strict compatibility with
  590. MariaDB syntax or keywords. Although MySQL and MariaDB's common code
  591. history suggests there may be significant overlap between the two,
  592. compatibility between the two is not a target for this lexer.
  593. """
  594. name = 'MySQL'
  595. aliases = ['mysql']
  596. mimetypes = ['text/x-mysql']
  597. url = 'https://www.mysql.com'
  598. version_added = ''
  599. flags = re.IGNORECASE
  600. tokens = {
  601. 'root': [
  602. (r'\s+', Whitespace),
  603. # Comments
  604. (r'(?:#|--\s+).*', Comment.Single),
  605. (r'/\*\+', Comment.Special, 'optimizer-hints'),
  606. (r'/\*', Comment.Multiline, 'multiline-comment'),
  607. # Hexadecimal literals
  608. (r"x'([0-9a-f]{2})+'", Number.Hex), # MySQL requires paired hex characters in this form.
  609. (r'0x[0-9a-f]+', Number.Hex),
  610. # Binary literals
  611. (r"b'[01]+'", Number.Bin),
  612. (r'0b[01]+', Number.Bin),
  613. # Numeric literals
  614. (r'[0-9]+\.[0-9]*(e[+-]?[0-9]+)?', Number.Float), # Mandatory integer, optional fraction and exponent
  615. (r'[0-9]*\.[0-9]+(e[+-]?[0-9]+)?', Number.Float), # Mandatory fraction, optional integer and exponent
  616. (r'[0-9]+e[+-]?[0-9]+', Number.Float), # Exponents with integer significands are still floats
  617. (r'[0-9]+(?=[^0-9a-z$_\u0080-\uffff])', Number.Integer), # Integers that are not in a schema object name
  618. # Date literals
  619. (r"\{\s*d\s*(?P<quote>['\"])\s*\d{2}(\d{2})?.?\d{2}.?\d{2}\s*(?P=quote)\s*\}",
  620. Literal.Date),
  621. # Time literals
  622. (r"\{\s*t\s*(?P<quote>['\"])\s*(?:\d+\s+)?\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?\s*(?P=quote)\s*\}",
  623. Literal.Date),
  624. # Timestamp literals
  625. (
  626. r"\{\s*ts\s*(?P<quote>['\"])\s*"
  627. r"\d{2}(?:\d{2})?.?\d{2}.?\d{2}" # Date part
  628. r"\s+" # Whitespace between date and time
  629. r"\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?" # Time part
  630. r"\s*(?P=quote)\s*\}",
  631. Literal.Date
  632. ),
  633. # String literals
  634. (r"'", String.Single, 'single-quoted-string'),
  635. (r'"', String.Double, 'double-quoted-string'),
  636. # Variables
  637. (r'@@(?:global\.|persist\.|persist_only\.|session\.)?[a-z_]+', Name.Variable),
  638. (r'@[a-z0-9_$.]+', Name.Variable),
  639. (r"@'", Name.Variable, 'single-quoted-variable'),
  640. (r'@"', Name.Variable, 'double-quoted-variable'),
  641. (r"@`", Name.Variable, 'backtick-quoted-variable'),
  642. (r'\?', Name.Variable), # For demonstrating prepared statements
  643. # Operators
  644. (r'[!%&*+/:<=>^|~-]+', Operator),
  645. # Exceptions; these words tokenize differently in different contexts.
  646. (r'\b(set)\b(?!\s*\()', Keyword),
  647. (r'\b(character)(\s+)(set)\b', bygroups(Keyword, Whitespace, Keyword)),
  648. # In all other known cases, "SET" is tokenized by MYSQL_DATATYPES.
  649. (words(_mysql_builtins.MYSQL_CONSTANTS, prefix=r'\b', suffix=r'\b'),
  650. Name.Constant),
  651. (words(_mysql_builtins.MYSQL_DATATYPES, prefix=r'\b', suffix=r'\b'),
  652. Keyword.Type),
  653. (words(_mysql_builtins.MYSQL_KEYWORDS, prefix=r'\b', suffix=r'\b'),
  654. Keyword),
  655. (words(_mysql_builtins.MYSQL_FUNCTIONS, prefix=r'\b', suffix=r'\b(\s*)(\()'),
  656. bygroups(Name.Function, Whitespace, Punctuation)),
  657. # Schema object names
  658. #
  659. # Note: Although the first regex supports unquoted all-numeric
  660. # identifiers, this will not be a problem in practice because
  661. # numeric literals have already been handled above.
  662. #
  663. ('[0-9a-z$_\u0080-\uffff]+', Name),
  664. (r'`', Name.Quoted, 'schema-object-name'),
  665. # Punctuation
  666. (r'[(),.;]', Punctuation),
  667. ],
  668. # Multiline comment substates
  669. # ---------------------------
  670. 'optimizer-hints': [
  671. (r'[^*a-z]+', Comment.Special),
  672. (r'\*/', Comment.Special, '#pop'),
  673. (words(_mysql_builtins.MYSQL_OPTIMIZER_HINTS, suffix=r'\b'),
  674. Comment.Preproc),
  675. ('[a-z]+', Comment.Special),
  676. (r'\*', Comment.Special),
  677. ],
  678. 'multiline-comment': [
  679. (r'[^*]+', Comment.Multiline),
  680. (r'\*/', Comment.Multiline, '#pop'),
  681. (r'\*', Comment.Multiline),
  682. ],
  683. # String substates
  684. # ----------------
  685. 'single-quoted-string': [
  686. (r"[^'\\]+", String.Single),
  687. (r"''", String.Escape),
  688. (r"""\\[0'"bnrtZ\\%_]""", String.Escape),
  689. (r"'", String.Single, '#pop'),
  690. ],
  691. 'double-quoted-string': [
  692. (r'[^"\\]+', String.Double),
  693. (r'""', String.Escape),
  694. (r"""\\[0'"bnrtZ\\%_]""", String.Escape),
  695. (r'"', String.Double, '#pop'),
  696. ],
  697. # Variable substates
  698. # ------------------
  699. 'single-quoted-variable': [
  700. (r"[^']+", Name.Variable),
  701. (r"''", Name.Variable),
  702. (r"'", Name.Variable, '#pop'),
  703. ],
  704. 'double-quoted-variable': [
  705. (r'[^"]+', Name.Variable),
  706. (r'""', Name.Variable),
  707. (r'"', Name.Variable, '#pop'),
  708. ],
  709. 'backtick-quoted-variable': [
  710. (r'[^`]+', Name.Variable),
  711. (r'``', Name.Variable),
  712. (r'`', Name.Variable, '#pop'),
  713. ],
  714. # Schema object name substates
  715. # ----------------------------
  716. #
  717. # "Name.Quoted" and "Name.Quoted.Escape" are non-standard but
  718. # formatters will style them as "Name" by default but add
  719. # additional styles based on the token name. This gives users
  720. # flexibility to add custom styles as desired.
  721. #
  722. 'schema-object-name': [
  723. (r'[^`]+', Name.Quoted),
  724. (r'``', Name.Quoted.Escape),
  725. (r'`', Name.Quoted, '#pop'),
  726. ],
  727. }
  728. def analyse_text(text):
  729. rating = 0
  730. name_between_backtick_count = len(
  731. name_between_backtick_re.findall(text))
  732. name_between_bracket_count = len(
  733. name_between_bracket_re.findall(text))
  734. # Same logic as above in the TSQL analysis
  735. dialect_name_count = name_between_backtick_count + name_between_bracket_count
  736. if dialect_name_count >= 1 and \
  737. name_between_backtick_count >= 2 * name_between_bracket_count:
  738. # Found at least twice as many `name` as [name].
  739. rating += 0.5
  740. elif name_between_backtick_count > name_between_bracket_count:
  741. rating += 0.2
  742. elif name_between_backtick_count > 0:
  743. rating += 0.1
  744. return rating
  745. class GoogleSqlLexer(RegexLexer):
  746. """
  747. GoogleSQL is Google's standard SQL dialect, formerly known as ZetaSQL.
  748. The list of keywords includes reserved words for future use.
  749. """
  750. name = 'GoogleSQL'
  751. aliases = ['googlesql', 'zetasql']
  752. filenames = ['*.googlesql', '*.googlesql.sql']
  753. mimetypes = ['text/x-google-sql', 'text/x-google-sql-aux']
  754. url = 'https://cloud.google.com/bigquery/googlesql'
  755. version_added = '2.19'
  756. flags = re.IGNORECASE
  757. tokens = {
  758. 'root': [
  759. (r'\s+', Whitespace),
  760. # Comments
  761. (r'(?:#|--\s+).*', Comment.Single),
  762. (r'/\*', Comment.Multiline, 'multiline-comment'),
  763. # Hexadecimal literals
  764. (r"x'([0-9a-f]{2})+'", Number.Hex),
  765. (r'0x[0-9a-f]+', Number.Hex),
  766. # Binary literals
  767. (r"b'[01]+'", Number.Bin),
  768. (r'0b[01]+', Number.Bin),
  769. # Numeric literals
  770. (r'[0-9]+\.[0-9]*(e[+-]?[0-9]+)?', Number.Float), # Mandatory integer, optional fraction and exponent
  771. (r'[0-9]*\.[0-9]+(e[+-]?[0-9]+)?', Number.Float), # Mandatory fraction, optional integer and exponent
  772. (r'[0-9]+e[+-]?[0-9]+', Number.Float), # Exponents with integer significands are still floats
  773. (r'[0-9]+(?=[^0-9a-z$_\u0080-\uffff])', Number.Integer), # Integers that are not in a schema object name
  774. # Date literals
  775. (r"\{\s*d\s*(?P<quote>['\"])\s*\d{2}(\d{2})?.?\d{2}.?\d{2}\s*(?P=quote)\s*\}",
  776. Literal.Date),
  777. # Time literals
  778. (r"\{\s*t\s*(?P<quote>['\"])\s*(?:\d+\s+)?\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?\s*(?P=quote)\s*\}",
  779. Literal.Date),
  780. # Timestamp literals
  781. (
  782. r"\{\s*ts\s*(?P<quote>['\"])\s*"
  783. r"\d{2}(?:\d{2})?.?\d{2}.?\d{2}" # Date part
  784. r"\s+" # Whitespace between date and time
  785. r"\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?" # Time part
  786. r"\s*(?P=quote)\s*\}",
  787. Literal.Date
  788. ),
  789. # String literals
  790. (r"'", String.Single, 'single-quoted-string'),
  791. (r'"', String.Double, 'double-quoted-string'),
  792. # Variables
  793. (r'@@(?:global\.|persist\.|persist_only\.|session\.)?[a-z_]+', Name.Variable),
  794. (r'@[a-z0-9_$.]+', Name.Variable),
  795. (r"@'", Name.Variable, 'single-quoted-variable'),
  796. (r'@"', Name.Variable, 'double-quoted-variable'),
  797. (r"@`", Name.Variable, 'backtick-quoted-variable'),
  798. (r'\?', Name.Variable), # For demonstrating prepared statements
  799. # Exceptions; these words tokenize differently in different contexts.
  800. (r'\b(set)\b(?!\s*\()', Keyword),
  801. (r'\b(character)(\s+)(set)\b', bygroups(Keyword, Whitespace, Keyword)),
  802. # Constants, types, keywords, functions, operators
  803. (words(_googlesql_builtins.constants, prefix=r'\b', suffix=r'\b'), Name.Constant),
  804. (words(_googlesql_builtins.types, prefix=r'\b', suffix=r'\b'), Keyword.Type),
  805. (words(_googlesql_builtins.keywords, prefix=r'\b', suffix=r'\b'), Keyword),
  806. (words(_googlesql_builtins.functionnames, prefix=r'\b', suffix=r'\b(\s*)(\()'),
  807. bygroups(Name.Function, Whitespace, Punctuation)),
  808. (words(_googlesql_builtins.operators, prefix=r'\b', suffix=r'\b'), Operator),
  809. # Schema object names
  810. #
  811. # Note: Although the first regex supports unquoted all-numeric
  812. # identifiers, this will not be a problem in practice because
  813. # numeric literals have already been handled above.
  814. #
  815. ('[0-9a-z$_\u0080-\uffff]+', Name),
  816. (r'`', Name.Quoted, 'schema-object-name'),
  817. # Punctuation
  818. (r'[(),.;]', Punctuation),
  819. ],
  820. # Multiline comment substates
  821. # ---------------------------
  822. 'multiline-comment': [
  823. (r'[^*]+', Comment.Multiline),
  824. (r'\*/', Comment.Multiline, '#pop'),
  825. (r'\*', Comment.Multiline),
  826. ],
  827. # String substates
  828. # ----------------
  829. 'single-quoted-string': [
  830. (r"[^'\\]+", String.Single),
  831. (r"''", String.Escape),
  832. (r"""\\[0'"bnrtZ\\%_]""", String.Escape),
  833. (r"'", String.Single, '#pop'),
  834. ],
  835. 'double-quoted-string': [
  836. (r'[^"\\]+', String.Double),
  837. (r'""', String.Escape),
  838. (r"""\\[0'"bnrtZ\\%_]""", String.Escape),
  839. (r'"', String.Double, '#pop'),
  840. ],
  841. # Variable substates
  842. # ------------------
  843. 'single-quoted-variable': [
  844. (r"[^']+", Name.Variable),
  845. (r"''", Name.Variable),
  846. (r"'", Name.Variable, '#pop'),
  847. ],
  848. 'double-quoted-variable': [
  849. (r'[^"]+', Name.Variable),
  850. (r'""', Name.Variable),
  851. (r'"', Name.Variable, '#pop'),
  852. ],
  853. 'backtick-quoted-variable': [
  854. (r'[^`]+', Name.Variable),
  855. (r'``', Name.Variable),
  856. (r'`', Name.Variable, '#pop'),
  857. ],
  858. # Schema object name substates
  859. # ----------------------------
  860. #
  861. # "Name.Quoted" and "Name.Quoted.Escape" are non-standard but
  862. # formatters will style them as "Name" by default but add
  863. # additional styles based on the token name. This gives users
  864. # flexibility to add custom styles as desired.
  865. #
  866. 'schema-object-name': [
  867. (r'[^`]+', Name.Quoted),
  868. (r'``', Name.Quoted.Escape),
  869. (r'`', Name.Quoted, '#pop'),
  870. ],
  871. }
  872. def analyse_text(text):
  873. tokens = collections.Counter(text.split())
  874. return 0.001 * sum(count for t, count in tokens.items()
  875. if t in googlesql_identifiers)
  876. class SqliteConsoleLexer(Lexer):
  877. """
  878. Lexer for example sessions using sqlite3.
  879. """
  880. name = 'sqlite3con'
  881. aliases = ['sqlite3']
  882. filenames = ['*.sqlite3-console']
  883. mimetypes = ['text/x-sqlite3-console']
  884. url = 'https://www.sqlite.org'
  885. version_added = '0.11'
  886. _example = "sqlite3/sqlite3.sqlite3-console"
  887. def get_tokens_unprocessed(self, data):
  888. sql = SqlLexer(**self.options)
  889. curcode = ''
  890. insertions = []
  891. for match in line_re.finditer(data):
  892. line = match.group()
  893. prompt_match = sqlite_prompt_re.match(line)
  894. if prompt_match is not None:
  895. insertions.append((len(curcode),
  896. [(0, Generic.Prompt, line[:7])]))
  897. insertions.append((len(curcode),
  898. [(7, Whitespace, ' ')]))
  899. curcode += line[8:]
  900. else:
  901. if curcode:
  902. yield from do_insertions(insertions,
  903. sql.get_tokens_unprocessed(curcode))
  904. curcode = ''
  905. insertions = []
  906. if line.startswith('SQL error: '):
  907. yield (match.start(), Generic.Traceback, line)
  908. else:
  909. yield (match.start(), Generic.Output, line)
  910. if curcode:
  911. yield from do_insertions(insertions,
  912. sql.get_tokens_unprocessed(curcode))
  913. class RqlLexer(RegexLexer):
  914. """
  915. Lexer for Relation Query Language.
  916. """
  917. name = 'RQL'
  918. url = 'http://www.logilab.org/project/rql'
  919. aliases = ['rql']
  920. filenames = ['*.rql']
  921. mimetypes = ['text/x-rql']
  922. version_added = '2.0'
  923. flags = re.IGNORECASE
  924. tokens = {
  925. 'root': [
  926. (r'\s+', Whitespace),
  927. (r'(DELETE|SET|INSERT|UNION|DISTINCT|WITH|WHERE|BEING|OR'
  928. r'|AND|NOT|GROUPBY|HAVING|ORDERBY|ASC|DESC|LIMIT|OFFSET'
  929. r'|TODAY|NOW|TRUE|FALSE|NULL|EXISTS)\b', Keyword),
  930. (r'[+*/<>=%-]', Operator),
  931. (r'(Any|is|instance_of|CWEType|CWRelation)\b', Name.Builtin),
  932. (r'[0-9]+', Number.Integer),
  933. (r'[A-Z_]\w*\??', Name),
  934. (r"'(''|[^'])*'", String.Single),
  935. (r'"(""|[^"])*"', String.Single),
  936. (r'[;:()\[\],.]', Punctuation)
  937. ],
  938. }