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

Improve support for single quotes in Haskell lexer #1524

Merged
merged 5 commits into from Jun 3, 2020

Conversation

pyrmont
Copy link
Contributor

@pyrmont pyrmont commented May 30, 2020

Haskell uses matching single quotes to express character literals (e.g. 'A'). However, the unmatched single quote character, ', is also used for various purposes (e.g. as part of a variable name like x' or in the name of a promoted type like 'Bar). Currently, Rouge's Haskell lexer permits these use of unmatched single quote by having separate rules for naming and then a general rule of lower precedence for when the lexer encounters '.

A better approach would be to only treat ' as being part of a character literal if the lexer detects that there is a matching ' within an appropriate number of characters. This PR does that and then creates a more general rule for matching names.

This fixes #1514.

@pyrmont pyrmont added the needs-review The PR needs to be reviewed label May 30, 2020
@pyrmont
Copy link
Contributor Author

pyrmont commented May 30, 2020

@Fresheyeball The problem you were encountering with your code was actually due to the fact that Rouge was treating class as being a keyword (despite the fact that it was followed by ') and then assuming that the ' represented the beginning of a character literal.

The idiomatic way to fix that in Rouge is to have a more general rule for matching names that can then take advantage of Array's include? method to match against the array of keywords. I've made that change and then adjusted the precedence of the rules to account for the fact that the general rule would match character literals and numbers.

It looks to me like this fixes the problem without introducing any regressions but let me know if you have any feedback!

@pyrmont pyrmont self-assigned this May 30, 2020
@pyrmont pyrmont merged commit 60e4f8f into rouge-ruby:master Jun 3, 2020
@pyrmont pyrmont deleted the bugfix.haskell-prime branch June 3, 2020 20:40
@pyrmont pyrmont removed the needs-review The PR needs to be reviewed label Jun 3, 2020
@Fresheyeball
Copy link

Looks good to me! 😄 Can't wait for this to make it's way to Gitlab

@pyrmont
Copy link
Contributor Author

pyrmont commented Jun 3, 2020

@Fresheyeball We release new versions of Rouge on a monthly cadence. This will be part of v3.20.0 and will go out to RubyGems on Tuesday 9 June. I'm not sure how regularly GitLab pulls in a new version of the library but I imagine it should arrive pretty soon! 🎉

mattt pushed a commit to NSHipster/rouge that referenced this pull request May 19, 2021
Haskell uses matching single quotes to express character literals (e.g.
`'A'`). However, the unmatched single quote character, `'`, is also
used for various purposes (e.g. as part of a variable name like `x'` or
in the name of a promoted type like `'Bar`). Currently, Rouge's Haskell
lexer permits the use of an unmatched single quote by having separate
rules for naming and then a general rule of lower precedence for when
the lexer encounters `'`.

A better approach would be to only treat `'` as being part of a
character literal if the lexer detects that there is a matching `'`
within an appropriate number of characters. This commit does that and
then creates a more general rule for matching names.
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.

Haskell prime breaks highlighting
2 participants