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" ]
94 "/*": "openblockcomment",
95 "*/": "closeblockcomment"
100 for line in collections["gram"]:
102 for word in line.split():
103 # replace strings with keyword-names or symbol-names from table
104 if word.startswith("\""):
107 word = symnames[word]
111 raise Exception("non-alpha apparent keyword: "
113 if word not in tokens:
114 if (word in collections["keyword"] or
115 word in collections["reserved"]):
118 raise Exception("unknown keyword/reserved word: "
125 for word in collections["keyword"] + collections["reserved"]:
126 if word not in tokens:
129 for sym in collections["unop"] + collections["binop"] + symnames.keys():
131 if word not in tokens:
135 print("%start parser, token;")
136 print("%%token %s ;" % ("\n\t, ".join(tokens)))
137 for coll in ["keyword", "reserved"]:
138 print("%s: %s ; " % (coll, "\n\t| ".join(collections[coll])));
139 for coll in ["binop", "unop"]:
140 print("%s: %s ; " % (coll, "\n\t| ".join([symnames[x]
141 for x in collections[coll]])));
142 print("\n".join(lines));