assert prefix is not None
current_pattern = None
+current_subpattern = None
patterns = dict()
+subpatterns = dict()
+subpattern_args = dict()
state_types = dict()
udata_types = dict()
blocks = list()
return "".join(t)
-def process_pmgfile(f):
+def process_pmgfile(f, filename):
+ linenr = 0
global current_pattern
+ global current_subpattern
while True:
+ linenr += 1
line = f.readline()
if line == "": break
line = line.strip()
if current_pattern is not None:
block = dict()
block["type"] = "final"
- block["pattern"] = current_pattern
+ block["pattern"] = (current_pattern, current_subpattern)
blocks.append(block)
line = line.split()
assert len(line) == 2
assert line[1] not in patterns
current_pattern = line[1]
+ current_subpattern = ""
patterns[current_pattern] = len(blocks)
+ subpatterns[(current_pattern, current_subpattern)] = len(blocks)
+ subpattern_args[(current_pattern, current_subpattern)] = list()
state_types[current_pattern] = dict()
udata_types[current_pattern] = dict()
continue
assert current_pattern is not None
+ if cmd == "subpattern":
+ block = dict()
+ block["type"] = "final"
+ block["pattern"] = (current_pattern, current_subpattern)
+ blocks.append(block)
+ line = line.split()
+ assert len(line) == 2
+ current_subpattern = line[1]
+ subpattern_args[(current_pattern, current_subpattern)] = list()
+ assert (current_pattern, current_subpattern) not in subpatterns
+ subpatterns[(current_pattern, current_subpattern)] = len(blocks)
+ continue
+
+ if cmd == "arg":
+ line = line.split()
+ assert len(line) > 1
+ subpattern_args[(current_pattern, current_subpattern)] += line[1:]
+ continue
+
if cmd == "state":
m = re.match(r"^state\s+<(.*?)>\s+(([A-Za-z_][A-Za-z_0-9]*\s+)*[A-Za-z_][A-Za-z_0-9]*)\s*$", line)
assert m
if cmd == "match":
block = dict()
block["type"] = "match"
- block["pattern"] = current_pattern
+ block["src"] = "%s:%d" % (filename, linenr)
+ block["pattern"] = (current_pattern, current_subpattern)
line = line.split()
assert len(line) == 2
- assert line[1] not in state_types[current_pattern]
+ assert (line[1] not in state_types[current_pattern]) or (state_types[current_pattern][line[1]] == "Cell*")
block["cell"] = line[1]
state_types[current_pattern][line[1]] = "Cell*";
block["index"] = list()
block["filter"] = list()
block["optional"] = False
+ block["semioptional"] = False
while True:
+ linenr += 1
l = f.readline()
assert l != ""
a = l.split()
block["optional"] = True
continue
+ if a[0] == "semioptional":
+ block["semioptional"] = True
+ continue
+
assert False
+ if block["optional"]:
+ assert not block["semioptional"]
+
blocks.append(block)
continue
if cmd == "code":
block = dict()
block["type"] = "code"
- block["pattern"] = current_pattern
+ block["src"] = "%s:%d" % (filename, linenr)
+ block["pattern"] = (current_pattern, current_subpattern)
block["code"] = list()
+ block["fcode"] = list()
block["states"] = set()
for s in line.split()[1:]:
assert s in state_types[current_pattern]
block["states"].add(s)
+ codetype = "code"
+
while True:
+ linenr += 1
l = f.readline()
assert l != ""
a = l.split()
if len(a) == 0: continue
if a[0] == "endcode": break
- block["code"].append(rewrite_cpp(l.rstrip()))
+ if a[0] == "finally":
+ codetype = "fcode"
+ continue
+
+ block[codetype].append(rewrite_cpp(l.rstrip()))
blocks.append(block)
continue
for fn in pmgfiles:
with open(fn, "r") as f:
- process_pmgfile(f)
+ process_pmgfile(f, fn)
if current_pattern is not None:
block = dict()
block["type"] = "final"
- block["pattern"] = current_pattern
+ block["pattern"] = (current_pattern, current_subpattern)
blocks.append(block)
current_pattern = None
+current_subpattern = None
if debug:
pp.pprint(blocks)
print(" blacklist_dirty = false;", file=f)
for index in range(len(blocks)):
block = blocks[index]
- if block["pattern"] != current_pattern:
+ if block["pattern"] != (current_pattern, current_subpattern):
continue
if block["type"] == "match":
print(" if (st_{}.{} != nullptr && blacklist_cells.count(st_{}.{})) {{".format(current_pattern, block["cell"], current_pattern, block["cell"]), file=f)
print(" run_{}([](){{}});".format(current_pattern, current_pattern), file=f)
print(" }", file=f)
print("", file=f)
+
+ for p, s in sorted(subpatterns.keys()):
+ print(" void block_subpattern_{}_{}() {{ block_{}(); }}".format(p, s, subpatterns[(p, s)]), file=f)
+
current_pattern = None
+ current_subpattern = None
for index in range(len(blocks)):
block = blocks[index]
+ if block["type"] in ("match", "code"):
+ print(" // {}".format(block["src"]), file=f)
+
print(" void block_{}() {{".format(index), file=f)
- current_pattern = block["pattern"]
+ current_pattern, current_subpattern = block["pattern"]
if block["type"] == "final":
print(" on_accept();", file=f)
nonconst_st = set()
restore_st = set()
- for i in range(patterns[current_pattern], index):
+ for s in subpattern_args[(current_pattern, current_subpattern)]:
+ const_st.add(s)
+
+ for i in range(subpatterns[(current_pattern, current_subpattern)], index):
if blocks[i]["type"] == "code":
for s in blocks[i]["states"]:
const_st.add(s)
t = state_types[current_pattern][s]
print(" {} &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t, s, current_pattern, s), file=f)
+ for u in sorted(udata_types[current_pattern].keys()):
+ t = udata_types[current_pattern][u]
+ print(" {} &{} YS_ATTRIBUTE(unused) = ud_{}.{};".format(t, u, current_pattern, u), file=f)
+
if len(restore_st):
print("", file=f)
for s in sorted(restore_st):
if block["type"] == "code":
print("", file=f)
- print(" do {", file=f)
print("#define reject do {{ check_blacklist_{}(); goto rollback_label; }} while(0)".format(current_pattern), file=f)
print("#define accept do {{ on_accept(); check_blacklist_{}(); if (rollback) goto rollback_label; }} while(0)".format(current_pattern), file=f)
print("#define branch do {{ block_{}(); if (rollback) goto rollback_label; }} while(0)".format(index+1), file=f)
+ print("#define subpattern(pattern_name) block_subpattern_{}_ ## pattern_name ()".format(current_pattern), file=f)
for line in block["code"]:
- print(" " + line, file=f)
+ print(" " + line, file=f)
print("", file=f)
- print(" block_{}();".format(index+1), file=f)
+ print(" block_{}();".format(index+1), file=f)
+
print("#undef reject", file=f)
print("#undef accept", file=f)
print("#undef branch", file=f)
- print(" } while (0);", file=f)
+ print("#undef subpattern", file=f)
+
print("", file=f)
print("rollback_label:", file=f)
print(" YS_ATTRIBUTE(unused);", file=f)
+ if len(block["fcode"]):
+ print("#define accept do {{ on_accept(); check_blacklist_{}(); }} while(0)".format(current_pattern), file=f)
+ for line in block["fcode"]:
+ print(" " + line, file=f)
+ print("#undef accept", file=f)
+
if len(restore_st) or len(nonconst_st):
print("", file=f)
for s in sorted(restore_st):
elif block["type"] == "match":
assert len(restore_st) == 0
+ print(" Cell* backup_{} = {};".format(block["cell"], block["cell"]), file=f)
+
if len(block["if"]):
for expr in block["if"]:
print("", file=f)
print(" if (!({})) {{".format(expr), file=f)
print(" {} = nullptr;".format(block["cell"]), file=f)
print(" block_{}();".format(index+1), file=f)
+ print(" {} = backup_{};".format(block["cell"], block["cell"]), file=f)
print(" return;", file=f)
print(" }", file=f)
print(" std::get<{}>(key) = {};".format(field, entry[2]), file=f)
print(" const vector<Cell*> &cells = index_{}[key];".format(index), file=f)
+ if block["semioptional"]:
+ print(" bool found_semioptional_match = false;", file=f)
+
print("", file=f)
print(" for (int idx = 0; idx < GetSize(cells); idx++) {", file=f)
print(" {} = cells[idx];".format(block["cell"]), file=f)
print(" if (blacklist_cells.count({})) continue;".format(block["cell"]), file=f)
for expr in block["filter"]:
print(" if (!({})) continue;".format(expr), file=f)
+ if block["semioptional"]:
+ print(" found_semioptional_match = true;", file=f)
print(" block_{}();".format(index+1), file=f)
print(" if (rollback) {", file=f)
print(" if (rollback != {}) {{".format(index+1), file=f)
- print(" {} = nullptr;".format(block["cell"]), file=f)
+ print(" {} = backup_{};".format(block["cell"], block["cell"]), file=f)
print(" return;", file=f)
print(" }", file=f)
print(" rollback = 0;", file=f)
if block["optional"]:
print(" block_{}();".format(index+1), file=f)
+ if block["semioptional"]:
+ print(" if (!found_semioptional_match) block_{}();".format(index+1), file=f)
+
+ print(" {} = backup_{};".format(block["cell"], block["cell"]), file=f)
+
else:
assert False