Skip to content

Commit 1a4a679

Browse files
committed
Add proper XID_Start and XID_Continue rules and use CharPos for span comparison, closes rust-lang#15679
1 parent 896cb36 commit 1a4a679

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 */
@@ -194,7 +190,7 @@ LIT_STR_RAW
194190
: 'r' LIT_STR_RAW_INNER SUFFIX?
195191
;
196192

197-
IDENT : XID_start XID_continue* ;
193+
IDENT : XID_Start XID_Continue* ;
198194

199195
LIFETIME : '\'' IDENT ;
200196

src/grammar/verify.rs

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

2929
use syntax::ast;
3030
use syntax::ast::Name;
31+
use syntax::codemap::Pos;
3132
use syntax::parse::token;
3233
use syntax::parse::lexer::TokenAndSpan;
3334

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

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

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

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

271280
macro_rules! matches {

0 commit comments

Comments
 (0)