Zulip Chat Archive

Stream: metaprogramming / tactics

Topic: Parsing a string


Jon Eugster (Feb 21 2024 at 17:19):

I don't fully understand how parsing works and all, but can I somehow feed some String to a parser to get a new syntax object from it?

Concretely, I'm trying to modify an interpolated string before feeding it back to s!. I had hoped this would be cleaner than duplicating all of the backend of s!. Is that possible?

Here's a MWE:

import Lean

open Lean Elab Term

syntax:max "t!" interpolatedStr(term) : term

elab_rules : term
  | `(t! $interpStr) => withFreshMacroScope do
    let key : String := "…replace it with this {1+1}"
    -- TODO: try to parse `key` and feed it to `s!` below
    let modified : TSyntax `interpolatedStrKind := interpStr
    Term.elabTerm ( `(s! $modified)) none

#eval t!"starting with this {1+2}…"

Jon Eugster (Feb 21 2024 at 17:19):

I'd hope this could print …replace it with this 2 in the infoview.

Adam Topaz (Feb 21 2024 at 17:29):

I think docs#Lean.Parser.runParserCategory is useful for this?

Kyle Miller (Feb 21 2024 at 18:58):

There isn't too much code you need for processing an interpolatedStr(term). The idea is that the underlying Syntax is an array of strings or terms, and you can loop through them and do whatever processing you want, depending on which of the two it is.

If you want more examples, the first two functions in the Basic file in this message show some processing. That creates a separate term, but you could have the output be another interpolatedStrKind.

Jon Eugster (Feb 21 2024 at 21:53):

@Kyle Miller I indeed took your code there as inspiration:grinning_face_with_smiling_eyes: but I'd like to use .po files for the translation part. I have the creation of that .po file down fine, but now I need to parse it again to get the translations into lean. And that's where I thought the easiest would be to just re-parse the translated string as an interpolated string.

Jon Eugster (Feb 22 2024 at 11:27):

thanks @Adam Topaz, got it working :)

working example


Last updated: May 02 2025 at 03:31 UTC