graph.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """
  2. pygments.lexers.graph
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for graph query languages.
  5. :copyright: Copyright 2006-2021 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
  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. <https://neo4j.com/docs/developer-manual/3.3/cypher/>`_
  17. For the Cypher version in Neo4j 3.3
  18. .. versionadded:: 2.0
  19. """
  20. name = 'Cypher'
  21. aliases = ['cypher']
  22. filenames = ['*.cyp', '*.cypher']
  23. flags = re.MULTILINE | re.IGNORECASE
  24. tokens = {
  25. 'root': [
  26. include('comment'),
  27. include('keywords'),
  28. include('clauses'),
  29. include('relations'),
  30. include('strings'),
  31. include('whitespace'),
  32. include('barewords'),
  33. ],
  34. 'comment': [
  35. (r'^.*//.*\n', 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'(all|any|as|asc|ascending|assert|call|case|create|'
  44. r'create\s+index|create\s+unique|delete|desc|descending|'
  45. r'distinct|drop\s+constraint\s+on|drop\s+index\s+on|end|'
  46. r'ends\s+with|fieldterminator|foreach|in|is\s+node\s+key|'
  47. r'is\s+null|is\s+unique|limit|load\s+csv\s+from|match|merge|none|'
  48. r'not|null|on\s+match|on\s+create|optional\s+match|order\s+by|'
  49. r'remove|return|set|skip|single|start|starts\s+with|then|union|'
  50. r'union\s+all|unwind|using\s+periodic\s+commit|yield|where|when|'
  51. r'with)\b', Keyword),
  52. ],
  53. 'relations': [
  54. (r'(-\[)(.*?)(\]->)', bygroups(Operator, using(this), Operator)),
  55. (r'(<-\[)(.*?)(\]-)', bygroups(Operator, using(this), Operator)),
  56. (r'(-\[)(.*?)(\]-)', bygroups(Operator, using(this), Operator)),
  57. (r'-->|<--|\[|\]', Operator),
  58. (r'<|>|<>|=|<=|=>|\(|\)|\||:|,|;', Punctuation),
  59. (r'[.*{}]', Punctuation),
  60. ],
  61. 'strings': [
  62. (r'"(?:\\[tbnrf\'"\\]|[^\\"])*"', String),
  63. (r'`(?:``|[^`])+`', Name.Variable),
  64. ],
  65. 'whitespace': [
  66. (r'\s+', Whitespace),
  67. ],
  68. 'barewords': [
  69. (r'[a-z]\w*', Name),
  70. (r'\d+', Number),
  71. ],
  72. }