An Erlang lexer and syntax highlighter in Gleam

Release: v1.0.0

+48 -18
+5
CHANGELOG.md
··· 1 + # Changelog 2 + 3 + ## v1.0.0 - 2025-04-01 4 + 5 + - Initial release.
+37 -8
README.md
··· 1 - # pearl 1 + # Pearl 2 + 3 + An Erlang lexer and syntax highlighter for Gleam! 2 4 3 5 [![Package Version](https://img.shields.io/hexpm/v/pearl)](https://hex.pm/packages/pearl) 4 6 [![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/pearl/) 5 7 8 + Pearl is a lexer and syntax highlighter for Erlang, written in Gleam. 9 + The `pearl` module, based on [`glexer`](https://hexdocs.mp/glexer) and 10 + [`just`](https://hexdocs.mp/just), exposes a 11 + standard lexer API, allowing you to convert Erlang source code into tokens. 12 + The `pearl/highlight` module allows you to highlight Erlang code using ansi 13 + colours, html or a custom format. Heavily inspired by [`contour`](https://hexdocs.pm/contour). 14 + 6 15 ```sh 7 16 gleam add pearl@1 8 17 ``` 18 + 9 19 ```gleam 10 20 import pearl 21 + import pearl/highlight 11 22 12 - pub fn main() -> Nil { 13 - // TODO: An example of the project in use 23 + pub fn main() { 24 + let code = "console.log('Hello, world!');" 25 + 26 + let lexer = pearl.new(code) 27 + // Lex syntax tokens for parsing or other uses 28 + let #(tokens, errors) = pearl.tokenise(lexer) 29 + let assert [] = errors 30 + parse_erlang(tokens) 31 + 32 + // Highlight with ansi codes to print in the terminal 33 + let highlighted = highlight.ansi(code) 34 + io.println(highlighted) 35 + 36 + // Render to html to show in the browser 37 + let html = highlight.html(code) 38 + io.println("<pre><code>" <> html <> "</code></pre>") 39 + 40 + // Convert to "highlighting tokens" to highlight in some other way 41 + let highlight_tokens = highlight.tokens(code) 42 + highlight_tokens_some_other_way(highlight_tokens) 14 43 } 15 44 ``` 16 45 17 46 Further documentation can be found at <https://hexdocs.pm/pearl>. 18 47 19 - ## Development 48 + ### Feature completeness 20 49 21 - ```sh 22 - gleam run # Run the project 23 - gleam test # Run the tests 24 - ``` 50 + As far as I can tell, `pearl` can lex all valid Erlang programs. However, I 51 + couldn't find a technical specification for Erlang syntax so what I've implemented 52 + is based on the Erlang documentation and other Erlang parsers. If there is 53 + something missing, please open an issue and I'll implement it as soon as possible.
+6 -10
gleam.toml
··· 1 1 name = "pearl" 2 2 version = "1.0.0" 3 3 4 - # Fill out these fields if you intend to generate HTML documentation or publish 5 - # your project to the Hex package manager. 6 - # 7 - # description = "" 8 - # licences = ["Apache-2.0"] 9 - # repository = { type = "github", user = "", repo = "" } 10 - # links = [{ title = "Website", href = "" }] 11 - # 12 - # For a full reference of all the available options, you can have a look at 13 - # https://gleam.run/writing-gleam/gleam-toml/. 4 + description = "An Erlang lexer and syntax highlighter for Gleam!" 5 + licences = ["Apache-2.0"] 6 + repository = { type = "github", user = "GearsDatapacks", repo = "pearl" } 7 + links = [ 8 + { title = "Sponsor", href = "https://github.com/sponsors/GearsDatapacks" }, 9 + ] 14 10 15 11 [dependencies] 16 12 gleam_stdlib = ">= 0.44.0 and < 2.0.0"