We've shown typical translations of Cypher in Javascript. Here we consider how we might automate that translation.
The openCypher standardization effort includes grammars in EBNF and Railroad Diagrams. opencypher
The PEG.js parser generator for javascript hosts an interactive grammar development environment that builds and runs a partial grammar against test input. pegjs
.
We've had encouraging initial success parsing one sample query with excerpts from the openCypher grammar.
match (n:Employee) <-[:Manager]- (:Project)
Match = 'match' _ p:PatternElement { return `match(${p})` } PatternElement = l:NodePattern _ x:(RelationshipPattern NodePattern)* { return `eval(${l},${x})` } NodePattern = '(' v:Variable? ':' l:Label ')' { return `\nnode(${v},${l})` } RelationshipPattern = '<-' _ d:RelationshipDetail? '-' _ { return `\nin(${d})` } RelationshipDetail = '[' v:Variable? ':' t:Type ']' { return `rel(${v},${t})` } Variable = v:Word { return v } Label = l:Word { return `'${l}'` } Type = t:Word { return `'${t}'` } Word = [A-Za-z]+ { return text() } _ = [ \t\n\r]*
This produced the mock translation including the essential elements but only suggestive javascript actions.
match(eval( node(n,'Employee'), in(rel(null,'Manager')), node(null,'Project')))
.
We download the generated parser, hacked the exports, and run it within an html script. github
http://ward.dojo.fed.wiki/assets/pages/mock-graph-data/cypher.html HEIGHT 300