Last active
August 29, 2015 14:02
-
-
Save kl0tl/629ee8728d0f067ecf08 to your computer and use it in GitHub Desktop.
`@Annotation` déclaration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Annotations can only occur immediately before a variable, a function declaration or an assignment | |
``` | |
@Component('position') | |
module.exports = function (x, y) { | |
return {x: x || 0, y: y || 0}; | |
} | |
@Component('position) | |
function position(x, y) { | |
return {x: x || 0, y: y || 0}; | |
} | |
``` | |
Parens are optionals if no argument is provided | |
``` | |
@Component | |
function position(x, y) { | |
return {x: x || 0, y: y || 0}; | |
} | |
``` | |
Multiples annotations per declarations are valid | |
``` | |
@Entity('sprite') | |
@RequireComponents('position', 'texture') | |
module.exports = function (entity) { | |
... | |
}; | |
```` | |
Annotations must be constructor functions | |
The following is an expanded version of the first example | |
``` | |
module.exports = function (x, y) { | |
return {x: x || 0, y: y || 0}; | |
}; | |
module.exports.annotations = [new Component('position')]; | |
``` | |
*/ | |
macro function_params { | |
rule { ($args:ident (,) ...) } | |
} | |
macro annotation { | |
rule { $name:ident ($args:expr (,) ...) } | |
rule { $name:ident } | |
} | |
macro @ { | |
rule { | |
$annotation:annotation (@) ... | |
function $name:ident $params:function_params { $body ... } | |
} => { | |
function $name $params { $body ... } | |
$name.annotations = [$(new $annotation) (,) ...]; | |
} | |
rule { | |
$annotation:annotation (@) ... | |
var $symbol:ident = $value:expr | |
} => { | |
var $symbol = $value | |
$symbol.annotations = [$(new $annotation) (,) ...]; | |
} | |
rule { | |
$annotation:annotation (@) ... | |
$symbol:ident = $value:expr | |
} => { | |
$symbol = $value | |
$symbol.annotations = [$(new $annotation) (,) ...]; | |
} | |
rule { | |
$annotation:annotation (@) ... | |
$path:ident (.) ... $symbol:ident = $value:expr | |
} => { | |
($path (.) ... . $symbol = $value).annotations = [ | |
$(new $annotation) (,) ... | |
]; | |
} | |
} | |
export @; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment