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