|
| 1 | +/** |
| 2 | + * Parses RA expressions. |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * A predicate that contains RA. |
| 7 | + */ |
| 8 | +signature class RApredicate { |
| 9 | + string getLineOfRA(int n); |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Parses strings of RA provided by an RA predicate, and represented the |
| 14 | + */ |
| 15 | +module RAParser<RApredicate Predicate> { |
| 16 | + private string parseRaExpr(Predicate p, int line, int arity, int lhs) { |
| 17 | + exists(string str | str = p.getLineOfRA(line).trim() | |
| 18 | + arity = str.regexpCapture("\\{([0-9]+)\\} r([0-9]+) = (.+)", 1).toInt() and |
| 19 | + lhs = str.regexpCapture("\\{([0-9]+)\\} r([0-9]+) = (.+)", 2).toInt() and |
| 20 | + result = str.regexpCapture("\\{([0-9]+)\\} r([0-9]+) = (.+)", 3) |
| 21 | + ) |
| 22 | + } |
| 23 | + |
| 24 | + bindingset[str] |
| 25 | + private int parseReturn(string str) { |
| 26 | + result = str.trim().regexpCapture("return r([0-9]+)", 1).toInt() |
| 27 | + } |
| 28 | + |
| 29 | + private newtype TRA = |
| 30 | + TReturn(Predicate p, int line, int v) { v = parseReturn(p.getLineOfRA(line)) } or |
| 31 | + TUnknown(Predicate p, int line, int lhs, int arity, string rhs) { |
| 32 | + rhs = parseRaExpr(p, line, arity, lhs) |
| 33 | + } |
| 34 | + |
| 35 | + /** An RA Expression. */ |
| 36 | + abstract class RAExpr extends TRA { |
| 37 | + /** Gets the predicate this RA expression belongs to. */ |
| 38 | + abstract Predicate getPredicate(); |
| 39 | + |
| 40 | + /** Gets the line index of this RA expression. */ |
| 41 | + abstract int getLine(); |
| 42 | + |
| 43 | + /** Gets the LHS of the expression of the form `rNN = ...` */ |
| 44 | + abstract int getLhs(); |
| 45 | + |
| 46 | + /** Gets a variable of the form `rNN` in the RHS of the RA expression. */ |
| 47 | + abstract int getARhsVariable(); |
| 48 | + |
| 49 | + /** Gets the given arity of the RA expression. */ |
| 50 | + abstract int getArity(); |
| 51 | + |
| 52 | + /** Gets a predicate name referenced in the RHS of an RA expression. */ |
| 53 | + abstract string getARhsPredicate(); |
| 54 | + |
| 55 | + final string toString() { result = this.getPredicate().getLineOfRA(this.getLine()) } |
| 56 | + |
| 57 | + /** Gets a child of this RA expression - not by index yet. */ |
| 58 | + RAExpr getAChild() { |
| 59 | + result.getPredicate() = this.getPredicate() and result.getLhs() = this.getARhsVariable() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * A generic RA expression - where we haven't precisely parsed the RA expression type. |
| 65 | + * For Hackathon purposes, we probably don't need more than this. |
| 66 | + */ |
| 67 | + class RAUnknownExpr extends RAExpr, TUnknown { |
| 68 | + Predicate p; |
| 69 | + int line; |
| 70 | + string rhs; |
| 71 | + int arity; |
| 72 | + int lhs; |
| 73 | + |
| 74 | + RAUnknownExpr() { this = TUnknown(p, line, lhs, arity, rhs) } |
| 75 | + |
| 76 | + override int getLine() { result = line } |
| 77 | + |
| 78 | + override Predicate getPredicate() { result = p } |
| 79 | + |
| 80 | + override int getLhs() { result = lhs } |
| 81 | + |
| 82 | + override int getARhsVariable() { |
| 83 | + result = rhs.splitAt(" ").regexpCapture("r([0-9]+)", 1).toInt() |
| 84 | + } |
| 85 | + |
| 86 | + // This is a dumb regex to find a predicate name - they always contain a `#` (TODO...) |
| 87 | + override string getARhsPredicate() { result = rhs.splitAt(" ") and result.indexOf("#") > 0 } |
| 88 | + |
| 89 | + override int getArity() { result = arity } |
| 90 | + } |
| 91 | + |
| 92 | + class RAReturnExpr extends RAExpr, TReturn { |
| 93 | + RAReturnExpr() { this = TReturn(p, line, res) } |
| 94 | + |
| 95 | + Predicate p; |
| 96 | + int line; |
| 97 | + int res; |
| 98 | + |
| 99 | + override Predicate getPredicate() { result = p } |
| 100 | + |
| 101 | + override int getLine() { result = line } |
| 102 | + |
| 103 | + override int getLhs() { none() } |
| 104 | + |
| 105 | + override int getARhsVariable() { result = res } |
| 106 | + |
| 107 | + override int getArity() { none() } |
| 108 | + |
| 109 | + override string getARhsPredicate() { none() } |
| 110 | + } |
| 111 | +} |
0 commit comments