| ... | ... | @@ -89,7 +89,7 @@ const Generator = struct { |
| 89 | 89 | \\/// the grammar. |
| 90 | 90 | \\/// Returns error.MaxDepth if more than `max_depth` levels of recursion/iteration are reached. |
| 91 | 91 | \\pub fn parse(source: []const u8) Error!bool { |
| 92 | | \\ var p: Parser = .{ .source = source, .i = 0, .expr_depth = 1 }; |
| 92 | \\ var p: Parser = .{ .source = source, .i = 0, .expr_depth = 1, .block_depth = 1 }; |
| 93 | 93 | \\ return p.parseRoot(); |
| 94 | 94 | \\} |
| 95 | 95 | \\ |
| ... | ... | @@ -97,6 +97,7 @@ const Generator = struct { |
| 97 | 97 | \\ source: []const u8, |
| 98 | 98 | \\ i: usize, |
| 99 | 99 | \\ expr_depth: usize, |
| 100 | \\ block_depth: usize, |
| 100 | 101 | \\ |
| 101 | 102 | ); |
| 102 | 103 | for (g.p.getExtra(node.get(g.p).root)) |def| { |
| ... | ... | @@ -110,6 +111,7 @@ const Generator = struct { |
| 110 | 111 | const id = def.id.get(g.p).id; |
| 111 | 112 | assert(g.suffix == 0); |
| 112 | 113 | try g.w.print("pub fn parse{s}(p: *Parser) Error!bool {{", .{id}); |
| 114 | // The grammar can infinitely recurse through the Expr rule |
| 113 | 115 | if (mem.eql(u8, "Expr", id)) { |
| 114 | 116 | try g.w.print( |
| 115 | 117 | \\if (p.expr_depth >= max_depth) return error.MaxDepth; |
| ... | ... | @@ -117,6 +119,14 @@ const Generator = struct { |
| 117 | 119 | \\defer p.expr_depth -= 1; |
| 118 | 120 | , .{}); |
| 119 | 121 | } |
| 122 | // The grammar can infinitely recurse through the Block rule |
| 123 | if (mem.eql(u8, "Block", id)) { |
| 124 | try g.w.print( |
| 125 | \\if (p.block_depth >= max_depth) return error.MaxDepth; |
| 126 | \\p.block_depth += 1; |
| 127 | \\defer p.block_depth -= 1; |
| 128 | , .{}); |
| 129 | } |
| 120 | 130 | try g.w.writeAll("return "); |
| 121 | 131 | try g.genExpr(def.expr); |
| 122 | 132 | try g.w.writeAll(";}"); |