gpulost / lepl

Automatically exported from code.google.com/p/lepl
Other
0 stars 0 forks source link

lepl.functions.String bug #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
>> print String().parse('"line1\\\nline2"')
>> ['line1\\\nline2']

What is the expected output? What do you see instead?
['line1line2']

What version of the product are you using? On what operating system?
Ubuntu 9.04/python2.6.2/lepl3.3

Thanks!

Original issue reported on code.google.com by aachu...@gmail.com on 15 Oct 2009 at 6:53

GoogleCodeExporter commented 9 years ago
So the idea is that you want the newline to be dropped? (Just trying to 
understand
what the issue is).

Thanks, Andrew

Original comment by acooke....@gmail.com on 15 Oct 2009 at 10:10

GoogleCodeExporter commented 9 years ago
I haven't forgotten this - I'm still working out what the best thing to do is 
(I'm
not sure this is a bug, particularly, because it's what I would expect to 
happen, but
I can see what you want, and there should be a way to do that (and there are 
other
related issues that this has started me worrying about)).

But this weekend I hope to at least post some kind fo work-around.

Andrew

Original comment by acooke....@gmail.com on 17 Oct 2009 at 1:00

GoogleCodeExporter commented 9 years ago
Thanks!

Original comment by aachu...@gmail.com on 17 Oct 2009 at 4:35

GoogleCodeExporter commented 9 years ago

Here are a couple of alternatives.  The first probably *isn't* what you want - 
it
refuses to match multiple lines.  The second *is* what you want, I think.

{{{
def String1(quote='"', escape='\\', exclude='\n'):
    '''
    This won't match newlines (will fail rather than match multiple lines).
    '''
    q = Literal(quote)
    content = AnyBut(Or(q, Any(exclude)))
    if escape:
        content = Or(content, And(Drop(escape), q))
    content = Repeat(content, add_=True)
    return And(Drop(q), content, Drop(q))

def String2(quote='"', escape='\\', ignore='\n'):
    '''
    This will ignore (drop) newlines.
    '''
    q = Literal(quote)
    content = AnyBut(Or(q, Any(ignore)))
    if escape:
        content = Or(content, And(Drop(escape), q))
    content = Or(content, Drop(Any(ignore)))
    content = Repeat(content, add_=True)
    return And(Drop(q), content, Drop(q))
}}}
{{{
>>> String2().parse('"line1\nline2"')
['line1line2']
}}}

I'll add something like this in the next release (changing from bug to 
enhancement).
 Even better would be a regular expression based version, since that could be used
for tokens.  I'll work on it.

Andrew

Original comment by acooke....@gmail.com on 18 Oct 2009 at 8:06

GoogleCodeExporter commented 9 years ago
Oh, ignore the wiki markup (the {{{}}} stuff).

And here's another way to work round it:

>>> string = String() > (lambda x: x[0].replace("\n", ""))
>>> string.parse('"line1\nline2"')
['line1line2']

Original comment by acooke....@gmail.com on 18 Oct 2009 at 8:09

GoogleCodeExporter commented 9 years ago
Thanks, it's works!

Original comment by aachu...@gmail.com on 8 Nov 2009 at 4:25

GoogleCodeExporter commented 9 years ago
I just added String1 and String2 above as SingleLineString and SkipString to 
Lepl 4.

Original comment by acooke....@gmail.com on 2 Apr 2010 at 11:55