4 # This script is for extracting the grammar from the rust docs.
8 collections = { "gram": [],
18 for line in fileinput.input(openhook=fileinput.hook_encoded("utf-8")):
20 if line.startswith("~~~~"):
23 if coll in ["keyword", "reserved", "binop", "unop"]:
24 for word in line.split():
25 if word not in collections[coll]:
26 collections[coll].append(word)
28 collections[coll].append(line)
31 if line.startswith("~~~~"):
32 for cname in collections:
33 if ("." + cname) in line:
38 # Define operator symbol-names here
40 tokens = ["non_star", "non_slash", "non_eol",
41 "non_single_quote", "non_double_quote", "ident" ]
95 "/*": "openblockcomment",
96 "*/": "closeblockcomment"
101 for line in collections["gram"]:
103 for word in line.split():
104 # replace strings with keyword-names or symbol-names from table
105 if word.startswith("\""):
108 word = symnames[word]
112 raise Exception("non-alpha apparent keyword: "
114 if word not in tokens:
115 if (word in collections["keyword"] or
116 word in collections["reserved"]):
119 raise Exception("unknown keyword/reserved word: "
126 for word in collections["keyword"] + collections["reserved"]:
127 if word not in tokens:
130 for sym in collections["unop"] + collections["binop"] + symnames.keys():
132 if word not in tokens:
136 print("%start parser, token;")
137 print("%%token %s ;" % ("\n\t, ".join(tokens)))
138 for coll in ["keyword", "reserved"]:
139 print("%s: %s ; " % (coll, "\n\t| ".join(collections[coll])));
140 for coll in ["binop", "unop"]:
141 print("%s: %s ; " % (coll, "\n\t| ".join([symnames[x]
142 for x in collections[coll]])));
143 print("\n".join(lines));