Skip to content

Commit

Permalink
Support multi-letter sigils
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 10, 2024
1 parent 532ebf9 commit f578da0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/makeup/lexers/elixir_lexer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,15 @@ defmodule Makeup.Lexers.ElixirLexer do

sigils_interpol =
for {ldelim, rdelim} <- sigil_delimiters do
sigil(ldelim, rdelim, [?a..?z], combinators_inside_string)
sigil(ldelim, rdelim, utf8_string([?a..?z], 1), combinators_inside_string)
end

sigils_no_interpol =
for {ldelim, rdelim} <- sigil_delimiters do
sigil(ldelim, rdelim, [?A..?Z], [escape_delim(rdelim), iex_prompt_inside_string])
sigil(ldelim, rdelim, ascii_string([?A..?Z], min: 1), [
escape_delim(rdelim),
iex_prompt_inside_string
])
end

all_sigils = sigils_interpol ++ sigils_no_interpol
Expand Down
10 changes: 5 additions & 5 deletions lib/makeup/lexers/elixir_lexer/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ defmodule Makeup.Lexers.ElixirLexer.Helper do
string("\\" <> rdelim_first_char)
end

def sigil(ldelim, rdelim, ranges, middle) do
left = string("~") |> utf8_char(ranges) |> string(ldelim)
def sigil(ldelim, rdelim, name, middle) do
left = string("~") |> concat(name) |> string(ldelim)
right = string(rdelim)

choices = middle ++ [utf8_char([])]
Expand All @@ -31,9 +31,9 @@ defmodule Makeup.Lexers.ElixirLexer.Helper do
def build_sigil(rest, acc, context, line, offset) do
type =
case Enum.at(acc, -2) do
sigil when sigil in ~c"sScC" -> :string
sigil when sigil in ~c"rR" -> :string_regex
sigil when sigil in ~c"TDNU" -> :literal_date
sigil when sigil in ~w"s S c C" -> :string
sigil when sigil in ~w"r" -> :string_regex
sigil when sigil in ~w"T D N U" -> :literal_date
_ -> :string_sigil
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ defmodule ElixirLexerTokenizerTestSnippet do
assert lex("~U[2020-01-01 01:23:45Z]") == [{:literal_date, %{}, "~U[2020-01-01 01:23:45Z]"}]
end

test "custom sigils" do
assert lex("~VEC[1 2.0 3]f64") == [{:string_sigil, %{}, "~VEC[1 2.0 3]f64"}]
end

describe "strings and sigils" do
test "unicode codepoints" do
assert lex(~S["\u0000"]) == [
Expand Down

0 comments on commit f578da0

Please sign in to comment.