Lua parser and pretty-printer

Latest on Hackage:0.10.0

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow to host generated Haddocks.

BSD3 licensed by Ömer Sinan Ağacan, Eric Mertens
Maintained by Eric Mertens

language-lua - Lua 5.3 lexer, parser and pretty-printer

Build Status Coverage Status Hackage

This package is just like any other language x packages. It provides lexer, parser and pretty-printer for Lua programming language.

Haddock documentation


This module provides 3 parsers.

  • chunk: Lua file parser.
  • exp: Lua expression parser.
  • stat: Lua statement parser.

Lexing is needed before running a parser. parseText function runs lexer before parsing. So if you want to parse a Lua expression, you can call parseText exp string, where string is the Lua expression to parse.

parseFile is a helper to parse Lua files. Example: parseFile "/path/to/lua/file". This is same as `parseText chunk `fmap` readFile path`.

Note that parseText may result with failure, so it's return type is Either ParserError a.


Lexer is not exported by top-level Language.Lua module. You need to import Language.Lua.Lexer. After that, llex string scans the string and returns token list. Tokens are defined in Language.Lua.Token.


Pretty-printer is still under development, and subject to lots of changes. It works, but lots of functionality will be added.

For now, you can use pprint syntax_tree to pretty-print a Lua syntax tree.




  • Track the end positions of tokens and the AST in addition to the start


  • Transition to parsing Text using a simplified alex lexer and happy parser


  • We can now parse Unicode strings from UTF-8 encoded files. By default these literals are not interpreted, and we have StringLiteral.interpretStringLiteral for interpretation and StringLiteral.constructStringLiteral for constructing Lua string literals from UTF-8 encoded ByteStrings.

    Main invariant is that if you print a parsed Lua string, it should be printed the same way. (including long strings)

  • Empty statements(EmptyStat) are now printed by pretty printer to avoid ambiguous printing. Previously these two statements were printed the same:


    But the first line does not have the parse ambiguity(3 function applications in one statement or two function application statements).

  • Forgotten Annotated instance for Name implemented.

  • Some warnings are fixed for GHC < 7.10.


  • Integer division parsing fixed.


  • language-lua now supports Lua 5.3.
  • Some warnings printed with base >= 4.8 are fixed.

  • Some bugs related with assignment statement parsing are fixed(#22).
  • Wrong error message for expected == operator is fixed(#23).

  • Build fixed for GHC 7.10.

  • Build-tools field is added to Cabal file.


  • Fixed some issues with string parsing. (#17)
  • Added named variant of text parsers to allow specifying source names. (#18)

  • Missing test files are added to Cabal package.


  • base dependency is relaxed for GHC 7.10. Note that alex version >3.1.4 is required to compile with GHC 7.10.


  • Generic and NFData instances are implemented for syntax trees.


  • Fixed a long string literal parsing bug which was causing long strings to terminate at wrong points.
  • \z escape characters in strings are now scanned and interpreted correctly.
  • This is first version that parses all of Lua 5.2.2 test suite.


  • 2-years-old operator parsing bug fixed. Chained/nested operator expressions are now properly parsed.


  • Language.Lua.Annotated.Lexer module exposed.


  • Fixed a bug that made lexer accept invalid escape sequences in strings.
  • Strings are now interpreted -- string "\n" is now parsed to Haskell string "\n", instead of "\\n".
  • Fixed character code parsing.


  • Printer now takes operator precedences into account while printing Binop and Unop expressions and prints parenthesis as necessary.
  • Printer now does not put line break in Binop expressions.


  • Data and Typeable instances are implemented for syntax tree.


  • More tweaks in pretty printer.
  • Started using 2 spaces for indentation(instead of 4 as before).


  • Some tweaks in pretty-printer.


  • Table and FunDef nodes are removed from simplified syntax.


  • Fixed incorrectly exported name exp in Language.Lua.Parser module.


  • Added non-annotated syntax to make code-generation easier.


  • Minor internal changes.


  • Some tweaks in pretty-printer.


  • Syntax tree is annotated. All parsers(parseText, parseFile) annotate resulting tree with source positions.
Depends on 5 packages:
Used by 2 packages:
comments powered byDisqus