# Syntax validation of a julia file

**URL:** <https://discourse.julialang.org/t/syntax-validation-of-a-julia-file/55946>\
**Category:** General Usage\
**Created:** [February 24, 2021, 5:35pm UTC](https://discourse.julialang.org/t/syntax-validation-of-a-julia-file/55946 "2021-02-24T17:35:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![artemsolod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artemsolod/32/20704_2.png) [@artemsolod](https://discourse.julialang.org/u/artemsolod)\
**Post date:** [February 24, 2021, 5:35pm UTC](https://discourse.julialang.org/t/syntax-validation-of-a-julia-file/55946/1 "2021-02-24T17:35:16Z")

</div>

I am trying to come up with a script to check program’s syntax without running it. Ideally this should not rely on external packages as the goal in mind is to use this within online judge system at competitive programming platform [codeforces.com](https://codeforces.com). Initial request for this came up here [https://github.com/MikeMirzayanov/binary-heap-benchmark/pull/16](https://github.com/MikeMirzayanov/binary-heap-benchmark/pull/16)

I would appreciate your comments on whether something along these lines is reasonable, platform-independent and safe from false-positive. One particular point is whether `[:incomplete, :error` is an exhaustive list of the things that need to be caught. Thanks!

```julia
function validatesyntax(fname)
  inp = read(fname, String)

  ast = ccall(:jl_parse_all, Any, (Cstring, Int64, Cstring, Int64), inp, length(inp), fname, length(fname))
  if length(ast.args) > 1 && (ast.args[end].head in [:incomplete, :error])
    println("syntax error $(ast.args[end])")
  end
end

(length(ARGS) == 1) && validatesyntax(ARGS[1])

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 24, 2021, 5:49pm UTC](https://discourse.julialang.org/t/syntax-validation-of-a-julia-file/55946/2 "2021-02-24T17:49:46Z")

</div>

> [@artemsolod](#):
>
> I am trying to come up with a script to check program’s syntax without running it.

In Julia 1.5 or later, you can do:

```julia
validatesyntax(fname) = include(expr -> Meta.isexpr(expr, (:error, :incomplete)) ? expr : nothing, fname)

```

where you are passing a syntax-transformer to `include` that leaves parse errors as-is but replaces other expressions with `nothing` before executing them.

(Even in earlier Julia versions, you can repeatedly call `Meta.parse` rather than doing a low-level `ccall`. Doing a `ccall` correctly is tricky — for example, you really want to pass `sizeof(fname)` rather than `length(fname)`, the latter being incorrect for non-ASCII strings.)

> [@artemsolod](#):
>
> One particular point is whether `[:incomplete, :error` is an exhaustive list of the things that need to be caught.

Yes, I believe so.

---

<div class="post-metadata">

**Author:** ![artemsolod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artemsolod/32/20704_2.png) [@artemsolod](https://discourse.julialang.org/u/artemsolod)\
**Post date:** [February 25, 2021, 10:26am UTC](https://discourse.julialang.org/t/syntax-validation-of-a-julia-file/55946/4 "2021-02-25T10:26:04Z")

</div>

Hi, Steven, thanks for helping out! This looks much tidier indeed. I hope it’s ok if a variant of your code will be used in the aforementioned online judge system.
