Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

lex.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"

Include dependency graph for lex.c:

Include dependency graph

Go to the source code of this file.

Data Structures

struct  fsm

Defines

#define ACT(tok, act)   ((tok<<7)+act)
#define C_ALPH   2
#define C_EOF   4
#define C_NUM   3
#define C_WS   1
#define C_XX   5
#define GETACT(st)   (st>>7)&0x1ff
#define MAXSTATE   32
#define QBSBIT   0100

Enumerations

enum  state {
  START = 0, NUM1, NUM2, NUM3,
  ID1, ST1, ST2, ST3,
  COM1, COM2, COM3, COM4,
  CC1, CC2, WS1, PLUS1,
  MINUS1, STAR1, SLASH1, PCT1,
  SHARP1, CIRC1, GT1, GT2,
  LT1, LT2, OR1, AND1,
  ASG1, NOT1, DOTS1, S_SELF = MAXSTATE,
  S_SELFB, S_EOF, S_NL, S_EOFSTR,
  S_STNL, S_COMNL, S_EOFCOM, S_COMMENT,
  S_EOB, S_WS, S_NAME
}

Functions

void expandlex (void)
int fillbuf (Source *s)
void fixlex (void)
int foldline (Source *s)
int gettokens (Tokenrow *trp, int reset)
Sourcesetsource (char *name, int fd, char *str)
int trigraph (Source *s)
void unsetsource (void)

Variables

short bigfsm [256][MAXSTATE]
fsm fsm []
int tokkind [256]
int tottok


Define Documentation

#define ACT tok,
act   )     ((tok<<7)+act)
 

Definition at line 26 of file lex.c.

#define C_ALPH   2
 

Definition at line 32 of file lex.c.

Referenced by expandlex().

#define C_EOF   4
 

Definition at line 34 of file lex.c.

#define C_NUM   3
 

Definition at line 33 of file lex.c.

Referenced by expandlex().

#define C_WS   1
 

Definition at line 31 of file lex.c.

#define C_XX   5
 

Definition at line 35 of file lex.c.

Referenced by expandlex().

#define GETACT st   )     (st>>7)&0x1ff
 

Definition at line 28 of file lex.c.

Referenced by gettokens().

#define MAXSTATE   32
 

Definition at line 25 of file lex.c.

#define QBSBIT   0100
 

Definition at line 27 of file lex.c.


Enumeration Type Documentation

enum state
 

Enumeration values:
START 
NUM1 
NUM2 
NUM3 
ID1 
ST1 
ST2 
ST3 
COM1 
COM2 
COM3 
COM4 
CC1 
CC2 
WS1 
PLUS1 
MINUS1 
STAR1 
SLASH1 
PCT1 
SHARP1 
CIRC1 
GT1 
GT2 
LT1 
LT2 
OR1 
AND1 
ASG1 
NOT1 
DOTS1 
S_SELF 
S_SELFB 
S_EOF 
S_NL 
S_EOFSTR 
S_STNL 
S_COMNL 
S_EOFCOM 
S_COMMENT 
S_EOB 
S_WS 
S_NAME 

Definition at line 37 of file lex.c.

Referenced by AAS_UpdateEntity(), BotAI_GetClientState(), BotAI_GetEntityState(), BotAI_GetSnapshotEntity(), BotAIStartFrame(), BotCheckEvents(), BotCheckForGrenades(), BotCheckSnapshot(), BotClearPath(), BotTravel_Grapple(), CG_AddRefEntityWithPowerups(), CG_SetInitialSnapshot(), CL_DeltaEntity(), CL_GetParseEntityState(), decode_mcu(), dump_buffer(), emit_bits(), emit_restart(), encode_mcu_huff(), encode_one_block(), Export_BotLibUpdateEntity(), finish_pass_huff(), flush_bits(), GetClientState(), gettokens(), IN_DIMouse(), jpeg_fill_bit_buffer(), jpeg_huff_decode(), MD4Transform(), RB_BeginSurface(), S_AdpcmDecode(), S_AdpcmEncode(), S_AdpcmEncodeSound(), S_AdpcmGetSamples(), SV_BuildClientSnapshot(), trap_BotAllocGoalState(), trap_GetClientState(), and trap_Key_SetOverstrikeMode().

