-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFDGParser.pm
60 lines (47 loc) · 2.12 KB
/
CFDGParser.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
package CFDGParser;
use constant NO_MATCH => -1;
use Regexp::Grammars;
use Data::Dump 'dump';
$\ = "\n";
my $grammar = qr {
<nocontext:>startshape\s+<startshape=shape_name> <[rules=shape_definition]>* <[paths=path_definition]>*
<rule: shape_name>
\w+
<rule: shape_definition>
rule <shape_name> \{ <[calls=shape_call]>* \}
<rule: shape_call>
(<loop_cnt=num> \* \{ <[loop_transformations=transformation]>+ \})? <call_name=shape_name> \{ <[transformations=transformation]>* \}
<rule: transformation>
<cmd= (s|size) > <[values=signed_num]>{1,2} % <_sep=( )>
| <cmd= (x) > <[values=signed_num]>
| <cmd= (y) > <[values=signed_num]>
| <cmd= (r|rotate)> <[values=signed_num]>
| <cmd= (skew) > <[values=signed_num]>{1,2} % <_sep=( )>
| <cmd= (h|hue) > <[values=signed_num]>
| <cmd= (sat|saturation) > <[values=signed_num]>
| <cmd= (b|brightness) > <[values=signed_num]>
| <cmd= (a|alpha) > <[values=signed_num]>
<rule: signed_num>
-?\d+(\.\d+)?
<rule: num>
\d+(\.\d+)?
<rule: path_definition>
path <path_name=shape_name> \{ <[directives=path_directive]>* \}
<rule: path_directive>
<command=(MOVETO|MOVEREL)> \{ x <x=signed_num> y <y=signed_num> \}
| <command=(LINETO|LINEREL)> \{ x <x=signed_num> y <y=signed_num> \}
| <command=(ARCTO|ARCREL)> \{ x <x=signed_num> y <y=signed_num> x_radius <x_radius=num> y_radius <y_radius=num> ellipse_angle <ellipse_angle=num>\}
| <command=(ARCTO|ARCREL)> \{ x <x=signed_num> y <y=signed_num> (r|radius) <radius=num> \}
| <command=(CURVETO|CURVEREL)> \{ x <x=signed_num> y <y=signed_num> x1 <x1=signed_num> y1 <y1=signed_num> (x2 <x2=signed_num> y2 <y2=signed_num>)? \}
| <command=(CLOSEPOLY)> \{ \}
| <command=(STROKE)> \{ (w|width) <width=num> \}
}xms;
sub parse ($){
my $text = shift;
if($text =~ $grammar) {
return \%/;
} else {
return NO_MATCH;
}
}
1;