Skip to content

Commit

Permalink
Removed Name as a datatype - it is just a synonym for String now.
Browse files Browse the repository at this point in the history
  • Loading branch information
harpocrates committed Mar 13, 2017
1 parent 6a42db1 commit 6b15d8a
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 169 deletions.
28 changes: 14 additions & 14 deletions src/Language/Rust/Parser/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Language.Rust.Data.InputStream
import Language.Rust.Data.Position
import Language.Rust.Parser.ParseMonad
import Language.Rust.Syntax.Token
import Language.Rust.Syntax.Ident (mkIdent, Ident(..), Name(..))
import Language.Rust.Syntax.Ident (mkIdent, Ident(..))

import Data.Word (Word8)
import Data.Char (chr)
Expand Down Expand Up @@ -941,7 +941,7 @@ $hexit = [0-9a-fA-F]

tokens :-

$white+ { \s -> pure (Space Whitespace (Name s)) }
$white+ { \s -> pure (Space Whitespace s) }

"=" { token Equal }
"<" { token Less }
Expand Down Expand Up @@ -999,27 +999,27 @@ $white+ { \s -> pure (Space Whitespace (Name s)) }
"$" { token Dollar }
"_" { token Underscore }

@lit_integer { \i -> literal (IntegerTok (Name i)) }
@lit_float { \f -> literal (FloatTok (Name f)) }
@lit_integer { \i -> literal (IntegerTok i) }
@lit_float { \f -> literal (FloatTok f) }
@lit_float / [^\._a-zA-Z]
{ \f -> literal (FloatTok (Name f)) }
{ \f -> literal (FloatTok f) }
@lit_float2 / [^\.]
{ \f -> literal (FloatTok (Name f)) }
{ \f -> literal (FloatTok f) }

@lit_byte { \c -> literal (ByteTok (Name (drop 2 (init c)))) }
@lit_char { \c -> literal (CharTok (Name (drop 1 (init c)))) }
@lit_str { \s -> literal (StrTok (Name (drop 1 (init s)))) }
@lit_byte_str { \s -> literal (ByteStrTok (Name (drop 2 (init s)))) }
@lit_byte { \c -> literal (ByteTok (drop 2 (init c))) }
@lit_char { \c -> literal (CharTok (drop 1 (init c))) }
@lit_str { \s -> literal (StrTok (drop 1 (init s))) }
@lit_byte_str { \s -> literal (ByteStrTok (drop 2 (init s))) }

@lit_raw_str { \s -> let n = length s - 2
in do
str <- rawString n
literal (StrRawTok (Name str) (fromIntegral n))
literal (StrRawTok str (fromIntegral n))
}
@lit_raw_bstr { \s -> let n = length s - 3
in do
str <- rawString n
literal (ByteStrRawTok (Name str) (fromIntegral n))
literal (ByteStrRawTok str (fromIntegral n))
}

<lits> "" ;
Expand All @@ -1036,8 +1036,8 @@ $white+ { \s -> pure (Space Whitespace (Name s)) }
@inner_doc_line { \c -> pure (Doc (drop 3 c) InnerDoc) }
@inner_doc_inline { \_ -> Doc <$> nestedComment <*> pure InnerDoc }

@line_comment { \c -> pure (Space Comment (Name (drop 2 c))) }
@inline_comment { \_ -> Space Comment <$> (Name <$> nestedComment) }
@line_comment { \c -> pure (Space Comment (drop 2 c)) }
@inline_comment { \_ -> Space Comment <$> nestedComment }

