3 # Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
13 # This script is for extracting the grammar from the rust docs.
17 collections = { "gram": [],
27 for line in fileinput.input(openhook=fileinput.hook_encoded("utf-8")):
29 if line.startswith("~~~~"):
32 if coll in ["keyword", "reserved", "binop", "unop"]:
33 for word in line.split():
34 if word not in collections[coll]:
35 collections[coll].append(word)
37 collections[coll].append(line)
40 if line.startswith("~~~~"):
41 for cname in collections:
42 if ("." + cname) in line:
47 # Define operator symbol-names here
49 tokens = ["non_star", "non_slash", "non_eol",
50 "non_single_quote", "non_double_quote", "ident" ]
104 "/*": "openblockcomment",
105 "*/": "closeblockcomment",
106 "macro_rules": "macro_rules",
114 for line in collections["gram"]:
116 for word in line.split():
117 # replace strings with keyword-names or symbol-names from table
118 if word.startswith("\""):
121 word = symnames[word]
125 raise Exception("non-alpha apparent keyword: "
127 if word not in tokens:
128 if (word in collections["keyword"] or
129 word in collections["reserved"]):
132 raise Exception("unknown keyword/reserved word: "
139 for word in collections["keyword"] + collections["reserved"]:
140 if word not in tokens:
143 for sym in collections["unop"] + collections["binop"] + symnames.keys():
145 if word not in tokens:
149 print("%start parser, token;")
150 print("%%token %s ;" % ("\n\t, ".join(tokens)))
151 for coll in ["keyword", "reserved"]:
152 print("%s: %s ; " % (coll, "\n\t| ".join(collections[coll])));
153 for coll in ["binop", "unop"]:
154 print("%s: %s ; " % (coll, "\n\t| ".join([symnames[x]
155 for x in collections[coll]])));
156 print("\n".join(lines));