Skip to content

Lexer for Agda - #1825

Open
ionathanch wants to merge 18 commits into
rouge-ruby:mainfrom
ionathanch:master
Open

ionathanch wants to merge 18 commits into
rouge-ruby:mainfrom
ionathanch:master

Conversation

@ionathanch

@ionathanch ionathanch commented May 25, 2022

Copy link
Copy Markdown

[Issue #709] Based on Agda's lexer, I've filled out the remaining bits of the Agda lexer. It doesn't quite match the behaviour, especially in places where the semantics of the keywords matter (e.g. imports, pragmas), but this should be sufficient to cover most use cases and look nice, and I think Rouge should have at least some Agda support with improvements as needed later on. (And thanks @ayberkt for the initial work!)

@ionathanch ionathanch changed the title Lexer for Agda (#709) Lexer for Agda May 25, 2022
@tancnle tancnle added the needs-review The PR needs to be reviewed label May 29, 2022
@awsloth

awsloth commented Feb 2, 2025

Copy link
Copy Markdown

Not sure the etiquette on this, but I would like to boost this. The support for agda here would be great!

Comment thread lib/rouge/demos/agda Outdated
Comment thread lib/rouge/lexers/agda.rb
Comment thread lib/rouge/lexers/agda.rb Outdated
@ionathanch
ionathanch requested a review from jneen August 24, 2026 16:24
Comment thread lib/rouge/lexers/agda.rb
state :hole do
rule %r/!}/, Comment::Special, :pop!
rule %r/{!/, Comment::Special, :hole
rule %r/./, Comment::Special

@jneen jneen Aug 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If possible, it's much better for performance to capture more than a single character at a time here. Maybe above this rule add one for %r/[^{!]+/? It avoids having to pop in and out of the regex engine on every character.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(same goes for the other rules that use %r/./ - try to have a greedier regex first if possible)

@jneen

jneen commented Aug 24, 2026

Copy link
Copy Markdown
Member

To fix the Linelint error all you've gotta do is remove the extra blank line at the end of spec/visual/samples/agda. And if you've got more gnarly difficult-to-parse agda code please feel free to toss it in there.

* Builtin pragmas always have the form `{#- BUILTIN <builtin> ... #-}`
* Other pragmas always have the form `{#- <pragma> ... #-}`
* Match as many chars as possible in block comments/holes
* Fix lexing line comments after many spaces
(More examples to follow)
@ionathanch
ionathanch requested a review from jneen August 26, 2026 16:50
Comment thread lib/rouge/lexers/agda.rb Outdated
keywords %r/\w+/ do
rule BUILTINS, Keyword::Pseudo
end
rule %r/\s+/, Comment::Preproc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is here twice for some reason, and in a bunch of the other rules.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Do you mean the \s+ rules? That was to ensure that the builtin (and pragma below) keywords have to be followed by spaces

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's unfortunately not how the lexer model works - keywords will end when the covering regex ends, so at the first non-\w character. If you really need a space (and not, e.g. a parenthesis or other punctuation), you can use a lookahead, like keywords %r/\w+(?=\s)/. I'm not super familiar with Agda, but I think that's unlikely to be correct.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah I see... reading the lexer dev doc more closely, I've misunderstood how the states work. I think I need to move the keyword rule up to where the pragma rule is then

Comment thread lib/rouge/lexers/agda.rb Outdated
rule BUILTINS, Keyword::Pseudo
end
rule %r/\s+/, Comment::Preproc
rule %r/.+#-}/, Comment::Preproc, :pop!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This will unfortunately break if the brace isn't closed. The way you had it before was fine, but it needed a negative capture to grab stuff a bit more greedily.

@ionathanch
ionathanch requested a review from jneen September 13, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-review The PR needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants