Skip to content

Commit

Permalink
syntax: Some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed May 23, 2019
1 parent ca2a50f commit 90d15e7
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 228 deletions.
350 changes: 146 additions & 204 deletions src/libsyntax/parse/literal.rs

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub enum LitKind {
Err,
}

/// A literal token.
#[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)]
pub struct Lit {
pub kind: LitKind,
Expand All @@ -81,6 +82,7 @@ pub struct Lit {
}

impl LitKind {
/// An English article for the literal token kind.
crate fn article(self) -> &'static str {
match self {
Integer | Err => "an",
Expand All @@ -91,13 +93,13 @@ impl LitKind {
crate fn descr(self) -> &'static str {
match self {
Bool => panic!("literal token contains `Lit::Bool`"),
Byte => "byte literal",
Char => "char literal",
Integer => "integer literal",
Float => "float literal",
Str | StrRaw(..) => "string literal",
ByteStr | ByteStrRaw(..) => "byte string literal",
Err => "invalid literal",
Byte => "byte",
Char => "char",
Integer => "integer",
Float => "float",
Str | StrRaw(..) => "string",
ByteStr | ByteStrRaw(..) => "byte string",
Err => "error",
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ fn binop_to_string(op: BinOpToken) -> &'static str {
}
}

pub fn literal_to_string(token::Lit { kind, symbol, suffix }: token::Lit) -> String {
pub fn literal_to_string(lit: token::Lit) -> String {
let token::Lit { kind, symbol, suffix } = lit;
let mut out = match kind {
token::Byte => format!("b'{}'", symbol),
token::Char => format!("'{}'", symbol),
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax_ext/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl<'a> Rustc<'a> {
}
}

pub fn lit(&mut self, kind: token::LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Literal {
fn lit(&mut self, kind: token::LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Literal {
Literal {
lit: token::Lit::new(kind, symbol, suffix),
span: server::Span::call_site(self),
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ symbols! {
extern_prelude,
extern_types,
f16c_target_feature,
f32,
f64,
feature,
ffi_returns_twice,
field_init_shorthand,
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/parser/lex-bad-numeric-literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ fn main() {
0o; //~ ERROR: no valid digits
1e+; //~ ERROR: expected at least one digit in exponent
0x539.0; //~ ERROR: hexadecimal float literal is not supported
9900000000000000000000000000999999999999999999999999999999; //~ ERROR: integer literal is too large
9900000000000000000000000000999999999999999999999999999999; //~ ERROR: integer literal is too large
9900000000000000000000000000999999999999999999999999999999;
//~^ ERROR: integer literal is too large
9900000000000000000000000000999999999999999999999999999999;
//~^ ERROR: integer literal is too large
0x; //~ ERROR: no valid digits
0xu32; //~ ERROR: no valid digits
0ou32; //~ ERROR: no valid digits
Expand Down
20 changes: 10 additions & 10 deletions src/test/ui/parser/lex-bad-numeric-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -65,43 +65,43 @@ LL | 0x539.0;
| ^^^^^^^

error: no valid digits found for number
--> $DIR/lex-bad-numeric-literals.rs:18:5
--> $DIR/lex-bad-numeric-literals.rs:20:5
|
LL | 0x;
| ^^

error: no valid digits found for number
--> $DIR/lex-bad-numeric-literals.rs:19:5
--> $DIR/lex-bad-numeric-literals.rs:21:5
|
LL | 0xu32;
| ^^

error: no valid digits found for number
--> $DIR/lex-bad-numeric-literals.rs:20:5
--> $DIR/lex-bad-numeric-literals.rs:22:5
|
LL | 0ou32;
| ^^

error: no valid digits found for number
--> $DIR/lex-bad-numeric-literals.rs:21:5
--> $DIR/lex-bad-numeric-literals.rs:23:5
|
LL | 0bu32;
| ^^

error: no valid digits found for number
--> $DIR/lex-bad-numeric-literals.rs:22:5
--> $DIR/lex-bad-numeric-literals.rs:24:5
|
LL | 0b;
| ^^

error: octal float literal is not supported
--> $DIR/lex-bad-numeric-literals.rs:24:5
--> $DIR/lex-bad-numeric-literals.rs:26:5
|
LL | 0o123.456;
| ^^^^^^^^^

error: binary float literal is not supported
--> $DIR/lex-bad-numeric-literals.rs:26:5
--> $DIR/lex-bad-numeric-literals.rs:28:5
|
LL | 0b111.101;
| ^^^^^^^^^
Expand All @@ -119,19 +119,19 @@ LL | 9900000000000000000000000000999999999999999999999999999999;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: integer literal is too large
--> $DIR/lex-bad-numeric-literals.rs:17:5
--> $DIR/lex-bad-numeric-literals.rs:18:5
|
LL | 9900000000000000000000000000999999999999999999999999999999;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: octal float literal is not supported
--> $DIR/lex-bad-numeric-literals.rs:23:5
--> $DIR/lex-bad-numeric-literals.rs:25:5
|
LL | 0o123f64;
| ^^^^^^^^ not supported

error: binary float literal is not supported
--> $DIR/lex-bad-numeric-literals.rs:25:5
--> $DIR/lex-bad-numeric-literals.rs:27:5
|
LL | 0b101f64;
| ^^^^^^^^ not supported
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/no-hex-float-literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ fn main() {
0x567.89;
//~^ ERROR hexadecimal float literal is not supported
0xDEAD.BEEFp-2f;
//~^ ERROR invalid suffix `f` for integer literal
//~^ ERROR invalid suffix `f` for float literal
//~| ERROR `{integer}` is a primitive type and therefore doesn't have fields
}
4 changes: 2 additions & 2 deletions src/test/ui/parser/no-hex-float-literal.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ error: hexadecimal float literal is not supported
LL | 0x567.89;
| ^^^^^^^^

error: invalid suffix `f` for integer literal
error: invalid suffix `f` for float literal
--> $DIR/no-hex-float-literal.rs:6:18
|
LL | 0xDEAD.BEEFp-2f;
| ^^ invalid suffix `f`
|
= help: the suffix must be one of the integral types (`u32`, `isize`, etc)
= help: valid suffixes are `f32` and `f64`

error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/no-hex-float-literal.rs:2:11
Expand Down

0 comments on commit 90d15e7

Please sign in to comment.