Skip to content

Commit

Permalink
Avoid re-parsing if delimiter reaches the end of the string (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored May 11, 2024
1 parent 02689b5 commit 3686f4c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/makeup/lexer/combinators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ defmodule Makeup.Lexer.Combinators do
Matches a given combinator, repeated 0 or more times, surrounded by left and right delimiters.
Delimiters can be combinators or literal strings (either both combinators or both literal strings).
It also succeeds if we get to the end of string and the closing delimiter is missing,
to avoid parsing the program multiple times in case of mismatched delimeters or invalid programs.
"""
def many_surrounded_by(combinator, left, right) when is_binary(left) and is_binary(right) do
token(left, :punctuation)
Expand All @@ -129,7 +132,7 @@ defmodule Makeup.Lexer.Combinators do
|> concat(combinator)
)
)
|> concat(token(right, :punctuation))
|> choice([token(right, :punctuation), eos()])
end

def many_surrounded_by(combinator, left, right) do
Expand All @@ -140,12 +143,15 @@ defmodule Makeup.Lexer.Combinators do
|> concat(combinator)
)
)
|> concat(right)
|> choice([right, eos()])
end

@doc """
Matches a given combinator, repeated 0 or more times, surrounded by left and right delimiters,
and wraps the `right` and `left` delimiters into a token of the given `ttype`.
It also succeeds if we get to the end of string and the closing delimiter is missing,
to avoid parsing the program multiple times in case of mismatched delimeters or invalid programs.
"""
def many_surrounded_by(combinator, left, right, ttype) do
token(left, ttype)
Expand All @@ -155,7 +161,7 @@ defmodule Makeup.Lexer.Combinators do
|> concat(combinator)
)
)
|> concat(token(right, ttype))
|> choice([token(right, ttype), eos()])
end

@doc false
Expand Down

0 comments on commit 3686f4c

Please sign in to comment.