Cypher-lite front-end: tokenize then parse·14 min
Parse a path pattern
0 / 1
# grade this stage (from your course directory)
npx theinference test --stage parse-path-pattern --watchParse a full path: a start node, then a chain of (relationship, node) segments, decoding direction from the arrow tokens and the optional *min..max. Reuse parseNodePattern for every node; loop while the next token starts a relationship (- or <-).
Your task
Implement parsePathPattern
Each segment is -[:TYPE]-> / <-[:TYPE]- / -[:TYPE]-. Decode the arrows: a right arrow (->) and no left arrow is OUT; a left arrow (<-) and no right is IN; neither is BOTH. Parse optional * var-length (*, *2, *1..3, *..3).
src/parser.ts
// PathPattern = { start: NodePattern; segments: Array<{ rel: RelPattern; node: NodePattern }> }
// RelPattern = { type: string|null; dir: 'OUT'|'IN'|'BOTH'; varLength: {min,max}|null }
parsePathPattern(): PathPattern {
const start = this.parseNodePattern();
const segments = [];
while (this.isPunct('-') || this.isPunct('<-')) {
const rel = this.parseRelPattern(); // reads <- / - , [:TYPE *m..n] , -> / -
const node = this.parseNodePattern();
segments.push({ rel, node });
}
return { start, segments };
}
Process command
your_program.sh parse-path "(a)-[:ACTED_IN]->(m)<-[:ACTED_IN]-(b)"
# two segments: first rel dir OUT, second rel dir IN
How it's graded
Process mode, stdout_json_match against the exact two-segment AST. The co-star shape (a)-[:ACTED_IN]->(m)<-[:ACTED_IN]-(b) is precisely what the capstone executor will run.