aboutsummaryrefslogtreecommitdiffstats
path: root/w3m-doc/w3mdoc.pl
blob: 60979263f25dd762d39f1eb81e26dc2e75b68039 (plain) (blame)
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
#!/usr/local/bin/perl

package w3mdoc;

sub CHECK {
  my($a, @b) = @_;
  for(@b) {
    defined($a->{$_}) || die("\"$a->{id}.$_\" is not defined.\n");
  }
}

sub DEF {
  my($a, $b, $c) = @_;

  if (! defined($data->{$a})) {
     $data->{$a} = bless { id => $a };
  } 
  $data->{$a}{$b} = $c;
}

sub SUB {
  local($_) = @_;
  my($a, $b);

  if (/^\@(\w+)\.(\w+)\@$/) {
    ($a, $b) = ($1, $2);
    defined($data->{$a}) || die("\"$a.$b\" is not defined.\n");
    $data->{$a}->CHECK($b);
    return $data->{$a}{$b};
  }
  if (/^\@(\w+)\((\w+)\)\@$/) {
    ($a, $b) = ($1, $2);
    defined(&{$a}) || die("\"$a()\" is not defined.\n");
    defined($data->{$b}) || die("\"$a($b)\" is not defined.\n");
    return $data->{$b}->$a();
  }
  return '@';
}

package main;

@ARGV || unshift(@ARGV, "-");
while(@ARGV) {
  $file = shift @ARGV;
  &make_doc($file);
}

sub make_doc {
  my($file) = @_;
  my($in_def, $in_code, $code, $a, $b);
  local(*F);
  local($_);

  open(F, $file) || die("$file: $!\n");
  $in_def = 0;
  $in_code = 0;
  while(<F>) {
    if ($in_def) {
      if (/^\@end/) {
        $in_def = 0;
        next;
      }
      s/^\s+//;
      s/^(\w+)\.(\w+)// || next;
      ($a, $b) = ($1, $2);
      s/^\s+//;
      s/\s+$//;
      &w3mdoc::DEF($a, $b, $_);
      next;
    }
    if ($in_code) {
      if (/^\@end/) {
        eval "package w3mdoc; $code";
        $in_code = 0;
        next;
      }
      $code .= $_;
      next;
    }
    if (/^\@define/) {
      $in_def = 1;
      next;
    }
    if (/^\@code/) {
      $in_code = 1;
      $code = "";
      next;
    }
    if (s/^\@include\s+//) {
      s/\s+$//;
      &make_doc($_);
      next;
    }
    if (/^\@/) {
      die("unknown command: $_");
    }
    s/(\\@|\@(\w+(\.\w+|\(\w+\)))\@)/&w3mdoc::SUB($1)/eg;
    print;
  }
  close(F);
}