Customized format and print for our kind of tokens which gets called in debugging grammar reduce rules
(self, rule, tokens, parent, last_token_pos)
| 167 | setattr(self, i, None) |
| 168 | |
| 169 | def debug_reduce(self, rule, tokens, parent, last_token_pos): |
| 170 | """Customized format and print for our kind of tokens |
| 171 | which gets called in debugging grammar reduce rules |
| 172 | """ |
| 173 | |
| 174 | def fix(c): |
| 175 | s = str(c) |
| 176 | token_pos = s.find("_") |
| 177 | if token_pos == -1: |
| 178 | return s |
| 179 | else: |
| 180 | return s[:token_pos] |
| 181 | |
| 182 | prefix = "" |
| 183 | if parent and tokens: |
| 184 | p_token = tokens[parent] |
| 185 | if hasattr(p_token, "linestart") and p_token.linestart: |
| 186 | prefix = "L.%3d: " % p_token.linestart |
| 187 | else: |
| 188 | prefix = " " |
| 189 | if hasattr(p_token, "offset"): |
| 190 | prefix += "%3s" % fix(p_token.offset) |
| 191 | if len(rule[1]) > 1: |
| 192 | prefix += "-%-3s " % fix(tokens[last_token_pos - 1].offset) |
| 193 | else: |
| 194 | prefix += " " |
| 195 | else: |
| 196 | prefix = " " |
| 197 | |
| 198 | print("%s%s ::= %s (%d)" % (prefix, rule[0], " ".join(rule[1]), last_token_pos)) |
| 199 | |
| 200 | def error(self, instructions, index): |
| 201 | # Find the last line boundary |