Skip to content

Instantly share code, notes, and snippets.

@tcard
Last active December 23, 2015 06:29
Show Gist options
  • Save tcard/6594219 to your computer and use it in GitHub Desktop.
Save tcard/6594219 to your computer and use it in GitHub Desktop.
Just quickly wrote this little thingy for debugging ‘unexpected EOF’ errors in Go standard templates.
import os, sys
if len(sys.argv) > 1:
exp = open(sys.argv[1]).read()
else:
exp = raw_input("Write template (or try again with file path as argument): ")
for l in os.sys.stdin:
exp += l
print
print
print
tags = ['with', 'range', 'if']
stack = []
line = 1
col = 0
def print_op(op, line, col, i):
if op:
print op+":",
print "%s:%s" % (line, col),
j = i
while exp[j:j+2] != "}}":
j += 1
print exp[i:j+2]
def print_stack():
print "<<<"
for p in stack:
print_op(None, p[1], p[2], p[0])
print ">>>"
print
for i in range(len(exp)):
col += 1
for tag in tags:
if exp[i:i+len('{{'+tag)] == '{{'+tag:
stack.append((i, line, col))
print_op("Open", line, col, i)
print_stack()
if exp[i] == '\n':
line += 1
col = 0
elif exp[i:i+len('{{end')] == '{{end':
p = stack.pop()
print_op("Close at %s:%s" % (line, col), p[1], p[2], p[0])
print_stack()
if len(stack) != 0:
p = stack.pop()
print "UNCLOSED line", "%s:%s" % (p[1], p[2]),":", exp[p[0]:p[0]+20]
else:
print "OK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment