/usr/share/doc/libghc-language-python-doc/html/src/Language-Python-Common-StringEscape.html is in libghc-language-python-doc 0.4.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>src/Language/Python/Common/StringEscape.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# OPTIONS #-}</span>
<a name="line-2"></a><span class='hs-comment'>-----------------------------------------------------------------------------</span>
<a name="line-3"></a><span class='hs-comment'>-- |</span>
<a name="line-4"></a><span class='hs-comment'>-- Module : Language.Python.Common.StringEscape</span>
<a name="line-5"></a><span class='hs-comment'>-- Copyright : (c) 2009 Bernie Pope </span>
<a name="line-6"></a><span class='hs-comment'>-- License : BSD-style</span>
<a name="line-7"></a><span class='hs-comment'>-- Maintainer : bjpop@csse.unimelb.edu.au</span>
<a name="line-8"></a><span class='hs-comment'>-- Stability : experimental</span>
<a name="line-9"></a><span class='hs-comment'>-- Portability : ghc</span>
<a name="line-10"></a><span class='hs-comment'>--</span>
<a name="line-11"></a><span class='hs-comment'>-- Conversion to/from escaped characters in strings. Note: currently does not</span>
<a name="line-12"></a><span class='hs-comment'>-- support escaped Unicode character names.</span>
<a name="line-13"></a><span class='hs-comment'>-- </span>
<a name="line-14"></a><span class='hs-comment'>-- See:</span>
<a name="line-15"></a><span class='hs-comment'>-- </span>
<a name="line-16"></a><span class='hs-comment'>-- * Version 2.6 <<a href="http://www.python.org/doc/2.6/reference/lexical_analysis.html#string-literals">http://www.python.org/doc/2.6/reference/lexical_analysis.html#string-literals</a>></span>
<a name="line-17"></a><span class='hs-comment'>-- </span>
<a name="line-18"></a><span class='hs-comment'>-- * Version 3.1 <<a href="http://www.python.org/doc/3.1/reference/lexical_analysis.html#string-and-bytes-literals">http://www.python.org/doc/3.1/reference/lexical_analysis.html#string-and-bytes-literals</a>> </span>
<a name="line-19"></a><span class='hs-comment'>-----------------------------------------------------------------------------</span>
<a name="line-20"></a>
<a name="line-21"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Language</span><span class='hs-varop'>.</span><span class='hs-conid'>Python</span><span class='hs-varop'>.</span><span class='hs-conid'>Common</span><span class='hs-varop'>.</span><span class='hs-conid'>StringEscape</span> <span class='hs-layout'>(</span>
<a name="line-22"></a> <span class='hs-comment'>-- * String conversion. </span>
<a name="line-23"></a> <span class='hs-varid'>unescapeString</span><span class='hs-layout'>,</span>
<a name="line-24"></a> <span class='hs-varid'>unescapeRawString</span><span class='hs-layout'>,</span>
<a name="line-25"></a> <span class='hs-comment'>-- * Digits allowed in octal and hex representation.</span>
<a name="line-26"></a> <span class='hs-varid'>octalDigits</span><span class='hs-layout'>,</span>
<a name="line-27"></a> <span class='hs-varid'>hexDigits</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-28"></a>
<a name="line-29"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Numeric</span> <span class='hs-layout'>(</span><span class='hs-varid'>readHex</span><span class='hs-layout'>,</span> <span class='hs-varid'>readOct</span><span class='hs-layout'>)</span>
<a name="line-30"></a>
<a name="line-31"></a><span class='hs-comment'>-- | Convert escaped sequences of characters into /real/ characters in a normal Python string.</span>
<a name="line-32"></a>
<a name="line-33"></a><a name="unescapeString"></a><span class='hs-comment'>-- XXX does not handle escaped unicode literals</span>
<a name="line-34"></a><span class='hs-definition'>unescapeString</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-35"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\\'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- Backslash (\)</span>
<a name="line-36"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'\''</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\''</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- Single quote (')</span>
<a name="line-37"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'"'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'"'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- Double quote (")</span>
<a name="line-38"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'a'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\a'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- ASCII Bell (BEL)</span>
<a name="line-39"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'b'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\b'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- ASCII Backspace (BS)</span>
<a name="line-40"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'f'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\f'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- ASCII Formfeed (FF)</span>
<a name="line-41"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'n'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\n'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- ASCII Linefeed (LF)</span>
<a name="line-42"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'r'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\r'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- ASCII Carriage Return (CR)</span>
<a name="line-43"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'t'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\t'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- ASCII Horizontal Tab (TAB)</span>
<a name="line-44"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'v'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\v'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- ASCII Vertical Tab (VT)</span>
<a name="line-45"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'\n'</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span> <span class='hs-comment'>-- line continuation</span>
<a name="line-46"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-varid'>rest</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-varid'>o</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-47"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>o</span> <span class='hs-varop'>`elem`</span> <span class='hs-varid'>octalDigits</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unescapeNumeric</span> <span class='hs-num'>3</span> <span class='hs-varid'>octalDigits</span> <span class='hs-layout'>(</span><span class='hs-varid'>fst</span> <span class='hs-varop'>.</span> <span class='hs-varid'>head</span> <span class='hs-varop'>.</span> <span class='hs-varid'>readOct</span><span class='hs-layout'>)</span> <span class='hs-varid'>rest</span>
<a name="line-48"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-chr'>'\\'</span><span class='hs-conop'>:</span><span class='hs-chr'>'x'</span><span class='hs-conop'>:</span><span class='hs-varid'>rest</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-varid'>h</span><span class='hs-conop'>:</span><span class='hs-keyword'>_</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-49"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>h</span> <span class='hs-varop'>`elem`</span> <span class='hs-varid'>hexDigits</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unescapeNumeric</span> <span class='hs-num'>2</span> <span class='hs-varid'>hexDigits</span> <span class='hs-layout'>(</span><span class='hs-varid'>fst</span> <span class='hs-varop'>.</span> <span class='hs-varid'>head</span> <span class='hs-varop'>.</span> <span class='hs-varid'>readHex</span><span class='hs-layout'>)</span> <span class='hs-varid'>rest</span>
<a name="line-50"></a><span class='hs-definition'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>c</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>cs</span>
<a name="line-51"></a><span class='hs-definition'>unescapeString</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-52"></a>
<a name="line-53"></a><span class='hs-comment'>{-
<a name="line-54"></a>-- | This function is a placeholder for unescaping characters in raw strings.
<a name="line-55"></a>-- The Python documentation explicitly says that
<a name="line-56"></a>-- "When an 'r' or 'R' prefix is present, a character following a backslash is included
<a name="line-57"></a>-- in the string without change, and all backslashes are left in the string."
<a name="line-58"></a>-- However it also says that When an 'r' or 'R' prefix is used in conjunction with
<a name="line-59"></a>-- a 'u' or 'U' prefix, then the \uXXXX and \UXXXXXXXX escape sequences are processed
<a name="line-60"></a>-- while all other backslashes are left in the string. Currently the function is the identity
<a name="line-61"></a>-- but it ought to process unicode escape sequences.
<a name="line-62"></a>-}</span>
<a name="line-63"></a>
<a name="line-64"></a><a name="unescapeRawString"></a><span class='hs-comment'>-- XXX does not handle escaped unicode literals</span>
<a name="line-65"></a><span class='hs-definition'>unescapeRawString</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-66"></a><span class='hs-definition'>unescapeRawString</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>id</span>
<a name="line-67"></a>
<a name="line-68"></a><span class='hs-comment'>{-
<a name="line-69"></a>-- | Convert escaped sequences of characters into /real/ characters in a raw Python string.
<a name="line-70"></a>-- Note: despite their name, Python raw strings do allow a small set of character escapings,
<a name="line-71"></a>-- namely the single and double quote characters and the line continuation marker.
<a name="line-72"></a>unescapeRawString ('\\':'\'':cs) = '\'' : unescapeRawString cs -- Single quote (')
<a name="line-73"></a>unescapeRawString ('\\':'"':cs) = '"' : unescapeRawString cs -- Double quote (")
<a name="line-74"></a>unescapeRawString ('\\':'\n':cs) = unescapeRawString cs -- line continuation
<a name="line-75"></a>unescapeRawString (c:cs) = c : unescapeRawString cs
<a name="line-76"></a>unescapeRawString [] = []
<a name="line-77"></a>-}</span>
<a name="line-78"></a>
<a name="line-79"></a><a name="unescapeNumeric"></a><span class='hs-comment'>{-
<a name="line-80"></a> This is a bit complicated because Python allows between 1 and 3 octal
<a name="line-81"></a> characters after the \, and 1 and 2 hex characters after a \x.
<a name="line-82"></a>-}</span>
<a name="line-83"></a><span class='hs-definition'>unescapeNumeric</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-84"></a><span class='hs-definition'>unescapeNumeric</span> <span class='hs-varid'>n</span> <span class='hs-varid'>numericDigits</span> <span class='hs-varid'>readNumeric</span> <span class='hs-varid'>str</span>
<a name="line-85"></a> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>loop</span> <span class='hs-varid'>n</span> <span class='hs-conid'>[]</span> <span class='hs-varid'>str</span>
<a name="line-86"></a> <span class='hs-keyword'>where</span>
<a name="line-87"></a> <span class='hs-varid'>loop</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>acc</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>numericToChar</span> <span class='hs-varid'>acc</span><span class='hs-keyglyph'>]</span>
<a name="line-88"></a> <span class='hs-varid'>loop</span> <span class='hs-num'>0</span> <span class='hs-varid'>acc</span> <span class='hs-varid'>rest</span>
<a name="line-89"></a> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>numericToChar</span> <span class='hs-varid'>acc</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-varid'>rest</span>
<a name="line-90"></a> <span class='hs-varid'>loop</span> <span class='hs-varid'>n</span> <span class='hs-varid'>acc</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span>
<a name="line-91"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'>`elem`</span> <span class='hs-varid'>numericDigits</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>loop</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>acc</span><span class='hs-layout'>)</span> <span class='hs-varid'>cs</span>
<a name="line-92"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>numericToChar</span> <span class='hs-varid'>acc</span> <span class='hs-conop'>:</span> <span class='hs-varid'>unescapeString</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span>
<a name="line-93"></a> <span class='hs-varid'>numericToChar</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Char</span>
<a name="line-94"></a> <span class='hs-varid'>numericToChar</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>toEnum</span> <span class='hs-varop'>.</span> <span class='hs-varid'>readNumeric</span> <span class='hs-varop'>.</span> <span class='hs-varid'>reverse</span>
<a name="line-95"></a>
<a name="line-96"></a><a name="octalDigits"></a><span class='hs-definition'>octalDigits</span><span class='hs-layout'>,</span> <span class='hs-varid'>hexDigits</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span>
<a name="line-97"></a><span class='hs-comment'>-- | The set of valid octal digits in Python.</span>
<a name="line-98"></a><span class='hs-definition'>octalDigits</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"01234567"</span>
<a name="line-99"></a><a name="hexDigits"></a><span class='hs-comment'>-- | The set of valid hex digits in Python.</span>
<a name="line-100"></a><span class='hs-definition'>hexDigits</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"0123456789abcdef"</span>
</pre></body>
</html>
|