Skip to content

Commit 4cb3586

Browse files
Rollup merge of rust-lang#62851 - matklad:unescape, r=petrochenkov
move unescape module to rustc_lexer It makes sense to keep the definition of escape sequences closer to the lexer itself, and it is also a bit of code that I would like to share with rust-analyzer. r? @petrochenkov
2 parents 8afc53c + e63fe15 commit 4cb3586

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

src/librustc_lexer/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![cfg_attr(not(feature = "unicode-xid"), feature(unicode_internals))]
55

66
mod cursor;
7+
pub mod unescape;
78

89
use crate::cursor::{Cursor, EOF_CHAR};
910

src/libsyntax/parse/unescape.rs renamed to src/librustc_lexer/src/unescape.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::str::Chars;
55
use std::ops::Range;
66

77
#[derive(Debug, PartialEq, Eq)]
8-
pub(crate) enum EscapeError {
8+
pub enum EscapeError {
99
ZeroChars,
1010
MoreThanOneChar,
1111

@@ -35,22 +35,22 @@ pub(crate) enum EscapeError {
3535

3636
/// Takes a contents of a char literal (without quotes), and returns an
3737
/// unescaped char or an error
38-
pub(crate) fn unescape_char(literal_text: &str) -> Result<char, (usize, EscapeError)> {
38+
pub fn unescape_char(literal_text: &str) -> Result<char, (usize, EscapeError)> {
3939
let mut chars = literal_text.chars();
4040
unescape_char_or_byte(&mut chars, Mode::Char)
4141
.map_err(|err| (literal_text.len() - chars.as_str().len(), err))
4242
}
4343

4444
/// Takes a contents of a string literal (without quotes) and produces a
4545
/// sequence of escaped characters or errors.
46-
pub(crate) fn unescape_str<F>(literal_text: &str, callback: &mut F)
46+
pub fn unescape_str<F>(literal_text: &str, callback: &mut F)
4747
where
4848
F: FnMut(Range<usize>, Result<char, EscapeError>),
4949
{
5050
unescape_str_or_byte_str(literal_text, Mode::Str, callback)
5151
}
5252

53-
pub(crate) fn unescape_byte(literal_text: &str) -> Result<u8, (usize, EscapeError)> {
53+
pub fn unescape_byte(literal_text: &str) -> Result<u8, (usize, EscapeError)> {
5454
let mut chars = literal_text.chars();
5555
unescape_char_or_byte(&mut chars, Mode::Byte)
5656
.map(byte_from_char)
@@ -59,7 +59,7 @@ pub(crate) fn unescape_byte(literal_text: &str) -> Result<u8, (usize, EscapeErro
5959

6060
/// Takes a contents of a string literal (without quotes) and produces a
6161
/// sequence of escaped characters or errors.
62-
pub(crate) fn unescape_byte_str<F>(literal_text: &str, callback: &mut F)
62+
pub fn unescape_byte_str<F>(literal_text: &str, callback: &mut F)
6363
where
6464
F: FnMut(Range<usize>, Result<u8, EscapeError>),
6565
{
@@ -72,7 +72,7 @@ where
7272
/// sequence of characters or errors.
7373
/// NOTE: Raw strings do not perform any explicit character escaping, here we
7474
/// only translate CRLF to LF and produce errors on bare CR.
75-
pub(crate) fn unescape_raw_str<F>(literal_text: &str, callback: &mut F)
75+
pub fn unescape_raw_str<F>(literal_text: &str, callback: &mut F)
7676
where
7777
F: FnMut(Range<usize>, Result<char, EscapeError>),
7878
{
@@ -83,7 +83,7 @@ where
8383
/// sequence of characters or errors.
8484
/// NOTE: Raw strings do not perform any explicit character escaping, here we
8585
/// only translate CRLF to LF and produce errors on bare CR.
86-
pub(crate) fn unescape_raw_byte_str<F>(literal_text: &str, callback: &mut F)
86+
pub fn unescape_raw_byte_str<F>(literal_text: &str, callback: &mut F)
8787
where
8888
F: FnMut(Range<usize>, Result<u8, EscapeError>),
8989
{
@@ -93,26 +93,26 @@ where
9393
}
9494

9595
#[derive(Debug, Clone, Copy)]
96-
pub(crate) enum Mode {
96+
pub enum Mode {
9797
Char,
9898
Str,
9999
Byte,
100100
ByteStr,
101101
}
102102

103103
impl Mode {
104-
fn in_single_quotes(self) -> bool {
104+
pub fn in_single_quotes(self) -> bool {
105105
match self {
106106
Mode::Char | Mode::Byte => true,
107107
Mode::Str | Mode::ByteStr => false,
108108
}
109109
}
110110

111-
pub(crate) fn in_double_quotes(self) -> bool {
111+
pub fn in_double_quotes(self) -> bool {
112112
!self.in_single_quotes()
113113
}
114114

115-
pub(crate) fn is_bytes(self) -> bool {
115+
pub fn is_bytes(self) -> bool {
116116
match self {
117117
Mode::Byte | Mode::ByteStr => true,
118118
Mode::Char | Mode::Str => false,

src/libsyntax/parse/lexer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::parse::ParseSess;
22
use crate::parse::token::{self, Token, TokenKind};
33
use crate::symbol::{sym, Symbol};
4-
use crate::parse::unescape;
54
use crate::parse::unescape_error_reporting::{emit_unescape_error, push_escaped_char};
65

76
use errors::{FatalError, Diagnostic, DiagnosticBuilder};
87
use syntax_pos::{BytePos, Pos, Span, NO_EXPANSION};
98
use rustc_lexer::Base;
9+
use rustc_lexer::unescape;
1010

1111
use std::borrow::Cow;
1212
use std::char;

src/libsyntax/parse/literal.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ use crate::ast::{self, Lit, LitKind};
44
use crate::parse::parser::Parser;
55
use crate::parse::PResult;
66
use crate::parse::token::{self, Token, TokenKind};
7-
use crate::parse::unescape::{unescape_char, unescape_byte};
8-
use crate::parse::unescape::{unescape_str, unescape_byte_str};
9-
use crate::parse::unescape::{unescape_raw_str, unescape_raw_byte_str};
107
use crate::print::pprust;
118
use crate::symbol::{kw, sym, Symbol};
129
use crate::tokenstream::{TokenStream, TokenTree};
@@ -15,6 +12,9 @@ use errors::{Applicability, Handler};
1512
use log::debug;
1613
use rustc_data_structures::sync::Lrc;
1714
use syntax_pos::Span;
15+
use rustc_lexer::unescape::{unescape_char, unescape_byte};
16+
use rustc_lexer::unescape::{unescape_str, unescape_byte_str};
17+
use rustc_lexer::unescape::{unescape_raw_str, unescape_raw_byte_str};
1818

1919
use std::ascii;
2020

src/libsyntax/parse/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub mod token;
3232
crate mod classify;
3333
crate mod diagnostics;
3434
crate mod literal;
35-
crate mod unescape;
3635
crate mod unescape_error_reporting;
3736

3837
/// Info about a parsing session.

src/libsyntax/parse/unescape_error_reporting.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
use std::ops::Range;
44
use std::iter::once;
55

6+
use rustc_lexer::unescape::{EscapeError, Mode};
67
use syntax_pos::{Span, BytePos};
78

89
use crate::errors::{Handler, Applicability};
910

10-
use super::unescape::{EscapeError, Mode};
11-
1211
pub(crate) fn emit_unescape_error(
1312
handler: &Handler,
1413
// interior part of the literal, without quotes

0 commit comments

Comments
 (0)