Skip to content

Commit 941c366

Browse files
author
Robbert van Renesse
committed
Better line tracking
1 parent 1a650a9 commit 941c366

File tree

5 files changed

+569
-537
lines changed

5 files changed

+569
-537
lines changed

Harmony.g4

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ set_rule
128128
;
129129

130130
iter_parse
131-
: for_parse (for_parse | where_parse)*
131+
: for_parse (NL? (for_parse | where_parse))*
132132
;
133133

134134
for_parse

harmony_model_checker/harmony/ast.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ def rec_comprehension(self, scope, code, iter, pc, accu, ctype, stmt):
161161
assert type == "for" or type == "where", type
162162

163163
if type == "for":
164-
(var, var2, expr) = rest
164+
(var, var2, expr, start, stop) = rest
165+
if ctype == "for":
166+
stmt = self.range(start, stop)
165167

166168
self.define(scope, var)
167169
if var2 != None:
@@ -188,8 +190,11 @@ def rec_comprehension(self, scope, code, iter, pc, accu, ctype, stmt):
188190

189191
else:
190192
assert type == "where"
191-
negate = isinstance(rest, NaryAST) and rest.op[0] == "not"
192-
cond = rest.args[0] if negate else rest
193+
(expr, start, stop) = rest
194+
if ctype == "for":
195+
stmt = self.range(start, stop)
196+
negate = isinstance(expr, NaryAST) and expr.op[0] == "not"
197+
cond = expr.args[0] if negate else expr
193198
cond.compile(scope, code, stmt)
194199
code.append(JumpCondOp(negate, pc), self.token, self.endtoken, stmt=stmt)
195200
self.rec_comprehension(scope, code, iter[1:], pc, accu, ctype, stmt)
@@ -971,16 +976,17 @@ def accept_visitor(self, visitor, *args, **kwargs):
971976

972977

973978
class BlockAST(AST):
974-
def __init__(self, endtoken, token, atomically, b):
979+
def __init__(self, endtoken, token, atomically, b, colon):
975980
AST.__init__(self, endtoken, token, atomically)
976981
assert len(b) > 0
977982
self.b = b
983+
self.colon = colon
978984

979985
def __repr__(self):
980986
return "Block(" + str(self.b) + ")"
981987

982988
def compile(self, scope, code, stmt):
983-
stmt = self.stmt()
989+
stmt = self.range(self.token, self.colon)
984990
ns = Scope(scope)
985991
for s in self.b:
986992
for ((lexeme, file, line, column), lb) in s.getLabels():
@@ -1063,16 +1069,17 @@ def accept_visitor(self, visitor, *args, **kwargs):
10631069

10641070

10651071
class WhileAST(AST):
1066-
def __init__(self, endtoken, token, atomically, cond, stat):
1072+
def __init__(self, endtoken, token, atomically, cond, stat, colon):
10671073
AST.__init__(self, endtoken, token, atomically)
10681074
self.cond = cond
10691075
self.stat = stat
1076+
self.colon = colon
10701077

10711078
def __repr__(self):
10721079
return "While(" + str(self.cond) + ", " + str(self.stat) + ")"
10731080

10741081
def compile(self, scope, code, stmt):
1075-
stmt = self.stmt()
1082+
stmt = self.range(self.token, self.colon)
10761083
if self.atomically:
10771084
code.append(AtomicIncOp(True), self.atomically, self.atomically, stmt=stmt)
10781085
negate = isinstance(self.cond, NaryAST) and self.cond.op[0] == "not"
@@ -1143,7 +1150,8 @@ def compile(self, scope, code, stmt):
11431150
if self.atomically:
11441151
code.append(AtomicIncOp(True), self.atomically, self.atomically, stmt=stmt)
11451152
ns = Scope(scope)
1146-
for (var, expr, token, op) in self.vars:
1153+
for (var, expr, token, endtoken, op) in self.vars:
1154+
stmt = self.range(token, endtoken)
11471155
expr.compile(ns, code, stmt)
11481156
code.append(StoreVarOp(var), token, op, stmt=stmt)
11491157
self.define(ns, var)
@@ -1321,15 +1329,16 @@ def accept_visitor(self, visitor, *args, **kwargs):
13211329

13221330

13231331
class AtomicAST(AST):
1324-
def __init__(self, endtoken, token, atomically, stat):
1332+
def __init__(self, endtoken, token, atomically, stat, colon):
13251333
AST.__init__(self, endtoken, token, atomically)
13261334
self.stat = stat
1335+
self.colon = colon
13271336

13281337
def __repr__(self):
13291338
return "Atomic(" + str(self.stat) + ")"
13301339

13311340
def compile(self, scope, code, stmt):
1332-
stmt = self.stmt()
1341+
stmt = self.range(self.atomically, self.colon)
13331342
code.append(AtomicIncOp(True), self.atomically, self.atomically, stmt=stmt)
13341343
self.stat.compile(scope, code, stmt)
13351344
code.append(AtomicDecOp(), self.atomically, self.endtoken, stmt=stmt)

0 commit comments

Comments
 (0)