00037            {
00038     START=0, NUM1, NUM2, NUM3, ID1, ST1, ST2, ST3, COM1, COM2, COM3, COM4,
00039     CC1, CC2, WS1, PLUS1, MINUS1, STAR1, SLASH1, PCT1, SHARP1,
00040     CIRC1, GT1, GT2, LT1, LT2, OR1, AND1, ASG1, NOT1, DOTS1,
00041     S_SELF=MAXSTATE, S_SELFB, S_EOF, S_NL, S_EOFSTR,
00042     S_STNL, S_COMNL, S_EOFCOM, S_COMMENT, S_EOB, S_WS, S_NAME
00043 };


Function Documentation

void expandlex void   ) 
 

Definition at line 239 of file lex.c.

References bigfsm, C_ALPH, C_NUM, C_XX, fp, fsm, i, and j.

Referenced by main().

00240 {
00241     /*const*/ struct fsm *fp;
00242     int i, j, nstate;
00243 
00244     for (fp = fsm; fp->state>=0; fp++) {
00245         for (i=0; fp->ch[i]; i++) {
00246             nstate = fp->nextstate;
00247             if (nstate >= S_SELF)
00248                 nstate = ~nstate;
00249             switch (fp->ch[i]) {
00250 
00251             case C_XX:      /* random characters */
00252                 for (j=0; j<256; j++)
00253                     bigfsm[j][fp->state] = nstate;
00254                 continue;
00255             case C_ALPH:
00256                 for (j=0; j<=256; j++)
00257                     if ('a'<=j&&j<='z' || 'A'<=j&&j<='Z'
00258                       || j=='_')
00259                         bigfsm[j][fp->state] = nstate;
00260                 continue;
00261             case C_NUM:
00262                 for (j='0'; j<='9'; j++)
00263                     bigfsm[j][fp->state] = nstate;
00264                 continue;
00265             default:
00266                 bigfsm[fp->ch[i]][fp->state] = nstate;
00267             }
00268         }
00269     }
00270     /* install special cases for ? (trigraphs),  \ (splicing), runes, and EOB */
00271     for (i=0; i<MAXSTATE; i++) {
00272         for (j=0; j<0xFF; j++)
00273             if (j=='?' || j=='\\') {
00274                 if (bigfsm[j][i]>0)
00275                     bigfsm[j][i] = ~bigfsm[j][i];
00276                 bigfsm[j][i] &= ~QBSBIT;
00277             }
00278         bigfsm[EOB][i] = ~S_EOB;
00279         if (bigfsm[EOFC][i]>=0)
00280             bigfsm[EOFC][i] = ~S_EOF;
00281     }
00282 }

int fillbuf Source s  ) 
 

Definition at line 516 of file lex.c.

References error(), FATAL, source::fd, source::inb, source::inl, source::inp, INS, n, read(), s, and Source.

00517 {
00518     int n, nr;
00519 
00520     nr = INS/8;
00521     if ((char *)s->inl+nr > (char *)s->inb+INS)
00522         error(FATAL, "Input buffer overflow");
00523     if (s->fd<0 || (n=read(s->fd, (char *)s->inl, INS/8)) <= 0)
00524         n = 0;
00525     if ((*s->inp&0xff) == EOB) /* sentinel character appears in input */
00526         *s->inp = EOFC;
00527     s->inl += n;
00528     s->inl[0] = s->inl[1]= s->inl[2]= s->inl[3] = EOB;
00529     if (n==0) {
00530         s->inl[0] = s->inl[1]= s->inl[2]= s->inl[3] = EOFC;
00531         return EOF;
00532     }
00533     return 0;
00534 }

Here is the call graph for this function:

void fixlex void   ) 
 

Definition at line 285 of file lex.c.

References bigfsm, and Cplusplus.

Referenced by main().

00286 {
00287     /* do C++ comments? */
00288     if (Cplusplus==0)
00289         bigfsm['/'][COM1] = bigfsm['x'][COM1];
00290 }

int foldline Source s  ) 
 

Definition at line 503 of file lex.c.

References fillbuf(), source::inl, source::inp, memmove(), s, and Source.

Referenced by gettokens().

00504 {
00505     while (s->inp+1 >= s->inl && fillbuf(s)!=EOF)
00506         ;
00507     if (s->inp[1] == '\n') {
00508         memmove(s->inp, s->inp+2, s->inl-s->inp+3);
00509         s->inl -= 2;
00510         return 1;
00511     }
00512     return 0;
00513 }

Here is the call graph for this function:

int gettokens Tokenrow trp,
int  reset
 

Definition at line 300 of file lex.c.

