Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added octal literal support. #10243

Merged
merged 3 commits into from
Nov 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/po/ja/rust.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,13 @@ msgid ""
"and continues as any mixture hex digits and underscores."
msgstr ""

#. type: Bullet: ' * '
#: doc/rust.md:326
msgid ""
"An _octal literal_ starts with the character sequence `U+0030` `U+006F` (`0o`) "
"and continues as any mixture octal digits and underscores."
msgstr ""

#. type: Bullet: ' * '
#: doc/rust.md:326
msgid ""
Expand Down Expand Up @@ -740,6 +747,7 @@ msgid ""
"123u; // type uint\n"
"123_u; // type uint\n"
"0xff_u8; // type u8\n"
"0o70_i16; // type i16\n"
"0b1111_1111_1001_0000_i32; // type i32\n"
"~~~~\n"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion doc/po/ja/tutorial.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ msgstr "## プリミティブ型とリテラル"
msgid ""
"There are general signed and unsigned integer types, `int` and `uint`, as "
"well as 8-, 16-, 32-, and 64-bit variants, `i8`, `u16`, etc. Integers can "
"be written in decimal (`144`), hexadecimal (`0x90`), or binary "
"be written in decimal (`144`), hexadecimal (`0x90`), octal (`0o70`), or binary "
"(`0b10010000`) base. Each integral type has a corresponding literal suffix "
"that can be used to indicate the type of a literal: `i` for `int`, `u` for "
"`uint`, `i8` for the `i8` type."
Expand Down
10 changes: 9 additions & 1 deletion doc/po/rust.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ msgstr ""

#. type: Plain text
#: doc/rust.md:319
msgid "An _integer literal_ has one of three forms:"
msgid "An _integer literal_ has one of four forms:"
msgstr ""

#. type: Bullet: ' * '
Expand All @@ -678,6 +678,13 @@ msgid ""
"and continues as any mixture hex digits and underscores."
msgstr ""

#. type: Bullet: ' * '
#: doc/rust.md:326
msgid ""
"An _octal literal_ starts with the character sequence `U+0030` `U+006F` (`0o`) "
"and continues as any mixture octal digits and underscores."
msgstr ""

#. type: Bullet: ' * '
#: doc/rust.md:326
msgid ""
Expand Down Expand Up @@ -740,6 +747,7 @@ msgid ""
"123u; // type uint\n"
"123_u; // type uint\n"
"0xff_u8; // type u8\n"
"0o70_i16; // type i16\n"
"0b1111_1111_1001_0000_i32; // type i32\n"
"~~~~\n"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion doc/po/tutorial.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ msgstr ""
msgid ""
"There are general signed and unsigned integer types, `int` and `uint`, as "
"well as 8-, 16-, 32-, and 64-bit variants, `i8`, `u16`, etc. Integers can "
"be written in decimal (`144`), hexadecimal (`0x90`), or binary "
"be written in decimal (`144`), hexadecimal (`0x90`), octal (`0o70`), or binary "
"(`0b10010000`) base. Each integral type has a corresponding literal suffix "
"that can be used to indicate the type of a literal: `i` for `int`, `u` for "
"`uint`, `i8` for the `i8` type."
Expand Down
5 changes: 4 additions & 1 deletion doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,14 @@ as they are differentiated by suffixes.

##### Integer literals

An _integer literal_ has one of three forms:
An _integer literal_ has one of four forms:

* A _decimal literal_ starts with a *decimal digit* and continues with any
mixture of *decimal digits* and _underscores_.
* A _hex literal_ starts with the character sequence `U+0030` `U+0078`
(`0x`) and continues as any mixture hex digits and underscores.
* An _octal literal_ starts with the character sequence `U+0030` `U+006F`
(`0o`) and continues as any mixture octal digits and underscores.
* A _binary literal_ starts with the character sequence `U+0030` `U+0062`
(`0b`) and continues as any mixture binary digits and underscores.

