3 # This script is for extracting the grammar from the rust docs.
7 collections = { "gram": [],
17 for line in fileinput.input(openhook=fileinput.hook_encoded("utf-8")):
19 if line.startswith("~~~~"):
22 if coll in ["keyword", "reserved", "binop", "unop"]:
23 for word in line.split():
24 if word not in collections[coll]:
25 collections[coll].append(word)
27 collections[coll].append(line)
30 if line.startswith("~~~~"):
31 for cname in collections:
32 if ("." + cname) in line:
37 # Define operator symbol-names here
39 tokens = ["non_star", "non_slash", "non_eol",
40 "non_single_quote", "non_double_quote", "ident" ]
90 "/*": "openblockcomment",
91 "*/": "closeblockcomment"
96 for line in collections["gram"]:
98 for word in line.split():
99 # replace strings with keyword-names or symbol-names from table
100 if word.startswith("\""):
103 word = symnames[word]
107 raise Exception("non-alpha apparent keyword: "
109 if word not in tokens:
110 if (word in collections["keyword"] or
111 word in collections["reserved"]):
114 raise Exception("unknown keyword/reserved word: "
121 for word in collections["keyword"] + collections["reserved"]:
122 if word not in tokens:
125 for sym in collections["unop"] + collections["binop"] + symnames.keys():
127 if word not in tokens:
131 print("%start parser, token;")
132 print("%%token %s ;" % ("\n\t, ".join(tokens)))
133 for coll in ["keyword", "reserved"]:
134 print("%s: %s ; " % (coll, "\n\t| ".join(collections[coll])));
135 for coll in ["binop", "unop"]:
136 print("%s: %s ; " % (coll, "\n\t| ".join([symnames[x]
137 for x in collections[coll]])));
138 print("\n".join(lines));