forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_enum_to_cases.pl
79 lines (72 loc) · 2.16 KB
/
error_enum_to_cases.pl
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
#!/usr/bin/perl -w
use strict;
use English;
sub translateAvailability {
my $version = shift;
$version =~ s/^\s+|\s+$//g;
if ($version eq "NA") { return "unavailable"; }
$version =~ /([0-9]+)_([0-9]+)/;
return "introduced=$1.$2";
}
my $prefixLength = 2;
my %minimumValues = ();
my %maximumValues = ();
my %rangeAvailability = ();
my $prev_had_availability = 0;
foreach my $line (<STDIN>) {
chomp $line;
if ($line =~ /([A-Za-z_][A-Za-z_0-9]+).*=[^0-9A-Za-z_-]*([A-Za-z0-9_-]+)/) {
my $fullname = $1;
my $value = $2;
my $has_availability = 0;
my $availability = "";
# if ($line =~ /AVAILABLE\s*[(](([0-9]+_[0-9]+)|(NA))[ ]*,[ ]*(([0-9]+_[0-9]+)|(NA))[)]/) {
if ($line =~ /AVAILABLE[ ]*[(]([^),]*),([^)]*)[)]/) {
$has_availability = 1;
my $osx = $1;
my $ios = $2;
$osx = translateAvailability($osx);
$ios = translateAvailability($ios);
$availability = " \@available(OSX, $osx) \@available(iOS, $ios)\n";
}
# If the full name ends in "Minimum" or "Maximum", it's for a range.
my $rangeName = "";
if ($fullname =~ /(Minimum|Maximum)$/) {
$rangeName = substr $PREMATCH, $prefixLength;
if ($MATCH eq "Minimum") {
$minimumValues{$rangeName} = $value;
} else {
$maximumValues{$rangeName} = $value;
}
$rangeAvailability{$rangeName} = $availability;
} else {
if ($availability ne "") {
if ($prev_had_availability == 0) {
print("\n");
}
print("$availability");
}
my $casename = substr $fullname, $prefixLength;
print(" case $casename = $value\n");
if ($availability ne "") {
print("\n");
$prev_had_availability = 1;
} else {
$prev_had_availability = 0;
}
}
}
}
# Print properties for the ranges.
foreach my $key (sort keys(%minimumValues)) {
my $minimum = $minimumValues{$key};
my $maximum = $maximumValues{$key};
my $availability = $rangeAvailability{$key};
print "\n";
if ($availability ne "") {
print $availability;
}
print(" public var is$key: Bool {\n");
print(" return rawValue >= $minimum && rawValue <= $maximum;\n");
print(" }\n");
}