Roundtrip String -> Syntax Tree -> String with JuliaSyntax.jl

Hi!

I am really hyped about JuliaSyntax.jl, however I could not figure out how to perform a round trip from the parsed String to a syntax tree and back to a String.
Is this something possible already?

My usecase would be to use JuliaSyntax as a parser, work directly in the syntax tree form and then go back to a String representation.

Thanks!

2 Likes

Here you have an example (hope this fits your use case well enough):

using JuliaSyntax

text = """inc(x) = x + 1"""
there = parsestmt(JuliaSyntax.SyntaxNode, text)

# do some work on `there` object

#back to string "inc(x) = x + 1"
andbackagain = JuliaSyntax.sourcetext(there)

This works at statement/expression level - but the same will work if you are doing the parsing at file-level (just switch the parsestmt with the appropriate parse function).

1 Like

Hey,

I actually went back to this. Thanks for your answer but it unfortunately does not work for me.

sourcetext only works for SyntaxNode and not for GreenNode, additionaly it just return the source that it was given at the beginning (not that it’s actually easily possible to change the values in the SyntaxNode…)

I guess JuliaSyntax.jl is really more meant to be β€œread-only”

Even so, I have trouble to only read the lossless tree. I can see the tree with its source, but I cannot find how to access the text in the trivia bits.

For instance:

  ...
β”‚     47:359    β”‚    [block]
β”‚     47:48     β”‚      NewlineWs              "\n\t"
β”‚     49:68     β”‚      [=]
β”‚     49:51     β”‚        Identifier       βœ”   "vec"
β”‚     52:52     β”‚        Whitespace           " "
  ...

I want to check if there are tabs in indentation, as can be seen in the source text linked to that NewlineWs node above, but once I got hold of such a node, walking the tree, I don’t know how to retrieve the text for it:

> untokenize(node.head)
Internal Error: TypeError: in typeassert, expected String, got a value of type Nothing

> sourcetext(node)
Internal Error: MethodError: no method matching sourcefile(::JuliaSyntax.GreenNode{JuliaSyntax.SyntaxHead})
The function `sourcefile` exists, but no method is defined for this combination of argument types.

Closest candidates are:  ...

Internal Error: MethodError: no method matching sourcefile(::JuliaSyntax.SyntaxHead)
The function `sourcefile` exists, but no method is defined for this combination of argument types.

Closest candidates are:  ...

> untokenize(node)
Internal Error: MethodError: no method matching untokenize(::JuliaSyntax.GreenNode{JuliaSyntax.SyntaxHead})
The function `untokenize` exists, but no method is defined for this combination of argument types.

Closest candidates are:  ...

> string(node)
"JuliaSyntax.GreenNode{JuliaSyntax.SyntaxHead}(JuliaSyntax.SyntaxHead(K\"NewlineWs\", 0x0001), 0x00000002, nothing)"

That last one at least is not a fail, but it is not what I need either.

Also, but more of the same: how to access the contents of comments?