gsql.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. pygments.lexers.gsql
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for TigerGraph GSQL graph query language
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, bygroups, using, this, words
  10. from pygments.token import Keyword, Punctuation, Comment, Operator, Name, \
  11. String, Number, Whitespace
  12. __all__ = ["GSQLLexer"]
  13. class GSQLLexer(RegexLexer):
  14. """
  15. For GSQL queries (version 3.x).
  16. """
  17. name = 'GSQL'
  18. url = 'https://docs.tigergraph.com/dev/gsql-ref'
  19. aliases = ['gsql']
  20. filenames = ['*.gsql']
  21. version_added = '2.10'
  22. flags = re.MULTILINE | re.IGNORECASE
  23. tokens = {
  24. 'root': [
  25. include('comment'),
  26. include('keywords'),
  27. include('clauses'),
  28. include('accums'),
  29. include('relations'),
  30. include('strings'),
  31. include('whitespace'),
  32. include('barewords'),
  33. include('operators'),
  34. ],
  35. 'comment': [
  36. (r'\#.*', Comment.Single),
  37. (r'/\*(.|\n)*?\*/', Comment.Multiline),
  38. ],
  39. 'keywords': [
  40. (words((
  41. 'ACCUM', 'AND', 'ANY', 'API', 'AS', 'ASC', 'AVG', 'BAG', 'BATCH',
  42. 'BETWEEN', 'BOOL', 'BOTH', 'BREAK', 'BY', 'CASE', 'CATCH', 'COALESCE',
  43. 'COMPRESS', 'CONTINUE', 'COUNT', 'CREATE', 'DATETIME', 'DATETIME_ADD',
  44. 'DATETIME_SUB', 'DELETE', 'DESC', 'DISTRIBUTED', 'DO', 'DOUBLE',
  45. 'EDGE', 'ELSE', 'END', 'ESCAPE', 'EXCEPTION', 'FALSE', 'FILE',
  46. 'FILTER', 'FLOAT', 'FOREACH', 'FOR', 'FROM', 'GRAPH', 'GROUP',
  47. 'GSQL_INT_MAX', 'GSQL_INT_MIN', 'GSQL_UINT_MAX', 'HAVING', 'IF',
  48. 'IN', 'INSERT', 'INT', 'INTERPRET', 'INTERSECT', 'INTERVAL', 'INTO',
  49. 'IS', 'ISEMPTY', 'JSONARRAY', 'JSONOBJECT', 'LASTHOP', 'LEADING',
  50. 'LIKE', 'LIMIT', 'LIST', 'LOAD_ACCUM', 'LOG', 'MAP', 'MATCH', 'MAX',
  51. 'MIN', 'MINUS', 'NOT', 'NOW', 'NULL', 'OFFSET', 'OR', 'ORDER', 'PATH',
  52. 'PER', 'PINNED', 'POST_ACCUM', 'POST-ACCUM', 'PRIMARY_ID', 'PRINT',
  53. 'QUERY', 'RAISE', 'RANGE', 'REPLACE', 'RESET_COLLECTION_ACCUM',
  54. 'RETURN', 'RETURNS', 'RUN', 'SAMPLE', 'SELECT', 'SELECT_VERTEX',
  55. 'SET', 'SRC', 'STATIC', 'STRING', 'SUM', 'SYNTAX', 'TARGET',
  56. 'TAGSTGT', 'THEN', 'TO', 'TO_CSV', 'TO_DATETIME', 'TRAILING',
  57. 'TRIM', 'TRUE', 'TRY', 'TUPLE', 'TYPEDEF', 'UINT', 'UNION', 'UPDATE',
  58. 'VALUES', 'VERTEX', 'WHEN', 'WHERE', 'WHILE', 'WITH'),
  59. prefix=r'(?<!\.)', suffix=r'\b'), Keyword),
  60. ],
  61. 'clauses': [
  62. (words(('accum', 'having', 'limit', 'order', 'postAccum', 'sample', 'where')),
  63. Name.Builtin),
  64. ],
  65. 'accums': [
  66. (words(('andaccum', 'arrayaccum', 'avgaccum', 'bagaccum', 'bitwiseandaccum',
  67. 'bitwiseoraccum', 'groupbyaccum', 'heapaccum', 'listaccum',
  68. 'MapAccum', 'maxaccum', 'minaccum', 'oraccum', 'setaccum',
  69. 'sumaccum')), Name.Builtin),
  70. ],
  71. 'relations': [
  72. (r'(-\s?)(\(.*\:\w?\))(\s?-)', bygroups(Operator, using(this), Operator)),
  73. (r'->|<-', Operator),
  74. (r'[.*{}\[\]\<\>\_]', Punctuation),
  75. ],
  76. 'strings': [
  77. (r'"([^"\\]|\\.)*"', String),
  78. (r'@{1,2}\w+', Name.Variable),
  79. ],
  80. 'whitespace': [
  81. (r'\s+', Whitespace),
  82. ],
  83. 'barewords': [
  84. (r'[a-z]\w*', Name),
  85. (r'(\d+\.\d+|\d+)', Number),
  86. ],
  87. 'operators': [
  88. (r'\$|[^0-9|\/|\-](\-\=|\+\=|\*\=|\\\=|\=|\=\=|\=\=\=|'
  89. r'\+|\-|\*|\\|\+\=|\>|\<)[^\>|\/]', Operator),
  90. (r'(\||\(|\)|\,|\;|\=|\-|\+|\*|\/|\>|\<|\:)', Operator),
  91. ],
  92. }