mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-09-11 21:06:02 -06:00
escape identifiers if necessary
This commit is contained in:
@@ -29,7 +29,7 @@ import qualified Data.ByteString.UTF8 as UTF8
|
|||||||
import qualified Data.ByteString.Char8 as BS(append,isPrefixOf,drop,length)
|
import qualified Data.ByteString.Char8 as BS(append,isPrefixOf,drop,length)
|
||||||
-- Limit use of BS functions to the ones that work correctly on
|
-- Limit use of BS functions to the ones that work correctly on
|
||||||
-- UTF-8-encoded bytestrings!
|
-- UTF-8-encoded bytestrings!
|
||||||
import Data.Char(isDigit)
|
import Data.Char(chr)
|
||||||
import Data.Binary(Binary(..))
|
import Data.Binary(Binary(..))
|
||||||
import Text.JSON hiding (Result(..))
|
import Text.JSON hiding (Result(..))
|
||||||
import GF.Text.Pretty
|
import GF.Text.Pretty
|
||||||
@@ -104,7 +104,26 @@ ident2raw = Id . ident2utf8
|
|||||||
showIdent :: Ident -> String
|
showIdent :: Ident -> String
|
||||||
showIdent i = unpack $! ident2utf8 i
|
showIdent i = unpack $! ident2utf8 i
|
||||||
|
|
||||||
instance Pretty Ident where pp = pp . showIdent
|
instance Pretty Ident where
|
||||||
|
pp id
|
||||||
|
| valid_ident s = pp s
|
||||||
|
| otherwise = pp (escape s)
|
||||||
|
where
|
||||||
|
s = showIdent id
|
||||||
|
|
||||||
|
valid_ident s =
|
||||||
|
case s of
|
||||||
|
[] -> False
|
||||||
|
(c:cs) -> elem c ident_first && all (flip elem ident_rest) cs
|
||||||
|
where
|
||||||
|
l = ['a'..'z']++['A'..'Z']++[chr 192..chr 214]++[chr 216..chr 246]++[chr 248..chr 255]
|
||||||
|
ident_first = '_':l
|
||||||
|
ident_rest = ident_first ++ ['0'..'9'] ++ ['\'']
|
||||||
|
|
||||||
|
escape s = "\'"++concatMap slash s++"\'"
|
||||||
|
where
|
||||||
|
slash '\'' = "\\'"
|
||||||
|
slash c = [c]
|
||||||
|
|
||||||
instance Pretty RawIdent where pp = pp . showRawIdent
|
instance Pretty RawIdent where pp = pp . showRawIdent
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user