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

Implement squiggly heredocs for the parser translator #3341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Earlopain
Copy link
Contributor

In parser, the string content is dedented. This implements these rules as far as I was able to understand them. It's all a bit confusing with spaces/tabs, always learning more funny things about them.

I refered to the prism implementation, parser seems to adhere to it very closely.

@Earlopain Earlopain force-pushed the parser-translator-squigly-heredoc branch 2 times, most recently from a179339 to 02c0a66 Compare December 23, 2024 11:01
is_first_token_on_line = lexed[index - 1] && token.location.start_line != lexed[index - 2][0].location&.start_line
# The parser gem only removes indentation when the heredoc is not nested
not_nested = heredoc_stack.size == 1
if is_first_token_on_line && not_nested && (heredoc = heredoc_stack[0]).common_whitespace > 0
Copy link
Contributor Author

@Earlopain Earlopain Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any tip for getting this through sorbet? It complains that heredoc can be nil but that will never be the case because the previous condition checked that the first element is present.

When rewriting this into something that proves to sorbet that it can't be nil, it's telling me that the condition is always falsy.

This also fails:

index = 0
lexed = [:FIRST, :SECOND]
length = lexed.length

identifiers = []

while index < length
  token = lexed[index]
  index += 1

  case token
  when :FIRST

    identifiers.push("abc")
  when :SECOND
    if (identifier = identifiers[0])
      puts identifier
    end
  end
end

It seems to believe that the array is always empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got a workaround from sorbet/sorbet#8401 (comment) that seems to work

In parser, the string content is dedented. This implements these rules
as far as I was able to understand them. It's all a bit confusing with
spaces/tabs, always learning more funny things about them.

I refered to the prism implementation, parser seems to adhere to it very closely.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant