graph.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """
  2. pygments.lexers.graph
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for graph query 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, this, words
  10. from pygments.token import Keyword, Punctuation, Comment, Operator, Name,\
  11. String, Number, Whitespace
  12. __all__ = ['CypherLexer']
  13. class CypherLexer(RegexLexer):
  14. """
  15. For Cypher Query Language
  16. For the Cypher version in Neo4j 3.3
  17. .. versionadded:: 2.0
  18. """
  19. name = 'Cypher'
  20. url = 'https://neo4j.com/docs/developer-manual/3.3/cypher/'
  21. aliases = ['cypher']
  22. filenames = ['*.cyp', '*.cypher']
  23. flags = re.MULTILINE | re.IGNORECASE
  24. tokens = {
  25. 'root': [
  26. include('comment'),
  27. include('clauses'),
  28. include('keywords'),
  29. include('relations'),
  30. include('strings'),
  31. include('whitespace'),
  32. include('barewords'),
  33. ],
  34. 'comment': [
  35. (r'^.*//.*$', Comment.Single),
  36. ],
  37. 'keywords': [
  38. (r'(create|order|match|limit|set|skip|start|return|with|where|'
  39. r'delete|foreach|not|by|true|false)\b', Keyword),
  40. ],
  41. 'clauses': [
  42. # based on https://neo4j.com/docs/cypher-refcard/3.3/
  43. (r'(create)(\s+)(index|unique)\b',
  44. bygroups(Keyword, Whitespace, Keyword)),
  45. (r'(drop)(\s+)(contraint|index)(\s+)(on)\b',
  46. bygroups(Keyword, Whitespace, Keyword, Whitespace, Keyword)),
  47. (r'(ends)(\s+)(with)\b',
  48. bygroups(Keyword, Whitespace, Keyword)),
  49. (r'(is)(\s+)(node)(\s+)(key)\b',
  50. bygroups(Keyword, Whitespace, Keyword, Whitespace, Keyword)),
  51. (r'(is)(\s+)(null|unique)\b',
  52. bygroups(Keyword, Whitespace, Keyword)),
  53. (r'(load)(\s+)(csv)(\s+)(from)\b',
  54. bygroups(Keyword, Whitespace, Keyword, Whitespace, Keyword)),
  55. (r'(on)(\s+)(match|create)\b',
  56. bygroups(Keyword, Whitespace, Keyword)),
  57. (r'(optional)(\s+)(match)\b',
  58. bygroups(Keyword, Whitespace, Keyword)),
  59. (r'(order)(\s+)(by)\b',
  60. bygroups(Keyword, Whitespace, Keyword)),
  61. (r'(starts)(\s+)(with)\b',
  62. bygroups(Keyword, Whitespace, Keyword)),
  63. (r'(union)(\s+)(all)\b',
  64. bygroups(Keyword, Whitespace, Keyword)),
  65. (r'(using)(\s+)(periodic)(\s+)(commit)\b',
  66. bygroups(Keyword, Whitespace, Keyword, Whitespace, Keyword)),
  67. (words((
  68. 'all', 'any', 'as', 'asc', 'ascending', 'assert', 'call', 'case', 'create',
  69. 'delete', 'desc', 'descending', 'distinct', 'end', 'fieldterminator',
  70. 'foreach', 'in', 'limit', 'match', 'merge', 'none', 'not', 'null',
  71. 'remove', 'return', 'set', 'skip', 'single', 'start', 'then', 'union',
  72. 'unwind', 'yield', 'where', 'when', 'with'), suffix=r'\b'), Keyword),
  73. ],
  74. 'relations': [
  75. (r'(-\[)(.*?)(\]->)', bygroups(Operator, using(this), Operator)),
  76. (r'(<-\[)(.*?)(\]-)', bygroups(Operator, using(this), Operator)),
  77. (r'(-\[)(.*?)(\]-)', bygroups(Operator, using(this), Operator)),
  78. (r'-->|<--|\[|\]', Operator),
  79. (r'<|>|<>|=|<=|=>|\(|\)|\||:|,|;', Punctuation),
  80. (r'[.*{}]', Punctuation),
  81. ],
  82. 'strings': [
  83. (r'"(?:\\[tbnrf\'"\\]|[^\\"])*"', String),
  84. (r'`(?:``|[^`])+`', Name.Variable),
  85. ],
  86. 'whitespace': [
  87. (r'\s+', Whitespace),
  88. ],
  89. 'barewords': [
  90. (r'[a-z]\w*', Name),
  91. (r'\d+', Number),
  92. ],
  93. }