Skip to content

Commit be43713

Browse files
fhahnpczarn
authored andcommitted
Add proper XID_Start and XID_Continue rules and use CharPos for span comparison, closes rust-lang#15679
1 parent d86b6f6 commit be43713

File tree

4 files changed

+676
-7
lines changed

4 files changed

+676
-7
lines changed

src/grammar/RustLexer.g4

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ tokens {
1111
COMMENT
1212
}
1313

14-
/* Note: due to antlr limitations, we can't represent XID_start and
15-
* XID_continue properly. ASCII-only substitute. */
16-
17-
fragment XID_start : [_a-zA-Z] ;
18-
fragment XID_continue : [_a-zA-Z0-9] ;
14+
import xidstart , xidcontinue;
1915

2016

2117
/* Expression-operator symbols */
@@ -197,7 +193,7 @@ LIT_STR_RAW
197193

198194
QUESTION : '?';
199195

200-
IDENT : XID_start XID_continue* ;
196+
IDENT : XID_Start XID_Continue* ;
201197

202198
fragment QUESTION_IDENTIFIER : QUESTION? IDENT;
203199

src/grammar/verify.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc::session::{self, config};
2525

2626
use syntax::ast;
2727
use syntax::ast::Name;
28+
use syntax::codemap::Pos;
2829
use syntax::parse::token;
2930
use syntax::parse::lexer::TokenAndSpan;
3031

@@ -234,6 +235,13 @@ fn tok_cmp(a: &token::Token, b: &token::Token) -> bool {
234235
}
235236
}
236237

238+
fn span_cmp(rust_sp: syntax::codemap::Span, antlr_sp: syntax::codemap::Span, cm: &syntax::codemap::CodeMap) -> bool {
239+
println!("{} {}", cm.bytepos_to_file_charpos(rust_sp.lo).to_uint(), cm.bytepos_to_file_charpos(rust_sp.hi).to_uint());
240+
antlr_sp.lo.to_uint() == cm.bytepos_to_file_charpos(rust_sp.lo).to_uint() &&
241+
antlr_sp.hi.to_uint() == cm.bytepos_to_file_charpos(rust_sp.hi).to_uint() &&
242+
antlr_sp.expn_id == rust_sp.expn_id
243+
}
244+
237245
fn main() {
238246
fn next(r: &mut lexer::StringReader) -> TokenAndSpan {
239247
use syntax::parse::lexer::Reader;
@@ -259,14 +267,15 @@ fn main() {
259267
code,
260268
String::from_str("<n/a>"));
261269
let mut lexer = lexer::StringReader::new(session.diagnostic(), filemap);
270+
let ref cm = lexer.span_diagnostic.cm;
262271

263272
for antlr_tok in antlr_tokens {
264273
let rustc_tok = next(&mut lexer);
265274
if rustc_tok.tok == token::Eof && antlr_tok.tok == token::Eof {
266275
continue
267276
}
268277

269-
assert!(rustc_tok.sp == antlr_tok.sp, "{:?} and {:?} have different spans", rustc_tok,
278+
assert!(span_cmp(rustc_tok.sp, antlr_tok.sp, cm), "{:?} and {:?} have different spans", rustc_tok,
270279
antlr_tok);
271280

272281
macro_rules! matches {

0 commit comments

Comments
 (0)