-
Notifications
You must be signed in to change notification settings - Fork 108
/
mkhsboot.pl
72 lines (57 loc) · 1.81 KB
/
mkhsboot.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
#! /usr/bin/perl
#
# Copyright 2014, General Dynamics C4 Systems
#
# SPDX-License-Identifier: GPL-2.0-only
#
$literate = 0;
$literate = 1 if $ARGV[0] eq "-l";
$prefix = $literate ? "> " : "";
# read the entire input
$/ = undef;
$input = <STDIN>;
# find the module name
$input =~ /module\s+([A-Z][a-zA-Z0-9._]*)/m or die "no module";
$modname = $1;
# find the lists of hs-boot imports and exports
$input =~ /{-# BOOT-IMPORTS:((\s+#?[A-Z][a-zA-Z0-9._]+(\(.*?\))?(%[a-zA-Z]+)?)*) #-}/
or die "no imports";
$imports = $1;
$input =~ /{-# BOOT-EXPORTS:((\s+#?[a-zA-Z][a-zA-Z0-9_']+(\(.*?\))?)*) #-}/
or die "no exports";
$exports = $1;
# transform the import list to import statements
$imports =~ s/\s+/\n${prefix}import /g;
$imports =~ s/#/{-# SOURCE #-} /g;
$imports =~ s/%/ as /g;
# create the export list
$explist = $exports;
$explist =~ s/\s#\w+//g;
$explist =~ s/(\w|\))\s/\1, /g;
# print the module & import statements
print "${prefix}-- this file is automatically generated, do not edit\n";
print "${prefix}module $modname($explist) where\n${prefix}import Prelude hiding(Word)\n$imports\n\n";
$exports =~ s/#//g;
$exports =~ s/\(.*?\)//g;
# find and print all the type definitions...
while($exports =~ / ([A-Z][a-zA-Z0-9']*)/g) {
$name = $1;
if (not $literate and
$input =~ /^((type|newtype|data|class)\s+([A-Z][a-zA-Z0-9_' ]*=>\s+)?$name\W.*?)^\S/ms)
{
print "$1";
} elsif ($literate and
$input =~ /^(> (type|newtype|data|class)\s+([A-Z][a-zA-Z0-9_' ]*=>\s+)?$name\W.*?)^> \S/ms)
{
print "$1";
}
}
# ...and the type signatures
while($exports =~ / ([a-z][a-zA-Z0-9']*)/g) {
$name = $1;
if (not $literate and $input =~ /^($name\s+::.*?)^\S/ms) {
print "$1";
} elsif ($literate and $input =~ /^(> $name\s+::.*?)^> \S/ms) {
print "$1";
}
}