2010-12-27 20:56:36 -05:00
|
|
|
/**
|
|
|
|
* Property keyword parser module
|
|
|
|
*
|
2015-10-26 22:49:14 -04:00
|
|
|
* Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
|
Alias `constructor` member to `__construct`
This allows ease.js classes to mimic the structure of ES6 classes, which use
`constructor` to denote the constructor. This patch simply aliases it to
`__construct`, which ease.js handles as it would normally.
To that note, since the ES6 `class` keyword is purely syntatic sugar around
the prototype model, there is not much benefit to using it over ease.js if
benefits of ease.js are still desired, since the member definition syntax is
a feature of object literals:
```
// ease.js using ES6
let Person = Class(
{
_name: '',
// note that __construct still works as well
constructor( name ) {
this._name = ''+name;
},
sayHi() {
return "Hi, I'm " + this.getName();
},
// keywords still work as expected
'protected getName'() {
return this._name;
}
} );
// ES6 using `class` keyword
class Person
{
// note that ES6 will _not_ make this private
_name: '',
constructor( name ) {
this._name = ''+name;
},
sayHi() {
return "Hi, I'm " + this.getName();
}
// keywords unsupported (you'd have to use Symbols)
getName() {
return this._name;
}
}
// ES3/5 ease.js
var Person = Class(
{
_name: '',
__construct: function( name ) {
this._name = ''+name;
},
sayHi: function() {
return "Hi, I'm " + this._name;
},
'protected getName': function() {
return this._name;
}
} );
```
As you can see, the only change between writing ES6-style method definitions
is the syntax; all keywords and other features continue to work as expected.
2015-09-15 00:10:07 -04:00
|
|
|
* Free Software Foundation, Inc.
|
2010-12-27 20:56:36 -05:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2010-12-27 20:56:36 -05:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* ease.js is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2010-12-27 20:56:36 -05:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2010-12-27 20:56:36 -05:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-12-27 20:56:36 -05:00
|
|
|
*/
|
|
|
|
|
2011-05-22 22:11:57 -04:00
|
|
|
/**
|
|
|
|
* Known (permitted) keywords
|
|
|
|
* @type {Object.<string,boolean>}
|
|
|
|
*/
|
|
|
|
var _keywords = {
|
2014-04-20 02:28:38 -04:00
|
|
|
'public': 1,
|
|
|
|
'protected': 1<<1,
|
|
|
|
'private': 1<<2,
|
|
|
|
'static': 1<<3,
|
|
|
|
'abstract': 1<<4,
|
|
|
|
'const': 1<<5,
|
|
|
|
'virtual': 1<<6,
|
|
|
|
'override': 1<<7,
|
|
|
|
'proxy': 1<<8,
|
|
|
|
'weak': 1<<9,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keyword masks for conveniently checking the keyword bitfield
|
|
|
|
* @type {Object.<string,integer>}
|
|
|
|
*/
|
|
|
|
var _kmasks = {
|
|
|
|
amods: _keywords[ 'public' ]
|
|
|
|
| _keywords[ 'protected' ]
|
|
|
|
| _keywords[ 'private' ],
|
2014-04-20 02:31:28 -04:00
|
|
|
|
|
|
|
'virtual': _keywords[ 'abstract' ]
|
|
|
|
| _keywords[ 'virtual' ],
|
2011-05-22 22:11:57 -04:00
|
|
|
};
|
|
|
|
|
2010-12-27 20:56:36 -05:00
|
|
|
|
2014-04-20 02:40:36 -04:00
|
|
|
// expose magic values
|
|
|
|
exports.kvals = _keywords;
|
|
|
|
exports.kmasks = _kmasks;
|
|
|
|
|
|
|
|
|
2010-12-27 20:56:36 -05:00
|
|
|
/**
|
|
|
|
* Parses property keywords
|
|
|
|
*
|
|
|
|
* @param {string} prop property string, which may contain keywords
|
|
|
|
*
|
Alias `constructor` member to `__construct`
This allows ease.js classes to mimic the structure of ES6 classes, which use
`constructor` to denote the constructor. This patch simply aliases it to
`__construct`, which ease.js handles as it would normally.
To that note, since the ES6 `class` keyword is purely syntatic sugar around
the prototype model, there is not much benefit to using it over ease.js if
benefits of ease.js are still desired, since the member definition syntax is
a feature of object literals:
```
// ease.js using ES6
let Person = Class(
{
_name: '',
// note that __construct still works as well
constructor( name ) {
this._name = ''+name;
},
sayHi() {
return "Hi, I'm " + this.getName();
},
// keywords still work as expected
'protected getName'() {
return this._name;
}
} );
// ES6 using `class` keyword
class Person
{
// note that ES6 will _not_ make this private
_name: '',
constructor( name ) {
this._name = ''+name;
},
sayHi() {
return "Hi, I'm " + this.getName();
}
// keywords unsupported (you'd have to use Symbols)
getName() {
return this._name;
}
}
// ES3/5 ease.js
var Person = Class(
{
_name: '',
__construct: function( name ) {
this._name = ''+name;
},
sayHi: function() {
return "Hi, I'm " + this._name;
},
'protected getName': function() {
return this._name;
}
} );
```
As you can see, the only change between writing ES6-style method definitions
is the syntax; all keywords and other features continue to work as expected.
2015-09-15 00:10:07 -04:00
|
|
|
* @return {{name: string, bitwords: number, keywords: Object.<string, boolean>}}
|
2010-12-27 20:56:36 -05:00
|
|
|
*/
|
2010-12-27 23:12:37 -05:00
|
|
|
exports.parseKeywords = function ( prop )
|
2010-12-27 20:56:36 -05:00
|
|
|
{
|
|
|
|
var name = prop,
|
|
|
|
keywords = [],
|
2014-04-20 02:28:38 -04:00
|
|
|
bitwords = 0x00,
|
2010-12-27 20:56:36 -05:00
|
|
|
keyword_obj = {};
|
|
|
|
|
|
|
|
prop = ''+( prop );
|
|
|
|
|
2014-01-19 11:48:37 -05:00
|
|
|
// the keywords are all words, except for the last, which is the
|
|
|
|
// property name
|
|
|
|
if ( ( keywords = prop.split( /\s+/ ) ).length !== 1 )
|
2010-12-27 20:56:36 -05:00
|
|
|
{
|
2014-01-19 11:48:37 -05:00
|
|
|
name = keywords.pop();
|
2010-12-27 20:56:36 -05:00
|
|
|
|
2014-04-20 02:28:38 -04:00
|
|
|
var i = keywords.length;
|
2010-12-27 23:04:50 -05:00
|
|
|
while ( i-- )
|
|
|
|
{
|
2014-04-20 02:28:38 -04:00
|
|
|
var keyword = keywords[ i ],
|
|
|
|
kval = _keywords[ keyword ];
|
2011-05-22 22:11:57 -04:00
|
|
|
|
|
|
|
// ensure the keyword is recognized
|
2014-04-20 02:28:38 -04:00
|
|
|
if ( !kval )
|
2011-05-22 22:11:57 -04:00
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Unexpected keyword for '" + name + "': " + keyword
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-04-20 02:28:38 -04:00
|
|
|
// ease-of-access
|
2011-05-22 22:11:57 -04:00
|
|
|
keyword_obj[ keyword ] = true;
|
2014-04-20 02:28:38 -04:00
|
|
|
|
|
|
|
// permits quick and concise checks
|
|
|
|
bitwords |= kval;
|
2010-12-27 20:56:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 02:28:38 -04:00
|
|
|
// members with an underscore prefix are implicitly private, unless an
|
|
|
|
// access modifier is explicitly provided; double-underscore is ingored,
|
|
|
|
// as they denote special members that do not become part of the
|
|
|
|
// prototype and are reserved by ease.js
|
|
|
|
if ( ( name.match( /^_[^_]/ ) && !( bitwords & _kmasks.amods ) ) )
|
|
|
|
{
|
|
|
|
keyword_obj[ 'private' ] = true;
|
|
|
|
bitwords |= _keywords[ 'private' ];
|
|
|
|
}
|
|
|
|
|
2010-12-27 20:56:36 -05:00
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
keywords: keyword_obj,
|
2014-04-20 02:28:38 -04:00
|
|
|
bitwords: bitwords,
|
2010-12-27 20:56:36 -05:00
|
|
|
};
|
|
|
|
}
|