Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Lexing & Syntax Highlighting in Lua (using LPeg)
Lua C JavaScript
tag: 0.6

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

LXSH: Lexing & Syntax Highlighting in Lua

LXSH is a collection of lexers and highlighters written in Lua using the excellent pattern-matching library LPeg. The highlighters generate HTML designed to be easily embedded in web pages. Two programming languages are currently supported, these are Lua and C. The highlighters support three color schemes, here are some examples of both languages in every color scheme:

As you can see in the above examples the highlighters replace standard library identifiers (and then some) with hyperlinks to the relevant documentation. You can also try switching between style sheets while staying on the same page by using your web browsers View → Page styles menu (this works using so-called "alternate style sheets").


The easiest way to download and install LXSH is using LuaRocks:

$ luarocks install

If you don't have LuaRocks installed you can download the latest release directly from GitHub as a ZIP archive. To install create an lxsh directory in your $LUA_PATH and copy the contents of the src directory from the ZIP archive to the lxsh directory so that you end up with the following structure:

  • $LUA_PATH/lxsh/init.lua
  • $LUA_PATH/lxsh/lexers/*.lua
  • $LUA_PATH/lxsh/highlighters/*.lua
  • $LUA_PATH/lxsh/colors/*.lua
  • $LUA_PATH/lxsh/docs/*.lua


If you want to call a lexer or access an LPeg pattern defined by the lexer you can do so as follows (this example demonstrates the Lua lexer but the C lexer works the same):

> -- Load the lexer.
> lexer = require 'lxsh.lexers.lua'

> -- Run the lexer on a string of source code.
> for kind, text in lexer.gmatch 'i = i + 1 -- example' do
>>  print(kind, text)
>> end
identifier  i
operator    =
identifier  i
operator    +
number      1
comment     -- example

> -- Use one of the patterns defined by the lexer.
> lexer.patterns.comment:match '--[=[ this is a long comment ]=]'

The syntax highlighters can be used as follows:

> -- Load the highlighter.
> highlighter = require 'lxsh.highlighters.lua'

> -- Use it to highlight some code.
> print(highlighter "require 'lpeg'")
<a href="" style="color: #0e7c6b; text-decoration: none">require</a>
<span style="color: #a8660d">'lpeg'</span><span style="color: #2239a8; font-weight: bold">;</span>

You can customize the output of the highlighters by passing a table with one or more of the following options:

  • encodews: Replace newlines with <br> elements and ordinary spaces with non-breaking spaces so that whitespace is preserved when the highlighted code isn't embedded in a <pre> block
  • external: By default the highlighters generate inline CSS styles which makes it easier to use the output directly but also bloats the size significantly. If you want to reduce the size and don't mind including an external style sheet use this option. You'll need to make sure the required styles are loaded, e.g. by embedded the output of lxsh.includestyles() in the <head> of your HTML document (this has the added benefit of enabling users to pick their own preferred color scheme)
  • colors: The color scheme to use, one of the following:

Tokens produced by the lexers

The Lua lexer produces the following tokens:

  • comment
  • constant (true, false and nil)
  • error (invalid input)
  • identifier
  • keyword
  • number
  • operator
  • string
  • whitespace

The C lexer produces the following tokens:

  • comment
  • constant (character and string literals like 'C' and "Lua")
  • error (invalid input)
  • identifier
  • keyword
  • number
  • operator
  • preprocessor
  • whitespace


If you have questions, bug reports, suggestions, etc. the author can be contacted at The latest version is available at and


This software is licensed under the MIT license.
© 2010 Peter Odding <>.

Something went wrong with that request. Please try again.