summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/stex/bin/old/idcheck.l
blob: 9b1603527d2d13d0c1355d792abf3057b52cc972 (plain)
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
%{
#include <string.h>
int ur,ok,a,lin,i,j,c,n,m,d,s,q,fe,f,for_ok,curr_lin;
typedef char idstr[50];
idstr l[5000], r[5000];
%}

ID [a-zA-Z0-9._:\|-]*
WS [\n\t ]*

%s env inenv fid foc

%% 

"\\begin"{WS}"{"("proof"|"example")"}"{WS}"[" {
fe=1;
for_ok=0;
curr_lin=lin;
BEGIN(env); // stuff that "for" keyval is recommended for
}

"\\begin"{WS}"{"("proof"|"example")"}" if (f) printf("Missing 'for' at line %d\n",lin);

("\\begin"{WS}"{"("definition"|"assertion"|"proof"|"omtext"|"example"|"step"|"pfcase"|"module")"}"{WS}"[")|("\\justification"{WS}"[") {
fe=0;
BEGIN(env);
}

<env>"]" {
if (fe && !for_ok && f) printf("Missing 'for' at line %d\n",curr_lin);
BEGIN(INITIAL);
}

<env>"id"{WS}"="{WS} BEGIN(fid);

<fid>{ID} {
for (i=0; i<yyleng; i++) l[n][i]=yytext[i];
l[n][yyleng]=0;
if (s) printf("%s\n",l[n]);
n++;
BEGIN(env);
}

<env>("for"){WS}"="{WS} {
for_ok=1;
BEGIN(foc);
}
<env>("continues"|"for"|"uses"|"premises"){WS}"="{WS} BEGIN(foc);

<foc>{ID} {
for (j=0; j<yyleng; j++) r[m][j]=yytext[j];
r[m][yyleng]=0;
m++;
BEGIN(env);
}

<env>("premises"|"uses"){WS}"="{WS}"{" BEGIN(inenv);

<inenv>{ID} {
strncpy(r[m],yytext,yyleng);
r[m][yyleng]=0;
m++;
}

<inenv>"}" BEGIN(env);

\n {c++;lin++;}
.  c++;

%%

main(argc, argv)
     int argc;
     char **argv;
{
  if (argc==1) {
    printf("Usage: idcheck [-q | -s | -f] filename(s)\n");
    printf("Option -s shows all detected IDs. \n");
    printf("Option -f shows line numbers for examples and proofs\n          that have 'for' keyval missing. \n");
    printf("Option -q for quiet mode.\n");
  } else{
    s=0; q=0;
    a=1;
    if (argv[1][0]=='-') {
      a++;
      for (j=1; j<strlen(argv[1]); j++)
        if (argv[1][j]=='s') s=1; else
        if (argv[1][j]=='q') q=1; else
        if (argv[1][j]=='f') f=1;
    }   
    d=0;
    while (a<argc){
      if (!q) printf("Parsing %s...\n",argv[a]);
      yyin = fopen(argv[a] , "r");
      c = 0;
      lin = 1;
      n = 0;
      m = 0;
      yylex();
      fclose(yyin);
      if (!q) printf("%d characters skipped. Found %d IDs. \n",c,n);
      if (!q) printf("Checking for duplicates...\n");
      for (i=0; i<n; i++)
        for (j=i+1; j<n; j++)
	  if (strcmp(l[i],l[j])==0) {
	    printf("ID=%s is a duplicate in %s\n",l[i],argv[a]);
            d++;
	  }
      if (!q) printf("Found %d duplicates.\n",d);
      if (!q) printf("Checking references... (found %d)\n",m);
      ur=0;
      for (i=0;i<m; i++){
        ok=0;
        for (j=0; j<n; j++)
          if (strcmp(r[i],l[j])==0){
            ok=1;
            break;
          }
        if (!ok) { 
           printf("Unmatched reference for ID=%s\n",r[i]);
           ur++;
        }
      }
      if (!q) printf("Found %d unmatched references.\n",ur);
      a++;
    }
    return d+ur;
  }
  return 1;
}