Skip to content

Commit

Permalink
move unescape module to rustc_lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Jul 21, 2019
1 parent 83dfe7b commit e63fe15
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/librustc_lexer/src/lib.rs
Expand Up @@ -4,6 +4,7 @@
#![cfg_attr(not(feature = "unicode-xid"), feature(unicode_internals))]

mod cursor;
pub mod unescape;

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

Expand Down
Expand Up @@ -5,7 +5,7 @@ use std::str::Chars;
use std::ops::Range;

#[derive(Debug, PartialEq, Eq)]
pub(crate) enum EscapeError {
pub enum EscapeError {
ZeroChars,
MoreThanOneChar,

Expand Down Expand Up @@ -35,22 +35,22 @@ pub(crate) enum EscapeError {

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

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

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

/// Takes a contents of a string literal (without quotes) and produces a
/// sequence of escaped characters or errors.
pub(crate) fn unescape_byte_str<F>(literal_text: &str, callback: &mut F)
pub fn unescape_byte_str<F>(literal_text: &str, callback: &mut F)
where
F: FnMut(Range<usize>, Result<u8, EscapeError>),
{
Expand All @@ -72,7 +72,7 @@ where
/// sequence of characters or errors.
/// NOTE: Raw strings do not perform any explicit character escaping, here we
/// only translate CRLF to LF and produce errors on bare CR.
pub(crate) fn unescape_raw_str<F>(literal_text: &str, callback: &mut F)
pub fn unescape_raw_str<F>(literal_text: &str, callback: &mut F)
where
F: FnMut(Range<usize>, Result<char, EscapeError>),
{
Expand All @@ -83,7 +83,7 @@ where
/// sequence of characters or errors.
/// NOTE: Raw strings do not perform any explicit character escaping, here we
/// only translate CRLF to LF and produce errors on bare CR.
pub(crate) fn unescape_raw_byte_str<F>(literal_text: &str, callback: &mut F)
pub fn unescape_raw_byte_str<F>(literal_text: &str, callback: &mut F)
where
F: FnMut(Range<usize>, Result<u8, EscapeError>),
{
Expand All @@ -93,26 +93,26 @@ where
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum Mode {
pub enum Mode {
Char,
Str,
Byte,
ByteStr,
}

impl Mode {
fn in_single_quotes(self) -> bool {
pub fn in_single_quotes(self) -> bool {
match self {
Mode::Char | Mode::Byte => true,
Mode::Str | Mode::ByteStr => false,
}
}

pub(crate) fn in_double_quotes(self) -> bool {
pub fn in_double_quotes(self) -> bool {
!self.in_single_quotes()
}

pub(crate) fn is_bytes(self) -> bool {
pub fn is_bytes(self) -> bool {
match self {
Mode::Byte | Mode::ByteStr => true,
Mode::Char | Mode::Str => false,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/lexer/mod.rs
@@ -1,12 +1,12 @@
use crate::parse::ParseSess;
use crate::parse::token::{self, Token, TokenKind};
use crate::symbol::{sym, Symbol};
use crate::parse::unescape;
use crate::parse::unescape_error_reporting::{emit_unescape_error, push_escaped_char};

use errors::{FatalError, Diagnostic, DiagnosticBuilder};
use syntax_pos::{BytePos, Pos, Span, NO_EXPANSION};
use rustc_lexer::Base;
use rustc_lexer::unescape;

use std::borrow::Cow;
use std::char;
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/parse/literal.rs
Expand Up @@ -4,9 +4,6 @@ use crate::ast::{self, Lit, LitKind};
use crate::parse::parser::Parser;
use crate::parse::PResult;
use crate::parse::token::{self, Token, TokenKind};
use crate::parse::unescape::{unescape_char, unescape_byte};
use crate::parse::unescape::{unescape_str, unescape_byte_str};
use crate::parse::unescape::{unescape_raw_str, unescape_raw_byte_str};
use crate::print::pprust;
use crate::symbol::{kw, sym, Symbol};
use crate::tokenstream::{TokenStream, TokenTree};
Expand All @@ -15,6 +12,9 @@ use errors::{Applicability, Handler};
use log::debug;
use rustc_data_structures::sync::Lrc;
use syntax_pos::Span;
use rustc_lexer::unescape::{unescape_char, unescape_byte};
use rustc_lexer::unescape::{unescape_str, unescape_byte_str};
use rustc_lexer::unescape::{unescape_raw_str, unescape_raw_byte_str};

use std::ascii;

Expand Down
1 change: 0 additions & 1 deletion src/libsyntax/parse/mod.rs
Expand Up @@ -32,7 +32,6 @@ pub mod token;
crate mod classify;
crate mod diagnostics;
crate mod literal;
crate mod unescape;
crate mod unescape_error_reporting;

/// Info about a parsing session.
Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/parse/unescape_error_reporting.rs
Expand Up @@ -3,12 +3,11 @@
use std::ops::Range;
use std::iter::once;

use rustc_lexer::unescape::{EscapeError, Mode};
use syntax_pos::{Span, BytePos};

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

use super::unescape::{EscapeError, Mode};

pub(crate) fn emit_unescape_error(
handler: &Handler,
// interior part of the literal, without quotes
Expand Down

0 comments on commit e63fe15

Please sign in to comment.