osa1 / language-lua

Lua parser and pretty-printer
BSD 3-Clause "New" or "Revised" License
51 stars 18 forks source link

Escape sequences not interpreted in string-literals #7

Closed hvr closed 10 years ago

hvr commented 10 years ago

When evaluated with lua, the following chunk

print(string.byte("1\n34",1,5))

results in the output (showing that \n is supposed to be decoded)

49  10  51  52

However, when parsed with language-lua, the escape-sequence \n isn't decoded:

Block
  [FunCall
     (NormalFunCall (PEVar (VarName "print"))
        (Args
           [PrefixExp
              (PEFunCall
                 (NormalFunCall
                    (PEVar (SelectName (PEVar (VarName "string")) "byte"))
                    (Args [String "1\\n34", Number "1", Number "5"])))]))]
  Nothing

Moreover, language-lua accepts invalid escape sequences, which luas parser would reject:

Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> print("\invalid")
stdin:1: invalid escape sequence near '\i'
osa1 commented 10 years ago

The problem with interpreting strings is that then pretty-printing would be more involved. Anyhow, I fixed this and I think I fixed another bug on the way. Please re-open if your problem still exists.

hvr commented 10 years ago

Thanks! ...while this makes it more work for pretty-printing, it makes it easier for evaluating Lua expressions :-)

hvr commented 10 years ago

The example

a = '\97lo\10\04923"'

From the Lua reference manual fails to parse currently with

lexical error near line: 1 col: 7 at char \
osa1 commented 10 years ago

It turns out that interpreting character codes is a bit involved since Haskell's read doesn't handle that as in Lua. It should be fixed now. When you're happy with the result please tell me and I'll upload a new version to Hackage.

@hvr also, you're aware that we have hslua for Haskell<->Lua bridge, right? You can create a Lua state in it and feed it with strings to execute Lua statements.

For the future of language-lua I have this idea of using official Lua interpreter's lexer and parser and then create Haskell data structures using FFI.

hvr commented 10 years ago

Thanks, with this fix, language-lua now behaves to my satisfaction regarding string literals :-)

And yes, I'm aware of hslua (and it's great at what it does!), but I need more control over the operational aspect of Lua interpretation (especially avoiding the C FFI).

osa1 commented 10 years ago

I'm glad it worked. I just pushed a new version to Hackage, version number 0.4.5.