1 # Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
11 # This script is for extracting the grammar from the rust docs.
15 collections = { "gram": [],
25 for line in fileinput.input(openhook=fileinput.hook_encoded("utf-8")):
27 if line.startswith("~~~~"):
30 if coll in ["keyword", "reserved", "binop", "unop"]:
31 for word in line.split():
32 if word not in collections[coll]:
33 collections[coll].append(word)
35 collections[coll].append(line)
38 if line.startswith("~~~~"):
39 for cname in collections:
40 if ("." + cname) in line:
45 # Define operator symbol-names here
47 tokens = ["non_star", "non_slash", "non_eol",
48 "non_single_quote", "non_double_quote", "ident" ]
102 "/*": "openblockcomment",
103 "*/": "closeblockcomment",
104 "macro_rules": "macro_rules",
112 for line in collections["gram"]:
114 for word in line.split():
115 # replace strings with keyword-names or symbol-names from table
116 if word.startswith("\""):
119 word = symnames[word]
123 raise Exception("non-alpha apparent keyword: "
125 if word not in tokens:
126 if (word in collections["keyword"] or
127 word in collections["reserved"]):
130 raise Exception("unknown keyword/reserved word: "
137 for word in collections["keyword"] + collections["reserved"]:
138 if word not in tokens:
141 for sym in collections["unop"] + collections["binop"] + symnames.keys():
143 if word not in tokens:
147 print("%start parser, token;")
148 print("%%token %s ;" % ("\n\t, ".join(tokens)))
149 for coll in ["keyword", "reserved"]:
150 print("%s: %s ; " % (coll, "\n\t| ".join(collections[coll])));
151 for coll in ["binop", "unop"]:
152 print("%s: %s ; " % (coll, "\n\t| ".join([symnames[x]
153 for x in collections[coll]])));
154 print("\n".join(lines));