6. py2neo.cypher.lexer – Cypher Lexer

This module contains a Cypher language lexer based on the Pygments lexer framework. This can be used to parse statements and expressions for the Cypher variant available in Neo4j 3.4.

To parse a Cypher statement, create a CypherLexer or select by the name py2neo.cypher, then invoke the get_tokens() method:

>>> from pygments.lexers import get_lexer_by_name
>>> lexer = get_lexer_by_name("py2neo.cypher")
>>> list(lexer.get_tokens("MATCH (a:Person)-[:KNOWS]->(b) RETURN a"))
[(Token.Keyword, 'MATCH'),
 (Token.Text.Whitespace, ' '),
 (Token.Punctuation, '('),
 (Token.Name.Variable, 'a'),
 (Token.Punctuation, ':'),
 (Token.Name.Label, 'Person'),
 (Token.Punctuation, ')-['),
 (Token.Punctuation, ':'),
 (Token.Name.Label, 'KNOWS'),
 (Token.Punctuation, ']->('),
 (Token.Name.Variable, 'b'),
 (Token.Punctuation, ')'),
 (Token.Text.Whitespace, ' '),
 (Token.Keyword, 'RETURN'),
 (Token.Text.Whitespace, ' '),
 (Token.Name.Variable, 'a'),
 (Token.Text.Whitespace, '\n')]

To split multiple semicolon-separated statements within a single string, use instead the get_statements() method:

>>> list(lexer.get_statements("CREATE (:Person {name:'Alice'}); MATCH (a:Person {name:'Alice'}) RETURN id(a)"))
["CREATE (:Person {name:'Alice'})",
 "MATCH (a:Person {name:'Alice'}) RETURN id(a)"]
class py2neo.cypher.lexer.CypherLexer(**options)[source]

Pygments lexer for the Cypher Query Language as available in Neo4j 3.4.

get_statements(text)[source]

Split the text into statements delimited by semicolons and yield each statement in turn. Yielded statements are stripped of both leading and trailing whitespace. Empty statements are skipped.