On Sun, Jan 23, 2011 at 4:31 PM, Chung-chieh Shan <span dir="ltr">&lt;<a href="mailto:ccshan@post.harvard.edu">ccshan@post.harvard.edu</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Maybe Text.Show.Pretty.parseValue in the pretty-show package can help?<br></blockquote><div><br></div><div>That&#39;s what I was looking for, thanks!</div><div><br></div></div><div>On Sun, Jan 23, 2011 at 5:23 PM, Stephen Tetley <span dir="ltr">&lt;<a href="mailto:stephen.tetley@gmail.com">stephen.tetley@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
I don&#39;t think you can do this &quot;simply&quot; as you think you would always<br>have to build a parse tree.</blockquote><div><br></div><div>Isn&#39;t it enough to maintain a stack of open parens, brackets, char- and string-terminators and escape chars? Below is my attempt at solving the problem without an expression parser.</div>
<div><br></div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
In practice, if you follow the skeleton syntax tree style you might<br>find &quot;not caring&quot; about the details of syntax is almost as much work<br>as caring about them. I&#39;ve tried a couple of times to make a skeleton<br>
parser that does paren nesting and little else, but always given up<br>and just used a proper parser as the skeleton parser never seemed<br>robust.<br></blockquote><div><br></div><div>Indeed I doubt that the implementation below is robust and it&#39;s too tricky to be easily maintainable. I include it for reference. Let me know if you spot an obvious mistake..</div>
</div><div><br></div><div>Sebastian</div><div><div><br></div><div>splitTLC :: String -&gt; [String]</div><div>splitTLC = parse &quot;&quot;</div><div><br></div><div>type Stack  = String</div><div><br></div><div>parse :: Stack -&gt; String -&gt; [String]</div>
<div>parse _  &quot;&quot;     = []</div><div>parse st (c:cs) = next c st $ parse (updStack c st) cs</div><div><br></div><div>next :: Char -&gt; Stack -&gt; [String] -&gt; [String]</div><div>next c []    xs = if c==&#39;,&#39; then [] : xs else c &lt;: xs</div>
<div>next c (_:_) xs = c &lt;: xs</div><div><br></div><div>infixr 0 &lt;:</div><div><br></div><div>(&lt;:) :: Char -&gt; [String] -&gt; [String]</div><div>c &lt;: []     = [[c]]</div><div>c &lt;: (x:xs) = (c:x):xs</div><div>
<br></div><div>updStack :: Char -&gt; Stack -&gt; Stack</div><div>updStack char stack =</div><div>  case (char,stack) of</div><div>    -- char is an escaped character</div><div>    (_   ,&#39;\\&#39;:xs) -&gt; xs      -- the next character is not</div>
<div><br></div><div>    -- char is the escape character</div><div>    (&#39;\\&#39;,     xs) -&gt; &#39;\\&#39;:xs -- push it on the stack</div><div><br></div><div>    -- char is the string terminator</div><div>    (&#39;&quot;&#39; , &#39;&quot;&#39;:xs) -&gt; xs      -- closes current string literal</div>
<div>    (&#39;&quot;&#39; , &#39;&#39;&#39;:xs) -&gt; &#39;&#39;&#39;:xs  -- ignored inside character</div><div>    (&#39;&quot;&#39; ,     xs) -&gt; &#39;&quot;&#39;:xs  -- opens a new string</div><div><br></div><div>    -- char is the character terminator</div>
<div>    (&#39;&#39;&#39; , &#39;&#39;&#39;:xs) -&gt; xs      -- closes current character literal</div><div>    (&#39;&#39;&#39; , &#39;&quot;&#39;:xs) -&gt; &#39;&quot;&#39;:xs  -- ignored inside string</div><div>    (&#39;&#39;&#39; ,     xs) -&gt; &#39;&#39;&#39;:xs  -- opens a new character</div>
<div><br></div><div>    -- parens and brackets</div><div>    (_   , &#39;&quot;&#39;:xs) -&gt; &#39;&quot;&#39;:xs  -- are ignored inside strings</div><div>    (_   , &#39;&#39;&#39;:xs) -&gt; &#39;&#39;&#39;:xs  -- and characters</div>
<div>    (&#39;(&#39; ,     xs) -&gt; &#39;(&#39;:xs  -- new opening paren</div><div>    (&#39;)&#39; , &#39;(&#39;:xs) -&gt; xs      -- closing paren</div><div>    (&#39;[&#39; ,     xs) -&gt; &#39;[&#39;:xs  -- opening bracket</div>
<div>    (&#39;]&#39; , &#39;[&#39;:xs) -&gt; xs      -- closing bracket</div><div><br></div><div>    -- other character don&#39;t modify the stack (ignoring record syntax)</div><div>    (_   ,     xs) -&gt; xs</div></div>
<div><br></div>