diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..314acf9 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## v1.0.0 - 2025-04-01 + +- Initial release. diff --git a/README.md b/README.md index 444a1ba..31c570a 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,58 @@ -# just +# Just + +A JavaScript lexer and syntax highlighter for Gleam! [![Package Version](https://img.shields.io/hexpm/v/just)](https://hex.pm/packages/just) [![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/just/) +Just is a JavaScript lexer and syntax highlighter written in Gleam. +The `just` module, based on [`glexer`](https://hexdocs.mp/glexer) exposes a +standard lexer API, allowing you to convert JavaScript source code into tokens. +The `just/highlight` module allows you to highlight javascript code using ansi +colours, html or a custom format. Heavily inspired by [`contour`](https://hexdocs.pm/contour). + ```sh gleam add just@1 ``` + ```gleam import just +import just/highlight + +pub fn main() { + let code = "console.log('Hello, world!');" + + let lexer = just.new(code) |> just.strict_mode + // Lex syntax tokens for parsing or other uses + let #(tokens, errors) = just.tokenise(lexer) + let assert [] = errors + parse_js(tokens) -pub fn main() -> Nil { - // TODO: An example of the project in use + // Highlight with ansi codes to print in the terminal + let highlighted = highlight.ansi(code) + io.println(highlighted) + + // Render to html to show in the browser + let html = highlight.html(code) + io.println("
" <> html <> "
") + + // Convert to "highlighting tokens" to highlight in some other way + let highlight_tokens = highlight.tokens(code) + highlight_tokens_some_other_way(highlight_tokens) } ``` Further documentation can be found at . -## Development +### Missing features +The Just lexer should be able to accurately lex most valid JavaScript programs, +but there are a few things it is missing: -```sh -gleam run # Run the project -gleam test # Run the tests -``` +- Proper backtracking for Regular Expressions. Currently the lexer uses the previously + lexed token to determine whether to lex a `/` character as a division operator or a + regular expression. This means it can fail in some edge-cases. +- Lexing of full-unicode identifiers. JavaScript supports more than just ASCII characters + to make up its identifiers. Currently, Just doesn't support non-ASCII characters in + identifiers. +- Lexing identifier escape-sequences. Similarly, JavaScript allows unicode escape sequences + as part of identifiers (e.g. `let \u0065 = 10;`). This is currently not supported by Just. diff --git a/gleam.toml b/gleam.toml index d12d93e..e922181 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,16 +1,10 @@ name = "just" version = "1.0.0" -# Fill out these fields if you intend to generate HTML documentation or publish -# your project to the Hex package manager. -# -# description = "" -# licences = ["Apache-2.0"] -# repository = { type = "github", user = "", repo = "" } -# links = [{ title = "Website", href = "" }] -# -# For a full reference of all the available options, you can have a look at -# https://gleam.run/writing-gleam/gleam-toml/. +description = "A JavaScript lexer and syntax highlighter for Gleam!" +licences = ["Apache-2.0"] +repository = { type = "github", user = "GearsDatapacks", repo = "just" } +links = [] [dependencies] gleam_stdlib = ">= 0.44.0 and < 2.0.0"