Skip to content

Commit

Permalink
Relax whitespace requirement around operators.
Browse files Browse the repository at this point in the history
Fixes #637 #397
  • Loading branch information
gdotdesign committed Nov 17, 2024
1 parent 21b5e52 commit b184211
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
12 changes: 12 additions & 0 deletions spec/examples/pipe
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ component Main {
""
}
}
-------------------------------------------------------------------------------
component Main {
fun test (value : String) {
value
}

fun render : Html {
"test" |> test

<div/>
}
}
1 change: 0 additions & 1 deletion src/parser/top_level.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ module Mint
component ||
constant ||
property ||
operator ||
provider ||
function ||
comment ||
Expand Down
29 changes: 14 additions & 15 deletions src/parsers/operator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ module Mint
"!" => 16,
}

# Parses an operator.
#
# All operators must follow a whitespace because we don't have an end of
# line token, so the expression can leak through to the next entity, for
# example:
#
# const NAME = "Joe"
#
# /* This is a comment. */
# fun greet (name : String = NAME) { ... }
#
# In this case the start token of the comment (/*) can be interpreted as an
# operation (/) and the whitespace prevents that.
def operator : String?
parse do
whitespace
Expand All @@ -47,10 +34,22 @@ module Mint
position

operator =
OPERATORS.keys.find { |item| word! item }
OPERATORS.keys.find { |item| word? item }

next unless operator
next if operator != "|>" && !whitespace?

# It's not an operator if it's a closing tag `</div>`
# or fragment `<>` or anything starting with `<`
# or a comment `// comment`, `/* comment */`.
case operator
when "<"
next if base_expression
next if next_char == '/'
when "/"
next if next_char.in?('*', '/')
end

word! operator

case operator
when "or"
Expand Down

0 comments on commit b184211

Please sign in to comment.