@subst_nt { \(_:i) -> pure (SubstNt (mkIdent i) Plain) }
@match_nt { \(_:s) -> let (i,':':n) = Prelude.span (/= ':') s
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Rust/Parser/ParseMonad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Control.Monad.Trans.Except

-- | Pattern for Identifiers
pattern Identifier :: String -> Spanned Token
pattern Identifier s <- (Spanned (IdentTok (Ident (Name s) _)) _)
pattern Identifier s <- (Spanned (IdentTok (Ident s _)) _)

-- | the result of running a parser
data ParseResult a
Expand Down
36 changes: 18 additions & 18 deletions src/Language/Rust/Parser/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ arg_self :: { Arg Span }
-- Sort of like parse_opt_abi() -- currently doesn't handle raw string ABI
abi :: { Abi }
: str {% case unspan $1 of
(LiteralTok (StrTok (Name s)) Nothing) | isAbi s -> pure (read s)
(LiteralTok (StrTok s) Nothing) | isAbi s -> pure (read s)
_ -> fail "invalid ABI"
}
| {- empty -} { C }
Expand Down Expand Up @@ -1573,26 +1573,26 @@ mkTokenTree (Spanned t s) = Token s t
-- | Given a 'LitTok' token that is expected to result in a valid literal, construct the associated
-- literal. Note that this should _never_ fail on a token produced by the lexer.
lit :: Spanned Token -> Lit Span
lit (Spanned (IdentTok (Ident (Name "true") _)) s) = Bool True Unsuffixed s
lit (Spanned (IdentTok (Ident (Name "false") _)) s) = Bool False Unsuffixed s
lit (Spanned (IdentTok (Ident "true" _)) s) = Bool True Unsuffixed s
lit (Spanned (IdentTok (Ident "false" _)) s) = Bool False Unsuffixed s
lit (Spanned (LiteralTok litTok suffix_m) s) = parseLit litTok suffix s
where
suffix = case suffix_m of
Nothing -> Unsuffixed
(Just (Name "isize")) -> Is
(Just (Name "usize")) -> Us
(Just (Name "i8")) -> I8
(Just (Name "u8")) -> U8
(Just (Name "i16")) -> I16
(Just (Name "u16")) -> U16
(Just (Name "i32")) -> I32
(Just (Name "u32")) -> U32
(Just (Name "i64")) -> I64
(Just (Name "u64")) -> U64
(Just (Name "i128")) -> I128
(Just (Name "u128")) -> U128
(Just (Name "f32")) -> F32
(Just (Name "f64")) -> F64
(Just "isize") -> Is
(Just "usize") -> Us
(Just "i8") -> I8
(Just "u8") -> U8
(Just "i16") -> I16
(Just "u16") -> U16
(Just "i32") -> I32
(Just "u32") -> U32
(Just "i64") -> I64
(Just "u64") -> U64
(Just "i128") -> I128
(Just "u128") -> U128
(Just "f32") -> F32
(Just "f64") -> F64
_ -> error "lit"
isPathSegmentIdent :: Spanned Ident -> Bool
Expand All @@ -1602,7 +1602,7 @@ isTypePathSegmentIdent :: Spanned Ident -> Bool
isTypePathSegmentIdent i = True
-- | Check if a given string is one of the accepted ABIs
isAbi :: InternedString -> Bool
isAbi :: String -> Bool
isAbi s = s `elem` abis
where abis = [ "Cdecl", "Stdcall", "Fastcall", "Vectorcall", "Aapcs", "Win64", "SysV64"
, "Rust", "C", "System", "RustIntrinsic", "RustCall", "PlatformIntrinsic"
Expand Down
1 change: 0 additions & 1 deletion src/Language/Rust/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ instance Pretty Ident where pretty = printIdent
instance Pretty ImplPolarity where pretty = printPolarity
instance Pretty LitTok where pretty = printLitTok
instance Pretty Mutability where pretty = printMutability
instance Pretty Name where pretty = printName
instance Pretty RangeLimits where pretty = printRangeLimits
instance Pretty Token where pretty = printToken
instance Pretty TokenTree where pretty = printTt
Expand Down
27 changes: 14 additions & 13 deletions src/Language/Rust/Pretty/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import qualified Text.PrettyPrint.Annotated.WL as WL
import Data.ByteString (unpack)
import Data.Char (intToDigit, ord, chr)
import Data.Either (lefts, rights)
import Data.List.NonEmpty (NonEmpty(..), toList)
import Data.Foldable (toList)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as N
import Data.Maybe (listToMaybe)
import Data.Word (Word8)
Expand Down Expand Up @@ -108,11 +109,11 @@ printCrate (Crate items as macs x) = annotate x (vcat ls)

-- | Pretty print a name
printName :: Name -> Doc a
printName (Name s) = text s
printName = text

-- | Pretty print an identifier
printIdent :: Ident -> Doc a
printIdent (Ident (Name s) _) = text s
printIdent (Ident s _) = text s

-- | Pretty print a type
-- aka 'print_type' (with 'print_ty_fn' inlined)
Expand Down Expand Up @@ -211,14 +212,14 @@ printToken (SubstNt s _) = "$" <> printIdent s
printToken _ = error "printToken"

printLitTok :: LitTok -> Doc a
printLitTok (ByteTok (Name v)) = "b'" <> text v <> "'"
printLitTok (CharTok (Name v)) = "'" <> text v <> "'"
printLitTok (IntegerTok (Name v)) = text v
printLitTok (FloatTok (Name v)) = text v
printLitTok (StrTok (Name v)) = "\"" <> text v <> "\""
printLitTok (StrRawTok (Name v) n) = let pad = text (replicate n '#') in "r" <> pad <> "\"" <> text v <> "\"" <> pad
printLitTok (ByteStrTok (Name v)) = "b\"" <> text v <> "\""
printLitTok (ByteStrRawTok (Name v) n) = let pad = text (replicate n '#') in "rb" <> pad <> "\"" <> text v <> "\"" <> pad
printLitTok (ByteTok n) = "b'" <> printName n <> "'"
printLitTok (CharTok n) = "'" <> printName n <> "'"
printLitTok (IntegerTok n) = printName n
printLitTok (FloatTok n) = printName n
printLitTok (StrTok n) = "\"" <> printName n <> "\""
printLitTok (StrRawTok n m) = let pad = text (replicate m '#') in "r" <> pad <> "\"" <> printName n <> "\"" <> pad
printLitTok (ByteStrTok n) = "b\"" <> printName n <> "\""
printLitTok (ByteStrRawTok n m) = let pad = text (replicate m '#') in "rb" <> pad <> "\"" <> printName n <> "\"" <> pad

printNonterminal :: Nonterminal a -> Doc a
printNonterminal (NtItem item) = printItem item
Expand Down Expand Up @@ -608,7 +609,7 @@ synthComment com = "/*" <+> text com <+> "*/"

-- | Print ident as is, or as cooked string if containing a hyphen
printCookedIdent :: Ident -> Doc a
printCookedIdent ident@Ident{ name = Name str }
printCookedIdent ident@Ident{ name = str }
| '-' `elem` str = printStr Cooked str
| otherwise = printIdent ident

Expand Down Expand Up @@ -908,7 +909,7 @@ printPathParameters (AngleBracketed lts tys bds x) colons = annotate x (when col
-- | second argument says whether to put colons before params
-- aka print_path
printPath :: Path a -> Bool -> Doc a
printPath (Path global segs x) colons = annotate x (when global "::" <> hcat (punctuate "::" (printSegment `map` toList segs)))
printPath (Path global segs x) colons = annotate x (when global "::" <> hcat (punctuate "::" (printSegment `map` N.toList segs)))
where
printSegment :: (Ident, PathParameters a) -> Doc a
printSegment (ident,parameters) = printIdent ident <> printPathParameters parameters colons
Expand Down
8 changes: 4 additions & 4 deletions src/Language/Rust/Syntax/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ data ImplPolarity
-- https://docs.serde.rs/syntex_syntax/ast/struct.InlineAsm.html
data InlineAsm a
= InlineAsm
{ asm :: InternedString
{ asm :: String
, asmStrStyle :: StrStyle
, outputs :: [InlineAsmOutput a]
, inputs :: [(InternedString, Expr a)]
, clobbers :: [InternedString]
, inputs :: [(String, Expr a)]
, clobbers :: [String]
, volatile :: Bool
, alignstack :: Bool
, dialect :: AsmDialect
Expand All @@ -341,7 +341,7 @@ data InlineAsm a
-- https://docs.serde.rs/syntex_syntax/ast/struct.InlineAsmOutput.html
data InlineAsmOutput a
= InlineAsmOutput
{ constraint :: InternedString
{ constraint :: String
, expr :: Expr a
, isRw :: Bool
, isIndirect :: Bool
Expand Down
20 changes: 10 additions & 10 deletions src/Language/Rust/Syntax/Constants.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import Data.List (unfoldr)

-- TODO make this have proper error handling
parseLit :: LitTok -> Suffix -> a -> Lit a
parseLit (ByteTok (Name s)) = let Just (w8,"") = unescapeByte s in Byte w8
parseLit (CharTok (Name s)) = let Just (c,"") = unescapeChar s in Char c
parseLit (IntegerTok (Name s)) = Int (unescapeInteger s)
parseLit (FloatTok (Name s)) = Float (unescapeFloat s)
parseLit (StrTok (Name s)) = Str (unfoldr unescapeChar s) Cooked
parseLit (StrRawTok (Name s) n) = Str s (Raw n)
parseLit (ByteStrTok (Name s)) = ByteStr (BS.pack (unfoldr unescapeByte s)) Cooked
parseLit (ByteStrRawTok (Name s) n) = ByteStr (BSW.pack s) (Raw n)
parseLit (ByteTok s) = let Just (w8,"") = unescapeByte s in Byte w8
parseLit (CharTok s) = let Just (c,"") = unescapeChar s in Char c
parseLit (IntegerTok s) = Int (unescapeInteger s)
parseLit (FloatTok s) = Float (unescapeFloat s)
parseLit (StrTok s) = Str (unfoldr unescapeChar s) Cooked
parseLit (StrRawTok s n) = Str s (Raw n)
parseLit (ByteStrTok s) = ByteStr (BS.pack (unfoldr unescapeByte s)) Cooked
parseLit (ByteStrRawTok s n) = ByteStr (BSW.pack s) (Raw n)

-- | Given a string of characters read from a Rust source, extract the next underlying char taking
-- into account escapes and unicode.
Expand Down Expand Up @@ -89,6 +89,6 @@ readHex n cs = let digits = take n cs
else Nothing

-- | Convert a list of characters to the number they represent.
numBase :: Num a => a -> [Char] -> a
numBase b cs = foldl (\n x -> fromIntegral (digitToInt x) + b * n) 0 cs
numBase :: Num a => a -> String -> a
numBase b = foldl (\n x -> fromIntegral (digitToInt x) + b * n) 0

16 changes: 3 additions & 13 deletions src/Language/Rust/Syntax/Ident.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE DuplicateRecordFields, OverloadedStrings #-}

module Language.Rust.Syntax.Ident (Ident(..), name, hash, mkIdent, invalidIdent, Name(..), InternedString) where
module Language.Rust.Syntax.Ident (Ident(..), name, hash, mkIdent, invalidIdent, Name) where

import Data.List (foldl')
import Data.Char (ord)
Expand All @@ -27,7 +27,7 @@ instance Eq Ident where
i1 == i2 = hash i1 == hash i2 && name i1 == name i2

mkIdent :: String -> Ident
mkIdent s = Ident (Name s) (hashString s) -- 0 ()
mkIdent s = Ident s (hashString s) -- 0 ()

hashString :: String -> Int
hashString = foldl' f golden
Expand All @@ -39,15 +39,5 @@ invalidIdent :: Ident
invalidIdent = mkIdent ""

-- TODO: backpack
type InternedString = String
type Name = String

-- | A name is a part of an identifier, representing a string or gensym. It's the result of interning.
-- https://docs.serde.rs/syntex_syntax/ast/struct.Name.html
data Name = Name InternedString deriving (Eq) -- TODO, not quite

instance Show Name where
show (Name s) = s

-- | A SyntaxContext represents a chain of macro expansions (represented by marks).
-- https://docs.serde.rs/syntex_syntax/ext/hygiene/struct.SyntaxContext.html
--type SyntaxContext = Int
Loading

0 comments on commit 6b15d8a

Please sign in to comment.