LoginSignup
0
0

More than 3 years have passed since last update.

JuliaのMeta.parseでファイルをパーズしてみる

Last updated at Posted at 2019-11-22

実行環境 julia1.2, MacOS

juliaのソースをparseして、関数やstructの関係を解析するツールを作ってみたいなと思ったわけですが、ファイルの中身をstringにしてMeta.parse()の引数に渡してもエラーになるばかり。

Manualを読むと、Meta.parse()には第二引数があって、そこを始点としてparseをしてくれるらしい。

ということで、ファイルの中身を文字列として次のファイルに渡すと、parseできる単位(関数とかstruct)でparseしたものをExprのArray{Any,1}としてくれる関数を作ってみた。

function parsestr(str)
 ix=1
 ps=[]
 while true
   (pp, ni) = Meta.parse(str,ix)
   if pp==nothing; break end
   push!(ps, pp)
   ix=ni
 end
 return ps
end

追記
Expr以外にもstringが収穫されることもある
他にもあるのかも

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0