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

if-expression requires parentheses within string interpolation #1767

Closed
danielkroeni opened this issue Jul 21, 2021 · 7 comments
Closed

if-expression requires parentheses within string interpolation #1767

danielkroeni opened this issue Jul 21, 2021 · 7 comments
Assignees

Comments

@danielkroeni
Copy link
Contributor

When using an if-expression within string interpolation an error gets reported. But the error mentions that the token 'if' belongs to the expected tokens.

Steps to Reproduce

Evaluate the following string:
"\{ if True then "a" else "b" }"

Expected Behavior

I would expect that it evaluates to "a"
or a parse error that says that if is not allowed in this position.

Observed Behavior

Main> "\{ if True then "a" else "b" }"
Couldn't parse any alternatives:
1: Expected 'case', 'if', 'do', application or operator expression.
(Interactive):1:5--1:7
 1 | "\{ if True then "a" else "b" }"
         ^^

It works as expected when the if-expression is surrounded with parentheses.

Main> "\{ (if True then "a" else "b") }"
"a"

Idris2 Version

Version 0.4.0-5f3480120

@andylokandy
Copy link
Contributor

I printed the token for examples:

"\{ (if True then "a" else "b") }"
[multiline string begin, string interp begin, symbol (, if, identifier True, then, multiline string begin, string "a", string end, else, multiline string begin, string "b", string end, symbol ), string interp end, string end, end of input]

"\{ if True then "a" else "b" }"
[multiline string begin, string interp begin, if, identifier True, then, multiline string begin, string "a", string end, else, multiline string begin, string "b", string end, string interp end, string end, end of input]

seems that all strings are identified as multiline strings.

@andrevidela
Copy link
Collaborator

that's incorrect but that shouldn't cause the problem that we see here, mhhhh

@andrevidela
Copy link
Collaborator

andrevidela commented Jul 21, 2021

@andylokandy I quickly tried adding commit after parsing the if keyword but no change. It looks like it's stuck at simpleExpr.

@andrevidela
Copy link
Collaborator

I think I've figured it out. An if statement was not considering InterpEnd as a valid closing token for a block. Adding this in and running the tests, fingers crossed. The fix should also help with do notation etc

@andylokandy
Copy link
Contributor

@andrevidela that's amazing! Anyway I'll investigate the weird multiline string a bit.

andrevidela added a commit to andrevidela/Idris2 that referenced this issue Jul 21, 2021
The `if then else` syntax expects a block for the `then` and `else`
parts. Before this patch, the token `InterpEnd` was not a valid
follow up token to end a block. This adds `InterpEnd` as a closing
token for blocks, allowing `if then else` in interpolation slices
without additional parens.
@andylokandy
Copy link
Contributor

andylokandy commented Jul 22, 2021

I've figured it out! It was a silly printing problem, the flag indicating whether the string is multiline is showed in the opposite:

show (StringBegin True) = "string begin"
show (StringBegin False) = "multiline string begin"

pretty (StringBegin True) = reflow "string begin"
pretty (StringBegin False) = reflow "multiline string begin"

@andrevidela
Copy link
Collaborator

Those booleans are evil…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants