Skip to content

Commit 7e78565

Browse files
committed
Version 1.27
Bumped Rust compiler version. * 'crate' visibility (see #![feature(crate_visibility_modifier)]) * raw identifiers (see #![feature(raw_identifiers)]) * remove in-place syntax tests (didn't actually remove the AST node) * remove '_' token (it got merged with IdentTok)
1 parent 9f23b27 commit 7e78565

File tree

13 files changed

+60
-55
lines changed

13 files changed

+60
-55
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Revision history for language-rust
22

3+
## 0.2.0.27 -- 2018-04-22
4+
5+
* Bump Rust version
36

47
## 0.1.1.26 -- 2018-03-02
58

get-rust-sources.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ rm -rf $TEMP
7575
# Print disclaimer
7676
echo "WARNING: Don't expect the 'rustc-tests' suite to necessarily"
7777
echo " work on all the files this produces. A failure in the"
78-
echo " test suite is only a bug if `rustc` succeeds on the"
78+
echo " test suite is only a bug if \`rustc\` succeeds on the"
7979
echo " same file."
8080

language-rust.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: language-rust
2-
version: 0.1.1.26
2+
version: 0.2.0.27
33

44
synopsis: Parsing and pretty printing of Rust code
55
description: Language Rust is a library for the analysis of Rust code. It includes a

sample-sources/expressions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
fn main() {
22
let x = box "foo";
3-
let x = y <- 1;
43
let x = [1,2,3];
54
let x = foo(1,2,x);
65
let x = x.foo::<Bar, Baz>(a, b, c, d);
@@ -49,6 +48,7 @@ fn main() {
4948
return;
5049
yield 0;
5150
yield;
51+
let r#return = 0;
5252

5353
match true {
5454
true => move | | { 1 },

sample-sources/precedences.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55
- a == box b [0] [0] [0] .0 || c [0] ^= d % e = f != g;
66
& a *= ! b ^= c * d | e [0];
77
a ^ b < * c ? || box d ? %= e [0] = - f < box g % & h &= i ? % & & & j ?;
8-
a &= - box b ? >> c [0] <= d || & - e ^= f = & g .0 ? [0] <- h .0 .0 += i [0] << j ?;
8+
a &= - box b ? >> c [0] <= d || & - e ^= f = & g .0 ? [0] - h .0 .0 += i [0] << j ?;
99
a >> & b && - box c >= d .. e .0 [0] + ! f * g >= h && & - i [0] * j;
1010
* a > b *= c * d && e ^ - & f ? *= g %= h .0 [0] ? & i -= j ?;
1111
* a >> b %= c .0 != * - d [0] || e <= f << g .0 | ! h += - & i >= j .0;
@@ -14,9 +14,9 @@ fn main() {
1414
a * b &= c & * d ? |= & ! e [0] != & f .0 = g >> ! ! * - h ? ^ i ? %= - j;
1515
& a != b [0] % c - & & ! - box - d & e [0] += g ? << h /= i <<= .. .. j;
1616
a [0] >>= b | & ! c / ! & d [0] ? / box e .. - f * * & * - g < h && j;
17-
a & b <- c *= &&*d? << *e.0?.0 ^= !f?[0].0 >> -g - h .. i??[0] | j;
17+
a & b - c *= &&*d? << *e.0?.0 ^= !f?[0].0 >> -g - h .. i??[0] | j;
1818
-a[0] - b[0][0]??.0 != c -= d || e[0][0] *= &f = !!g[0] / h ^ i | box!j[0];
19-
a != -b.0 <- box!c.0?.0 *= -e? <= *-f.0 ^= g >> box box h + i + j;
19+
a != -b.0 - box!c.0?.0 *= -e? <= *-f.0 ^= g >> box box h + i + j;
2020
!&&-box a??.0 >> b |= c[0]? ^ d * e | &*f[0] <= box -g? ... &h * &i - j[0][0];
2121
a & b &= c[0] <= d + -!-box*e[0]?[0] % f.0? + -g >>= h[0] /= i = j;
2222
a[0][0] >>= *b.0 .0? / c ^ d >>= !e >= box f ... g >= h + i? || j;
@@ -72,8 +72,8 @@ fn range_expressions() {
7272
// precedences of ranges in general
7373
fn general_ranges() {
7474
let _ = ..;
75-
let _ = a <- c .. 3;
76-
let _ = a <- c == 3;
75+
let _ = a - c .. 3;
76+
let _ = a - c == 3;
7777

7878
let _ = 1 + 2..;
7979
let _ = 1 == 2..;

src/Language/Rust/Data/Ident.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Data.Semigroup ( Semigroup(..) )
2929
-- | An identifier
3030
data Ident
3131
= Ident { name :: Name -- ^ payload of the identifier
32+
, raw :: Bool -- ^ whether the identifier is raw
3233
, hash :: {-# UNPACK #-} !Int -- ^ hash for quick comparision
3334
} deriving (Data, Typeable, Generic, NFData)
3435

@@ -41,26 +42,28 @@ instance IsString Ident where
4142

4243
-- | Uses 'hash' to short-circuit
4344
instance Eq Ident where
44-
i1 == i2 = hash i1 == hash i2 && name i1 == name i2
45-
i1 /= i2 = hash i1 /= hash i2 || name i1 /= name i2
45+
i1 == i2 = hash i1 == hash i2 && name i1 == name i2 && raw i1 == raw i2
46+
i1 /= i2 = hash i1 /= hash i2 || name i1 /= name i2 || raw i1 /= raw i2
4647

4748
-- | Uses 'hash' to short-circuit
4849
instance Ord Ident where
4950
compare i1 i2 = case compare i1 i2 of
50-
EQ -> compare (name i1) (name i2)
51+
EQ -> compare (raw i1, name i1) (raw i2, name i2)
5152
rt -> rt
5253

54+
-- | "Forgets" about whether either argument was raw
5355
instance Monoid Ident where
5456
mappend = (<>)
5557
mempty = mkIdent ""
5658

59+
-- | "Forgets" about whether either argument was raw
5760
instance Semigroup Ident where
58-
Ident n1 _ <> Ident n2 _ = mkIdent (n1 <> n2)
61+
Ident n1 _ _ <> Ident n2 _ _ = mkIdent (n1 <> n2)
5962

6063

6164
-- | Smart constructor for making an 'Ident'.
6265
mkIdent :: String -> Ident
63-
mkIdent s = Ident s (hashString s)
66+
mkIdent s = Ident s False (hashString s)
6467

6568
-- | Hash a string into an 'Int'
6669
hashString :: String -> Int

src/Language/Rust/Parser/Internal.y

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ import qualified Data.List.NonEmpty as N
238238
innerDoc { Spanned (Doc _ Inner _) _ }
239239

240240
-- Identifiers.
241+
'_' { Spanned (IdentTok "_") _ }
241242
IDENT { Spanned IdentTok{} _ }
242-
'_' { Spanned Underscore _ }
243243

244244
-- Lifetimes.
245245
LIFETIME { Spanned (LifetimeTok _) _ }
@@ -278,7 +278,7 @@ import qualified Data.List.NonEmpty as N
278278
%nonassoc mut DEF EQ '::'
279279

280280
-- These are all identifiers of sorts ('union' and 'default' are "weak" keywords)
281-
%nonassoc IDENT ntIdent default union catch self Self super auto dyn
281+
%nonassoc IDENT ntIdent default union catch self Self super auto dyn crate
282282

283283
-- These are all very low precedence unary operators
284284
%nonassoc box return yield break continue for IMPLTRAIT LAMBDA
@@ -575,6 +575,7 @@ mod_path :: { Path Span }
575575

576576
self_or_ident :: { Spanned Ident }
577577
: ident { $1 }
578+
| crate { Spanned "crate" (spanOf $1) }
578579
| self { Spanned "self" (spanOf $1) }
579580
| Self { Spanned "Self" (spanOf $1) }
580581
| super { Spanned "super" (spanOf $1) }
@@ -585,7 +586,7 @@ self_or_ident :: { Spanned Ident }
585586
-----------
586587

587588
lifetime :: { Lifetime Span }
588-
: LIFETIME { let Spanned (LifetimeTok (Ident l _)) s = $1 in Lifetime l s }
589+
: LIFETIME { let Spanned (LifetimeTok (Ident l _ _)) s = $1 in Lifetime l s }
589590

590591
-- parse_trait_ref()
591592
trait_ref :: { TraitRef Span }
@@ -1125,7 +1126,7 @@ blockpostfix_expr :: { Expr Span }
11251126

11261127
-- labels on loops
11271128
label :: { Label Span }
1128-
: LIFETIME { let Spanned (LifetimeTok (Ident l _)) s = $1 in Label l s }
1129+
: LIFETIME { let Spanned (LifetimeTok (Ident l _ _)) s = $1 in Label l s }
11291130

11301131
-- Literal expressions (composed of just literals)
11311132
lit_expr :: { Expr Span }
@@ -1510,6 +1511,7 @@ vis :: { Spanned (Visibility Span) }
15101511
: {- empty -} %prec VIS { Spanned InheritedV mempty }
15111512
| pub %prec VIS { Spanned PublicV (spanOf $1) }
15121513
| pub '(' crate ')' { Spanned CrateV ($1 # $4) }
1514+
| crate { Spanned CrateV (spanOf $1) }
15131515
| pub '(' in mod_path ')' { Spanned (RestrictedV $4) ($1 # $5) }
15141516
| pub '(' super ')' { Spanned (RestrictedV (Path False [PathSegment "super" Nothing (spanOf
15151517
$3)] (spanOf $3))) ($1 # $4) }
@@ -1903,8 +1905,8 @@ addAttrs as (Yield as' e s) = Yield (as ++ as') e s
19031905
-- | Given a 'LitTok' token that is expected to result in a valid literal, construct the associated
19041906
-- literal. Note that this should _never_ fail on a token produced by the lexer.
19051907
lit :: Spanned Token -> Lit Span
1906-
lit (Spanned (IdentTok (Ident "true" _)) s) = Bool True Unsuffixed s
1907-
lit (Spanned (IdentTok (Ident "false" _)) s) = Bool False Unsuffixed s
1908+
lit (Spanned (IdentTok (Ident "true" False _)) s) = Bool True Unsuffixed s
1909+
lit (Spanned (IdentTok (Ident "false" False _)) s) = Bool False Unsuffixed s
19081910
lit (Spanned (LiteralTok litTok suffix_m) s) = translateLit litTok suffix s
19091911
where
19101912
suffix = case suffix_m of

src/Language/Rust/Parser/Lexer.x

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,7 @@ import Data.Word ( Word8 )
914914
| \xdb40 [\xdd00-\xddee]
915915

916916
@ident = @xid_start @xid_continue*
917+
@raw_ident = r \# @ident
917918

918919
@lifetime = \' @ident
919920

@@ -1041,7 +1042,6 @@ $white+ { \s -> pure (Space Whitespace s) }
10411042
"}" { token (CloseDelim Brace) }
10421043
"#" { token Pound }
10431044
"$" { token Dollar }
1044-
"_" { token Underscore }
10451045

10461046
@lit_integer { \i -> literal (IntegerTok i) }
10471047
@lit_float { \f -> literal (FloatTok f) }
@@ -1070,6 +1070,7 @@ $white+ { \s -> pure (Space Whitespace s) }
10701070
<lits> @ident { \s -> pure (IdentTok (mkIdent s)) }
10711071

10721072
\? { token Question }
1073+
@raw_ident { \s -> pure (IdentTok ((mkIdent (drop 2 s)){ raw = True })) }
10731074
@ident { \s -> pure (IdentTok (mkIdent s)) }
10741075
@lifetime { \s -> (pure (LifetimeTok (mkIdent (tail s))) :: P Token) }
10751076

@@ -1102,7 +1103,7 @@ literal lit = do
11021103
AlexToken (pos',inp') len action -> do
11031104
tok <- action (peekChars len inp)
11041105
case tok of
1105-
IdentTok (Ident suf _) -> do
1106+
IdentTok (Ident suf False _) -> do
11061107
setPosition pos'
11071108
setInput inp'
11081109
pure (LiteralTok lit (Just suf))

src/Language/Rust/Pretty/Internal.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ printName = pretty
124124

125125
-- | Print an identifier
126126
printIdent :: Ident -> Doc a
127-
printIdent (Ident s _) = pretty s
127+
printIdent (Ident s False _) = pretty s
128+
printIdent (Ident s True _) = "r#" <> pretty s
128129

129130
-- | Print a type (@print_type@ with @print_ty_fn@ inlined)
130131
-- Types are expected to always be only one line
@@ -278,7 +279,6 @@ printToken (CloseDelim NoDelim) = ""
278279
printToken (LiteralTok l s) = noIndent $ printLitTok l <> perhaps printName s
279280
-- Name components
280281
printToken (IdentTok i) = printIdent i
281-
printToken Underscore = "_"
282282
printToken (LifetimeTok i) = "'" <> printIdent i
283283
printToken (Space Whitespace _) = " "
284284
printToken (Space Comment n) = "/*" <> printName n <> " */"
@@ -599,8 +599,8 @@ printAttr (SugaredDoc Outer False c x) _ = annotate x (flatAlt ("///" <> pretty
599599

600600
-- | Print an identifier as is, or as cooked string if containing a hyphen
601601
printCookedIdent :: Ident -> Doc a
602-
printCookedIdent ident@(Ident str _)
603-
| '-' `elem` str = printStr Cooked str
602+
printCookedIdent ident@(Ident str raw _)
603+
| '-' `elem` str && not raw = printStr Cooked str
604604
| otherwise = printIdent ident
605605

606606

src/Language/Rust/Pretty/Resolve.hs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,13 @@ instance (Typeable a, Monoid a) => Resolve (SourceFile a) where resolveM = resol
231231
-- * it is a keyword
232232
--
233233
resolveIdent :: Ident -> ResolveM Ident
234-
resolveIdent i@(Ident s _) =
234+
resolveIdent i@(Ident s r _) =
235235
scope i $ case toks of
236236
Right [Spanned (IdentTok i') _]
237237
| i /= i' -> err i ("identifier `" ++ s ++ "' does not lex properly")
238-
| i `elem` keywords -> err i ("identifier `" ++ s ++ "' is a keyword")
238+
| i `elem` keywords && not r -> do
239+
correct ("identifier `" ++ s ++ "' is a keyword")
240+
pure i{ raw = True }
239241
| otherwise -> pure i
240242
_ -> err i ("identifier `" ++ s ++ "' does not lex properly")
241243

@@ -246,8 +248,9 @@ resolveIdent i@(Ident s _) =
246248
\ static struct super trait true type unsafe use where while\
247249
\ abstract alignof become do final macro offsetof override priv\
248250
\ proc pure sizeof typeof unsized virtual yield"
249-
250-
toks = execParser (lexTokens lexToken) (inputStreamFromString s) initPos
251+
252+
s' = (if r then "r#" else "") ++ s
253+
toks = execParser (lexTokens lexToken) (inputStreamFromString s') initPos
251254

252255
instance Resolve Ident where resolveM = resolveIdent
253256

@@ -356,9 +359,10 @@ resolvePath t p@(Path g segs x) = scope p $
356359
resolveSeg :: (Typeable a, Monoid a) => PathSegment a -> ResolveM (PathSegment a)
357360
resolveSeg (PathSegment i a x') = do
358361
i' <- case i of
359-
Ident "self" _ -> pure i
360-
Ident "Self" _ -> pure i
361-
Ident "super" _ -> pure i
362+
Ident "self" False _ -> pure i
363+
Ident "Self" False _ -> pure i
364+
Ident "super" False _ -> pure i
365+
Ident "crate" False _ -> pure i
362366
_ -> resolveIdent i
363367
a' <- traverse resolvePathParameters a
364368
pure (PathSegment i' a' x')
@@ -540,8 +544,8 @@ resolveArg GeneralArg a@(Arg p t x) = scope a $ do
540544

541545
-- | Check whether an argument is one of the "self"-alike forms
542546
isSelfAlike :: Arg a -> Bool
543-
isSelfAlike (Arg Nothing (PathTy Nothing (Path False [PathSegment (Ident "self" _) Nothing _] _) _) _) = True
544-
isSelfAlike (Arg Nothing (Rptr _ _ (PathTy Nothing (Path False [PathSegment (Ident "self" _) Nothing _] _) _) _) _) = True
547+
isSelfAlike (Arg Nothing (PathTy Nothing (Path False [PathSegment (Ident "self" False _) Nothing _] _) _) _) = True
548+
isSelfAlike (Arg Nothing (Rptr _ _ (PathTy Nothing (Path False [PathSegment (Ident "self" False _) Nothing _] _) _) _) _) = True
545549
isSelfAlike _ = False
546550

547551
instance (Typeable a, Monoid a) => Resolve (Arg a) where resolveM = resolveArg NamedArg

0 commit comments

Comments
 (0)