chiark
/
gitweb
/
~mdw
/
fwd
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Miscellaneous tidying up, to make this code independent of `fw'. It
[fwd]
/
scan.c
diff --git
a/scan.c
b/scan.c
index cd082f569489b015ff851bf62a6e47e40619fa89..0bc5cda5c3b94944792e2abe57e1e048c16bacbd 100644
(file)
--- a/
scan.c
+++ b/
scan.c
@@
-1,6
+1,6
@@
/* -*-c-*-
*
/* -*-c-*-
*
- * $Id: scan.c,v 1.
5 2002/01/30 09:29:34
mdw Exp $
+ * $Id: scan.c,v 1.
6 2002/02/22 23:44:16
mdw Exp $
*
* Character scanners
*
*
* Character scanners
*
@@
-29,6
+29,10
@@
/*----- Revision history --------------------------------------------------*
*
* $Log: scan.c,v $
/*----- Revision history --------------------------------------------------*
*
* $Log: scan.c,v $
+ * Revision 1.6 2002/02/22 23:44:16 mdw
+ * Miscellaneous tidying up, to make this code independent of `fw'. It
+ * might end up in a library somewhere.
+ *
* Revision 1.5 2002/01/30 09:29:34 mdw
* Initialize scanner properly.
*
* Revision 1.5 2002/01/30 09:29:34 mdw
* Initialize scanner properly.
*
@@
-49,8
+53,6
@@
/*----- Header files ------------------------------------------------------*/
/*----- Header files ------------------------------------------------------*/
-#include "config.h"
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@
-88,8
+90,7
@@
static void fscan_destroy(scansrc *ss)
fscan *fs = (fscan *)ss;
if (!(fs->f & SCF_NOCLOSE))
fclose(fs->fp);
fscan *fs = (fscan *)ss;
if (!(fs->f & SCF_NOCLOSE))
fclose(fs->fp);
- if (fs->f & SCF_FREENAME)
- free(fs->ss.src);
+ xfree(fs->ss.src);
DESTROY(fs);
}
DESTROY(fs);
}
@@
-100,7
+101,7
@@
static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy };
/* --- @scan_file@ --- *
*
* Arguments: @FILE *fp@ = pointer to file descriptor
/* --- @scan_file@ --- *
*
* Arguments: @FILE *fp@ = pointer to file descriptor
- * @char *name@ = pointer to source file name
+ * @c
onst c
har *name@ = pointer to source file name
* @unsigned f@ = flags
*
* Returns: A scanner source.
* @unsigned f@ = flags
*
* Returns: A scanner source.
@@
-108,11
+109,11
@@
static scansrc_ops fscan_ops = { fscan_scan, fscan_destroy };
* Use: Creates a new scanner source for reading from a file.
*/
* Use: Creates a new scanner source for reading from a file.
*/
-scansrc *scan_file(FILE *fp, char *name, unsigned f)
+scansrc *scan_file(FILE *fp, c
onst c
har *name, unsigned f)
{
fscan *fs = CREATE(fscan);
fs->ss.ops = &fscan_ops;
{
fscan *fs = CREATE(fscan);
fs->ss.ops = &fscan_ops;
- fs->ss.src =
name
;
+ fs->ss.src =
xstrdup(name)
;
fs->ss.line = 1;
fs->fp = fp;
fs->f = f;
fs->ss.line = 1;
fs->fp = fp;
fs->f = f;
@@
-200,7
+201,7
@@
static scansrc_ops eof_ops = { eof_scan, eof_destroy };
/* --- The end of file marker --- */
/* --- The end of file marker --- */
-static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0,
EOF
};
+static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0,
DSTR_INIT
};
/*----- General scanner handling ------------------------------------------*/
/*----- General scanner handling ------------------------------------------*/
@@
-216,10
+217,9
@@
static scansrc scan_eof = { &scan_eof, &eof_ops, "<eof>", 0, EOF };
int scan(scanner *sc)
{
int ch;
int scan(scanner *sc)
{
int ch;
- if (sc->head->pushback != EOF) {
- ch = sc->head->pushback;
- sc->head->pushback = EOF;
- } else {
+ if (sc->head->pushback.len)
+ ch = sc->head->pushback.buf[--sc->head->pushback.len];
+ else {
scansrc *ss = sc->head;
if (ss == &scan_eof)
ch = EOF;
scansrc *ss = sc->head;
if (ss == &scan_eof)
ch = EOF;
@@
-246,7
+246,7
@@
int scan(scanner *sc)
void unscan(scanner *sc, int ch)
{
void unscan(scanner *sc, int ch)
{
-
sc->head->pushback = ch
;
+
DPUTC(&sc->head->pushback, ch)
;
}
/* --- @scan_push@ --- *
}
/* --- @scan_push@ --- *
@@
-265,7
+265,7
@@
void scan_push(scanner *sc, scansrc *ss)
if (sc->head == &scan_eof)
sc->tail = &ss->next;
sc->head = ss;
if (sc->head == &scan_eof)
sc->tail = &ss->next;
sc->head = ss;
-
ss->pushback = EOF
;
+
dstr_create(&ss->pushback)
;
ss->tok = 0;
ss->t = 0;
}
ss->tok = 0;
ss->t = 0;
}
@@
-285,7
+285,7
@@
void scan_add(scanner *sc, scansrc *ss)
ss->next = &scan_eof;
*sc->tail = ss;
sc->tail = &ss->next;
ss->next = &scan_eof;
*sc->tail = ss;
sc->tail = &ss->next;
-
ss->pushback = EOF
;
+
dstr_create(&ss->pushback)
;
ss->tok = 0;
ss->t = 0;
}
ss->tok = 0;
ss->t = 0;
}
@@
-323,12
+323,13
@@
void scan_destroy(scanner *sc)
scansrc *sss = ss;
ss = ss->next;
if (sss->tok)
scansrc *sss = ss;
ss = ss->next;
if (sss->tok)
- free(sss->tok);
+ xfree(sss->tok);
+ dstr_destroy(&sss->pushback);
sss->ops->destroy(sss);
}
dstr_destroy(&sc->d);
if (scan_eof.tok)
sss->ops->destroy(sss);
}
dstr_destroy(&sc->d);
if (scan_eof.tok)
- free(scan_eof.tok);
+
x
free(scan_eof.tok);
scan_eof.tok = 0;
sc->head = &scan_eof;
sc->tail = &sc->head;
scan_eof.tok = 0;
sc->head = &scan_eof;
sc->tail = &sc->head;