parse: Use string literal parsing in the asm macro
This commit is contained in:
parent
b85a3da421
commit
a699f17483
4 changed files with 56 additions and 59 deletions
|
|
@ -1073,6 +1073,22 @@ impl<'a> Parser<'a> {
|
|||
self.maybe_recover_from_bad_qpath(expr, true)
|
||||
}
|
||||
|
||||
pub fn parse_str_lit(&mut self) -> Result<ast::StrLit, Option<Lit>> {
|
||||
match self.parse_opt_lit() {
|
||||
Some(lit) => match lit.kind {
|
||||
ast::LitKind::Str(symbol_unescaped, style) => Ok(ast::StrLit {
|
||||
style,
|
||||
symbol: lit.token.symbol,
|
||||
suffix: lit.token.suffix,
|
||||
span: lit.span,
|
||||
symbol_unescaped,
|
||||
}),
|
||||
_ => Err(Some(lit)),
|
||||
}
|
||||
None => Err(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn parse_lit(&mut self) -> PResult<'a, Lit> {
|
||||
self.parse_opt_lit().ok_or_else(|| {
|
||||
let msg = format!("unexpected token: {}", self.this_token_descr());
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ use super::diagnostics::{Error, dummy_arg, ConsumeClosingDelim};
|
|||
|
||||
use crate::maybe_whole;
|
||||
|
||||
use syntax::ast::{self, Abi, DUMMY_NODE_ID, Ident, Attribute, AttrKind, AttrStyle, AnonConst, Item};
|
||||
use syntax::ast::{self, DUMMY_NODE_ID, Ident, Attribute, AttrKind, AttrStyle, AnonConst, Item};
|
||||
use syntax::ast::{ItemKind, ImplItem, ImplItemKind, TraitItem, TraitItemKind, UseTree, UseTreeKind};
|
||||
use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness};
|
||||
use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness, Extern, StrLit};
|
||||
use syntax::ast::{Visibility, VisibilityKind, Mutability, FnHeader, ForeignItem, ForeignItemKind};
|
||||
use syntax::ast::{Ty, TyKind, Generics, TraitRef, EnumDef, VariantData, StructField};
|
||||
use syntax::ast::{Mac, MacDelimiter, Block, BindingMode, FnDecl, FnSig, SelfKind, Param};
|
||||
|
|
@ -105,7 +105,7 @@ impl<'a> Parser<'a> {
|
|||
return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
|
||||
}
|
||||
|
||||
let abi = self.parse_opt_abi();
|
||||
let abi = self.parse_abi();
|
||||
|
||||
if self.eat_keyword(kw::Fn) {
|
||||
// EXTERN FUNCTION ITEM
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ use crate::{Directory, DirectoryOwnership};
|
|||
use crate::lexer::UnmatchedBrace;
|
||||
|
||||
use syntax::ast::{
|
||||
self, DUMMY_NODE_ID, AttrStyle, Attribute, CrateSugar, Extern, Ident,
|
||||
IsAsync, MacDelimiter, Mutability, StrStyle, Visibility, VisibilityKind, Unsafety,
|
||||
self, DUMMY_NODE_ID, AttrStyle, Attribute, CrateSugar, Extern, Ident, StrLit,
|
||||
IsAsync, MacDelimiter, Mutability, Visibility, VisibilityKind, Unsafety,
|
||||
};
|
||||
|
||||
use syntax::print::pprust;
|
||||
|
|
@ -1214,34 +1214,32 @@ impl<'a> Parser<'a> {
|
|||
/// Parses `extern string_literal?`.
|
||||
fn parse_extern(&mut self) -> PResult<'a, Extern> {
|
||||
Ok(if self.eat_keyword(kw::Extern) {
|
||||
Extern::from_abi(self.parse_opt_abi())
|
||||
Extern::from_abi(self.parse_abi())
|
||||
} else {
|
||||
Extern::None
|
||||
})
|
||||
}
|
||||
|
||||
/// Parses a string literal as an ABI spec.
|
||||
fn parse_opt_abi(&mut self) -> Option<StrLit> {
|
||||
if let Some(ast::Lit { token: token::Lit { symbol, suffix, .. }, span, kind })
|
||||
= self.parse_opt_lit() {
|
||||
match kind {
|
||||
ast::LitKind::Str(symbol_unescaped, style) => return Some(StrLit {
|
||||
style, symbol, suffix, span, symbol_unescaped,
|
||||
}),
|
||||
ast::LitKind::Err(_) => {}
|
||||
fn parse_abi(&mut self) -> Option<StrLit> {
|
||||
match self.parse_str_lit() {
|
||||
Ok(str_lit) => Some(str_lit),
|
||||
Err(Some(lit)) => match lit.kind {
|
||||
ast::LitKind::Err(_) => None,
|
||||
_ => {
|
||||
self.struct_span_err(span, "non-string ABI literal")
|
||||
self.struct_span_err(lit.span, "non-string ABI literal")
|
||||
.span_suggestion(
|
||||
span,
|
||||
lit.span,
|
||||
"specify the ABI with a string literal",
|
||||
"\"C\"".to_string(),
|
||||
Applicability::MaybeIncorrect,
|
||||
)
|
||||
.emit();
|
||||
None
|
||||
}
|
||||
}
|
||||
Err(None) => None,
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// We are parsing `async fn`. If we are on Rust 2015, emit an error.
|
||||
|
|
@ -1333,34 +1331,6 @@ impl<'a> Parser<'a> {
|
|||
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace) ||
|
||||
*t == token::BinOp(token::Star))
|
||||
}
|
||||
|
||||
fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option<ast::Name>)> {
|
||||
let ret = match self.token.kind {
|
||||
token::Literal(token::Lit { kind: token::Str, symbol, suffix }) =>
|
||||
(symbol, ast::StrStyle::Cooked, suffix),
|
||||
token::Literal(token::Lit { kind: token::StrRaw(n), symbol, suffix }) =>
|
||||
(symbol, ast::StrStyle::Raw(n), suffix),
|
||||
_ => return None
|
||||
};
|
||||
self.bump();
|
||||
Some(ret)
|
||||
}
|
||||
|
||||
pub fn parse_str(&mut self) -> PResult<'a, (Symbol, StrStyle)> {
|
||||
match self.parse_optional_str() {
|
||||
Some((s, style, suf)) => {
|
||||
let sp = self.prev_span;
|
||||
self.expect_no_suffix(sp, "a string literal", suf);
|
||||
Ok((s, style))
|
||||
}
|
||||
_ => {
|
||||
let msg = "expected string literal";
|
||||
let mut err = self.fatal(msg);
|
||||
err.span_label(self.token.span, msg);
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
crate fn make_unclosed_delims_error(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue