On Sun, Jan 23, 2011 at 4:31 PM, Chung-chieh Shan <span dir="ltr"><<a href="mailto:ccshan@post.harvard.edu">ccshan@post.harvard.edu</a>></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'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"><<a href="mailto:stephen.tetley@gmail.com">stephen.tetley@gmail.com</a>></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't think you can do this "simply" as you think you would always<br>have to build a parse tree.</blockquote><div><br></div><div>Isn'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 "not caring" about the details of syntax is almost as much work<br>as caring about them. I'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'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 -> [String]</div><div>splitTLC = parse ""</div><div><br></div><div>type Stack = String</div><div><br></div><div>parse :: Stack -> String -> [String]</div>
<div>parse _ "" = []</div><div>parse st (c:cs) = next c st $ parse (updStack c st) cs</div><div><br></div><div>next :: Char -> Stack -> [String] -> [String]</div><div>next c [] xs = if c==',' then [] : xs else c <: xs</div>
<div>next c (_:_) xs = c <: xs</div><div><br></div><div>infixr 0 <:</div><div><br></div><div>(<:) :: Char -> [String] -> [String]</div><div>c <: [] = [[c]]</div><div>c <: (x:xs) = (c:x):xs</div><div>
<br></div><div>updStack :: Char -> Stack -> Stack</div><div>updStack char stack =</div><div> case (char,stack) of</div><div> -- char is an escaped character</div><div> (_ ,'\\':xs) -> xs -- the next character is not</div>
<div><br></div><div> -- char is the escape character</div><div> ('\\', xs) -> '\\':xs -- push it on the stack</div><div><br></div><div> -- char is the string terminator</div><div> ('"' , '"':xs) -> xs -- closes current string literal</div>
<div> ('"' , ''':xs) -> ''':xs -- ignored inside character</div><div> ('"' , xs) -> '"':xs -- opens a new string</div><div><br></div><div> -- char is the character terminator</div>
<div> (''' , ''':xs) -> xs -- closes current character literal</div><div> (''' , '"':xs) -> '"':xs -- ignored inside string</div><div> (''' , xs) -> ''':xs -- opens a new character</div>
<div><br></div><div> -- parens and brackets</div><div> (_ , '"':xs) -> '"':xs -- are ignored inside strings</div><div> (_ , ''':xs) -> ''':xs -- and characters</div>
<div> ('(' , xs) -> '(':xs -- new opening paren</div><div> (')' , '(':xs) -> xs -- closing paren</div><div> ('[' , xs) -> '[':xs -- opening bracket</div>
<div> (']' , '[':xs) -> xs -- closing bracket</div><div><br></div><div> -- other character don't modify the stack (ignoring record syntax)</div><div> (_ , xs) -> xs</div></div>
<div><br></div>