@@ -108,13 +108,10 @@ fn parse_token_list(file: &str) -> HashMap<String, Token> {
108
108
res
109
109
}
110
110
111
- fn str_to_binop ( mut s : & str ) -> BinOp {
112
- if s. ends_with ( "'" ) {
113
- s = s. slice_to ( s. len ( ) - 1 ) ;
114
- }
115
-
111
+ fn str_to_binop ( s : & str ) -> BinOp {
116
112
match s {
117
113
"+" => PLUS ,
114
+ "/" => SLASH ,
118
115
"-" => MINUS ,
119
116
"*" => STAR ,
120
117
"%" => PERCENT ,
@@ -123,12 +120,35 @@ fn str_to_binop(mut s: &str) -> BinOp {
123
120
"|" => OR ,
124
121
"<<" => SHL ,
125
122
">>" => SHR ,
126
- _ => fail ! ( "Bad binop str {}" , s)
123
+ _ => fail ! ( "Bad binop str `{}`" , s)
124
+ }
125
+ }
126
+
127
+ /// Assuming a raw string/binary literal, strip out the leading/trailing
128
+ /// hashes and surrounding quotes/raw/binary prefix.
129
+ fn fix ( mut lit : & str ) -> ast:: Name {
130
+ if lit. char_at ( 0 ) == 'r' {
131
+ if lit. char_at ( 1 ) == 'b' {
132
+ lit = lit. slice_from ( 2 )
133
+ } else {
134
+ lit = lit. slice_from ( 1 ) ;
135
+ }
136
+ } else if lit. char_at ( 0 ) == 'b' {
137
+ lit = lit. slice_from ( 1 ) ;
127
138
}
139
+
140
+ let leading_hashes = count ( lit) ;
141
+
142
+ // +1/-1 to adjust for single quotes
143
+ parse:: token:: intern ( lit. slice ( leading_hashes + 1 , lit. len ( ) - leading_hashes - 1 ) )
144
+ }
145
+
146
+ fn count ( lit : & str ) -> uint {
147
+ lit. chars ( ) . take_while ( |c| * c == '#' ) . count ( )
128
148
}
129
149
130
150
fn parse_antlr_token ( s : & str , tokens : & HashMap < String , Token > ) -> TokenAndSpan {
131
- let re = regex ! ( r"\[@(?P<seq>\d+),(?P<start>\d+):(?P<end>\d+)='(?P<content>.+?),<(?P<toknum>-?\d+)>,\d+:\d+]" ) ;
151
+ let re = regex ! ( r"\[@(?P<seq>\d+),(?P<start>\d+):(?P<end>\d+)='(?P<content>.+?)' ,<(?P<toknum>-?\d+)>,\d+:\d+]" ) ;
132
152
133
153
let m = re. captures ( s) . expect ( format ! ( "The regex didn't match {}" , s) . as_slice ( ) ) ;
134
154
let start = m. name ( "start" ) ;
@@ -137,9 +157,24 @@ fn parse_antlr_token(s: &str, tokens: &HashMap<String, Token>) -> TokenAndSpan {
137
157
let content = m. name ( "content" ) ;
138
158
139
159
let proto_tok = tokens. find_equiv ( & toknum) . expect ( format ! ( "didn't find token {} in the map" , toknum) . as_slice ( ) ) ;
160
+
161
+ let nm = parse:: token:: intern ( content) ;
162
+
163
+ debug ! ( "What we got: content (`{}`), proto: {}" , content, proto_tok) ;
164
+
140
165
let real_tok = match * proto_tok {
141
- BINOP ( PLUS ) => BINOP ( str_to_binop ( content) ) ,
142
- BINOPEQ ( PLUS ) => BINOPEQ ( str_to_binop ( content. slice_to ( content. len ( ) - 2 ) ) ) ,
166
+ BINOP ( ..) => BINOP ( str_to_binop ( content) ) ,
167
+ BINOPEQ ( ..) => BINOPEQ ( str_to_binop ( content. slice_to ( content. len ( ) - 1 ) ) ) ,
168
+ LIT_STR ( ..) => LIT_STR ( fix ( content) ) ,
169
+ LIT_STR_RAW ( ..) => LIT_STR_RAW ( fix ( content) , count ( content) ) ,
170
+ LIT_CHAR ( ..) => LIT_CHAR ( nm) ,
171
+ DOC_COMMENT ( ..) => DOC_COMMENT ( nm) ,
172
+ LIT_INTEGER ( ..) => LIT_INTEGER ( nm) ,
173
+ LIT_FLOAT ( ..) => LIT_FLOAT ( nm) ,
174
+ LIT_BINARY ( ..) => LIT_BINARY ( nm) ,
175
+ LIT_BINARY_RAW ( ..) => LIT_BINARY_RAW ( fix ( content) , count ( content) ) ,
176
+ IDENT ( ..) => IDENT ( ast:: Ident { name : nm, ctxt : 0 } , true ) ,
177
+ LIFETIME ( ..) => LIFETIME ( ast:: Ident { name : nm, ctxt : 0 } ) ,
143
178
ref t => t. clone ( )
144
179
} ;
145
180
@@ -161,6 +196,16 @@ fn parse_antlr_token(s: &str, tokens: &HashMap<String, Token>) -> TokenAndSpan {
161
196
}
162
197
}
163
198
199
+ fn tok_cmp ( a : & Token , b : & Token ) -> bool {
200
+ match a {
201
+ & IDENT ( id, _) => match b {
202
+ & IDENT ( id2, _) => id == id2,
203
+ _ => false
204
+ } ,
205
+ _ => a == b
206
+ }
207
+ }
208
+
164
209
fn main ( ) {
165
210
fn next ( r : & mut lexer:: StringReader ) -> TokenAndSpan {
166
211
use syntax:: parse:: lexer:: Reader ;
@@ -173,7 +218,8 @@ fn main() {
173
218
174
219
let code = File :: open ( & Path :: new ( std:: os:: args ( ) . get ( 1 ) . as_slice ( ) ) ) . unwrap ( ) . read_to_string ( ) . unwrap ( ) ;
175
220
let options = config:: basic_options ( ) ;
176
- let session = session:: build_session ( options, None ) ;
221
+ let session = session:: build_session ( options, None ,
222
+ syntax:: diagnostics:: registry:: Registry :: new ( [ ] ) ) ;
177
223
let filemap = parse:: string_to_filemap ( & session. parse_sess ,
178
224
code,
179
225
String :: from_str ( "<n/a>" ) ) ;
@@ -191,10 +237,16 @@ fn main() {
191
237
( $( $x: pat) ,+ ) => (
192
238
match rustc_tok. tok {
193
239
$( $x => match antlr_tok. tok {
194
- $x => ( ) ,
240
+ $x => {
241
+ if !tok_cmp( & rustc_tok. tok, & antlr_tok. tok) {
242
+ // FIXME #15677: needs more robust escaping in
243
+ // antlr
244
+ warn!( "Different names for {} and {}" , rustc_tok, antlr_tok) ;
245
+ }
246
+ }
195
247
_ => fail!( "{} is not {}" , antlr_tok, rustc_tok)
196
248
} , ) *
197
- ref c => assert!( c == antlr_tok. tok, "{} is not {}" , rustc_tok, antlr_tok)
249
+ ref c => assert!( c == & antlr_tok. tok, "{} is not {}" , rustc_tok, antlr_tok)
198
250
}
199
251
)
200
252
)
0 commit comments