codeql.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """
  2. pygments.lexers.codeql
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for CodeQL query language.
  5. The grammar is originating from:
  6. https://github.com/github/vscode-codeql/blob/main/syntaxes/README.md
  7. :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
  8. :license: BSD, see LICENSE for details.
  9. """
  10. __all__ = ['CodeQLLexer']
  11. import re
  12. from pygments.lexer import RegexLexer, words
  13. from pygments.token import Comment, Operator, Keyword, Name, String, \
  14. Number, Punctuation, Whitespace
  15. class CodeQLLexer(RegexLexer):
  16. name = 'CodeQL'
  17. aliases = ['codeql', 'ql']
  18. filenames = ['*.ql', '*.qll']
  19. mimetypes = []
  20. url = 'https://github.com/github/codeql'
  21. version_added = '2.19'
  22. flags = re.MULTILINE | re.UNICODE
  23. tokens = {
  24. 'root': [
  25. # Whitespace and comments
  26. (r'\s+', Whitespace),
  27. (r'//.*?\n', Comment.Single),
  28. (r'/\*', Comment.Multiline, 'multiline-comments'),
  29. # Keywords
  30. (words((
  31. 'module', 'import', 'class', 'extends', 'implements',
  32. 'predicate', 'select', 'where', 'from', 'as', 'and', 'or', 'not',
  33. 'in', 'if', 'then', 'else', 'exists', 'forall', 'instanceof',
  34. 'private', 'predicate', 'abstract', 'cached', 'external',
  35. 'final', 'library', 'override', 'query'
  36. ), suffix=r'\b'), Keyword.Builtin),
  37. # Special Keywords
  38. (words(('this'), # class related keywords
  39. prefix=r'\b', suffix=r'\b\??:?'), Name.Builtin.Pseudo),
  40. # Types
  41. (words((
  42. 'boolean', 'date', 'float', 'int', 'string'
  43. ), suffix=r'\b'), Keyword.Type),
  44. # Literals
  45. (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
  46. (r'[0-9]+\.[0-9]+', Number.Float),
  47. (r'[0-9]+', Number.Integer),
  48. # Operators
  49. (r'<=|>=|<|>|=|!=|\+|-|\*|/', Operator),
  50. # Punctuation
  51. (r'[.,;:\[\]{}()]+', Punctuation),
  52. # Identifiers
  53. (r'@[a-zA-Z_]\w*', Name.Variable), # Variables with @ prefix
  54. (r'[A-Z][a-zA-Z0-9_]*', Name.Class), # Types and classes
  55. (r'[a-z][a-zA-Z0-9_]*', Name.Variable), # Variables and predicates
  56. ],
  57. 'multiline-comments': [
  58. (r'[^*/]+', Comment.Multiline),
  59. (r'/\*', Comment.Multiline, '#push'),
  60. (r'\*/', Comment.Multiline, '#pop'),
  61. (r'[*/]', Comment.Multiline),
  62. ],
  63. }