]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/lexer/unicode_chars.rs
Rollup merge of #30020 - Manishearth:unit, r=bluss
[rust.git] / src / libsyntax / parse / lexer / unicode_chars.rs
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.
4 //
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.
10
11 // Characters and their corresponding confusables were collected from
12 // http://www.unicode.org/Public/security/revision-06/confusables.txt
13
14 use codemap::mk_sp as make_span;
15 use super::StringReader;
16
17 const UNICODE_ARRAY: &'static [(char, &'static str, char)] = &[
18     ('ߺ', "Nko Lajanyalan", '_'),
19     ('﹍', "Dashed Low Line", '_'),
20     ('﹎', "Centreline Low Line", '_'),
21     ('﹏', "Wavy Low Line", '_'),
22     ('‐', "Hyphen", '-'),
23     ('‑', "Non-Breaking Hyphen", '-'),
24     ('‒', "Figure Dash", '-'),
25     ('–', "En Dash", '-'),
26     ('﹘', "Small Em Dash", '-'),
27     ('⁃', "Hyphen Bullet", '-'),
28     ('˗', "Modifier Letter Minus Sign", '-'),
29     ('−', "Minus Sign", '-'),
30     ('٫', "Arabic Decimal Separator", ','),
31     ('‚', "Single Low-9 Quotation Mark", ','),
32     ('ꓹ', "Lisu Letter Tone Na Po", ','),
33     (';', "Greek Question Mark", ';'),
34     ('ः', "Devanagari Sign Visarga", ':'),
35     ('ઃ', "Gujarati Sign Visarga", ':'),
36     (':', "Fullwidth Colon", ':'),
37     ('։', "Armenian Full Stop", ':'),
38     ('܃', "Syriac Supralinear Colon", ':'),
39     ('܄', "Syriac Sublinear Colon", ':'),
40     ('︰', "Presentation Form For Vertical Two Dot Leader", ':'),
41     ('᠃', "Mongolian Full Stop", ':'),
42     ('᠉', "Mongolian Manchu Full Stop", ':'),
43     ('⁚', "Two Dot Punctuation", ':'),
44     ('׃', "Hebrew Punctuation Sof Pasuq", ':'),
45     ('˸', "Modifier Letter Raised Colon", ':'),
46     ('꞉', "Modifier Letter Colon", ':'),
47     ('∶', "Ratio", ':'),
48     ('ː', "Modifier Letter Triangular Colon", ':'),
49     ('ꓽ', "Lisu Letter Tone Mya Jeu", ':'),
50     ('!', "Fullwidth Exclamation Mark", '!'),
51     ('ǃ', "Latin Letter Retroflex Click", '!'),
52     ('ʔ', "Latin Letter Glottal Stop", '?'),
53     ('ॽ', "Devanagari Letter Glottal Stop", '?'),
54     ('Ꭾ', "Cherokee Letter He", '?'),
55     ('𝅭', "Musical Symbol Combining Augmentation Dot", '.'),
56     ('․', "One Dot Leader", '.'),
57     ('۔', "Arabic Full Stop", '.'),
58     ('܁', "Syriac Supralinear Full Stop", '.'),
59     ('܂', "Syriac Sublinear Full Stop", '.'),
60     ('꘎', "Vai Full Stop", '.'),
61     ('𐩐', "Kharoshthi Punctuation Dot", '.'),
62     ('٠', "Arabic-Indic Digit Zero", '.'),
63     ('۰', "Extended Arabic-Indic Digit Zero", '.'),
64     ('ꓸ', "Lisu Letter Tone Mya Ti", '.'),
65     ('՝', "Armenian Comma", '\''),
66     (''', "Fullwidth Apostrophe", '\''),
67     ('‘', "Left Single Quotation Mark", '\''),
68     ('’', "Right Single Quotation Mark", '\''),
69     ('‛', "Single High-Reversed-9 Quotation Mark", '\''),
70     ('′', "Prime", '\''),
71     ('‵', "Reversed Prime", '\''),
72     ('՚', "Armenian Apostrophe", '\''),
73     ('׳', "Hebrew Punctuation Geresh", '\''),
74     ('`', "Greek Varia", '\''),
75     ('`', "Fullwidth Grave Accent", '\''),
76     ('΄', "Greek Tonos", '\''),
77     ('´', "Greek Oxia", '\''),
78     ('᾽', "Greek Koronis", '\''),
79     ('᾿', "Greek Psili", '\''),
80     ('῾', "Greek Dasia", '\''),
81     ('ʹ', "Modifier Letter Prime", '\''),
82     ('ʹ', "Greek Numeral Sign", '\''),
83     ('ˊ', "Modifier Letter Acute Accent", '\''),
84     ('ˋ', "Modifier Letter Grave Accent", '\''),
85     ('˴', "Modifier Letter Middle Grave Accent", '\''),
86     ('ʻ', "Modifier Letter Turned Comma", '\''),
87     ('ʽ', "Modifier Letter Reversed Comma", '\''),
88     ('ʼ', "Modifier Letter Apostrophe", '\''),
89     ('ʾ', "Modifier Letter Right Half Ring", '\''),
90     ('ꞌ', "Latin Small Letter Saltillo", '\''),
91     ('י', "Hebrew Letter Yod", '\''),
92     ('ߴ', "Nko High Tone Apostrophe", '\''),
93     ('ߵ', "Nko Low Tone Apostrophe", '\''),
94     ('"', "Fullwidth Quotation Mark", '"'),
95     ('“', "Left Double Quotation Mark", '"'),
96     ('”', "Right Double Quotation Mark", '"'),
97     ('‟', "Double High-Reversed-9 Quotation Mark", '"'),
98     ('″', "Double Prime", '"'),
99     ('‶', "Reversed Double Prime", '"'),
100     ('〃', "Ditto Mark", '"'),
101     ('״', "Hebrew Punctuation Gershayim", '"'),
102     ('˝', "Double Acute Accent", '"'),
103     ('ʺ', "Modifier Letter Double Prime", '"'),
104     ('˶', "Modifier Letter Middle Double Acute Accent", '"'),
105     ('˵', "Modifier Letter Middle Double Grave Accent", '"'),
106     ('ˮ', "Modifier Letter Double Apostrophe", '"'),
107     ('ײ', "Hebrew Ligature Yiddish Double Yod", '"'),
108     ('❞', "Heavy Double Comma Quotation Mark Ornament", '"'),
109     ('❝', "Heavy Double Turned Comma Quotation Mark Ornament", '"'),
110     ('[', "Fullwidth Left Square Bracket", '('),
111     ('❨', "Medium Left Parenthesis Ornament", '('),
112     ('❲', "Light Left Tortoise Shell Bracket Ornament", '('),
113     ('〔', "Left Tortoise Shell Bracket", '('),
114     ('﴾', "Ornate Left Parenthesis", '('),
115     (']', "Fullwidth Right Square Bracket", ')'),
116     ('❩', "Medium Right Parenthesis Ornament", ')'),
117     ('❳', "Light Right Tortoise Shell Bracket Ornament", ')'),
118     ('〕', "Right Tortoise Shell Bracket", ')'),
119     ('﴿', "Ornate Right Parenthesis", ')'),
120     ('❴', "Medium Left Curly Bracket Ornament", '{'),
121     ('❵', "Medium Right Curly Bracket Ornament", '}'),
122     ('⁎', "Low Asterisk", '*'),
123     ('٭', "Arabic Five Pointed Star", '*'),
124     ('∗', "Asterisk Operator", '*'),
125     ('᜵', "Philippine Single Punctuation", '/'),
126     ('⁁', "Caret Insertion Point", '/'),
127     ('∕', "Division Slash", '/'),
128     ('⁄', "Fraction Slash", '/'),
129     ('╱', "Box Drawings Light Diagonal Upper Right To Lower Left", '/'),
130     ('⟋', "Mathematical Rising Diagonal", '/'),
131     ('⧸', "Big Solidus", '/'),
132     ('㇓', "Cjk Stroke Sp", '/'),
133     ('〳', "Vertical Kana Repeat Mark Upper Half", '/'),
134     ('丿', "Cjk Unified Ideograph-4E3F", '/'),
135     ('⼃', "Kangxi Radical Slash", '/'),
136     ('\', "Fullwidth Reverse Solidus", '\\'),
137     ('﹨', "Small Reverse Solidus", '\\'),
138     ('∖', "Set Minus", '\\'),
139     ('⟍', "Mathematical Falling Diagonal", '\\'),
140     ('⧵', "Reverse Solidus Operator", '\\'),
141     ('⧹', "Big Reverse Solidus", '\\'),
142     ('㇔', "Cjk Stroke D", '\\'),
143     ('丶', "Cjk Unified Ideograph-4E36", '\\'),
144     ('⼂', "Kangxi Radical Dot", '\\'),
145     ('ꝸ', "Latin Small Letter Um", '&'),
146     ('﬩', "Hebrew Letter Alternative Plus Sign", '+'),
147     ('‹', "Single Left-Pointing Angle Quotation Mark", '<'),
148     ('❮', "Heavy Left-Pointing Angle Quotation Mark Ornament", '<'),
149     ('˂', "Modifier Letter Left Arrowhead", '<'),
150     ('꓿', "Lisu Punctuation Full Stop", '='),
151     ('›', "Single Right-Pointing Angle Quotation Mark", '>'),
152     ('❯', "Heavy Right-Pointing Angle Quotation Mark Ornament", '>'),
153     ('˃', "Modifier Letter Right Arrowhead", '>'),
154     ('Ⲻ', "Coptic Capital Letter Dialect-P Ni", '-'),
155     ('Ɂ', "Latin Capital Letter Glottal Stop", '?'),
156     ('Ⳇ', "Coptic Capital Letter Old Coptic Esh", '/'), ];
157
158 const ASCII_ARRAY: &'static [(char, &'static str)] = &[
159     ('_', "Underscore"),
160     ('-', "Minus/Hyphen"),
161     (',', "Comma"),
162     (';', "Semicolon"),
163     (':', "Colon"),
164     ('!', "Exclamation Mark"),
165     ('?', "Question Mark"),
166     ('.', "Period"),
167     ('\'', "Single Quote"),
168     ('"', "Quotation Mark"),
169     ('(', "Left Parenthesis"),
170     (')', "Right Parenthesis"),
171     ('{', "Left Curly Brace"),
172     ('}', "Right Curly Brace"),
173     ('*', "Asterisk"),
174     ('/', "Slash"),
175     ('\\', "Backslash"),
176     ('&', "Ampersand"),
177     ('+', "Plus Sign"),
178     ('<', "Less-Than Sign"),
179     ('=', "Equals Sign"),
180     ('>', "Greater-Than Sign"), ];
181
182 pub fn check_for_substitution(reader: &StringReader, ch: char) {
183     UNICODE_ARRAY
184     .iter()
185     .find(|&&(c, _, _)| c == ch)
186     .map(|&(_, u_name, ascii_char)| {
187         let span = make_span(reader.last_pos, reader.pos);
188         match ASCII_ARRAY.iter().find(|&&(c, _)| c == ascii_char) {
189             Some(&(ascii_char, ascii_name)) => {
190                 let msg =
191                     format!("unicode character '{}' ({}) looks much like '{}' ({}), but it's not",
192                             ch, u_name, ascii_char, ascii_name);
193                 reader.help_span(span, &msg);
194             },
195             None => {
196                 reader
197                 .span_diagnostic
198                 .span_bug_no_panic(span,
199                                    &format!("substitution character not found for '{}'", ch));
200             }
201         }
202     });
203 }