Annotation of researchv10no/cmd/twig/lex.c, revision 1.1

1.1     ! root        1: #include <ctype.h>
        !             2: #include "common.h"
        !             3: #include "code.h"
        !             4: #include "sym.h"
        !             5: #include "y.tab.h"
        !             6: 
        !             7: int yyline = 1;
        !             8: char token_buffer[MAXIDSIZE+1];
        !             9: extern YYSTYPE yylval;
        !            10: static void ScanCodeBlock();
        !            11: static void ScanComment();
        !            12: static Code *curCdBlock;
        !            13: static char get();
        !            14: 
        !            15: yylex()
        !            16: {
        !            17:        register c;
        !            18:        register char *cp;
        !            19:        int in_ident = 0;
        !            20:        yylval.y_nodep = (struct node *) NULL;
        !            21:        cp = token_buffer;
        !            22:        while((c=getchar())!=EOF) {
        !            23:                switch(c) {
        !            24:                case ' ': case '\t': case '\f':
        !            25:                        continue;
        !            26:                case '@': case '[': case ']': case ';': case ':':
        !            27:                case '(': case ')': case ',': case '=':
        !            28:                case '*':
        !            29:                        if(debug_flag&DB_LEX) {
        !            30:                                putc(c,stderr);
        !            31:                                putc('\n', stderr);
        !            32:                        }
        !            33:                        *cp++ = c;
        !            34:                        *cp = '\0';
        !            35:                        return(c);
        !            36: 
        !            37:                case '{':
        !            38:                        ScanCodeBlock();
        !            39:                        yylval.y_code = curCdBlock;
        !            40:                        curCdBlock = NULL;
        !            41:                        *cp++ = '{'; *cp++ = '}';
        !            42:                        return(CBLOCK);
        !            43: 
        !            44:                case '\n':
        !            45:                        yyline++;
        !            46:                        continue;
        !            47:                case '/':
        !            48:                        if ((c=getchar())=='*') {
        !            49:                                ScanComment(get);
        !            50:                                continue;
        !            51:                        } else {
        !            52:                                ungetc(c, stdin);
        !            53:                                c = '/';
        !            54:                        }
        !            55:                        /* FALL THRU */
        !            56: 
        !            57:                default:
        !            58:                        if (isdigit(c)) {
        !            59:                                int errs = 0;
        !            60:                                do {
        !            61:                                        if(cp > &token_buffer[MAXIDSIZE]) {
        !            62:                                                token_buffer[MAXIDSIZE] = '\0';
        !            63:                                                yyerror("number too long");
        !            64:                                                errs++;
        !            65:                                        } else *cp++ = c;
        !            66:                                        c = getchar();
        !            67:                                } while (isdigit(c));
        !            68:                                if(isalpha(c))
        !            69:                                        yyerror2("illegal digit '%c'", c);
        !            70:                                ungetc(c, stdin);
        !            71:                                if(errs)
        !            72:                                        return(ERROR);
        !            73:                                yylval.y_int = atoi(token_buffer);
        !            74:                                return(NUMBER);
        !            75:                        }
        !            76:                        if (isalpha(c)) {
        !            77:                                SymbolEntry *sp;
        !            78:                                int errs = 0;
        !            79:                                do {
        !            80:                                        if(cp > &token_buffer[MAXIDSIZE]) {
        !            81:                                                token_buffer[MAXIDSIZE] = '\0';
        !            82:                                                yyerror("ID too long");
        !            83:                                                errs++;
        !            84:                                        } else *cp++ = c;
        !            85:                                        c = getchar();
        !            86:                                } while (isalpha(c)||isdigit(c)||c=='_');
        !            87:                                ungetc(c, stdin);
        !            88:                                if(errs)
        !            89:                                        return(ERROR);
        !            90:                                *cp = '\0';
        !            91: 
        !            92:                                sp = SymbolLookup (token_buffer);
        !            93:                                if (sp==NULL) {
        !            94:                                        /* undefined */
        !            95:                                    yylval.y_symp = SymbolAllocate(token_buffer);
        !            96:                                } else {
        !            97:                                    /* already defined */
        !            98:                                    if (sp->attr == A_KEYWORD)
        !            99:                                                return (sp->sd.keyword);
        !           100:                                    yylval.y_symp = sp;
        !           101:                                }
        !           102:                                if(debug_flag&DB_LEX)
        !           103:                                        fprintf(stderr, "ID\n");
        !           104:                                return(ID);
        !           105:                        }
        !           106:                        yyerror2("illegal character (\\%03o)", c);
        !           107:                }
        !           108:        }
        !           109:        strcpy(token_buffer, "EOF");
        !           110:        return(0);
        !           111: }
        !           112: 
        !           113: void
        !           114: LexInit()
        !           115: {
        !           116: }
        !           117: 
        !           118: lexCleanup()
        !           119: {
        !           120: }
        !           121: 
        !           122: /*
        !           123:  * Beware: ungets of the characters from these routines may screw up the
        !           124:  * line count
        !           125:  */
        !           126: static char
        !           127: getput()
        !           128: {
        !           129:        /* keutzer
        !           130:        char c;
        !           131:        */
        !           132:        int c;
        !           133:        c = getchar();
        !           134:        if(c=='\n') yyline++;
        !           135:        if(c!=EOF)
        !           136:                curCdBlock = CodeStoreChar(curCdBlock, c);
        !           137:        return(c);
        !           138: }
        !           139: 
        !           140: static char
        !           141: get()
        !           142: {
        !           143:        /* keutzer
        !           144:        char c;
        !           145:        */
        !           146:        int c;
        !           147:        c = getchar();
        !           148:        if(c=='\n') yyline++;
        !           149:        return(c);
        !           150: }
        !           151: 
        !           152: extern int nerrors;
        !           153: 
        !           154: static void
        !           155: ScanComment(rdfunc)
        !           156:        char (*rdfunc)();
        !           157: {
        !           158:        int startline = yyline;
        !           159:        /* keutzer
        !           160:        char c;
        !           161:        */
        !           162:        int c;
        !           163:        int saw_star = 0;
        !           164:        while ((c=rdfunc())!=EOF) {
        !           165:                if (c=='*')
        !           166:                        saw_star++;
        !           167:                else if(c=='/' && saw_star) {
        !           168:                        return;
        !           169:                } else saw_star = 0;
        !           170:        }
        !           171:        yyerror2("unexpected EOF in comment beginning on line %d", startline);
        !           172:        nerrors++;
        !           173:        cleanup(1);
        !           174: }
        !           175: 
        !           176: static
        !           177: ScanString(rdfunc)
        !           178:        char (*rdfunc)();
        !           179: {
        !           180:        int startline = yyline;
        !           181:        /* keutzer
        !           182:        char c;
        !           183:        */
        !           184:        int c;
        !           185:        int saw_backsl = 0;
        !           186: 
        !           187:        while((c=rdfunc())!=EOF) {
        !           188:                if (c=='"' && !saw_backsl)
        !           189:                        return;
        !           190:                if (c=='\\' && !saw_backsl) {
        !           191:                        saw_backsl = 1;
        !           192:                        continue;
        !           193:                }
        !           194:                saw_backsl = 0;
        !           195:        }
        !           196:        /* fall thru due to EOF */
        !           197:        yyerror2("unexpected EOF in string beginning on line %d", startline);
        !           198:        nerrors++;
        !           199:        cleanup(1);
        !           200: }
        !           201: 
        !           202: static
        !           203: ScanChar()
        !           204: {
        !           205:        int startline = yyline;
        !           206:        /* keutzer
        !           207:        char c;
        !           208:        */
        !           209:        int c;
        !           210:        int saw_backsl = 0;
        !           211: 
        !           212:        while((c=getput(stdin))!=EOF) {
        !           213:                if (c=='\'' && !saw_backsl)
        !           214:                        return;
        !           215:                if (c=='\\' && !saw_backsl) {
        !           216:                        saw_backsl = 1;
        !           217:                        continue;
        !           218:                }
        !           219:                saw_backsl = 0;
        !           220:        }
        !           221:        /* fall thru due to EOF */
        !           222:        yyerror2("unexpected EOF in character constant beginning on line %d",
        !           223:                 startline);
        !           224:        nerrors++;
        !           225:        cleanup(1);
        !           226: }
        !           227: 
        !           228: static void
        !           229: ScanTreeReference()
        !           230: {
        !           231: 
        !           232:        /* keutzer
        !           233:        char c;
        !           234:        */
        !           235:        int c;
        !           236:        c = getchar();
        !           237:        if(c=='%') {
        !           238:                curCdBlock = CodeStoreString (curCdBlock, "_ll[(");
        !           239:                while ((c=getchar())!=EOF && c!='$') {
        !           240:                        if(!isdigit(c)) {
        !           241:                                yyerror("unclosed $ reference");
        !           242:                                ungetc(c,stdin);
        !           243:                                break;
        !           244:                        }
        !           245:                        curCdBlock = CodeStoreChar(curCdBlock, c);
        !           246:                }
        !           247:                curCdBlock = CodeStoreString (curCdBlock, ")-1]");
        !           248:                return;
        !           249:        }
        !           250:        else if(c=='$') {
        !           251:                curCdBlock = CodeStoreString(curCdBlock, "root");
        !           252:                return;
        !           253:        } else curCdBlock = CodeStoreString(curCdBlock, "_mtG(root,");
        !           254:        do {
        !           255:                if(!isdigit(c) && c!='.') {
        !           256:                        yyerror("unclosed $ reference");
        !           257:                        ungetc(c,stdin);
        !           258:                        break;
        !           259:                }
        !           260:                curCdBlock = CodeStoreChar(curCdBlock, c=='.' ? ',' : c);
        !           261:        } while((c=getchar())!=EOF && c!='$');
        !           262:        curCdBlock = CodeStoreString(curCdBlock, ", -1)");
        !           263: }
        !           264: 
        !           265: static void
        !           266: ScanCodeBlock()
        !           267: {
        !           268:        int startline = yyline;
        !           269:        /* keutzer
        !           270:        char c;
        !           271:        */
        !           272:        int c;
        !           273:        if(curCdBlock==NULL) {
        !           274:                curCdBlock = CodeGetBlock();
        !           275:                curCdBlock = CodeMarkLine(curCdBlock,yyline);
        !           276:        }
        !           277:        while((c=getc(stdin))!=EOF) {
        !           278:                if (c=='}')
        !           279:                        return;
        !           280:                else if (c=='$') ScanTreeReference();
        !           281:                else curCdBlock = CodeStoreChar(curCdBlock, c);
        !           282:                if (c=='\n') yyline++;
        !           283:                if (c=='"') ScanString(getput);
        !           284:                else if (c=='\'') ScanChar();
        !           285:                else if (c=='/') {
        !           286:                        if ((c=getc(stdin))=='*') {
        !           287:                                curCdBlock = CodeStoreChar(curCdBlock, '*');
        !           288:                                ScanComment(getput);
        !           289:                        }
        !           290:                        else ungetc(c, stdin);
        !           291:                }
        !           292:                else if (c=='{') {
        !           293:                        ScanCodeBlock();
        !           294:                        curCdBlock = CodeStoreChar (curCdBlock, '}');
        !           295:                }
        !           296:        }
        !           297:        yyerror2("{ on line %d has no closing }", startline);
        !           298:        nerrors++;
        !           299: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.