/
syntax.pm
158 lines (108 loc) · 3.18 KB
/
syntax.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package mop::internals::syntax;
use v5.16;
use warnings;
use version ();
use Devel::CallParser ();
our $VERSION = '0.02';
our $AUTHORITY = 'cpan:STEVAN';
my @available_keywords = qw(class role method has);
# keep the local metaclass around
our $CURRENT_META;
sub setup_for {
my ($pkg) = @_;
$^H{__PACKAGE__ . '/twigils'} = 1;
mop::internals::util::install_sub($pkg, 'mop::internals::syntax', $_)
for @available_keywords;
}
sub teardown_for {
my ($pkg) = @_;
mop::internals::util::uninstall_sub($pkg, $_)
for @available_keywords;
}
sub new_meta {
my ($metaclass, $name, $version, $roles, $superclass) = @_;
$metaclass->new(
name => $name,
version => $version,
roles => [map {
mop::meta($_) or die "Could not find metaclass for role: $_"
} @{ $roles }],
(defined $superclass
? (superclass => $superclass)
: ()),
);
}
sub build_meta {
my ($meta, $body, @traits) = @_;
while (@traits) {
my ($trait, $args) = splice @traits, 0, 2;
mop::traits::util::apply_trait(
$trait, $meta, $args ? @$args : (),
);
}
$meta->FINALIZE;
$body->();
}
sub add_method {
my ($name, $body, @traits) = @_;
if ($body) {
$CURRENT_META->add_method(
$CURRENT_META->method_class->new(
name => $name,
body => mop::internals::util::subname(
(join '::' => $CURRENT_META->name, $name),
$body,
),
)
);
while (@traits) {
my ($trait, $args) = splice @traits, 0, 2;
mop::traits::util::apply_trait(
$trait, $CURRENT_META->get_method($name), $args ? @$args : (),
);
}
}
else {
$CURRENT_META->add_required_method($name);
}
return;
}
sub add_attribute {
my ($name, $default, @traits) = @_;
$CURRENT_META->add_attribute(
$CURRENT_META->attribute_class->new(
name => $name,
default => $default,
)
);
while (@traits) {
my ($trait, $args) = splice @traits, 0, 2;
mop::traits::util::apply_trait(
$trait, $CURRENT_META->get_attribute($name), $args ? @$args : (),
);
}
return;
}
1;
__END__
=pod
=head1 NAME
mop::internals::syntax - internal use only
=head1 DESCRIPTION
This is for internal use only, there is no public API here.
=head1 BUGS
Since this module is still under development we would prefer to not
use the RT bug queue and instead use the built in issue tracker on
L<Github|http://www.github.com>.
=head2 L<Git Repository|https://github.com/stevan/p5-mop-redux>
=head2 L<Issue Tracker|https://github.com/stevan/p5-mop-redux/issues>
=head1 AUTHOR
Stevan Little <stevan.little@iinteractive.com>
Jesse Luehrs <doy@tozt.net>
Florian Ragwitz <rafl@debian.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Infinity Interactive.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=for Pod::Coverage .+
=cut