Expand Down Expand Up @@ -376,6 +378,7 @@ Examples of integer literals of various forms:
123u; // type uint
123_u; // type uint
0xff_u8; // type u8
0o70_i16; // type i16
0b1111_1111_1001_0000_i32; // type i32
~~~~

Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn is_four(x: int) -> bool {

There are general signed and unsigned integer types, `int` and `uint`,
as well as 8-, 16-, 32-, and 64-bit variants, `i8`, `u16`, etc.
Integers can be written in decimal (`144`), hexadecimal (`0x90`), or
Integers can be written in decimal (`144`), hexadecimal (`0x90`), octal (`0o70`), or
binary (`0b10010000`) base. Each integral type has a corresponding literal
suffix that can be used to indicate the type of a literal: `i` for `int`,
`u` for `uint`, `i8` for the `i8` type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,18 @@
[0-9a-fA-F]
</define-regex>

<define-regex id="oct_digit" extended="true">
[0-7]
</define-regex>

<context id="number" style-ref="number">
<match extended="true">
((?&lt;=\.\.)|(?&lt;![\w\.]))
(
[1-9][0-9_]*\%{num_suffix}?|
0[0-9_]*\%{num_suffix}?|
0b[01_]+\%{int_suffix}?|
0o(\%{oct_digit}|_)+\%{int_suffix}?|
0x(\%{hex_digit}|_)+\%{int_suffix}?
)
((?![\w\.].)|(?=\.\.))
Expand Down
1 change: 1 addition & 0 deletions src/etc/kate/rust.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
<Detect2Chars char="/" char1="/" attribute="Comment" context="Commentar 1"/>
<Detect2Chars char="/" char1="*" attribute="Comment" context="Commentar 2" beginRegion="Comment"/>
<RegExpr String="0x[0-9a-fA-F_]+&rustIntSuf;" attribute="Number" context="#stay"/>
<RegExpr String="0o[0-7_]+&rustIntSuf;" attribute="Number" context="#stay"/>
<RegExpr String="0b[0-1_]+&rustIntSuf;" attribute="Number" context="#stay"/>
<RegExpr String="[0-9][0-9_]*\.[0-9_]*([eE][+-]?[0-9_]+)?(f32|f64|f)?" attribute="Number" context="#stay"/>
<RegExpr String="[0-9][0-9_]*&rustIntSuf;" attribute="Number" context="#stay"/>
Expand Down
4 changes: 4 additions & 0 deletions src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ syn match rustNumber display "\<[0-9][0-9_]*\(i\|i8\|i16\|i32\|i64\)\>"
syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\>"
syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\(u\|u8\|u16\|u32\|u64\)\>"
syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\(i8\|i16\|i32\|i64\)\>"
syn match rustOctNumber display "\<0o[0-7_]\+\>"
syn match rustOctNumber display "\<0o[0-7_]\+\(u\|u8\|u16\|u32\|u64\)\>"
syn match rustOctNumber display "\<0o[0-7_]\+\(i8\|i16\|i32\|i64\)\>"
syn match rustBinNumber display "\<0b[01_]\+\>"
syn match rustBinNumber display "\<0b[01_]\+\(u\|u8\|u16\|u32\|u64\)\>"
syn match rustBinNumber display "\<0b[01_]\+\(i8\|i16\|i32\|i64\)\>"
Expand Down Expand Up @@ -198,6 +201,7 @@ syn region rustFoldBraces start="{" end="}" transparent fold

" Default highlighting {{{1
hi def link rustHexNumber rustNumber
hi def link rustOctNumber rustNumber
hi def link rustBinNumber rustNumber
hi def link rustIdentifierPrime rustIdentifier
hi def link rustTrait rustType
Expand Down
13 changes: 13 additions & 0 deletions src/libsyntax/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ fn hex_digit_val(c: char) -> int {
fail!();
}

fn oct_digit_val(c: char) -> int {
if in_range(c, '0', '7') { return (c as int) - ('0' as int); }
fail!();
}

fn bin_digit_value(c: char) -> int { if c == '0' { return 0; } return 1; }

pub fn is_whitespace(c: char) -> bool {
Expand All @@ -293,6 +298,8 @@ fn is_hex_digit(c: char) -> bool {
in_range(c, 'A', 'F');
}

fn is_oct_digit(c: char) -> bool { return in_range(c, '0', '7'); }

fn is_bin_digit(c: char) -> bool { return c == '0' || c == '1'; }

// EFFECT: eats whitespace and comments.
Expand Down Expand Up @@ -464,6 +471,10 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
bump(rdr);
bump(rdr);
base = 16u;
} else if c == '0' && n == 'o' {
bump(rdr);
bump(rdr);
base = 8u;
} else if c == '0' && n == 'b' {
bump(rdr);
bump(rdr);
Expand Down Expand Up @@ -529,6 +540,8 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
match base {
16u => fatal_span(rdr, start_bpos, rdr.last_pos,
~"hexadecimal float literal is not supported"),
8u => fatal_span(rdr, start_bpos, rdr.last_pos,
~"octal float literal is not supported"),
2u => fatal_span(rdr, start_bpos, rdr.last_pos,
~"binary float literal is not supported"),
_ => ()
Expand Down
17 changes: 17 additions & 0 deletions src/test/compile-fail/no-oct-float-literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern:octal float literal is not supported

fn main() {
0o123f64;
0o123.456;
0o123p4f;
}
27 changes: 27 additions & 0 deletions src/test/run-pass/integer-literal-radix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub fn main() {
let a = 0xBEEF;
let b = 0o755;
let c = 0b10101;
let d = -0xBEEF;
let e = -0o755;
let f = -0b10101;

assert_eq!(a, 48879);
assert_eq!(b, 493);
assert_eq!(c, 21);
assert_eq!(d, -48879);
assert_eq!(e, -493);
assert_eq!(f, -21);


}