References bigfsm, tokenrow::bp, c, cursource, ERROR, error(), FATAL, source::fd, fillbuf(), token::flag, foldline(), GETACT, growtokenrow(), token::hideset, source::inb, source::inl, source::inp, INS, token::len, source::lineinc, tokenrow::lp, tokenrow::max, memmove(), NL, outbuf, quicklook, s, S_COMMENT, S_COMNL, S_EOB, S_EOF, S_EOFCOM, S_EOFSTR, S_NAME, S_NL, S_SELF, S_SELFB, S_STNL, S_WS, Source, state, token::t, Token, Tokenrow, trigraph(), token::type, uchar, WARNING, and token::wslen.

Referenced by doconcat(), gatherargs(), process(), and setup().

00301 {
00302     register int c, state, oldstate;
00303     register uchar *ip;
00304     register Token *tp, *maxp;
00305     int runelen;
00306     Source *s = cursource;
00307     int nmac = 0;
00308     extern char outbuf[];
00309 
00310     tp = trp->lp;
00311     ip = s->inp;
00312     if (reset) {
00313         s->lineinc = 0;
00314         if (ip>=s->inl) {       /* nothing in buffer */
00315             s->inl = s->inb;
00316             fillbuf(s);
00317             ip = s->inp = s->inb;
00318         } else if (ip >= s->inb+(3*INS/4)) {
00319             memmove(s->inb, ip, 4+s->inl-ip);
00320             s->inl = s->inb+(s->inl-ip);
00321             ip = s->inp = s->inb;
00322         }
00323     }
00324     maxp = &trp->bp[trp->max];
00325     runelen = 1;
00326     for (;;) {
00327        continue2:
00328         if (tp>=maxp) {
00329             trp->lp = tp;
00330             tp = growtokenrow(trp);
00331             maxp = &trp->bp[trp->max];
00332         }
00333         tp->type = UNCLASS;
00334         tp->hideset = 0;
00335         tp->t = ip;
00336         tp->wslen = 0;
00337         tp->flag = 0;
00338         state = START;
00339         for (;;) {
00340             oldstate = state;
00341             c = *ip;
00342             if ((state = bigfsm[c][state]) >= 0) {
00343                 ip += runelen;
00344                 runelen = 1;
00345                 continue;
00346             }
00347             state = ~state;
00348         reswitch:
00349             switch (state&0177) {
00350             case S_SELF:
00351                 ip += runelen;
00352                 runelen = 1;
00353             case S_SELFB:
00354                 tp->type = GETACT(state);
00355                 tp->len = ip - tp->t;
00356                 tp++;
00357                 goto continue2;
00358 
00359             case S_NAME:    /* like S_SELFB but with nmac check */
00360                 tp->type = NAME;
00361                 tp->len = ip - tp->t;
00362                 nmac |= quicklook(tp->t[0], tp->len>1?tp->t[1]:0);
00363                 tp++;
00364                 goto continue2;
00365 
00366             case S_WS:
00367                 tp->wslen = ip - tp->t;
00368                 tp->t = ip;
00369                 state = START;
00370                 continue;
00371 
00372             default:
00373                 if ((state&QBSBIT)==0) {
00374                     ip += runelen;
00375                     runelen = 1;
00376                     continue;
00377                 }
00378                 state &= ~QBSBIT;
00379                 s->inp = ip;
00380                 if (c=='?') {   /* check trigraph */
00381                     if (trigraph(s)) {
00382                         state = oldstate;
00383                         continue;
00384                     }
00385                     goto reswitch;
00386                 }
00387                 if (c=='\\') { /* line-folding */
00388                     if (foldline(s)) {
00389                         s->lineinc++;
00390                         state = oldstate;
00391                         continue;
00392                     }
00393                     goto reswitch;
00394                 }
00395                 error(WARNING, "Lexical botch in cpp");
00396                 ip += runelen;
00397                 runelen = 1;
00398                 continue;
00399 
00400             case S_EOB:
00401                 s->inp = ip;
00402                 fillbuf(cursource);
00403                 state = oldstate;
00404                 continue;
00405 
00406             case S_EOF:
00407                 tp->type = END;
00408                 tp->len = 0;
00409                 s->inp = ip;
00410                 if (tp!=trp->bp && (tp-1)->type!=NL && cursource->fd!=-1)
00411                     error(WARNING,"No newline at end of file");
00412                 trp->lp = tp+1;
00413                 return nmac;
00414 
00415             case S_STNL:
00416                 error(ERROR, "Unterminated string or char const");
00417             case S_NL:
00418                 tp->t = ip;
00419                 tp->type = NL;
00420                 tp->len = 1;
00421                 tp->wslen = 0;
00422                 s->lineinc++;
00423                 s->inp = ip+1;
00424                 trp->lp = tp+1;
00425                 return nmac;
00426 
00427             case S_EOFSTR:
00428                 error(FATAL, "EOF in string or char constant");
00429                 break;
00430 
00431             case S_COMNL:
00432                 s->lineinc++;
00433                 state = COM2;
00434                 ip += runelen;
00435                 runelen = 1;
00436                 if (ip >= s->inb+(7*INS/8)) { /* very long comment */
00437                     memmove(tp->t, ip, 4+s->inl-ip);
00438                     s->inl -= ip-tp->t;
00439                     ip = tp->t+1;
00440                 }
00441                 continue;
00442 
00443             case S_EOFCOM:
00444                 error(WARNING, "EOF inside comment");
00445                 --ip;
00446             case S_COMMENT:
00447                 ++ip;
00448                 tp->t = ip;
00449                 tp->t[-1] = ' ';
00450                 tp->wslen = 1;
00451                 state = START;
00452                 continue;
00453             }
00454             break;
00455         }
00456         ip += runelen;
00457         runelen = 1;
00458         tp->len = ip - tp->t;
00459         tp++;
00460     }
00461 }

