2

I have hundreds of small javascript files that follow the following format where Ext.define defines a javascript class. I need to be able to parse these files in a Java app and get the "SOMECLASSNAME" and the values for various properties like "extend" and "requires". I want to ignore comments and I don't care about the functions.

I have actually already implemented this using regular expressions and it works. However, it's fairly brittle. I've already dealt with issues like files that use " instead of ' and inconveniently placed comments which break the regexes. I would really like to do this using a less brittle solution and that means using a javascript parser.

I've searched using various searches like "java javascript ast parser", but nothing seems to fit what I'm trying to do. The closest I've found is Using ScriptEngine in java, How can I extract function list? but that seems to require that the script be runnable and each of these files isn't runnable by itself.

Ext.define('SOMECLASSNAME', {
    extend: 'JSCommon.remoting.RESTService',
    alternateClassName: 'AccountSettingService',

    requires: [
        'JSCommon.security.Privilege',
        'JSCommon.security.PrivilegeConstants',
        'JSCommon.security.store.UIPrivilege',
        'JSCommon.util.StringUtil'
    ],

    statics: {
        /**
         * Creates and returns a new instance of the ReportsService
         */
        getNewInstance: function() {
            return Ext.create('JSCommon.acctSetting.AccountSettingService');
        }
    },

    config: {
        customer: 'abc',
        service: 'companySetting'
    },

    constructor: function(config) {
        this.initConfig(config);
        return this;
    },

    getCompanyID: function() {
        return PlatformCache.getCompanyContextByAttribute('id');
    },

    getAccountSetting: function(onResult, onFault) {
        var me = this;
        var svc = RESTService.getNewInstance();
        var ops = Ext.create('JSCommon.remoting.URLElements');
        var params = Ext.create('JSCommon.remoting.URLParams');

        params.add('action', 'find');
        var postbody = {
        };

        svc.sendGETRequest(svc.getURL(this.getCustomer(), me.getCompanyID(), me.getService(), ops, params), postbody, onResult, onFault);
    }
});
3
  • Have you had a go at Rhino? Commented Jun 17, 2013 at 22:47
  • In case it can help, here is a simple class that I wrote -- I can get handles to the two functions, but your case is a little more complex Commented Jun 17, 2013 at 22:52
  • How would I use Rhino? I saw stackoverflow.com/questions/15658569/… but AstRoot doesn't seem to be an actual class in Rhino (or perhaps I just have the wrong version?). Commented Jun 19, 2013 at 19:28

1 Answer 1

0

As you do not try to parse the complete javascript language, you might not need a full blown javascript engine. Have you had a look at ANTLR? As you can simplify your problem to a hand full of properties (if I understood you correctly) I'd think you should be able to write a more or less simple grammar for your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.