# How to extract the body of a function?

**URL:** <https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 9, 2017, 4:36pm UTC](https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567 "2017-03-09T16:36:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [March 9, 2017, 4:36pm UTC](https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567/1 "2017-03-09T16:36:16Z")

</div>

I need to parse functions and extract the  
function body. How can I achieve this with  
the parser or by other means?

For example given the text of the function

```
function fun(n::Int)
	s = 2 * n
	println(s)
end

```

the result should by the list of strings

```
["s = 2 * n", "println(s)"]

```

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [March 9, 2017, 4:44pm UTC](https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567/2 "2017-03-09T16:44:08Z")

</div>

```julia
julia> parse("""
 function fun(n::Int)
   s = 2 * n
   println(s)
 end
""")

```

The result shall be an expression (defined in the [Metaprogramming](http://docs.julialang.org/en/stable/manual/metaprogramming/) section of the manual). See also [JuliaParser](https://github.com/JuliaLang/JuliaParser.jl).

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [March 9, 2017, 5:05pm UTC](https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567/3 "2017-03-09T17:05:29Z")

</div>

It’s nice to get something like

```
(:function, (:call, :fun, (:(::), :n, :Int)), 
(:block,
    (:line, 2, :none),
    (:(=), :s, (:call, :*, 2, :n)),
    (:line, 3, :none),
    (:call, :println, :s)
  ))

```

but how can I proceed from this back to the required form

```
["s = 2 * n", "println(s)"]

```

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [March 9, 2017, 7:11pm UTC](https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567/4 "2017-03-09T19:11:41Z")

</div>

```julia
[string(subex) for subex in ex.args[2].args if subex.head != :line]

```

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [March 9, 2017, 9:34pm UTC](https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567/5 "2017-03-09T21:34:24Z")

</div>

Now that’s brilliant! Thanks!  
For other beginners here the full solution:

```
using JuliaParser
src = """
 function fun(n::Int)
   s = 2 * n
   println(s)
 end
"""
ast = Parser.parse(src);
Meta.show_sexpr(ast)
funbody = [string(subex) for subex in ast.args[2].args if subex.head != :line]
print(funbody)

```

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [March 10, 2017, 6:30am UTC](https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567/6 "2017-03-10T06:30:57Z")

</div>

Here is another option: [https://github.com/KristofferC/Tokenize.jl](https://github.com/KristofferC/Tokenize.jl)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 11, 2017, 7:09pm UTC](https://discourse.julialang.org/t/how-to-extract-the-body-of-a-function/2567/7 "2017-03-11T19:09:10Z")

</div>

Tokenize doesn’t really do any parsing though.
