home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume8
/
mgt
/
part01
/
parse.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-02-23
|
5KB
|
265 lines
/*
"mgt" Copyright 1990 Shodan
All Rights Reserved.
Program by Greg Hale
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that this entire comment and copyright notice appear in all
copies and that both that copyright notice and this permission notice
appear in supporting documentation. No representations are made about
the suitability of this software for any purpose. It is provided "as
is" without express or implied warranty.
Please send copies of extensions to:
hale@scam.berkeley.edu
128.32.138.4 scam.berkeley.edu sting sting.Berkeley.EDU
Donations for the 'From My Go Teacher' series may be sent to:
Shodan
P.O. Box 4456
Berkeley, CA 94704
(415) 849-9475
*/
#include <strings.h>
#include "mgt.h"
#include "var.h"
#include "file.h"
static char *tokenStr[] = {
"USer", "ANalysis", "CoPyright", "SOurce",
"TiMe", "KoMi", "PlayBlack", "PlayWhite",
"PlaCe", "DaTe", "EVent", "GameName",
"FileFormat", "VieW", "GaMe",
"BlackSpec","WhiteSpec",
"White", "Black", "\(", "\)", "\;", "Comment[",
"AddWhite", "AddBlack",
"Letter",
"\[", "AddEmpty","Name[",
"Size[", "[","]"
};
static token tokenVal[] = {
t_WS, t_WS, t_WS, t_WS,
t_WS, t_WS, t_WS, t_WS,
t_WS, t_WS, t_WS, t_WS,
t_WS, t_WS, t_WS,
t_BlackSpec, t_WhiteSpec,
t_White, t_Black, t_Open, t_Close, t_NewNode, t_Comment,
t_AddWhite, t_AddBlack,
t_Letter,
t_LParen, t_AddEmpty, t_Name,
t_Size, t_LBracket, t_RBracket
};
char buf[1024],*curin;
FUNCTION void readInit()
{
buf[0]='\0';
curin = &buf[0];
}
FUNCTION char readChar()
{
if (!*curin) {
curin = &buf[0];
buf[0] = '\0';
fgets(&buf[0],1023,input);
return 0;
}
return *(curin++);
}
FUNCTION void readAdvance(i)
int i;
{
while (i-->0)
readChar();
}
FUNCTION void getCoordStr(co)
coord *co;
{
co->x = lettertox(curin[1]);
co->y = curin[2] -'a';
readAdvance(4);
}
FUNCTION boolean isCoordStr()
{
return curin[0] =='[' && curin[1] >='a' && curin[1] <= 's'
&& curin[2] >= 'a' && curin[2] <= 's' && curin[3] == ']';
}
FUNCTION void getCoordList(clp)
coordListP clp;
{
coord co;
while (isCoordStr()) {
getCoordStr(&co);
setCoord(co.x,co.y,clp);
}
}
FUNCTION token tokenize()
{
int i;
token t;
if (feof(input))
return t_EOF;
for (i = 0; i < sizeof(tokenStr)/sizeof(tokenStr[0]); i++) {
if (!strncmp(curin, tokenStr[i], strlen(tokenStr[i]))) {
readAdvance(strlen(tokenStr[i]));
return tokenVal[i];
}
}
readAdvance(1);
return t_WS;
}
/*************************************************/
FUNCTION void addMoveArrayProp(t,n)
token t;
nodep n;
{
coordListP cl;
property *p;
p = (property *)calloc(1,sizeof(property));
cl = (coordListP)calloc(1,sizeof(coordList));
if (!p || !cl)
barf("Memory allocation failure (addMoveArrayProp)");
p->t = t;
p->d = (data *)cl;
getCoordList(cl);
addprop(n,p);
}
FUNCTION void doSize()
{
int size;
char *s,c,b[25];
s = &b[0];
while ((c = readChar()) != ']')
if (c && c!='\\')
*s++ = c;
*s = 0;
size = atoi(b);
if ((size > 0) && (size < 19))
boardsize = size;
}
doComment(n,t)
nodep n;
token t;
{
char c,*s;
char buffer[(2<<10) + 1];
property *p;
p = (property *)calloc(1,sizeof(property));
if (!p)
barf("Memory Allocation Failure (doComment)");
s = &buffer[0];
while ((c = readChar()) != ']')
if (c && c!='\\')
*s++ = c;
*s = 0;
p->t = t;
p->d = (data *) dupStr(&buffer[0]);
addprop(n,p);
}
FUNCTION void addChild(n,c) /* add child c to node n */
nodep n,c;
{
if (n) {
if (n->child) {
nodep s;
s = treeLastSibling(child(n,false),false);
s->nextSibling = c;
c->lastSibling = s;
c->parent = n;
} else {
n->child = c;
c->parent = n;
}
}
}
FUNCTION nodep parse()
{
nodep r,n,new,temp;
token t;
r = n = 0;
for(;;) {
t = tokenize();
switch (t) {
case t_LBracket:
do {
t = tokenize();
} while (t != t_RBracket && t != t_EOF);
break;
case t_Size:
doSize();
break;
case t_AddWhite:
case t_AddBlack:
case t_AddEmpty:
case t_White:
case t_Black:
case t_Letter:
if (n) addMoveArrayProp(t,n);
else fprintf(stderr,"Error - property w/o node in data\n");
break;
case t_Name:
case t_Comment:
if (n) doComment(n,t);
else fprintf(stderr,"Error - property w/o node in data\n");
break;
case t_NewNode:
new = newNode();
if (!r)
r = new;
else
addChild(n,new);
n = new;
break;
case t_Open:
new = parse();
addChild(n,new);
if (!r)
r = new;
break;
case t_Close:
case t_EOF:
return r;
default:
break;
}
}
}