Posted 16 years ago by Vincent Parrett
Version: 4.0.0259
Avatar
Hi

I've been working on a VBScript syntax language for the last few months (not full time) and I'm about to embark on the same task for JavaScript. The VBScript implementation is not 100% complete, but I decided to get started on JavaScript now as they will share some common code.

I've been looking at the makeup of JavaScript and the only major issue I see at the moment (I'm sure I will find more later) is how to parse JavaScript classes. JavaScript classes are something of a hack, in that they are not declared as such :

function House(name,rooms,price,garage) {
this.name=name;
this.rooms=rooms;
this.price=price;
this.garage=garage;
}

house1 = new House('House 1',4,100000,false);

The thing that determines whether the method House is a constructor or not is the assignment to "this.xxxxx"

As to how to deal with this in the grammar/Ast, my first thought would be to walk the block statement (as it's assigned to the method declaration) and check for assignment statements and then check for this.xxx on the left side of the assignment.. if so then flag the method as a class constructor.

Oh, and it gets worse with class methods :

function view() {
with (this) document.write(name+' has '+rooms+' rooms, '+(garage?'a':'no')+' garage, and costs £'+price+'<BR>');
}

House.prototype.view = view;

The assignment of methods can happen outside of the object constructor, I can see that is going to be fun to parse and hook up for intellisense!

Anyone have any other suggestions?

Regards

Vincent.

Comments (1)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Perhaps just parse them all as normal methods but have some sort of flag on the AST class that you can set if you run into the "this" variables that mark it as class-related?

Has anyone else done an advanced Javascript implementation yet who might be able to chime in?


Actipro Software Support

The latest build of this product (v24.1.0) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.