mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Auto merge of #33860 - doomrobo:fix-grammar-verification, r=nagisa
antlr grammar verification script now compiles under latest nightly This is kind of a moving target, since none of libsyntax is stable, but at least this compiles for the time being.
This commit is contained in:
commit
6e00b55568
@ -8,7 +8,7 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#![feature(plugin, rustc_private, str_char, collections)]
|
#![feature(plugin, rustc_private)]
|
||||||
|
|
||||||
extern crate syntax;
|
extern crate syntax;
|
||||||
extern crate rustc;
|
extern crate rustc;
|
||||||
@ -24,6 +24,7 @@ use std::path::Path;
|
|||||||
|
|
||||||
use syntax::parse;
|
use syntax::parse;
|
||||||
use syntax::parse::lexer;
|
use syntax::parse::lexer;
|
||||||
|
use rustc::dep_graph::DepGraph;
|
||||||
use rustc::session::{self, config};
|
use rustc::session::{self, config};
|
||||||
use rustc::middle::cstore::DummyCrateStore;
|
use rustc::middle::cstore::DummyCrateStore;
|
||||||
|
|
||||||
@ -32,17 +33,17 @@ use syntax::ast;
|
|||||||
use syntax::ast::Name;
|
use syntax::ast::Name;
|
||||||
use syntax::codemap;
|
use syntax::codemap;
|
||||||
use syntax::codemap::Pos;
|
use syntax::codemap::Pos;
|
||||||
use syntax::parse::token;
|
use syntax::parse::token::{self, BinOpToken, DelimToken, Lit, Token};
|
||||||
use syntax::parse::lexer::TokenAndSpan;
|
use syntax::parse::lexer::TokenAndSpan;
|
||||||
|
|
||||||
fn parse_token_list(file: &str) -> HashMap<String, token::Token> {
|
fn parse_token_list(file: &str) -> HashMap<String, token::Token> {
|
||||||
fn id() -> token::Token {
|
fn id() -> token::Token {
|
||||||
token::Ident(ast::Ident::with_empty_ctxt(Name(0)), token::Plain)
|
Token::Ident(ast::Ident::with_empty_ctxt(Name(0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut res = HashMap::new();
|
let mut res = HashMap::new();
|
||||||
|
|
||||||
res.insert("-1".to_string(), token::Eof);
|
res.insert("-1".to_string(), Token::Eof);
|
||||||
|
|
||||||
for line in file.split('\n') {
|
for line in file.split('\n') {
|
||||||
let eq = match line.trim().rfind('=') {
|
let eq = match line.trim().rfind('=') {
|
||||||
@ -54,65 +55,65 @@ fn parse_token_list(file: &str) -> HashMap<String, token::Token> {
|
|||||||
let num = &line[eq + 1..];
|
let num = &line[eq + 1..];
|
||||||
|
|
||||||
let tok = match val {
|
let tok = match val {
|
||||||
"SHR" => token::BinOp(token::Shr),
|
"SHR" => Token::BinOp(BinOpToken::Shr),
|
||||||
"DOLLAR" => token::Dollar,
|
"DOLLAR" => Token::Dollar,
|
||||||
"LT" => token::Lt,
|
"LT" => Token::Lt,
|
||||||
"STAR" => token::BinOp(token::Star),
|
"STAR" => Token::BinOp(BinOpToken::Star),
|
||||||
"FLOAT_SUFFIX" => id(),
|
"FLOAT_SUFFIX" => id(),
|
||||||
"INT_SUFFIX" => id(),
|
"INT_SUFFIX" => id(),
|
||||||
"SHL" => token::BinOp(token::Shl),
|
"SHL" => Token::BinOp(BinOpToken::Shl),
|
||||||
"LBRACE" => token::OpenDelim(token::Brace),
|
"LBRACE" => Token::OpenDelim(DelimToken::Brace),
|
||||||
"RARROW" => token::RArrow,
|
"RARROW" => Token::RArrow,
|
||||||
"LIT_STR" => token::Literal(token::Str_(Name(0)), None),
|
"LIT_STR" => Token::Literal(Lit::Str_(Name(0)), None),
|
||||||
"DOTDOT" => token::DotDot,
|
"DOTDOT" => Token::DotDot,
|
||||||
"MOD_SEP" => token::ModSep,
|
"MOD_SEP" => Token::ModSep,
|
||||||
"DOTDOTDOT" => token::DotDotDot,
|
"DOTDOTDOT" => Token::DotDotDot,
|
||||||
"NOT" => token::Not,
|
"NOT" => Token::Not,
|
||||||
"AND" => token::BinOp(token::And),
|
"AND" => Token::BinOp(BinOpToken::And),
|
||||||
"LPAREN" => token::OpenDelim(token::Paren),
|
"LPAREN" => Token::OpenDelim(DelimToken::Paren),
|
||||||
"ANDAND" => token::AndAnd,
|
"ANDAND" => Token::AndAnd,
|
||||||
"AT" => token::At,
|
"AT" => Token::At,
|
||||||
"LBRACKET" => token::OpenDelim(token::Bracket),
|
"LBRACKET" => Token::OpenDelim(DelimToken::Bracket),
|
||||||
"LIT_STR_RAW" => token::Literal(token::StrRaw(Name(0), 0), None),
|
"LIT_STR_RAW" => Token::Literal(Lit::StrRaw(Name(0), 0), None),
|
||||||
"RPAREN" => token::CloseDelim(token::Paren),
|
"RPAREN" => Token::CloseDelim(DelimToken::Paren),
|
||||||
"SLASH" => token::BinOp(token::Slash),
|
"SLASH" => Token::BinOp(BinOpToken::Slash),
|
||||||
"COMMA" => token::Comma,
|
"COMMA" => Token::Comma,
|
||||||
"LIFETIME" => token::Lifetime(ast::Ident::with_empty_ctxt(Name(0))),
|
"LIFETIME" => Token::Lifetime(ast::Ident::with_empty_ctxt(Name(0))),
|
||||||
"CARET" => token::BinOp(token::Caret),
|
"CARET" => Token::BinOp(BinOpToken::Caret),
|
||||||
"TILDE" => token::Tilde,
|
"TILDE" => Token::Tilde,
|
||||||
"IDENT" => id(),
|
"IDENT" => id(),
|
||||||
"PLUS" => token::BinOp(token::Plus),
|
"PLUS" => Token::BinOp(BinOpToken::Plus),
|
||||||
"LIT_CHAR" => token::Literal(token::Char(Name(0)), None),
|
"LIT_CHAR" => Token::Literal(Lit::Char(Name(0)), None),
|
||||||
"LIT_BYTE" => token::Literal(token::Byte(Name(0)), None),
|
"LIT_BYTE" => Token::Literal(Lit::Byte(Name(0)), None),
|
||||||
"EQ" => token::Eq,
|
"EQ" => Token::Eq,
|
||||||
"RBRACKET" => token::CloseDelim(token::Bracket),
|
"RBRACKET" => Token::CloseDelim(DelimToken::Bracket),
|
||||||
"COMMENT" => token::Comment,
|
"COMMENT" => Token::Comment,
|
||||||
"DOC_COMMENT" => token::DocComment(Name(0)),
|
"DOC_COMMENT" => Token::DocComment(Name(0)),
|
||||||
"DOT" => token::Dot,
|
"DOT" => Token::Dot,
|
||||||
"EQEQ" => token::EqEq,
|
"EQEQ" => Token::EqEq,
|
||||||
"NE" => token::Ne,
|
"NE" => Token::Ne,
|
||||||
"GE" => token::Ge,
|
"GE" => Token::Ge,
|
||||||
"PERCENT" => token::BinOp(token::Percent),
|
"PERCENT" => Token::BinOp(BinOpToken::Percent),
|
||||||
"RBRACE" => token::CloseDelim(token::Brace),
|
"RBRACE" => Token::CloseDelim(DelimToken::Brace),
|
||||||
"BINOP" => token::BinOp(token::Plus),
|
"BINOP" => Token::BinOp(BinOpToken::Plus),
|
||||||
"POUND" => token::Pound,
|
"POUND" => Token::Pound,
|
||||||
"OROR" => token::OrOr,
|
"OROR" => Token::OrOr,
|
||||||
"LIT_INTEGER" => token::Literal(token::Integer(Name(0)), None),
|
"LIT_INTEGER" => Token::Literal(Lit::Integer(Name(0)), None),
|
||||||
"BINOPEQ" => token::BinOpEq(token::Plus),
|
"BINOPEQ" => Token::BinOpEq(BinOpToken::Plus),
|
||||||
"LIT_FLOAT" => token::Literal(token::Float(Name(0)), None),
|
"LIT_FLOAT" => Token::Literal(Lit::Float(Name(0)), None),
|
||||||
"WHITESPACE" => token::Whitespace,
|
"WHITESPACE" => Token::Whitespace,
|
||||||
"UNDERSCORE" => token::Underscore,
|
"UNDERSCORE" => Token::Underscore,
|
||||||
"MINUS" => token::BinOp(token::Minus),
|
"MINUS" => Token::BinOp(BinOpToken::Minus),
|
||||||
"SEMI" => token::Semi,
|
"SEMI" => Token::Semi,
|
||||||
"COLON" => token::Colon,
|
"COLON" => Token::Colon,
|
||||||
"FAT_ARROW" => token::FatArrow,
|
"FAT_ARROW" => Token::FatArrow,
|
||||||
"OR" => token::BinOp(token::Or),
|
"OR" => Token::BinOp(BinOpToken::Or),
|
||||||
"GT" => token::Gt,
|
"GT" => Token::Gt,
|
||||||
"LE" => token::Le,
|
"LE" => Token::Le,
|
||||||
"LIT_BYTE_STR" => token::Literal(token::ByteStr(Name(0)), None),
|
"LIT_BINARY" => Token::Literal(Lit::ByteStr(Name(0)), None),
|
||||||
"LIT_BYTE_STR_RAW" => token::Literal(token::ByteStrRaw(Name(0), 0), None),
|
"LIT_BINARY_RAW" => Token::Literal(Lit::ByteStrRaw(Name(0), 0), None),
|
||||||
"QUESTION" => token::Question,
|
"QUESTION" => Token::Question,
|
||||||
"SHEBANG" => token::Shebang(Name(0)),
|
"SHEBANG" => Token::Shebang(Name(0)),
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -125,16 +126,16 @@ fn parse_token_list(file: &str) -> HashMap<String, token::Token> {
|
|||||||
|
|
||||||
fn str_to_binop(s: &str) -> token::BinOpToken {
|
fn str_to_binop(s: &str) -> token::BinOpToken {
|
||||||
match s {
|
match s {
|
||||||
"+" => token::Plus,
|
"+" => BinOpToken::Plus,
|
||||||
"/" => token::Slash,
|
"/" => BinOpToken::Slash,
|
||||||
"-" => token::Minus,
|
"-" => BinOpToken::Minus,
|
||||||
"*" => token::Star,
|
"*" => BinOpToken::Star,
|
||||||
"%" => token::Percent,
|
"%" => BinOpToken::Percent,
|
||||||
"^" => token::Caret,
|
"^" => BinOpToken::Caret,
|
||||||
"&" => token::And,
|
"&" => BinOpToken::And,
|
||||||
"|" => token::Or,
|
"|" => BinOpToken::Or,
|
||||||
"<<" => token::Shl,
|
"<<" => BinOpToken::Shl,
|
||||||
">>" => token::Shr,
|
">>" => BinOpToken::Shr,
|
||||||
_ => panic!("Bad binop str `{}`", s),
|
_ => panic!("Bad binop str `{}`", s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,13 +143,14 @@ fn str_to_binop(s: &str) -> token::BinOpToken {
|
|||||||
/// Assuming a string/byte string literal, strip out the leading/trailing
|
/// Assuming a string/byte string literal, strip out the leading/trailing
|
||||||
/// hashes and surrounding quotes/raw/byte prefix.
|
/// hashes and surrounding quotes/raw/byte prefix.
|
||||||
fn fix(mut lit: &str) -> ast::Name {
|
fn fix(mut lit: &str) -> ast::Name {
|
||||||
if lit.char_at(0) == 'r' {
|
let prefix: Vec<char> = lit.chars().take(2).collect();
|
||||||
if lit.char_at(1) == 'b' {
|
if prefix[0] == 'r' {
|
||||||
|
if prefix[1] == 'b' {
|
||||||
lit = &lit[2..]
|
lit = &lit[2..]
|
||||||
} else {
|
} else {
|
||||||
lit = &lit[1..];
|
lit = &lit[1..];
|
||||||
}
|
}
|
||||||
} else if lit.char_at(0) == 'b' {
|
} else if prefix[0] == 'b' {
|
||||||
lit = &lit[1..];
|
lit = &lit[1..];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +162,8 @@ fn fix(mut lit: &str) -> ast::Name {
|
|||||||
|
|
||||||
/// Assuming a char/byte literal, strip the 'b' prefix and the single quotes.
|
/// Assuming a char/byte literal, strip the 'b' prefix and the single quotes.
|
||||||
fn fixchar(mut lit: &str) -> ast::Name {
|
fn fixchar(mut lit: &str) -> ast::Name {
|
||||||
if lit.char_at(0) == 'b' {
|
let prefix = lit.chars().next().unwrap();
|
||||||
|
if prefix == 'b' {
|
||||||
lit = &lit[1..];
|
lit = &lit[1..];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,26 +200,25 @@ fn parse_antlr_token(s: &str, tokens: &HashMap<String, token::Token>, surrogate_
|
|||||||
debug!("What we got: content (`{}`), proto: {:?}", content, proto_tok);
|
debug!("What we got: content (`{}`), proto: {:?}", content, proto_tok);
|
||||||
|
|
||||||
let real_tok = match *proto_tok {
|
let real_tok = match *proto_tok {
|
||||||
token::BinOp(..) => token::BinOp(str_to_binop(content)),
|
Token::BinOp(..) => Token::BinOp(str_to_binop(content)),
|
||||||
token::BinOpEq(..) => token::BinOpEq(str_to_binop(&content[..content.len() - 1])),
|
Token::BinOpEq(..) => Token::BinOpEq(str_to_binop(&content[..content.len() - 1])),
|
||||||
token::Literal(token::Str_(..), n) => token::Literal(token::Str_(fix(content)), n),
|
Token::Literal(Lit::Str_(..), n) => Token::Literal(Lit::Str_(fix(content)), n),
|
||||||
token::Literal(token::StrRaw(..), n) => token::Literal(token::StrRaw(fix(content),
|
Token::Literal(Lit::StrRaw(..), n) => Token::Literal(Lit::StrRaw(fix(content),
|
||||||
count(content)), n),
|
count(content)), n),
|
||||||
token::Literal(token::Char(..), n) => token::Literal(token::Char(fixchar(content)), n),
|
Token::Literal(Lit::Char(..), n) => Token::Literal(Lit::Char(fixchar(content)), n),
|
||||||
token::Literal(token::Byte(..), n) => token::Literal(token::Byte(fixchar(content)), n),
|
Token::Literal(Lit::Byte(..), n) => Token::Literal(Lit::Byte(fixchar(content)), n),
|
||||||
token::DocComment(..) => token::DocComment(nm),
|
Token::DocComment(..) => Token::DocComment(nm),
|
||||||
token::Literal(token::Integer(..), n) => token::Literal(token::Integer(nm), n),
|
Token::Literal(Lit::Integer(..), n) => Token::Literal(Lit::Integer(nm), n),
|
||||||
token::Literal(token::Float(..), n) => token::Literal(token::Float(nm), n),
|
Token::Literal(Lit::Float(..), n) => Token::Literal(Lit::Float(nm), n),
|
||||||
token::Literal(token::ByteStr(..), n) => token::Literal(token::ByteStr(nm), n),
|
Token::Literal(Lit::ByteStr(..), n) => Token::Literal(Lit::ByteStr(nm), n),
|
||||||
token::Literal(token::ByteStrRaw(..), n) => token::Literal(token::ByteStrRaw(fix(content),
|
Token::Literal(Lit::ByteStrRaw(..), n) => Token::Literal(Lit::ByteStrRaw(fix(content),
|
||||||
count(content)), n),
|
count(content)), n),
|
||||||
token::Ident(..) => token::Ident(ast::Ident::with_empty_ctxt(nm),
|
Token::Ident(..) => Token::Ident(ast::Ident::with_empty_ctxt(nm)),
|
||||||
token::ModName),
|
Token::Lifetime(..) => Token::Lifetime(ast::Ident::with_empty_ctxt(nm)),
|
||||||
token::Lifetime(..) => token::Lifetime(ast::Ident::with_empty_ctxt(nm)),
|
|
||||||
ref t => t.clone()
|
ref t => t.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
let start_offset = if real_tok == token::Eof {
|
let start_offset = if real_tok == Token::Eof {
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
@ -245,8 +247,8 @@ fn parse_antlr_token(s: &str, tokens: &HashMap<String, token::Token>, surrogate_
|
|||||||
|
|
||||||
fn tok_cmp(a: &token::Token, b: &token::Token) -> bool {
|
fn tok_cmp(a: &token::Token, b: &token::Token) -> bool {
|
||||||
match a {
|
match a {
|
||||||
&token::Ident(id, _) => match b {
|
&Token::Ident(id) => match b {
|
||||||
&token::Ident(id2, _) => id == id2,
|
&Token::Ident(id2) => id == id2,
|
||||||
_ => false
|
_ => false
|
||||||
},
|
},
|
||||||
_ => a == b
|
_ => a == b
|
||||||
@ -287,7 +289,7 @@ fn main() {
|
|||||||
debug!("Pairs: {:?}", surrogate_pairs_pos);
|
debug!("Pairs: {:?}", surrogate_pairs_pos);
|
||||||
|
|
||||||
let options = config::basic_options();
|
let options = config::basic_options();
|
||||||
let session = session::build_session(options, None,
|
let session = session::build_session(options, &DepGraph::new(false), None,
|
||||||
syntax::diagnostics::registry::Registry::new(&[]),
|
syntax::diagnostics::registry::Registry::new(&[]),
|
||||||
Rc::new(DummyCrateStore));
|
Rc::new(DummyCrateStore));
|
||||||
let filemap = session.parse_sess.codemap().new_filemap(String::from("<n/a>"), code);
|
let filemap = session.parse_sess.codemap().new_filemap(String::from("<n/a>"), code);
|
||||||
@ -310,7 +312,7 @@ fn main() {
|
|||||||
|
|
||||||
for antlr_tok in antlr_tokens {
|
for antlr_tok in antlr_tokens {
|
||||||
let rustc_tok = next(&mut lexer);
|
let rustc_tok = next(&mut lexer);
|
||||||
if rustc_tok.tok == token::Eof && antlr_tok.tok == token::Eof {
|
if rustc_tok.tok == Token::Eof && antlr_tok.tok == Token::Eof {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,19 +339,19 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
matches!(
|
matches!(
|
||||||
token::Literal(token::Byte(..), _),
|
Token::Literal(Lit::Byte(..), _),
|
||||||
token::Literal(token::Char(..), _),
|
Token::Literal(Lit::Char(..), _),
|
||||||
token::Literal(token::Integer(..), _),
|
Token::Literal(Lit::Integer(..), _),
|
||||||
token::Literal(token::Float(..), _),
|
Token::Literal(Lit::Float(..), _),
|
||||||
token::Literal(token::Str_(..), _),
|
Token::Literal(Lit::Str_(..), _),
|
||||||
token::Literal(token::StrRaw(..), _),
|
Token::Literal(Lit::StrRaw(..), _),
|
||||||
token::Literal(token::ByteStr(..), _),
|
Token::Literal(Lit::ByteStr(..), _),
|
||||||
token::Literal(token::ByteStrRaw(..), _),
|
Token::Literal(Lit::ByteStrRaw(..), _),
|
||||||
token::Ident(..),
|
Token::Ident(..),
|
||||||
token::Lifetime(..),
|
Token::Lifetime(..),
|
||||||
token::Interpolated(..),
|
Token::Interpolated(..),
|
||||||
token::DocComment(..),
|
Token::DocComment(..),
|
||||||
token::Shebang(..)
|
Token::Shebang(..)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user