lit.rs 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. // SPDX-License-Identifier: Apache-2.0 OR MIT
  2. #[cfg(feature = "parsing")]
  3. use crate::lookahead;
  4. #[cfg(feature = "parsing")]
  5. use crate::parse::{Parse, Parser};
  6. use crate::{Error, Result};
  7. use proc_macro2::{Ident, Literal, Span};
  8. #[cfg(feature = "parsing")]
  9. use proc_macro2::{TokenStream, TokenTree};
  10. use std::ffi::{CStr, CString};
  11. use std::fmt::{self, Display};
  12. #[cfg(feature = "extra-traits")]
  13. use std::hash::{Hash, Hasher};
  14. use std::str::{self, FromStr};
  15. ast_enum_of_structs! {
  16. /// A Rust literal such as a string or integer or boolean.
  17. ///
  18. /// # Syntax tree enum
  19. ///
  20. /// This type is a [syntax tree enum].
  21. ///
  22. /// [syntax tree enum]: crate::expr::Expr#syntax-tree-enums
  23. #[non_exhaustive]
  24. pub enum Lit {
  25. /// A UTF-8 string literal: `"foo"`.
  26. Str(LitStr),
  27. /// A byte string literal: `b"foo"`.
  28. ByteStr(LitByteStr),
  29. /// A nul-terminated C-string literal: `c"foo"`.
  30. CStr(LitCStr),
  31. /// A byte literal: `b'f'`.
  32. Byte(LitByte),
  33. /// A character literal: `'a'`.
  34. Char(LitChar),
  35. /// An integer literal: `1` or `1u16`.
  36. Int(LitInt),
  37. /// A floating point literal: `1f64` or `1.0e10f64`.
  38. ///
  39. /// Must be finite. May not be infinite or NaN.
  40. Float(LitFloat),
  41. /// A boolean literal: `true` or `false`.
  42. Bool(LitBool),
  43. /// A raw token literal not interpreted by Syn.
  44. Verbatim(Literal),
  45. }
  46. }
  47. ast_struct! {
  48. /// A UTF-8 string literal: `"foo"`.
  49. pub struct LitStr {
  50. repr: Box<LitRepr>,
  51. }
  52. }
  53. ast_struct! {
  54. /// A byte string literal: `b"foo"`.
  55. pub struct LitByteStr {
  56. repr: Box<LitRepr>,
  57. }
  58. }
  59. ast_struct! {
  60. /// A nul-terminated C-string literal: `c"foo"`.
  61. pub struct LitCStr {
  62. repr: Box<LitRepr>,
  63. }
  64. }
  65. ast_struct! {
  66. /// A byte literal: `b'f'`.
  67. pub struct LitByte {
  68. repr: Box<LitRepr>,
  69. }
  70. }
  71. ast_struct! {
  72. /// A character literal: `'a'`.
  73. pub struct LitChar {
  74. repr: Box<LitRepr>,
  75. }
  76. }
  77. struct LitRepr {
  78. token: Literal,
  79. suffix: Box<str>,
  80. }
  81. ast_struct! {
  82. /// An integer literal: `1` or `1u16`.
  83. pub struct LitInt {
  84. repr: Box<LitIntRepr>,
  85. }
  86. }
  87. struct LitIntRepr {
  88. token: Literal,
  89. digits: Box<str>,
  90. suffix: Box<str>,
  91. }
  92. ast_struct! {
  93. /// A floating point literal: `1f64` or `1.0e10f64`.
  94. ///
  95. /// Must be finite. May not be infinite or NaN.
  96. pub struct LitFloat {
  97. repr: Box<LitFloatRepr>,
  98. }
  99. }
  100. struct LitFloatRepr {
  101. token: Literal,
  102. digits: Box<str>,
  103. suffix: Box<str>,
  104. }
  105. ast_struct! {
  106. /// A boolean literal: `true` or `false`.
  107. pub struct LitBool {
  108. pub value: bool,
  109. pub span: Span,
  110. }
  111. }
  112. impl LitStr {
  113. pub fn new(value: &str, span: Span) -> Self {
  114. let mut token = Literal::string(value);
  115. token.set_span(span);
  116. LitStr {
  117. repr: Box::new(LitRepr {
  118. token,
  119. suffix: Box::<str>::default(),
  120. }),
  121. }
  122. }
  123. pub fn value(&self) -> String {
  124. let repr = self.repr.token.to_string();
  125. let (value, _suffix) = value::parse_lit_str(&repr);
  126. String::from(value)
  127. }
  128. /// Parse a syntax tree node from the content of this string literal.
  129. ///
  130. /// All spans in the syntax tree will point to the span of this `LitStr`.
  131. ///
  132. /// # Example
  133. ///
  134. /// ```
  135. /// use syn::{Attribute, Error, Expr, Lit, Meta, Path, Result};
  136. ///
  137. /// // Parses the path from an attribute that looks like:
  138. /// //
  139. /// // #[path = "a::b::c"]
  140. /// //
  141. /// // or returns `None` if the input is some other attribute.
  142. /// fn get_path(attr: &Attribute) -> Result<Option<Path>> {
  143. /// if !attr.path().is_ident("path") {
  144. /// return Ok(None);
  145. /// }
  146. ///
  147. /// if let Meta::NameValue(meta) = &attr.meta {
  148. /// if let Expr::Lit(expr) = &meta.value {
  149. /// if let Lit::Str(lit_str) = &expr.lit {
  150. /// return lit_str.parse().map(Some);
  151. /// }
  152. /// }
  153. /// }
  154. ///
  155. /// let message = "expected #[path = \"...\"]";
  156. /// Err(Error::new_spanned(attr, message))
  157. /// }
  158. /// ```
  159. #[cfg(feature = "parsing")]
  160. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  161. pub fn parse<T: Parse>(&self) -> Result<T> {
  162. self.parse_with(T::parse)
  163. }
  164. /// Invoke parser on the content of this string literal.
  165. ///
  166. /// All spans in the syntax tree will point to the span of this `LitStr`.
  167. ///
  168. /// # Example
  169. ///
  170. /// ```
  171. /// # use proc_macro2::Span;
  172. /// # use syn::{LitStr, Result};
  173. /// #
  174. /// # fn main() -> Result<()> {
  175. /// # let lit_str = LitStr::new("a::b::c", Span::call_site());
  176. /// #
  177. /// # const IGNORE: &str = stringify! {
  178. /// let lit_str: LitStr = /* ... */;
  179. /// # };
  180. ///
  181. /// // Parse a string literal like "a::b::c" into a Path, not allowing
  182. /// // generic arguments on any of the path segments.
  183. /// let basic_path = lit_str.parse_with(syn::Path::parse_mod_style)?;
  184. /// #
  185. /// # Ok(())
  186. /// # }
  187. /// ```
  188. #[cfg(feature = "parsing")]
  189. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  190. pub fn parse_with<F: Parser>(&self, parser: F) -> Result<F::Output> {
  191. use proc_macro2::Group;
  192. // Token stream with every span replaced by the given one.
  193. fn respan_token_stream(stream: TokenStream, span: Span) -> TokenStream {
  194. stream
  195. .into_iter()
  196. .map(|token| respan_token_tree(token, span))
  197. .collect()
  198. }
  199. // Token tree with every span replaced by the given one.
  200. fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree {
  201. match &mut token {
  202. TokenTree::Group(g) => {
  203. let stream = respan_token_stream(g.stream(), span);
  204. *g = Group::new(g.delimiter(), stream);
  205. g.set_span(span);
  206. }
  207. other => other.set_span(span),
  208. }
  209. token
  210. }
  211. // Parse string literal into a token stream with every span equal to the
  212. // original literal's span.
  213. let span = self.span();
  214. let mut tokens = TokenStream::from_str(&self.value())?;
  215. tokens = respan_token_stream(tokens, span);
  216. let result = crate::parse::parse_scoped(parser, span, tokens)?;
  217. let suffix = self.suffix();
  218. if !suffix.is_empty() {
  219. return Err(Error::new(
  220. self.span(),
  221. format!("unexpected suffix `{}` on string literal", suffix),
  222. ));
  223. }
  224. Ok(result)
  225. }
  226. pub fn span(&self) -> Span {
  227. self.repr.token.span()
  228. }
  229. pub fn set_span(&mut self, span: Span) {
  230. self.repr.token.set_span(span);
  231. }
  232. pub fn suffix(&self) -> &str {
  233. &self.repr.suffix
  234. }
  235. pub fn token(&self) -> Literal {
  236. self.repr.token.clone()
  237. }
  238. }
  239. impl LitByteStr {
  240. pub fn new(value: &[u8], span: Span) -> Self {
  241. let mut token = Literal::byte_string(value);
  242. token.set_span(span);
  243. LitByteStr {
  244. repr: Box::new(LitRepr {
  245. token,
  246. suffix: Box::<str>::default(),
  247. }),
  248. }
  249. }
  250. pub fn value(&self) -> Vec<u8> {
  251. let repr = self.repr.token.to_string();
  252. let (value, _suffix) = value::parse_lit_byte_str(&repr);
  253. value
  254. }
  255. pub fn span(&self) -> Span {
  256. self.repr.token.span()
  257. }
  258. pub fn set_span(&mut self, span: Span) {
  259. self.repr.token.set_span(span);
  260. }
  261. pub fn suffix(&self) -> &str {
  262. &self.repr.suffix
  263. }
  264. pub fn token(&self) -> Literal {
  265. self.repr.token.clone()
  266. }
  267. }
  268. impl LitCStr {
  269. pub fn new(value: &CStr, span: Span) -> Self {
  270. let mut token = Literal::c_string(value);
  271. token.set_span(span);
  272. LitCStr {
  273. repr: Box::new(LitRepr {
  274. token,
  275. suffix: Box::<str>::default(),
  276. }),
  277. }
  278. }
  279. pub fn value(&self) -> CString {
  280. let repr = self.repr.token.to_string();
  281. let (value, _suffix) = value::parse_lit_c_str(&repr);
  282. value
  283. }
  284. pub fn span(&self) -> Span {
  285. self.repr.token.span()
  286. }
  287. pub fn set_span(&mut self, span: Span) {
  288. self.repr.token.set_span(span);
  289. }
  290. pub fn suffix(&self) -> &str {
  291. &self.repr.suffix
  292. }
  293. pub fn token(&self) -> Literal {
  294. self.repr.token.clone()
  295. }
  296. }
  297. impl LitByte {
  298. pub fn new(value: u8, span: Span) -> Self {
  299. let mut token = Literal::u8_suffixed(value);
  300. token.set_span(span);
  301. LitByte {
  302. repr: Box::new(LitRepr {
  303. token,
  304. suffix: Box::<str>::default(),
  305. }),
  306. }
  307. }
  308. pub fn value(&self) -> u8 {
  309. let repr = self.repr.token.to_string();
  310. let (value, _suffix) = value::parse_lit_byte(&repr);
  311. value
  312. }
  313. pub fn span(&self) -> Span {
  314. self.repr.token.span()
  315. }
  316. pub fn set_span(&mut self, span: Span) {
  317. self.repr.token.set_span(span);
  318. }
  319. pub fn suffix(&self) -> &str {
  320. &self.repr.suffix
  321. }
  322. pub fn token(&self) -> Literal {
  323. self.repr.token.clone()
  324. }
  325. }
  326. impl LitChar {
  327. pub fn new(value: char, span: Span) -> Self {
  328. let mut token = Literal::character(value);
  329. token.set_span(span);
  330. LitChar {
  331. repr: Box::new(LitRepr {
  332. token,
  333. suffix: Box::<str>::default(),
  334. }),
  335. }
  336. }
  337. pub fn value(&self) -> char {
  338. let repr = self.repr.token.to_string();
  339. let (value, _suffix) = value::parse_lit_char(&repr);
  340. value
  341. }
  342. pub fn span(&self) -> Span {
  343. self.repr.token.span()
  344. }
  345. pub fn set_span(&mut self, span: Span) {
  346. self.repr.token.set_span(span);
  347. }
  348. pub fn suffix(&self) -> &str {
  349. &self.repr.suffix
  350. }
  351. pub fn token(&self) -> Literal {
  352. self.repr.token.clone()
  353. }
  354. }
  355. impl LitInt {
  356. pub fn new(repr: &str, span: Span) -> Self {
  357. let (digits, suffix) = match value::parse_lit_int(repr) {
  358. Some(parse) => parse,
  359. None => panic!("not an integer literal: `{}`", repr),
  360. };
  361. let mut token: Literal = repr.parse().unwrap();
  362. token.set_span(span);
  363. LitInt {
  364. repr: Box::new(LitIntRepr {
  365. token,
  366. digits,
  367. suffix,
  368. }),
  369. }
  370. }
  371. pub fn base10_digits(&self) -> &str {
  372. &self.repr.digits
  373. }
  374. /// Parses the literal into a selected number type.
  375. ///
  376. /// This is equivalent to `lit.base10_digits().parse()` except that the
  377. /// resulting errors will be correctly spanned to point to the literal token
  378. /// in the macro input.
  379. ///
  380. /// ```
  381. /// use syn::LitInt;
  382. /// use syn::parse::{Parse, ParseStream, Result};
  383. ///
  384. /// struct Port {
  385. /// value: u16,
  386. /// }
  387. ///
  388. /// impl Parse for Port {
  389. /// fn parse(input: ParseStream) -> Result<Self> {
  390. /// let lit: LitInt = input.parse()?;
  391. /// let value = lit.base10_parse::<u16>()?;
  392. /// Ok(Port { value })
  393. /// }
  394. /// }
  395. /// ```
  396. pub fn base10_parse<N>(&self) -> Result<N>
  397. where
  398. N: FromStr,
  399. N::Err: Display,
  400. {
  401. self.base10_digits()
  402. .parse()
  403. .map_err(|err| Error::new(self.span(), err))
  404. }
  405. pub fn suffix(&self) -> &str {
  406. &self.repr.suffix
  407. }
  408. pub fn span(&self) -> Span {
  409. self.repr.token.span()
  410. }
  411. pub fn set_span(&mut self, span: Span) {
  412. self.repr.token.set_span(span);
  413. }
  414. pub fn token(&self) -> Literal {
  415. self.repr.token.clone()
  416. }
  417. }
  418. impl From<Literal> for LitInt {
  419. fn from(token: Literal) -> Self {
  420. let repr = token.to_string();
  421. if let Some((digits, suffix)) = value::parse_lit_int(&repr) {
  422. LitInt {
  423. repr: Box::new(LitIntRepr {
  424. token,
  425. digits,
  426. suffix,
  427. }),
  428. }
  429. } else {
  430. panic!("not an integer literal: `{}`", repr);
  431. }
  432. }
  433. }
  434. impl Display for LitInt {
  435. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  436. self.repr.token.fmt(formatter)
  437. }
  438. }
  439. impl LitFloat {
  440. pub fn new(repr: &str, span: Span) -> Self {
  441. let (digits, suffix) = match value::parse_lit_float(repr) {
  442. Some(parse) => parse,
  443. None => panic!("not a float literal: `{}`", repr),
  444. };
  445. let mut token: Literal = repr.parse().unwrap();
  446. token.set_span(span);
  447. LitFloat {
  448. repr: Box::new(LitFloatRepr {
  449. token,
  450. digits,
  451. suffix,
  452. }),
  453. }
  454. }
  455. pub fn base10_digits(&self) -> &str {
  456. &self.repr.digits
  457. }
  458. pub fn base10_parse<N>(&self) -> Result<N>
  459. where
  460. N: FromStr,
  461. N::Err: Display,
  462. {
  463. self.base10_digits()
  464. .parse()
  465. .map_err(|err| Error::new(self.span(), err))
  466. }
  467. pub fn suffix(&self) -> &str {
  468. &self.repr.suffix
  469. }
  470. pub fn span(&self) -> Span {
  471. self.repr.token.span()
  472. }
  473. pub fn set_span(&mut self, span: Span) {
  474. self.repr.token.set_span(span);
  475. }
  476. pub fn token(&self) -> Literal {
  477. self.repr.token.clone()
  478. }
  479. }
  480. impl From<Literal> for LitFloat {
  481. fn from(token: Literal) -> Self {
  482. let repr = token.to_string();
  483. if let Some((digits, suffix)) = value::parse_lit_float(&repr) {
  484. LitFloat {
  485. repr: Box::new(LitFloatRepr {
  486. token,
  487. digits,
  488. suffix,
  489. }),
  490. }
  491. } else {
  492. panic!("not a float literal: `{}`", repr);
  493. }
  494. }
  495. }
  496. impl Display for LitFloat {
  497. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  498. self.repr.token.fmt(formatter)
  499. }
  500. }
  501. impl LitBool {
  502. pub fn new(value: bool, span: Span) -> Self {
  503. LitBool { value, span }
  504. }
  505. pub fn value(&self) -> bool {
  506. self.value
  507. }
  508. pub fn span(&self) -> Span {
  509. self.span
  510. }
  511. pub fn set_span(&mut self, span: Span) {
  512. self.span = span;
  513. }
  514. pub fn token(&self) -> Ident {
  515. let s = if self.value { "true" } else { "false" };
  516. Ident::new(s, self.span)
  517. }
  518. }
  519. #[cfg(feature = "extra-traits")]
  520. mod debug_impls {
  521. use crate::lit::{LitBool, LitByte, LitByteStr, LitCStr, LitChar, LitFloat, LitInt, LitStr};
  522. use std::fmt::{self, Debug};
  523. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  524. impl Debug for LitStr {
  525. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  526. self.debug(formatter, "LitStr")
  527. }
  528. }
  529. impl LitStr {
  530. pub(crate) fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
  531. formatter
  532. .debug_struct(name)
  533. .field("token", &format_args!("{}", self.repr.token))
  534. .finish()
  535. }
  536. }
  537. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  538. impl Debug for LitByteStr {
  539. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  540. self.debug(formatter, "LitByteStr")
  541. }
  542. }
  543. impl LitByteStr {
  544. pub(crate) fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
  545. formatter
  546. .debug_struct(name)
  547. .field("token", &format_args!("{}", self.repr.token))
  548. .finish()
  549. }
  550. }
  551. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  552. impl Debug for LitCStr {
  553. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  554. self.debug(formatter, "LitCStr")
  555. }
  556. }
  557. impl LitCStr {
  558. pub(crate) fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
  559. formatter
  560. .debug_struct(name)
  561. .field("token", &format_args!("{}", self.repr.token))
  562. .finish()
  563. }
  564. }
  565. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  566. impl Debug for LitByte {
  567. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  568. self.debug(formatter, "LitByte")
  569. }
  570. }
  571. impl LitByte {
  572. pub(crate) fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
  573. formatter
  574. .debug_struct(name)
  575. .field("token", &format_args!("{}", self.repr.token))
  576. .finish()
  577. }
  578. }
  579. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  580. impl Debug for LitChar {
  581. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  582. self.debug(formatter, "LitChar")
  583. }
  584. }
  585. impl LitChar {
  586. pub(crate) fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
  587. formatter
  588. .debug_struct(name)
  589. .field("token", &format_args!("{}", self.repr.token))
  590. .finish()
  591. }
  592. }
  593. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  594. impl Debug for LitInt {
  595. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  596. self.debug(formatter, "LitInt")
  597. }
  598. }
  599. impl LitInt {
  600. pub(crate) fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
  601. formatter
  602. .debug_struct(name)
  603. .field("token", &format_args!("{}", self.repr.token))
  604. .finish()
  605. }
  606. }
  607. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  608. impl Debug for LitFloat {
  609. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  610. self.debug(formatter, "LitFloat")
  611. }
  612. }
  613. impl LitFloat {
  614. pub(crate) fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
  615. formatter
  616. .debug_struct(name)
  617. .field("token", &format_args!("{}", self.repr.token))
  618. .finish()
  619. }
  620. }
  621. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  622. impl Debug for LitBool {
  623. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  624. self.debug(formatter, "LitBool")
  625. }
  626. }
  627. impl LitBool {
  628. pub(crate) fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
  629. formatter
  630. .debug_struct(name)
  631. .field("value", &self.value)
  632. .finish()
  633. }
  634. }
  635. }
  636. #[cfg(feature = "clone-impls")]
  637. #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))]
  638. impl Clone for LitRepr {
  639. fn clone(&self) -> Self {
  640. LitRepr {
  641. token: self.token.clone(),
  642. suffix: self.suffix.clone(),
  643. }
  644. }
  645. }
  646. #[cfg(feature = "clone-impls")]
  647. #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))]
  648. impl Clone for LitIntRepr {
  649. fn clone(&self) -> Self {
  650. LitIntRepr {
  651. token: self.token.clone(),
  652. digits: self.digits.clone(),
  653. suffix: self.suffix.clone(),
  654. }
  655. }
  656. }
  657. #[cfg(feature = "clone-impls")]
  658. #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))]
  659. impl Clone for LitFloatRepr {
  660. fn clone(&self) -> Self {
  661. LitFloatRepr {
  662. token: self.token.clone(),
  663. digits: self.digits.clone(),
  664. suffix: self.suffix.clone(),
  665. }
  666. }
  667. }
  668. macro_rules! lit_extra_traits {
  669. ($ty:ident) => {
  670. #[cfg(feature = "clone-impls")]
  671. #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))]
  672. impl Clone for $ty {
  673. fn clone(&self) -> Self {
  674. $ty {
  675. repr: self.repr.clone(),
  676. }
  677. }
  678. }
  679. #[cfg(feature = "extra-traits")]
  680. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  681. impl PartialEq for $ty {
  682. fn eq(&self, other: &Self) -> bool {
  683. self.repr.token.to_string() == other.repr.token.to_string()
  684. }
  685. }
  686. #[cfg(feature = "extra-traits")]
  687. #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
  688. impl Hash for $ty {
  689. fn hash<H>(&self, state: &mut H)
  690. where
  691. H: Hasher,
  692. {
  693. self.repr.token.to_string().hash(state);
  694. }
  695. }
  696. #[cfg(feature = "parsing")]
  697. pub_if_not_doc! {
  698. #[doc(hidden)]
  699. #[allow(non_snake_case)]
  700. pub fn $ty(marker: lookahead::TokenMarker) -> $ty {
  701. match marker {}
  702. }
  703. }
  704. };
  705. }
  706. lit_extra_traits!(LitStr);
  707. lit_extra_traits!(LitByteStr);
  708. lit_extra_traits!(LitCStr);
  709. lit_extra_traits!(LitByte);
  710. lit_extra_traits!(LitChar);
  711. lit_extra_traits!(LitInt);
  712. lit_extra_traits!(LitFloat);
  713. #[cfg(feature = "parsing")]
  714. pub_if_not_doc! {
  715. #[doc(hidden)]
  716. #[allow(non_snake_case)]
  717. pub fn LitBool(marker: lookahead::TokenMarker) -> LitBool {
  718. match marker {}
  719. }
  720. }
  721. /// The style of a string literal, either plain quoted or a raw string like
  722. /// `r##"data"##`.
  723. #[doc(hidden)] // https://github.com/dtolnay/syn/issues/1566
  724. pub enum StrStyle {
  725. /// An ordinary string like `"data"`.
  726. Cooked,
  727. /// A raw string like `r##"data"##`.
  728. ///
  729. /// The unsigned integer is the number of `#` symbols used.
  730. Raw(usize),
  731. }
  732. #[cfg(feature = "parsing")]
  733. pub_if_not_doc! {
  734. #[doc(hidden)]
  735. #[allow(non_snake_case)]
  736. pub fn Lit(marker: lookahead::TokenMarker) -> Lit {
  737. match marker {}
  738. }
  739. }
  740. #[cfg(feature = "parsing")]
  741. pub(crate) mod parsing {
  742. use crate::buffer::Cursor;
  743. use crate::error::Result;
  744. use crate::lit::{
  745. value, Lit, LitBool, LitByte, LitByteStr, LitCStr, LitChar, LitFloat, LitFloatRepr, LitInt,
  746. LitIntRepr, LitStr,
  747. };
  748. use crate::parse::{Parse, ParseStream, Unexpected};
  749. use crate::token::{self, Token};
  750. use proc_macro2::{Literal, Punct, Span};
  751. use std::cell::Cell;
  752. use std::rc::Rc;
  753. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  754. impl Parse for Lit {
  755. fn parse(input: ParseStream) -> Result<Self> {
  756. input.step(|cursor| {
  757. if let Some((lit, rest)) = cursor.literal() {
  758. return Ok((Lit::new(lit), rest));
  759. }
  760. if let Some((ident, rest)) = cursor.ident() {
  761. let value = ident == "true";
  762. if value || ident == "false" {
  763. let lit_bool = LitBool {
  764. value,
  765. span: ident.span(),
  766. };
  767. return Ok((Lit::Bool(lit_bool), rest));
  768. }
  769. }
  770. if let Some((punct, rest)) = cursor.punct() {
  771. if punct.as_char() == '-' {
  772. if let Some((lit, rest)) = parse_negative_lit(punct, rest) {
  773. return Ok((lit, rest));
  774. }
  775. }
  776. }
  777. Err(cursor.error("expected literal"))
  778. })
  779. }
  780. }
  781. fn parse_negative_lit(neg: Punct, cursor: Cursor) -> Option<(Lit, Cursor)> {
  782. let (lit, rest) = cursor.literal()?;
  783. let mut span = neg.span();
  784. span = span.join(lit.span()).unwrap_or(span);
  785. let mut repr = lit.to_string();
  786. repr.insert(0, '-');
  787. if let Some((digits, suffix)) = value::parse_lit_int(&repr) {
  788. let mut token: Literal = repr.parse().unwrap();
  789. token.set_span(span);
  790. return Some((
  791. Lit::Int(LitInt {
  792. repr: Box::new(LitIntRepr {
  793. token,
  794. digits,
  795. suffix,
  796. }),
  797. }),
  798. rest,
  799. ));
  800. }
  801. let (digits, suffix) = value::parse_lit_float(&repr)?;
  802. let mut token: Literal = repr.parse().unwrap();
  803. token.set_span(span);
  804. Some((
  805. Lit::Float(LitFloat {
  806. repr: Box::new(LitFloatRepr {
  807. token,
  808. digits,
  809. suffix,
  810. }),
  811. }),
  812. rest,
  813. ))
  814. }
  815. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  816. impl Parse for LitStr {
  817. fn parse(input: ParseStream) -> Result<Self> {
  818. let head = input.fork();
  819. match input.parse() {
  820. Ok(Lit::Str(lit)) => Ok(lit),
  821. _ => Err(head.error("expected string literal")),
  822. }
  823. }
  824. }
  825. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  826. impl Parse for LitByteStr {
  827. fn parse(input: ParseStream) -> Result<Self> {
  828. let head = input.fork();
  829. match input.parse() {
  830. Ok(Lit::ByteStr(lit)) => Ok(lit),
  831. _ => Err(head.error("expected byte string literal")),
  832. }
  833. }
  834. }
  835. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  836. impl Parse for LitCStr {
  837. fn parse(input: ParseStream) -> Result<Self> {
  838. let head = input.fork();
  839. match input.parse() {
  840. Ok(Lit::CStr(lit)) => Ok(lit),
  841. _ => Err(head.error("expected C string literal")),
  842. }
  843. }
  844. }
  845. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  846. impl Parse for LitByte {
  847. fn parse(input: ParseStream) -> Result<Self> {
  848. let head = input.fork();
  849. match input.parse() {
  850. Ok(Lit::Byte(lit)) => Ok(lit),
  851. _ => Err(head.error("expected byte literal")),
  852. }
  853. }
  854. }
  855. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  856. impl Parse for LitChar {
  857. fn parse(input: ParseStream) -> Result<Self> {
  858. let head = input.fork();
  859. match input.parse() {
  860. Ok(Lit::Char(lit)) => Ok(lit),
  861. _ => Err(head.error("expected character literal")),
  862. }
  863. }
  864. }
  865. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  866. impl Parse for LitInt {
  867. fn parse(input: ParseStream) -> Result<Self> {
  868. let head = input.fork();
  869. match input.parse() {
  870. Ok(Lit::Int(lit)) => Ok(lit),
  871. _ => Err(head.error("expected integer literal")),
  872. }
  873. }
  874. }
  875. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  876. impl Parse for LitFloat {
  877. fn parse(input: ParseStream) -> Result<Self> {
  878. let head = input.fork();
  879. match input.parse() {
  880. Ok(Lit::Float(lit)) => Ok(lit),
  881. _ => Err(head.error("expected floating point literal")),
  882. }
  883. }
  884. }
  885. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  886. impl Parse for LitBool {
  887. fn parse(input: ParseStream) -> Result<Self> {
  888. let head = input.fork();
  889. match input.parse() {
  890. Ok(Lit::Bool(lit)) => Ok(lit),
  891. _ => Err(head.error("expected boolean literal")),
  892. }
  893. }
  894. }
  895. fn peek_impl(cursor: Cursor, peek: fn(ParseStream) -> bool) -> bool {
  896. let scope = Span::call_site();
  897. let unexpected = Rc::new(Cell::new(Unexpected::None));
  898. let buffer = crate::parse::new_parse_buffer(scope, cursor, unexpected);
  899. peek(&buffer)
  900. }
  901. macro_rules! impl_token {
  902. ($display:literal $name:ty) => {
  903. impl Token for $name {
  904. fn peek(cursor: Cursor) -> bool {
  905. fn peek(input: ParseStream) -> bool {
  906. <$name as Parse>::parse(input).is_ok()
  907. }
  908. peek_impl(cursor, peek)
  909. }
  910. fn display() -> &'static str {
  911. $display
  912. }
  913. }
  914. impl token::private::Sealed for $name {}
  915. };
  916. }
  917. impl_token!("literal" Lit);
  918. impl_token!("string literal" LitStr);
  919. impl_token!("byte string literal" LitByteStr);
  920. impl_token!("C-string literal" LitCStr);
  921. impl_token!("byte literal" LitByte);
  922. impl_token!("character literal" LitChar);
  923. impl_token!("integer literal" LitInt);
  924. impl_token!("floating point literal" LitFloat);
  925. impl_token!("boolean literal" LitBool);
  926. }
  927. #[cfg(feature = "printing")]
  928. mod printing {
  929. use crate::lit::{LitBool, LitByte, LitByteStr, LitCStr, LitChar, LitFloat, LitInt, LitStr};
  930. use proc_macro2::TokenStream;
  931. use quote::{ToTokens, TokenStreamExt};
  932. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  933. impl ToTokens for LitStr {
  934. fn to_tokens(&self, tokens: &mut TokenStream) {
  935. self.repr.token.to_tokens(tokens);
  936. }
  937. }
  938. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  939. impl ToTokens for LitByteStr {
  940. fn to_tokens(&self, tokens: &mut TokenStream) {
  941. self.repr.token.to_tokens(tokens);
  942. }
  943. }
  944. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  945. impl ToTokens for LitCStr {
  946. fn to_tokens(&self, tokens: &mut TokenStream) {
  947. self.repr.token.to_tokens(tokens);
  948. }
  949. }
  950. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  951. impl ToTokens for LitByte {
  952. fn to_tokens(&self, tokens: &mut TokenStream) {
  953. self.repr.token.to_tokens(tokens);
  954. }
  955. }
  956. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  957. impl ToTokens for LitChar {
  958. fn to_tokens(&self, tokens: &mut TokenStream) {
  959. self.repr.token.to_tokens(tokens);
  960. }
  961. }
  962. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  963. impl ToTokens for LitInt {
  964. fn to_tokens(&self, tokens: &mut TokenStream) {
  965. self.repr.token.to_tokens(tokens);
  966. }
  967. }
  968. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  969. impl ToTokens for LitFloat {
  970. fn to_tokens(&self, tokens: &mut TokenStream) {
  971. self.repr.token.to_tokens(tokens);
  972. }
  973. }
  974. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  975. impl ToTokens for LitBool {
  976. fn to_tokens(&self, tokens: &mut TokenStream) {
  977. tokens.append(self.token());
  978. }
  979. }
  980. }
  981. mod value {
  982. use crate::bigint::BigInt;
  983. use crate::lit::{
  984. Lit, LitBool, LitByte, LitByteStr, LitCStr, LitChar, LitFloat, LitFloatRepr, LitInt,
  985. LitIntRepr, LitRepr, LitStr,
  986. };
  987. use proc_macro2::{Literal, Span};
  988. use std::ascii;
  989. use std::char;
  990. use std::ffi::CString;
  991. use std::ops::{Index, RangeFrom};
  992. impl Lit {
  993. /// Interpret a Syn literal from a proc-macro2 literal.
  994. pub fn new(token: Literal) -> Self {
  995. let repr = token.to_string();
  996. match byte(&repr, 0) {
  997. // "...", r"...", r#"..."#
  998. b'"' | b'r' => {
  999. let (_, suffix) = parse_lit_str(&repr);
  1000. return Lit::Str(LitStr {
  1001. repr: Box::new(LitRepr { token, suffix }),
  1002. });
  1003. }
  1004. b'b' => match byte(&repr, 1) {
  1005. // b"...", br"...", br#"...#"
  1006. b'"' | b'r' => {
  1007. let (_, suffix) = parse_lit_byte_str(&repr);
  1008. return Lit::ByteStr(LitByteStr {
  1009. repr: Box::new(LitRepr { token, suffix }),
  1010. });
  1011. }
  1012. // b'...'
  1013. b'\'' => {
  1014. let (_, suffix) = parse_lit_byte(&repr);
  1015. return Lit::Byte(LitByte {
  1016. repr: Box::new(LitRepr { token, suffix }),
  1017. });
  1018. }
  1019. _ => {}
  1020. },
  1021. // c"...", cr"...", cr#"..."#
  1022. b'c' => {
  1023. let (_, suffix) = parse_lit_c_str(&repr);
  1024. return Lit::CStr(LitCStr {
  1025. repr: Box::new(LitRepr { token, suffix }),
  1026. });
  1027. }
  1028. // '...'
  1029. b'\'' => {
  1030. let (_, suffix) = parse_lit_char(&repr);
  1031. return Lit::Char(LitChar {
  1032. repr: Box::new(LitRepr { token, suffix }),
  1033. });
  1034. }
  1035. b'0'..=b'9' | b'-' => {
  1036. // 0, 123, 0xFF, 0o77, 0b11
  1037. if let Some((digits, suffix)) = parse_lit_int(&repr) {
  1038. return Lit::Int(LitInt {
  1039. repr: Box::new(LitIntRepr {
  1040. token,
  1041. digits,
  1042. suffix,
  1043. }),
  1044. });
  1045. }
  1046. // 1.0, 1e-1, 1e+1
  1047. if let Some((digits, suffix)) = parse_lit_float(&repr) {
  1048. return Lit::Float(LitFloat {
  1049. repr: Box::new(LitFloatRepr {
  1050. token,
  1051. digits,
  1052. suffix,
  1053. }),
  1054. });
  1055. }
  1056. }
  1057. // true, false
  1058. b't' | b'f' => {
  1059. if repr == "true" || repr == "false" {
  1060. return Lit::Bool(LitBool {
  1061. value: repr == "true",
  1062. span: token.span(),
  1063. });
  1064. }
  1065. }
  1066. b'(' if repr == "(/*ERROR*/)" => return Lit::Verbatim(token),
  1067. _ => {}
  1068. }
  1069. panic!("unrecognized literal: `{}`", repr);
  1070. }
  1071. pub fn suffix(&self) -> &str {
  1072. match self {
  1073. Lit::Str(lit) => lit.suffix(),
  1074. Lit::ByteStr(lit) => lit.suffix(),
  1075. Lit::CStr(lit) => lit.suffix(),
  1076. Lit::Byte(lit) => lit.suffix(),
  1077. Lit::Char(lit) => lit.suffix(),
  1078. Lit::Int(lit) => lit.suffix(),
  1079. Lit::Float(lit) => lit.suffix(),
  1080. Lit::Bool(_) | Lit::Verbatim(_) => "",
  1081. }
  1082. }
  1083. pub fn span(&self) -> Span {
  1084. match self {
  1085. Lit::Str(lit) => lit.span(),
  1086. Lit::ByteStr(lit) => lit.span(),
  1087. Lit::CStr(lit) => lit.span(),
  1088. Lit::Byte(lit) => lit.span(),
  1089. Lit::Char(lit) => lit.span(),
  1090. Lit::Int(lit) => lit.span(),
  1091. Lit::Float(lit) => lit.span(),
  1092. Lit::Bool(lit) => lit.span,
  1093. Lit::Verbatim(lit) => lit.span(),
  1094. }
  1095. }
  1096. pub fn set_span(&mut self, span: Span) {
  1097. match self {
  1098. Lit::Str(lit) => lit.set_span(span),
  1099. Lit::ByteStr(lit) => lit.set_span(span),
  1100. Lit::CStr(lit) => lit.set_span(span),
  1101. Lit::Byte(lit) => lit.set_span(span),
  1102. Lit::Char(lit) => lit.set_span(span),
  1103. Lit::Int(lit) => lit.set_span(span),
  1104. Lit::Float(lit) => lit.set_span(span),
  1105. Lit::Bool(lit) => lit.span = span,
  1106. Lit::Verbatim(lit) => lit.set_span(span),
  1107. }
  1108. }
  1109. }
  1110. /// Get the byte at offset idx, or a default of `b'\0'` if we're looking
  1111. /// past the end of the input buffer.
  1112. pub(crate) fn byte<S: AsRef<[u8]> + ?Sized>(s: &S, idx: usize) -> u8 {
  1113. let s = s.as_ref();
  1114. if idx < s.len() {
  1115. s[idx]
  1116. } else {
  1117. 0
  1118. }
  1119. }
  1120. fn next_chr(s: &str) -> char {
  1121. s.chars().next().unwrap_or('\0')
  1122. }
  1123. // Returns (content, suffix).
  1124. pub(crate) fn parse_lit_str(s: &str) -> (Box<str>, Box<str>) {
  1125. match byte(s, 0) {
  1126. b'"' => parse_lit_str_cooked(s),
  1127. b'r' => parse_lit_str_raw(s),
  1128. _ => unreachable!(),
  1129. }
  1130. }
  1131. fn parse_lit_str_cooked(mut s: &str) -> (Box<str>, Box<str>) {
  1132. assert_eq!(byte(s, 0), b'"');
  1133. s = &s[1..];
  1134. let mut content = String::new();
  1135. 'outer: loop {
  1136. let ch = match byte(s, 0) {
  1137. b'"' => break,
  1138. b'\\' => {
  1139. let b = byte(s, 1);
  1140. s = &s[2..];
  1141. match b {
  1142. b'x' => {
  1143. let (byte, rest) = backslash_x(s);
  1144. s = rest;
  1145. assert!(byte <= 0x7F, "invalid \\x byte in string literal");
  1146. char::from_u32(u32::from(byte)).unwrap()
  1147. }
  1148. b'u' => {
  1149. let (ch, rest) = backslash_u(s);
  1150. s = rest;
  1151. ch
  1152. }
  1153. b'n' => '\n',
  1154. b'r' => '\r',
  1155. b't' => '\t',
  1156. b'\\' => '\\',
  1157. b'0' => '\0',
  1158. b'\'' => '\'',
  1159. b'"' => '"',
  1160. b'\r' | b'\n' => loop {
  1161. let b = byte(s, 0);
  1162. match b {
  1163. b' ' | b'\t' | b'\n' | b'\r' => s = &s[1..],
  1164. _ => continue 'outer,
  1165. }
  1166. },
  1167. b => panic!(
  1168. "unexpected byte '{}' after \\ character in string literal",
  1169. ascii::escape_default(b),
  1170. ),
  1171. }
  1172. }
  1173. b'\r' => {
  1174. assert_eq!(byte(s, 1), b'\n', "bare CR not allowed in string");
  1175. s = &s[2..];
  1176. '\n'
  1177. }
  1178. _ => {
  1179. let ch = next_chr(s);
  1180. s = &s[ch.len_utf8()..];
  1181. ch
  1182. }
  1183. };
  1184. content.push(ch);
  1185. }
  1186. assert!(s.starts_with('"'));
  1187. let content = content.into_boxed_str();
  1188. let suffix = s[1..].to_owned().into_boxed_str();
  1189. (content, suffix)
  1190. }
  1191. fn parse_lit_str_raw(mut s: &str) -> (Box<str>, Box<str>) {
  1192. assert_eq!(byte(s, 0), b'r');
  1193. s = &s[1..];
  1194. let mut pounds = 0;
  1195. while byte(s, pounds) == b'#' {
  1196. pounds += 1;
  1197. }
  1198. assert_eq!(byte(s, pounds), b'"');
  1199. let close = s.rfind('"').unwrap();
  1200. for end in s[close + 1..close + 1 + pounds].bytes() {
  1201. assert_eq!(end, b'#');
  1202. }
  1203. let content = s[pounds + 1..close].to_owned().into_boxed_str();
  1204. let suffix = s[close + 1 + pounds..].to_owned().into_boxed_str();
  1205. (content, suffix)
  1206. }
  1207. // Returns (content, suffix).
  1208. pub(crate) fn parse_lit_byte_str(s: &str) -> (Vec<u8>, Box<str>) {
  1209. assert_eq!(byte(s, 0), b'b');
  1210. match byte(s, 1) {
  1211. b'"' => parse_lit_byte_str_cooked(s),
  1212. b'r' => parse_lit_byte_str_raw(s),
  1213. _ => unreachable!(),
  1214. }
  1215. }
  1216. fn parse_lit_byte_str_cooked(mut s: &str) -> (Vec<u8>, Box<str>) {
  1217. assert_eq!(byte(s, 0), b'b');
  1218. assert_eq!(byte(s, 1), b'"');
  1219. s = &s[2..];
  1220. // We're going to want to have slices which don't respect codepoint boundaries.
  1221. let mut v = s.as_bytes();
  1222. let mut out = Vec::new();
  1223. 'outer: loop {
  1224. let byte = match byte(v, 0) {
  1225. b'"' => break,
  1226. b'\\' => {
  1227. let b = byte(v, 1);
  1228. v = &v[2..];
  1229. match b {
  1230. b'x' => {
  1231. let (b, rest) = backslash_x(v);
  1232. v = rest;
  1233. b
  1234. }
  1235. b'n' => b'\n',
  1236. b'r' => b'\r',
  1237. b't' => b'\t',
  1238. b'\\' => b'\\',
  1239. b'0' => b'\0',
  1240. b'\'' => b'\'',
  1241. b'"' => b'"',
  1242. b'\r' | b'\n' => loop {
  1243. let byte = byte(v, 0);
  1244. if matches!(byte, b' ' | b'\t' | b'\n' | b'\r') {
  1245. v = &v[1..];
  1246. } else {
  1247. continue 'outer;
  1248. }
  1249. },
  1250. b => panic!(
  1251. "unexpected byte '{}' after \\ character in byte-string literal",
  1252. ascii::escape_default(b),
  1253. ),
  1254. }
  1255. }
  1256. b'\r' => {
  1257. assert_eq!(byte(v, 1), b'\n', "bare CR not allowed in string");
  1258. v = &v[2..];
  1259. b'\n'
  1260. }
  1261. b => {
  1262. v = &v[1..];
  1263. b
  1264. }
  1265. };
  1266. out.push(byte);
  1267. }
  1268. assert_eq!(byte(v, 0), b'"');
  1269. let suffix = s[s.len() - v.len() + 1..].to_owned().into_boxed_str();
  1270. (out, suffix)
  1271. }
  1272. fn parse_lit_byte_str_raw(s: &str) -> (Vec<u8>, Box<str>) {
  1273. assert_eq!(byte(s, 0), b'b');
  1274. let (value, suffix) = parse_lit_str_raw(&s[1..]);
  1275. (String::from(value).into_bytes(), suffix)
  1276. }
  1277. // Returns (content, suffix).
  1278. pub(crate) fn parse_lit_c_str(s: &str) -> (CString, Box<str>) {
  1279. assert_eq!(byte(s, 0), b'c');
  1280. match byte(s, 1) {
  1281. b'"' => parse_lit_c_str_cooked(s),
  1282. b'r' => parse_lit_c_str_raw(s),
  1283. _ => unreachable!(),
  1284. }
  1285. }
  1286. fn parse_lit_c_str_cooked(mut s: &str) -> (CString, Box<str>) {
  1287. assert_eq!(byte(s, 0), b'c');
  1288. assert_eq!(byte(s, 1), b'"');
  1289. s = &s[2..];
  1290. // We're going to want to have slices which don't respect codepoint boundaries.
  1291. let mut v = s.as_bytes();
  1292. let mut out = Vec::new();
  1293. 'outer: loop {
  1294. let byte = match byte(v, 0) {
  1295. b'"' => break,
  1296. b'\\' => {
  1297. let b = byte(v, 1);
  1298. v = &v[2..];
  1299. match b {
  1300. b'x' => {
  1301. let (b, rest) = backslash_x(v);
  1302. assert!(b != 0, "\\x00 is not allowed in C-string literal");
  1303. v = rest;
  1304. b
  1305. }
  1306. b'u' => {
  1307. let (ch, rest) = backslash_u(v);
  1308. assert!(ch != '\0', "\\u{{0}} is not allowed in C-string literal");
  1309. v = rest;
  1310. out.extend_from_slice(ch.encode_utf8(&mut [0u8; 4]).as_bytes());
  1311. continue 'outer;
  1312. }
  1313. b'n' => b'\n',
  1314. b'r' => b'\r',
  1315. b't' => b'\t',
  1316. b'\\' => b'\\',
  1317. b'\'' => b'\'',
  1318. b'"' => b'"',
  1319. b'\r' | b'\n' => loop {
  1320. let byte = byte(v, 0);
  1321. if matches!(byte, b' ' | b'\t' | b'\n' | b'\r') {
  1322. v = &v[1..];
  1323. } else {
  1324. continue 'outer;
  1325. }
  1326. },
  1327. b => panic!(
  1328. "unexpected byte '{}' after \\ character in byte literal",
  1329. ascii::escape_default(b),
  1330. ),
  1331. }
  1332. }
  1333. b'\r' => {
  1334. assert_eq!(byte(v, 1), b'\n', "bare CR not allowed in string");
  1335. v = &v[2..];
  1336. b'\n'
  1337. }
  1338. b => {
  1339. v = &v[1..];
  1340. b
  1341. }
  1342. };
  1343. out.push(byte);
  1344. }
  1345. assert_eq!(byte(v, 0), b'"');
  1346. let suffix = s[s.len() - v.len() + 1..].to_owned().into_boxed_str();
  1347. (CString::new(out).unwrap(), suffix)
  1348. }
  1349. fn parse_lit_c_str_raw(s: &str) -> (CString, Box<str>) {
  1350. assert_eq!(byte(s, 0), b'c');
  1351. let (value, suffix) = parse_lit_str_raw(&s[1..]);
  1352. (CString::new(String::from(value)).unwrap(), suffix)
  1353. }
  1354. // Returns (value, suffix).
  1355. pub(crate) fn parse_lit_byte(s: &str) -> (u8, Box<str>) {
  1356. assert_eq!(byte(s, 0), b'b');
  1357. assert_eq!(byte(s, 1), b'\'');
  1358. // We're going to want to have slices which don't respect codepoint boundaries.
  1359. let mut v = &s.as_bytes()[2..];
  1360. let b = match byte(v, 0) {
  1361. b'\\' => {
  1362. let b = byte(v, 1);
  1363. v = &v[2..];
  1364. match b {
  1365. b'x' => {
  1366. let (b, rest) = backslash_x(v);
  1367. v = rest;
  1368. b
  1369. }
  1370. b'n' => b'\n',
  1371. b'r' => b'\r',
  1372. b't' => b'\t',
  1373. b'\\' => b'\\',
  1374. b'0' => b'\0',
  1375. b'\'' => b'\'',
  1376. b'"' => b'"',
  1377. b => panic!(
  1378. "unexpected byte '{}' after \\ character in byte literal",
  1379. ascii::escape_default(b),
  1380. ),
  1381. }
  1382. }
  1383. b => {
  1384. v = &v[1..];
  1385. b
  1386. }
  1387. };
  1388. assert_eq!(byte(v, 0), b'\'');
  1389. let suffix = s[s.len() - v.len() + 1..].to_owned().into_boxed_str();
  1390. (b, suffix)
  1391. }
  1392. // Returns (value, suffix).
  1393. pub(crate) fn parse_lit_char(mut s: &str) -> (char, Box<str>) {
  1394. assert_eq!(byte(s, 0), b'\'');
  1395. s = &s[1..];
  1396. let ch = match byte(s, 0) {
  1397. b'\\' => {
  1398. let b = byte(s, 1);
  1399. s = &s[2..];
  1400. match b {
  1401. b'x' => {
  1402. let (byte, rest) = backslash_x(s);
  1403. s = rest;
  1404. assert!(byte <= 0x7F, "invalid \\x byte in character literal");
  1405. char::from_u32(u32::from(byte)).unwrap()
  1406. }
  1407. b'u' => {
  1408. let (ch, rest) = backslash_u(s);
  1409. s = rest;
  1410. ch
  1411. }
  1412. b'n' => '\n',
  1413. b'r' => '\r',
  1414. b't' => '\t',
  1415. b'\\' => '\\',
  1416. b'0' => '\0',
  1417. b'\'' => '\'',
  1418. b'"' => '"',
  1419. b => panic!(
  1420. "unexpected byte '{}' after \\ character in character literal",
  1421. ascii::escape_default(b),
  1422. ),
  1423. }
  1424. }
  1425. _ => {
  1426. let ch = next_chr(s);
  1427. s = &s[ch.len_utf8()..];
  1428. ch
  1429. }
  1430. };
  1431. assert_eq!(byte(s, 0), b'\'');
  1432. let suffix = s[1..].to_owned().into_boxed_str();
  1433. (ch, suffix)
  1434. }
  1435. fn backslash_x<S>(s: &S) -> (u8, &S)
  1436. where
  1437. S: Index<RangeFrom<usize>, Output = S> + AsRef<[u8]> + ?Sized,
  1438. {
  1439. let mut ch = 0;
  1440. let b0 = byte(s, 0);
  1441. let b1 = byte(s, 1);
  1442. ch += 0x10
  1443. * match b0 {
  1444. b'0'..=b'9' => b0 - b'0',
  1445. b'a'..=b'f' => 10 + (b0 - b'a'),
  1446. b'A'..=b'F' => 10 + (b0 - b'A'),
  1447. _ => panic!("unexpected non-hex character after \\x"),
  1448. };
  1449. ch += match b1 {
  1450. b'0'..=b'9' => b1 - b'0',
  1451. b'a'..=b'f' => 10 + (b1 - b'a'),
  1452. b'A'..=b'F' => 10 + (b1 - b'A'),
  1453. _ => panic!("unexpected non-hex character after \\x"),
  1454. };
  1455. (ch, &s[2..])
  1456. }
  1457. fn backslash_u<S>(mut s: &S) -> (char, &S)
  1458. where
  1459. S: Index<RangeFrom<usize>, Output = S> + AsRef<[u8]> + ?Sized,
  1460. {
  1461. if byte(s, 0) != b'{' {
  1462. panic!("{}", "expected { after \\u");
  1463. }
  1464. s = &s[1..];
  1465. let mut ch = 0;
  1466. let mut digits = 0;
  1467. loop {
  1468. let b = byte(s, 0);
  1469. let digit = match b {
  1470. b'0'..=b'9' => b - b'0',
  1471. b'a'..=b'f' => 10 + b - b'a',
  1472. b'A'..=b'F' => 10 + b - b'A',
  1473. b'_' if digits > 0 => {
  1474. s = &s[1..];
  1475. continue;
  1476. }
  1477. b'}' if digits == 0 => panic!("invalid empty unicode escape"),
  1478. b'}' => break,
  1479. _ => panic!("unexpected non-hex character after \\u"),
  1480. };
  1481. if digits == 6 {
  1482. panic!("overlong unicode escape (must have at most 6 hex digits)");
  1483. }
  1484. ch *= 0x10;
  1485. ch += u32::from(digit);
  1486. digits += 1;
  1487. s = &s[1..];
  1488. }
  1489. assert!(byte(s, 0) == b'}');
  1490. s = &s[1..];
  1491. if let Some(ch) = char::from_u32(ch) {
  1492. (ch, s)
  1493. } else {
  1494. panic!("character code {:x} is not a valid unicode character", ch);
  1495. }
  1496. }
  1497. // Returns base 10 digits and suffix.
  1498. pub(crate) fn parse_lit_int(mut s: &str) -> Option<(Box<str>, Box<str>)> {
  1499. let negative = byte(s, 0) == b'-';
  1500. if negative {
  1501. s = &s[1..];
  1502. }
  1503. let base = match (byte(s, 0), byte(s, 1)) {
  1504. (b'0', b'x') => {
  1505. s = &s[2..];
  1506. 16
  1507. }
  1508. (b'0', b'o') => {
  1509. s = &s[2..];
  1510. 8
  1511. }
  1512. (b'0', b'b') => {
  1513. s = &s[2..];
  1514. 2
  1515. }
  1516. (b'0'..=b'9', _) => 10,
  1517. _ => return None,
  1518. };
  1519. let mut value = BigInt::new();
  1520. let mut has_digit = false;
  1521. 'outer: loop {
  1522. let b = byte(s, 0);
  1523. let digit = match b {
  1524. b'0'..=b'9' => b - b'0',
  1525. b'a'..=b'f' if base > 10 => b - b'a' + 10,
  1526. b'A'..=b'F' if base > 10 => b - b'A' + 10,
  1527. b'_' => {
  1528. s = &s[1..];
  1529. continue;
  1530. }
  1531. // If looking at a floating point literal, we don't want to
  1532. // consider it an integer.
  1533. b'.' if base == 10 => return None,
  1534. b'e' | b'E' if base == 10 => {
  1535. let mut has_exp = false;
  1536. for (i, b) in s[1..].bytes().enumerate() {
  1537. match b {
  1538. b'_' => {}
  1539. b'-' | b'+' => return None,
  1540. b'0'..=b'9' => has_exp = true,
  1541. _ => {
  1542. let suffix = &s[1 + i..];
  1543. if has_exp && crate::ident::xid_ok(suffix) {
  1544. return None;
  1545. } else {
  1546. break 'outer;
  1547. }
  1548. }
  1549. }
  1550. }
  1551. if has_exp {
  1552. return None;
  1553. } else {
  1554. break;
  1555. }
  1556. }
  1557. _ => break,
  1558. };
  1559. if digit >= base {
  1560. return None;
  1561. }
  1562. has_digit = true;
  1563. value *= base;
  1564. value += digit;
  1565. s = &s[1..];
  1566. }
  1567. if !has_digit {
  1568. return None;
  1569. }
  1570. let suffix = s;
  1571. if suffix.is_empty() || crate::ident::xid_ok(suffix) {
  1572. let mut repr = value.to_string();
  1573. if negative {
  1574. repr.insert(0, '-');
  1575. }
  1576. Some((repr.into_boxed_str(), suffix.to_owned().into_boxed_str()))
  1577. } else {
  1578. None
  1579. }
  1580. }
  1581. // Returns base 10 digits and suffix.
  1582. pub(crate) fn parse_lit_float(input: &str) -> Option<(Box<str>, Box<str>)> {
  1583. // Rust's floating point literals are very similar to the ones parsed by
  1584. // the standard library, except that rust's literals can contain
  1585. // ignorable underscores. Let's remove those underscores.
  1586. let mut bytes = input.to_owned().into_bytes();
  1587. let start = (*bytes.first()? == b'-') as usize;
  1588. match bytes.get(start)? {
  1589. b'0'..=b'9' => {}
  1590. _ => return None,
  1591. }
  1592. let mut read = start;
  1593. let mut write = start;
  1594. let mut has_dot = false;
  1595. let mut has_e = false;
  1596. let mut has_sign = false;
  1597. let mut has_exponent = false;
  1598. while read < bytes.len() {
  1599. match bytes[read] {
  1600. b'_' => {
  1601. // Don't increase write
  1602. read += 1;
  1603. continue;
  1604. }
  1605. b'0'..=b'9' => {
  1606. if has_e {
  1607. has_exponent = true;
  1608. }
  1609. bytes[write] = bytes[read];
  1610. }
  1611. b'.' => {
  1612. if has_e || has_dot {
  1613. return None;
  1614. }
  1615. has_dot = true;
  1616. bytes[write] = b'.';
  1617. }
  1618. b'e' | b'E' => {
  1619. match bytes[read + 1..]
  1620. .iter()
  1621. .find(|b| **b != b'_')
  1622. .unwrap_or(&b'\0')
  1623. {
  1624. b'-' | b'+' | b'0'..=b'9' => {}
  1625. _ => break,
  1626. }
  1627. if has_e {
  1628. if has_exponent {
  1629. break;
  1630. } else {
  1631. return None;
  1632. }
  1633. }
  1634. has_e = true;
  1635. bytes[write] = b'e';
  1636. }
  1637. b'-' | b'+' => {
  1638. if has_sign || has_exponent || !has_e {
  1639. return None;
  1640. }
  1641. has_sign = true;
  1642. if bytes[read] == b'-' {
  1643. bytes[write] = bytes[read];
  1644. } else {
  1645. // Omit '+'
  1646. read += 1;
  1647. continue;
  1648. }
  1649. }
  1650. _ => break,
  1651. }
  1652. read += 1;
  1653. write += 1;
  1654. }
  1655. if has_e && !has_exponent {
  1656. return None;
  1657. }
  1658. let mut digits = String::from_utf8(bytes).unwrap();
  1659. let suffix = digits.split_off(read);
  1660. digits.truncate(write);
  1661. if suffix.is_empty() || crate::ident::xid_ok(&suffix) {
  1662. Some((digits.into_boxed_str(), suffix.into_boxed_str()))
  1663. } else {
  1664. None
  1665. }
  1666. }
  1667. }