graph.py 2.7 KB

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