Skip to content

Commit

Permalink
Add support for erlang prompt (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
garazdawi authored Dec 5, 2023
1 parent 3fee250 commit 3e930e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/makeup/lexers/erlang_lexer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule Makeup.Lexers.ErlangLexer do

simple_atom_name =
ascii_string([?a..?z], 1)
|> optional(ascii_string([?a..?z, ?_, ?0..?9, ?A..?Z], min: 1))
|> optional(ascii_string([?a..?z, ?_, ?@, ?0..?9, ?A..?Z], min: 1))
|> reduce({Enum, :join, []})

single_quote_escape = string("\\'")
Expand Down Expand Up @@ -206,6 +206,14 @@ defmodule Makeup.Lexers.ErlangLexer do
|> concat(token("/", :punctuation))
|> concat(number_integer)

# Erlang prompt
erl_prompt =
string("\n")
|> optional(string("(") |> concat(atom_name) |> string(")"))
|> optional(digits)
|> string("> ")
|> token(:generic_prompt, %{selectable: false})

# Tag the tokens with the language name.
# This makes it easier to postprocess files with multiple languages.
@doc false
Expand All @@ -215,6 +223,7 @@ defmodule Makeup.Lexers.ErlangLexer do

root_element_combinator =
choice([
erl_prompt,
module_attribute,
hashbang,
whitespace,
Expand Down
33 changes: 33 additions & 0 deletions test/makeup/erlang_lexer/erlang_lexer_tokenizer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ defmodule ErlangLexerTokenizer do
describe "atoms" do
test "are tokenized as such" do
assert lex("atom") == [{:string_symbol, %{}, "atom"}]
assert lex("at_om") == [{:string_symbol, %{}, "at_om"}]
assert lex("atom@atom") == [{:string_symbol, %{}, "atom@atom"}]
end

test "are tokenized as such even when quoted" do
Expand Down Expand Up @@ -384,4 +386,35 @@ defmodule ErlangLexerTokenizer do
assert {:number_integer, %{}, "0"} in tokens
end
end

describe "prompt" do
test "without number" do
assert lex("> a.") == [{:generic_prompt, %{selectable: false}, "> "},
{:string_symbol, %{}, "a"},
{:punctuation, %{}, "."}]
assert lex("(a@b)> a.") == [{:generic_prompt, %{selectable: false}, "(a@b)> "},
{:string_symbol, %{}, "a"},
{:punctuation, %{}, "."}]
end

test "with number" do
assert lex("1> a.") == [{:generic_prompt, %{selectable: false}, "1> "},
{:string_symbol, %{}, "a"},
{:punctuation, %{}, "."}]
assert lex("(a@b)1> a.") == [{:generic_prompt, %{selectable: false}, "(a@b)1> "},
{:string_symbol, %{}, "a"},
{:punctuation, %{}, "."}]
end

test "greater-than still works" do
assert lex("1>2") == [{:number_integer, %{}, "1"},
{:operator, %{}, ">"},
{:number_integer, %{}, "2"}]
assert lex("1 > 2") == [{:number_integer, %{}, "1"},
{:whitespace, %{}, " "},
{:operator, %{}, ">"},
{:whitespace, %{}, " "},
{:number_integer, %{}, "2"}]
end
end
end

0 comments on commit 3e930e2

Please sign in to comment.