Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call escape_raw_string to make parsing more predictable #34

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/stylemacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ inheritval = whitespace, ':'?, symbol ;
```
"""
macro styled_str(raw_content::String)
#------------------
# Helper functions
#------------------
# First undo the transforms of the parser, (assuming the input was entered
# with single `styled""` markers so this transform is unambiguously
# reversible and not as `@styled_str "."` or `styled"""."""`), since the
# `unescape_string` transforms will be a superset of those transforms
raw_content = Base.escape_raw_string(raw_content)

# If this were a module, I'd define the following struct.

Expand Down Expand Up @@ -128,6 +130,10 @@ macro styled_str(raw_content::String)
"grey", "gray", "bright_black", "bright_red", "bright_green", "bright_yellow",
"bright_blue", "bright_magenta", "bright_cyan", "bright_white")

#------------------
# Helper functions
#------------------

function styerr!(state, message, position::Union{Nothing, Int}=nothing, hint::String="around here")
if !isnothing(position) && position < 0
position = prevind(
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ end
@macroexpand styled"{(foreground=$color):val}"
end

# Trailing (and non-trailing) Backslashes
@test String(styled"\\") == "\\"
@test String(styled"\\\\") == "\\\\"
@test String(styled"\\\\\\") == "\\\\\\"
@test String(styled".\\") == ".\\"
@test String(styled".\\\\") == ".\\\\"
@test String(styled".\\\\\\") == ".\\\\\\"

# newlines
normal = "abc\
def"
Expand Down
Loading