···11-# just
11+# Just
22+33+A JavaScript lexer and syntax highlighter for Gleam!
2435[](https://hex.pm/packages/just)
46[](https://hexdocs.pm/just/)
5788+Just is a JavaScript lexer and syntax highlighter written in Gleam.
99+The `just` module, based on [`glexer`](https://hexdocs.mp/glexer) exposes a
1010+standard lexer API, allowing you to convert JavaScript source code into tokens.
1111+The `just/highlight` module allows you to highlight javascript code using ansi
1212+colours, html or a custom format. Heavily inspired by [`contour`](https://hexdocs.pm/contour).
1313+614```sh
715gleam add just@1
816```
1717+918```gleam
1019import just
2020+import just/highlight
11211212-pub fn main() -> Nil {
1313- // TODO: An example of the project in use
2222+pub fn main() {
2323+ let code = "console.log('Hello, world!');"
2424+2525+ let lexer = just.new(code) |> just.strict_mode
2626+ // Lex syntax tokens for parsing or other uses
2727+ let #(tokens, errors) = just.tokenise(lexer)
2828+ let assert [] = errors
2929+ parse_js(tokens)
3030+3131+ // Highlight with ansi codes to print in the terminal
3232+ let highlighted = highlight.ansi(code)
3333+ io.println(highlighted)
3434+3535+ // Render to html to show in the browser
3636+ let html = highlight.html(code)
3737+ io.println("<pre><code>" <> html <> "</code></pre>")
3838+3939+ // Convert to "highlighting tokens" to highlight in some other way
4040+ let highlight_tokens = highlight.tokens(code)
4141+ highlight_tokens_some_other_way(highlight_tokens)
1442}
1543```
16441745Further documentation can be found at <https://hexdocs.pm/just>.
18461919-## Development
4747+### Missing features
4848+The Just lexer should be able to accurately lex most valid JavaScript programs,
4949+but there are a few things it is missing:
20502121-```sh
2222-gleam run # Run the project
2323-gleam test # Run the tests
2424-```
5151+- Proper backtracking for Regular Expressions. Currently the lexer uses the previously
5252+ lexed token to determine whether to lex a `/` character as a division operator or a
5353+ regular expression. This means it can fail in some edge-cases.
5454+- Lexing of full-unicode identifiers. JavaScript supports more than just ASCII characters
5555+ to make up its identifiers. Currently, Just doesn't support non-ASCII characters in
5656+ identifiers.
5757+- Lexing identifier escape-sequences. Similarly, JavaScript allows unicode escape sequences
5858+ as part of identifiers (e.g. `let \u0065 = 10;`). This is currently not supported by Just.
+4-10
gleam.toml
···11name = "just"
22version = "1.0.0"
3344-# Fill out these fields if you intend to generate HTML documentation or publish
55-# your project to the Hex package manager.
66-#
77-# description = ""
88-# licences = ["Apache-2.0"]
99-# repository = { type = "github", user = "", repo = "" }
1010-# links = [{ title = "Website", href = "" }]
1111-#
1212-# For a full reference of all the available options, you can have a look at
1313-# https://gleam.run/writing-gleam/gleam-toml/.
44+description = "A JavaScript lexer and syntax highlighter for Gleam!"
55+licences = ["Apache-2.0"]
66+repository = { type = "github", user = "GearsDatapacks", repo = "just" }
77+links = []
148159[dependencies]
1610gleam_stdlib = ">= 0.44.0 and < 2.0.0"