summaryrefslogtreecommitdiff
path: root/support/schemetex/schemetex.l
blob: f00087c969dee8bd623865750d73c0508992c166 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
%{
/* schemeTeX -- Scheme to TeX.  John D. Ramsdell.
 * Simple support for literate programming in Scheme.
 * Usage:   schemeTeX < {Scheme TeX file} > {TeX file}
 */

#if !defined lint
static char ID[] = "@(#)schemeTeX.l	1.3	88/06/30";
static char copyright[] = "Copyright 1988 by The MITRE Corporation."; 
/* Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies.  The
MITRE Corporation makes no representations about the suitability of
this software for any purpose.  It is provided "as is" without express
or implied warranty. */
#endif

/* SchemeTeX defines a new source file format in which source lines
are divided into text and code.  Lines of code start with a line
beginning with '(', and continue until the line that contains the
matching ')'.  The text lines remain, and they are treated as
comments.  If the first character of a text line is ';', it is
stripped from the output.  This is provided for those who want to use
an unmodified version of their Scheme system's LOAD.  When producing a
document, both the text lines and the code lines are copied into the
document source file, but the code lines are surrounded by a pair of
formatting commands, as is comments beginning with ';' within code
lines.  SchemeTeX is currently set up for use with LaTeX. */

/* Define STRIP if you want to remove all comments in a Scheme TeX
file. */

/* Modify the following for use with something other than LaTeX. 
Also see tex_verbatim_echo.  */
#define BEGIN_COMMENT	"\\notastyped{"
#define BEGIN_CODE	"\\begin{astyped}"
#define END_CODE	"\\end{astyped}"
#define TEX_ECHO	tex_verbatim_echo(yytext, stdout)
/* Lex is used for identifying code in an Scheme TeX file. */
int parens;			/* Used to balance parenthesis. */

/* All input occurs in the following routines so that TAB characters
can be expanded. TeX treats TAB characters as a space--not what is
wanted. */
#undef getc()
#define getc(STREAM) expanding_getc(STREAM)
int spaces = 0;			/* Spaces left to print a TAB. */
int column = 0;			/* Current input column. */
int expanding_getc(stream)
  FILE *stream;
{
  int c;
  if (spaces > 0) {
    spaces--;
    return ' ';
  }
  switch (c = fgetc(stream)) {
  case '\t':
    spaces = 8 - (7&column);
    column += spaces;
    return expanding_getc(stream);
  case '\n':
    column = 0;
    return c;
  default:
    column++;
    return c;
  }
}
%}
%%
#\\\(		{
#if defined STRIP		   
		  ECHO;
#else
		  TEX_ECHO;
#endif
		}
#\\\)		{
#if defined STRIP		   
		  ECHO;
#else
		  TEX_ECHO;
#endif
		}
\(		{ ECHO; parens++; }
\)		{ ECHO; parens--;
		  if (parens == 0) { /* End of code. */
		    char c;	/* Check that nothing follows. */
		    while ((c = input()) == ' ') output(c);
		    if (c == '\000') return 0; /* EOF */
		    if (c != '\n' && c != ';') return -1;
		    unput(c);
		  }
		}
\"[^"]*\"	{
		  if ((yyleng > 1) && (yytext[yyleng-2] == '\\'))
		    yymore();
		  else 
#if defined STRIP
		    ECHO;
#else
		    TEX_ECHO;
#endif
		}
;[^\n]*$	{
#if defined STRIP
                  ;
#else
                  fputs(BEGIN_COMMENT, stdout);
                  ECHO;
                  fputs("}", stdout);
#endif
		}
\n		{ ECHO; if (parens <= 0) return 0; }
.		{
#if defined STRIP		   
		  ECHO;
#else
		  TEX_ECHO;
#endif
		}
%%

fatal (s)
      char *s;
{
  fprintf(stderr, "On line %d, %s\n", yylineno, s);
  exit(1);
}

tex_verbatim_echo (s, f)
      char *s; FILE *f;
{
  for (; *s != '\000'; s++) 
    switch (*s) {
    case '\\': 
    case  '{': 
    case  '}': 
    case  '$': 
    case  '&': 
    case  '#': 
    case  '^': 
    case  '_': 
    case  '%': 
    case  '~': 
      fputs("\\verb-", f);
      putc(*s, f);
      putc('-', f);
      break;
    default: putc(*s, f);
    }
}

main()
{
  char c;
  do {				/* TeX mode and saw newline */
    c = input();
    if (c == '(') {		/* TeX mode changed to code mode. */
      unput(c);
#if !defined STRIP
      fputs(BEGIN_CODE,stdout); putc('\n', stdout);
#endif
      do {			/* Copy out code using yylex. */
	parens = 0;
	if (0 != yylex()) fatal("Bad code section.");
	if (parens != 0) fatal("Premature EOF.");
	c = input();
	unput(c);		/* Repeat when there is code */
      } while (c == '(');	/* immediately after copied code. */
#if !defined STRIP
      fputs(END_CODE, stdout); putc('\n', stdout);
#endif
    }
    else {			/* Found a text line. */
      if (c == ';') c = input(); /* For those who want to use bare load. */
      while (c != '\n') {
	if (c == '\000') exit(0);	/* EOF. */
#if !defined STRIP
	output(c);
#endif
	c = input();
      }
#if !defined STRIP
      output(c);
#endif
    }
  } while (1);
}