An Erlang lexer and syntax highlighter in Gleam
1# Pearl
2
3An Erlang lexer and syntax highlighter for Gleam!
4
5[](https://hex.pm/packages/pearl)
6[](https://hexdocs.pm/pearl/)
7
8Pearl is a lexer and syntax highlighter for Erlang, written in Gleam. The lexer
9is based on [`glexer`](https://hexdocs.pm/glexer) and [`just`](https://hexdocs.pm/just),
10allowing you to convert Erlang source code into tokens. There is also an API
11which allows you to highlight Erlang code using ansi colours, html or a custom
12format. Heavily inspired by [`contour`](https://hexdocs.pm/contour).
13
14```sh
15gleam add pearl@2
16```
17
18```gleam
19import pearl
20
21pub fn main() {
22 let code = "
23-module(hello).
24-export([hello_world/0]).
25
26hello_world() -> io:fwrite(\"Hello, world!\\n\").
27"
28
29 let lexer = pearl.new(code)
30 // Lex syntax tokens for parsing or other uses
31 let #(tokens, errors) = pearl.tokenise(lexer)
32 let assert [] = errors
33 parse_erlang(tokens)
34
35 // Highlight with ansi codes to print in the terminal
36 let highlighted = pearl.highlight_ansi(code)
37 io.println(highlighted)
38
39 // Render to html to show in the browser
40 let html = pearl.highlight_html(code)
41 io.println("<pre><code>" <> html <> "</code></pre>")
42
43 // Convert to "highlighting tokens" to highlight in some other way
44 let highlight_tokens = pearl.highlight_tokens(code)
45 highlight_tokens_some_other_way(highlight_tokens)
46}
47```
48
49Further documentation can be found at <https://hexdocs.pm/pearl>.
50
51### Feature completeness
52
53As far as I can tell, `pearl` can lex all valid Erlang programs. However, I
54couldn't find a technical specification for Erlang syntax so what I've implemented
55is based on the Erlang documentation and other Erlang parsers. If there is
56something missing, please open an issue and I'll implement it as soon as possible.