Can I check a script's syntax without executing it?

426 views
Skip to first unread message

Nathan Long

unread,
May 3, 2014, 7:08:48 AM5/3/14
to elixir-l...@googlegroups.com
Is it possible to have Elixir check a script's syntax without executing it?

I ask because I just had a problem editing an Elixir script. I'm using Vim with the Syntastic syntax checker; it somehow calls out to Elixir to find syntax errors.

In my script, I defined a function and immediately called it, so that when I ran `elixir myscript.exs`, I would see the output. At one point, every time I saved the file, my editor would hang. Eventually I tried running my program using `elixir myscript.exs`, and realized I had an infinite recursion problem. The syntax checker was running the script and having the same problem.

I would much prefer not to have my editor hang when checking such a script's syntax; I can discover the infinite recursion more easily from the command line and it won't screw up my editing session.

I wondered if there was a flag I could pass to elixir to say "only check the syntax", but I didn't see it when running `elixir --help`.

I am accustomed to this working in Ruby. For example, if I have this Ruby script:

=====
# foo.rb
def foo
  loop { puts "going forever..." }
end

foo
=====

...running `ruby -c foo.rb` will tell me the syntax is OK but will not execute the infinite loop.

Besides convenience, this might be a safety concern; if my script had done anything dangerous, I would have been very sad to find that simply by saving the file, I had executed it.

José Valim

unread,
May 3, 2014, 7:11:26 AM5/3/14
to elixir-l...@googlegroups.com
You can probably use Code.string_to_quoted/1 that will fail if you have invalid syntax. You can use that to build a small script to check the syntax for you. However, that doesn't check any of the semantics, so you could have total gibberish and it will still work. :)



José Valim
Skype: jv.ptec
Founder and Lead Developer


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nathan Long

unread,
May 3, 2014, 7:13:51 AM5/3/14
to elixir-l...@googlegroups.com
Wow, thanks for the fast response! Can you clarify what you mean by "semantics" here as opposed to syntax? Is it the difference between "deffff isn't a keyword" and "you have no function like foo/2"?


--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/B29noPHvQ-8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.

José Valim

unread,
May 3, 2014, 7:21:47 AM5/3/14
to elixir-l...@googlegroups.com
I mean code like below will gladly parse:

iex(3)> Code.string_to_quoted("defmodule 1, 2")
{:ok, {:defmodule, [line: 1], [1, 2]}}

That's because we have very few keywords (defmodule is just a function/macro like everything else).




José Valim
Skype: jv.ptec
Founder and Lead Developer


Reply all
Reply to author
Forward
0 new messages