Important: Please read the Qt Code of Conduct -

JavaScript parser

  • Hi,

    I would be interested in somehow getting an AST for a QML JS function, is there anything in the Qt stack that I could use for that?

    Any tips for this outside Qt? I see there are some open-source parsers for JS written in JS, I might try some of those if necessary.


  • Lifetime Qt Champion


    Do you mean something like esprima ?

  • @SGaist Thanks for the reply. Yes, that seems good by a glance, I am wondering if it would be possible to perhaps use this or some other open source / free JavaScript parser either as external library, or as imported C++/JS source code in a Qt project? This is just a quick question if you or someone would have a clue right away, of course I'll need to dive into the topic myself.

    Also I am wondering if it is really so that no tool in the Qt stack really produces an AST for JS or QML+JS? If I have understood correctly it might be indeed so that this stuff is generated straight into bytecode instructions or so on.

  • Lifetime Qt Champion

    You have a starting point here for the integration of JavaScript in a QML application.

    AFAIK, no. QML is backed by a dedicated engine. You can see this in the qtdeclarative module.

  • I am currently using Esprima, running on Node.js, via XmlHttpRequest. Was easy to setup.

  • Found a "hack" to integrate esprima to the build somewhat easily: By using QML WebView that loads a resource-included html page that includes the resource-included esprima.js file, and has some custom js functions to utilize it. The "experimental" features of WebView can be used to communicate with the js functions of the page -- or directly with esprima of course.

    Simply importing esprima.js to a QML file does not work on Qt 5.4 (not sure about later versions), as there seems to be some problems with the parsing. It seems, however, that the WV engine can utilize it, I think it uses V8 (likely does, as it is based on the Chromium project, if I have understood correctly).

    About the WV experimental features:

    Also, as a curiosity, I am using the esprima.js file that is fetched when you install the esprima module to a Node.js project with npm. This file is not present in this form in the Git repository.

Log in to reply