Skip to content

Commit

Permalink
Added support for Cypher (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Jul 18, 2020
1 parent 4f55052 commit 398e294
Show file tree
Hide file tree
Showing 16 changed files with 560 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Expand Up @@ -257,6 +257,10 @@
"modify": "css",
"owner": "milesj"
},
"cypher": {
"title": "Cypher",
"owner": "RunDevelopment"
},
"d": {
"title": "D",
"require": "clike",
Expand Down
37 changes: 37 additions & 0 deletions components/prism-cypher.js
@@ -0,0 +1,37 @@
Prism.languages.cypher = {
// https://neo4j.com/docs/cypher-manual/current/syntax/comments/
'comment': /\/\/.*/,
'string': {
pattern: /"(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'/,
greedy: true
},
'class-name': {
pattern: /(:\s*)(?:\w+|`(?:[^`\\\r\n])*`)(?=\s*[{):])/,
lookbehind: true,
greedy: true
},
'relationship': {
pattern: /(-\[\s*(?:\w+\s*|`(?:[^`\\\r\n])*`\s*)?:\s*|\|\s*:\s*)(?:\w+|`(?:[^`\\\r\n])*`)/,
lookbehind: true,
greedy: true,
alias: 'property'
},
'identifier': {
pattern: /`(?:[^`\\\r\n])*`/,
greedy: true,
alias: 'symbol'
},

'variable': /\$\w+/,

// https://neo4j.com/docs/cypher-manual/current/syntax/reserved/
'keyword': /\b(?:ADD|ALL|AND|AS|ASC|ASCENDING|ASSERT|BY|CALL|CASE|COMMIT|CONSTRAINT|CONTAINS|CREATE|CSV|DELETE|DESC|DESCENDING|DETACH|DISTINCT|DO|DROP|ELSE|END|ENDS|EXISTS|FOR|FOREACH|IN|INDEX|IS|JOIN|KEY|LIMIT|LOAD|MANDATORY|MATCH|MERGE|NODE|NOT|OF|ON|OPTIONAL|OR|ORDER(?=\s+BY)|PERIODIC|REMOVE|REQUIRE|RETURN|SCALAR|SCAN|SET|SKIP|START|STARTS|THEN|UNION|UNIQUE|UNWIND|USING|WHEN|WHERE|WITH|XOR|YIELD)\b/i,

'function': /\b\w+\b(?=\s*\()/,

'boolean': /\b(?:true|false|null)\b/i,
'number': /\b(?:0x[\da-fA-F]+|\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)\b/,
// https://neo4j.com/docs/cypher-manual/current/syntax/operators/
'operator': /:|<--?|--?>?|<>|=~?|[<>]=?|[+*/%^|]|\.\.\.?/,
'punctuation': /[()[\]{},;.]/
};
1 change: 1 addition & 0 deletions components/prism-cypher.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions examples/prism-cypher.html
@@ -0,0 +1,8 @@
<h2>Full example</h2>
<pre><code>MATCH (person:Person)-[:WORKS_FOR]->(company)
WHERE company.name STARTS WITH "Company"
AND EXISTS {
MATCH (person)-[:LIKES]->(t:Technology)
WHERE size((t)&lt;-[:LIKES]-()) >= 3
}
RETURN person.name as person, company.name AS company;</code></pre>
17 changes: 17 additions & 0 deletions tests/languages/cypher/boolean_feature.test
@@ -0,0 +1,17 @@
true TRUE
false FALSE
null

----------------------------------------------------

[
["boolean", "true"],
["boolean", "TRUE"],
["boolean", "false"],
["boolean", "FALSE"],
["boolean", "null"]
]

----------------------------------------------------

Checks for booleans and null.
114 changes: 114 additions & 0 deletions tests/languages/cypher/class-name_feature.test
@@ -0,0 +1,114 @@
(p:Student)
(p:Student { name: "John" })
(:`Student`)
(:`Student` { name: "John"})

(p:Student:Teacher)
(p:Student { name: "John" }:Teacher)
(:`Student`:`Teacher`)
(:`Student` { name: "John"}:`Teacher` { classes: "..." })

// no class names but still interesting cases

(p { name: "John" })
({ name: "John" })
()

----------------------------------------------------

[
["punctuation", "("],
"p",
["operator", ":"],
["class-name", "Student"],
["punctuation", ")"],
["punctuation", "("],
"p",
["operator", ":"],
["class-name", "Student"],
["punctuation", "{"],
" name",
["operator", ":"],
["string", "\"John\""],
["punctuation", "}"],
["punctuation", ")"],
["punctuation", "("],
["operator", ":"],
["class-name", "`Student`"],
["punctuation", ")"],
["punctuation", "("],
["operator", ":"],
["class-name", "`Student`"],
["punctuation", "{"],
" name",
["operator", ":"],
["string", "\"John\""],
["punctuation", "}"],
["punctuation", ")"],

["punctuation", "("],
"p",
["operator", ":"],
["class-name", "Student"],
["operator", ":"],
["class-name", "Teacher"],
["punctuation", ")"],
["punctuation", "("],
"p",
["operator", ":"],
["class-name", "Student"],
["punctuation", "{"],
" name",
["operator", ":"],
["string", "\"John\""],
["punctuation", "}"],
["operator", ":"],
["class-name", "Teacher"],
["punctuation", ")"],
["punctuation", "("],
["operator", ":"],
["class-name", "`Student`"],
["operator", ":"],
["class-name", "`Teacher`"],
["punctuation", ")"],
["punctuation", "("],
["operator", ":"],
["class-name", "`Student`"],
["punctuation", "{"],
" name",
["operator", ":"],
["string", "\"John\""],
["punctuation", "}"],
["operator", ":"],
["class-name", "`Teacher`"],
["punctuation", "{"],
" classes",
["operator", ":"],
["string", "\"...\""],
["punctuation", "}"],
["punctuation", ")"],

["comment", "// no class names but still interesting cases"],

["punctuation", "("],
"p ",
["punctuation", "{"],
" name",
["operator", ":"],
["string", "\"John\""],
["punctuation", "}"],
["punctuation", ")"],
["punctuation", "("],
["punctuation", "{"],
" name",
["operator", ":"],
["string", "\"John\""],
["punctuation", "}"],
["punctuation", ")"],
["punctuation", "("],
["punctuation", ")"]
]

----------------------------------------------------

Checks for class names.
11 changes: 11 additions & 0 deletions tests/languages/cypher/comment_feature.test
@@ -0,0 +1,11 @@
//comment

----------------------------------------------------

[
["comment", "//comment"]
]

----------------------------------------------------

Checks for comments.
13 changes: 13 additions & 0 deletions tests/languages/cypher/function_feature.test
@@ -0,0 +1,13 @@
foo()

----------------------------------------------------

[
["function", "foo"],
["punctuation", "("],
["punctuation", ")"]
]

----------------------------------------------------

Checks for function names.
138 changes: 138 additions & 0 deletions tests/languages/cypher/keyword_feature.test
@@ -0,0 +1,138 @@
ADD
ALL
AND
AS
ASC
ASCENDING
ASSERT
CALL
CASE
COMMIT
CONSTRAINT
CONTAINS
CREATE
CSV
DELETE
DESC
DESCENDING
DETACH
DISTINCT
DO
DROP
ELSE
END
ENDS
EXISTS
FOR
FOREACH
IN
INDEX
IS
JOIN
KEY
LIMIT
LOAD
MANDATORY
MATCH
MERGE
NODE
NOT
OF
ON
OPTIONAL
OR
ORDER BY
PERIODIC
REMOVE
REQUIRE
RETURN
SCALAR
SCAN
SET
SKIP
START
STARTS
THEN
UNION
UNIQUE
UNWIND
USING
WHEN
WHERE
WITH
XOR
YIELD

----------------------------------------------------

[
["keyword", "ADD"],
["keyword", "ALL"],
["keyword", "AND"],
["keyword", "AS"],
["keyword", "ASC"],
["keyword", "ASCENDING"],
["keyword", "ASSERT"],
["keyword", "CALL"],
["keyword", "CASE"],
["keyword", "COMMIT"],
["keyword", "CONSTRAINT"],
["keyword", "CONTAINS"],
["keyword", "CREATE"],
["keyword", "CSV"],
["keyword", "DELETE"],
["keyword", "DESC"],
["keyword", "DESCENDING"],
["keyword", "DETACH"],
["keyword", "DISTINCT"],
["keyword", "DO"],
["keyword", "DROP"],
["keyword", "ELSE"],
["keyword", "END"],
["keyword", "ENDS"],
["keyword", "EXISTS"],
["keyword", "FOR"],
["keyword", "FOREACH"],
["keyword", "IN"],
["keyword", "INDEX"],
["keyword", "IS"],
["keyword", "JOIN"],
["keyword", "KEY"],
["keyword", "LIMIT"],
["keyword", "LOAD"],
["keyword", "MANDATORY"],
["keyword", "MATCH"],
["keyword", "MERGE"],
["keyword", "NODE"],
["keyword", "NOT"],
["keyword", "OF"],
["keyword", "ON"],
["keyword", "OPTIONAL"],
["keyword", "OR"],
["keyword", "ORDER"],
["keyword", "BY"],
["keyword", "PERIODIC"],
["keyword", "REMOVE"],
["keyword", "REQUIRE"],
["keyword", "RETURN"],
["keyword", "SCALAR"],
["keyword", "SCAN"],
["keyword", "SET"],
["keyword", "SKIP"],
["keyword", "START"],
["keyword", "STARTS"],
["keyword", "THEN"],
["keyword", "UNION"],
["keyword", "UNIQUE"],
["keyword", "UNWIND"],
["keyword", "USING"],
["keyword", "WHEN"],
["keyword", "WHERE"],
["keyword", "WITH"],
["keyword", "XOR"],
["keyword", "YIELD"]
]

----------------------------------------------------

Checks for keywords.
35 changes: 35 additions & 0 deletions tests/languages/cypher/number_feature.test
@@ -0,0 +1,35 @@
123
123E45

3.14
6.022E23

0x13af
0xFC3A9
0x66eff

01372
02127
05671

----------------------------------------------------

[
["number", "123"],
["number", "123E45"],

["number", "3.14"],
["number", "6.022E23"],

["number", "0x13af"],
["number", "0xFC3A9"],
["number", "0x66eff"],

["number", "01372"],
["number", "02127"],
["number", "05671"]
]

----------------------------------------------------

Checks for numbers.

0 comments on commit 398e294

Please sign in to comment.