Here is the call graph for this function:

Source* setsource char *  name,
int  fd,
char *  str
 

Definition at line 542 of file lex.c.

References cursource, domalloc(), source::fd, source::filename, source::ifdepth, source::inb, source::inl, source::inp, INS, source::line, source::lineinc, source::next, s, Source, strlen(), and strncpy().

Referenced by doconcat(), doinclude(), expandrow(), and setup().

00543 {
00544     Source *s = new(Source);
00545     int len;
00546 
00547     s->line = 1;
00548     s->lineinc = 0;
00549     s->fd = fd;
00550     s->filename = name;
00551     s->next = cursource;
00552     s->ifdepth = 0;
00553     cursource = s;
00554     /* slop at right for EOB */
00555     if (str) {
00556         len = strlen(str);
00557         s->inb = domalloc(len+4);
00558         s->inp = s->inb;
00559         strncpy((char *)s->inp, str, len);
00560     } else {
00561         s->inb = domalloc(INS+4);
00562         s->inp = s->inb;
00563         len = 0;
00564     }
00565     s->inl = s->inp+len;
00566     s->inl[0] = s->inl[1] = EOB;
00567     return s;
00568 }

Here is the call graph for this function:

int trigraph Source s  ) 
 

Definition at line 465 of file lex.c.

References c, fillbuf(), source::inl, source::inp, memmove(), s, and Source.

Referenced by gettokens().

00466 {
00467     int c;
00468 
00469     while (s->inp+2 >= s->inl && fillbuf(s)!=EOF)
00470         ;
00471     if (s->inp[1]!='?')
00472         return 0;
00473     c = 0;
00474     switch(s->inp[2]) {
00475     case '=':
00476         c = '#'; break;
00477     case '(':
00478         c = '['; break;
00479     case '/':
00480         c = '\\'; break;
00481     case ')':
00482         c = ']'; break;
00483     case '\'':
00484         c = '^'; break;
00485     case '<':
00486         c = '{'; break;
00487     case '!':
00488         c = '|'; break;
00489     case '>':
00490         c = '}'; break;
00491     case '-':
00492         c = '~'; break;
00493     }
00494     if (c) {
00495         *s->inp = c;
00496         memmove(s->inp+1, s->inp+3, s->inl-s->inp+2);
00497         s->inl -= 2;
00498     }
00499     return c;
00500 }

Here is the call graph for this function:

void unsetsource void   ) 
 

Definition at line 571 of file lex.c.

References close(), cursource, dofree(), source::fd, source::inb, source::next, s, and Source.

Referenced by doconcat(), expandrow(), process(), and setup().

00572 {
00573     Source *s = cursource;
00574 
00575     if (s->fd>=0) {
00576         close(s->fd);
00577         dofree(s->inb);
00578     }
00579     cursource = s->next;
00580     dofree(s);
00581 }

Here is the call graph for this function:


Variable Documentation

short bigfsm[256][MAXSTATE]
 

Definition at line 236 of file lex.c.

Referenced by expandlex(), fixlex(), and gettokens().

struct fsm fsm[]
 

Definition at line 53 of file lex.c.

Referenced by expandlex().

int tokkind[256]
 

Definition at line 46 of file lex.c.

int tottok
 

Definition at line 45 of file lex.c.


Generated on Thu Aug 25 15:47:40 2005 for Quake III Arena by  doxygen 1.3.9.1