Ejemplo n.º 1
0
/* Return one whole line from the file into buf which holds at most n
   characters, for subsequent processing. Returns integer status. This
   routine also does all the "intelligent" work like processing cpp
   directives and so on. Note that often the routine is called
   recursively and no cpp directives are printed. */
int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[])
{
    gmx_cpp_t   handle = (gmx_cpp_t)*handlep;
    int         i, nn, len, status;
    const char *ptr, *ptr2;
    char       *name;
    char       *dname, *dval;
    gmx_bool    bEOF;

    if (!handle)
    {
        return eCPP_INVALID_HANDLE;
    }
    if (!handle->fp)
    {
        return eCPP_FILE_NOT_OPEN;
    }

    bEOF = feof(handle->fp);
    if (!bEOF)
    {
        /* Read the actual line now. */
        if (fgets2(buf, n-1, handle->fp) == NULL)
        {
            /* Recheck EOF, since we could have been at the end before
             * the fgets2 call, but we need to read past the end to know.
             */
            bEOF = feof(handle->fp);
            if (!bEOF)
            {
                /* Something strange happened, fgets returned NULL,
                 * but we are not at EOF.
                 */
                return eCPP_UNKNOWN;
            }
        }
    }

    if (bEOF)
    {
        if (handle->parent == NULL)
        {
            return eCPP_EOF;
        }
        cpp_close_file(handlep);
        *handlep      = handle->parent;
        handle->child = NULL;
        return cpp_read_line(handlep, n, buf);
    }
    else
    {
        if (n > handle->line_len)
        {
            handle->line_len = n;
            srenew(handle->line, n);
        }
        strcpy(handle->line, buf);
        handle->line_nr++;
    }
    /* Now we've read a line! */
    if (debug)
    {
        fprintf(debug, "%s : %4d : %s\n", handle->fn, handle->line_nr, buf);
    }

    /* Process directives if this line contains one */
    if (find_directive(buf, &dname, &dval))
    {
        status = process_directive(handlep, dname, dval);
        if (status != eCPP_OK)
        {
            return status;
        }
        /* Don't print lines with directives, go on to the next */
        return cpp_read_line(handlep, n, buf);
    }

    /* Check whether we're not ifdeffed out. The order of this statement
       is important. It has to come after #ifdef, #else and #endif, but
       anything else should be ignored. */
    if (is_ifdeffed_out(handle))
    {
        return cpp_read_line(handlep, n, buf);
    }

    /* Check whether we have any defines that need to be replaced. Note
       that we have to use a best fit algorithm, rather than first come
       first go. We do this by sorting the defines on length first, and
       then on alphabetical order. */
    for (i = 0; (i < ndef); i++)
    {
        if (defs[i].def)
        {
            nn  = 0;
            ptr = buf;
            while ((ptr = strstrw(ptr, defs[i].name)) != NULL)
            {
                nn++;
                ptr += strlen(defs[i].name);
            }
            if (nn > 0)
            {
                len = strlen(buf) + nn*max(4, 4+strlen(defs[i].def)-strlen(defs[i].name));
                snew(name, len);
                ptr = buf;
                while ((ptr2 = strstrw(ptr, defs[i].name)) != NULL)
                {
                    strncat(name, ptr, (int)(ptr2-ptr));
                    strcat(name, defs[i].def);
                    ptr = ptr2 + strlen(defs[i].name);
                }
                strcat(name, ptr);
                strcpy(buf, name);
                sfree(name);
            }
        }
    }

    return eCPP_OK;
}
Ejemplo n.º 2
0
/*----------------------------------------------------------------------
 * Parse a single token (called from the scanner)
 */
void
parse_token (token_t token, char *str)
{
    guint32 num;

    /*
     * This is implemented as a simple state machine of five states.
     * State transitions are caused by tokens being received from the
     * scanner. The code should be self_documenting.
     */

    if (debug>=2) {
        /* Sanitize - remove all '\r' */
        char *c;
        if (str!=NULL) {
            while ((c = strchr(str, '\r')) != NULL) *c=' ';
        }

        fprintf(stderr, "(%s, %s \"%s\") -> (",
                state_str[state], token_str[token], str ? str : "");
    }

    switch(state) {

    /* ----- Waiting for new packet -------------------------------------------*/
    case INIT:
        switch(token) {
        case T_TEXT:
            append_to_preamble(str);
            break;
        case T_DIRECTIVE:
            process_directive(str);
            break;
        case T_OFFSET:
            num = parse_num(str, TRUE);
            if (num==0) {
                /* New packet starts here */
                start_new_packet();
                state = READ_OFFSET;
            }
            break;
        case T_BYTE:
            if (offset_base == 0) {
                start_new_packet();
                write_byte(str);
                state = READ_BYTE;
            }
            break;
        default:
            break;
        }
        break;

    /* ----- Processing packet, start of new line -----------------------------*/
    case START_OF_LINE:
        switch(token) {
        case T_TEXT:
            append_to_preamble(str);
            break;
        case T_DIRECTIVE:
            process_directive(str);
            break;
        case T_OFFSET:
            num = parse_num(str, TRUE);
            if (num==0) {
                /* New packet starts here */
                start_new_packet();
                packet_start = 0;
                state = READ_OFFSET;
            } else if ((num - packet_start) != curr_offset) {
                /*
                 * The offset we read isn't the one we expected.
                 * This may only mean that we mistakenly interpreted
                 * some text as byte values (e.g., if the text dump
                 * of packet data included a number with spaces around
                 * it).  If the offset is less than what we expected,
                 * assume that's the problem, and throw away the putative
                 * extra byte values.
                 */
                if (num < curr_offset) {
                    unwrite_bytes(curr_offset - num);
                    state = READ_OFFSET;
                } else {
                    /* Bad offset; switch to INIT state */
                    if (debug>=1)
                        fprintf(stderr, "Inconsistent offset. Expecting %0X, got %0X. Ignoring rest of packet\n",
                                curr_offset, num);
                    write_current_packet();
                    state = INIT;
                }
            } else
                state = READ_OFFSET;
            break;
        case T_BYTE:
            if (offset_base == 0) {
                write_byte(str);
                state = READ_BYTE;
            }
            break;
        default:
            break;
        }
        break;

    /* ----- Processing packet, read offset -----------------------------------*/
    case READ_OFFSET:
        switch(token) {
        case T_BYTE:
            /* Record the byte */
            state = READ_BYTE;
            write_byte(str);
            break;
        case T_TEXT:
        case T_DIRECTIVE:
        case T_OFFSET:
            state = READ_TEXT;
            break;
        case T_EOL:
            state = START_OF_LINE;
            break;
        default:
            break;
        }
        break;

    /* ----- Processing packet, read byte -------------------------------------*/
    case READ_BYTE:
        switch(token) {
        case T_BYTE:
            /* Record the byte */
            write_byte(str);
            break;
        case T_TEXT:
        case T_DIRECTIVE:
        case T_OFFSET:
            state = READ_TEXT;
            break;
        case T_EOL:
            state = START_OF_LINE;
            break;
        default:
            break;
        }
        break;

    /* ----- Processing packet, read text -------------------------------------*/
    case READ_TEXT:
        switch(token) {
        case T_EOL:
            state = START_OF_LINE;
            break;
        default:
            break;
        }
        break;

    default:
        fprintf(stderr, "FATAL ERROR: Bad state (%d)", state);
        exit(-1);
    }

    if (debug>=2)
        fprintf(stderr, ", %s)\n", state_str[state]);

}
Ejemplo n.º 3
0
int lalr1::parse()
{
	char *token = new char[4096];
	int  lapg_head = 0, group = 0, lapg_i, lapg_size, chr;
	lapg_symbol *lapg_m = new lapg_symbol[512];
	lapg_symbol lapg_n = { NULL, -1, 0 };
	lapg_place lapg_current = { 1, 1 };

	lapg_m[0].state = 0;
	chr = *l++;if( l == end ) fillb();

	do {
		lapg_n.pos = lapg_current;
		for( lapg_size = 0, lapg_i = group; lapg_i >= 0; ) {
			if( lapg_size < 4096-1 ) token[lapg_size++] = chr;
			lapg_i = lapg_lexem[lapg_i][lapg_char2no[chr]];
			if( lapg_i >= -1 && chr ) { 
				lapg_current.column++;
				if( chr == '\n' ) lapg_current.column = 1, lapg_current.line++;
				chr = *l++;if( l == end ) fillb();
			}
		}
		token[lapg_size] = 0;

		if( lapg_i == -1 ) {
			error( 0, "invalid lexem at line %i, column %i: `%s`, skipped\n", lapg_n.pos.line, lapg_n.pos.column, token );
			continue;
		}

		token[lapg_size-1] = 0;
		lapg_n.lexem = -lapg_i-2;
		lapg_n.sym = NULL;
		switch( lapg_n.lexem ) {
			case 1: {
				#line 30 "syntax"
				 *(char* *)&lapg_n.sym = _strdup(token); 
			} break;
			case 2: {
				#line 31 "syntax"
				 *(char* *)&lapg_n.sym = strstrip(token); 
			} break;
			case 3: {
				#line 32 "syntax"
				 *(char* *)&lapg_n.sym = _strdup(token+1); 
			} break;
			case 4: {
				#line 33 "syntax"
				 *(char* *)&lapg_n.sym = _strdup(token+3); 
			} break;
			case 5: {
				#line 34 "syntax"
				 *(char* *)&lapg_n.sym = strstrip(token); 
			} break;
			case 6: {
				#line 35 "syntax"
				 *(char* *)&lapg_n.sym = strstrip(token); 
			} break;
			case 7: {
				#line 36 "syntax"
				 *(char* *)&lapg_n.sym = strstrip(token); 
			} break;
			case 8: {
				#line 37 "syntax"
				 *(int *)&lapg_n.sym = strtol(token,NULL,10); 
			} break;
			case 10: {
				#line 40 "syntax"
				 continue; 
			} break;
		}

		do {
			lapg_i = lapg_next( lapg_m[lapg_head].state, lapg_n.lexem );

			if( lapg_i >= 0 ) {
				lapg_symbol lapg_gg={(lapg_rlen[lapg_i])?lapg_m[lapg_head+1-lapg_rlen[lapg_i]].sym:NULL,lapg_rlex[lapg_i],0 };
				#ifdef DEBUG_syntax
					fprintf( stdout, "reduce to %s\n", lapg_syms[lapg_rlex[lapg_i]] );
				#endif
				lapg_gg.pos = (lapg_rlen[lapg_i])?lapg_m[lapg_head+1-lapg_rlen[lapg_i]].pos:lapg_n.pos;
				switch( lapg_i ) {
					case 2: {
						#line 64 "syntax"
						 lapg_gg.pos.line++; 
					} break;
					case 4: {
						#line 68 "syntax"
						lapg_gg.sym = concat( NULL, ((char*)lapg_m[lapg_head-0].sym), sourcename, lapg_m[lapg_head-0].pos.line );
					} break;
					case 5: {
						#line 70 "syntax"
						lapg_gg.sym = concat( *(char* *)&lapg_gg.sym, ((char*)lapg_m[lapg_head-0].sym), sourcename, (lapg_m[lapg_head-1].pos.line+1!=lapg_m[lapg_head-0].pos.line)?lapg_m[lapg_head-0].pos.line:-1 );
						lapg_gg.pos = lapg_m[lapg_head-0].pos;
					} break;
					case 8: {
						#line 78 "syntax"
						process_directive( ((char*)lapg_m[lapg_head-1].sym), ((char*)lapg_m[lapg_head-0].sym), lapg_m[lapg_head-2].pos.line, lapg_m[lapg_head-2].pos.column );
						delete[] ((char*)lapg_m[lapg_head-1].sym);
					} break;
					case 9: {
						#line 82 "syntax"
						process_directive( ((char*)lapg_m[lapg_head-1].sym), ((int)lapg_m[lapg_head-0].sym), lapg_m[lapg_head-2].pos.line, lapg_m[lapg_head-2].pos.column );
						delete[] ((char*)lapg_m[lapg_head-1].sym);
					} break;
					case 14: {
						#line 88 "syntax"
						 if( ((int)lapg_m[lapg_head-0].sym) < 0 || ((int)lapg_m[lapg_head-0].sym) >= BITS ) *(int *)&lapg_gg.sym = 0; else *(int *)&lapg_gg.sym = 1 << ((int)lapg_m[lapg_head-0].sym); 
					} break;
					case 16: {
						#line 89 "syntax"
						 *(int *)&lapg_gg.sym |= ((int)lapg_m[lapg_head-0].sym); 
					} break;
					case 17: {
						#line 90 "syntax"
						 lb.currentgroups = ((int)lapg_m[lapg_head-1].sym); 
					} break;
					case 18: {
						#line 94 "syntax"
						gb.terminal(((char*)lapg_m[lapg_head-1].sym)); delete[] ((char*)lapg_m[lapg_head-1].sym); 
					} break;
					case 19: {
						#line 97 "syntax"
						gb.terminal(((char*)lapg_m[lapg_head-2].sym),((char*)lapg_m[lapg_head-1].sym)); delete[] ((char*)lapg_m[lapg_head-2].sym); delete[] ((char*)lapg_m[lapg_head-1].sym);
					} break;
					case 22: {
						#line 100 "syntax"
						lb.lexem( gb.terminal(((char*)lapg_m[lapg_head-4].sym)), ((char*)lapg_m[lapg_head-2].sym), ((char*)lapg_m[lapg_head-4].sym), ((char*)lapg_m[lapg_head-0].sym), ((int)lapg_m[lapg_head-1].sym) );
						delete[] ((char*)lapg_m[lapg_head-4].sym); delete[] ((char*)lapg_m[lapg_head-2].sym); delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
					case 23: {
						#line 104 "syntax"
						lb.lexem( gb.terminal(((char*)lapg_m[lapg_head-3].sym)), ((char*)lapg_m[lapg_head-1].sym), ((char*)lapg_m[lapg_head-3].sym), NULL, ((int)lapg_m[lapg_head-0].sym) );
						delete[] ((char*)lapg_m[lapg_head-3].sym); delete[] ((char*)lapg_m[lapg_head-1].sym);
					} break;
					case 24: {
						#line 108 "syntax"
						lb.lexem( gb.terminal(((char*)lapg_m[lapg_head-5].sym), ((char*)lapg_m[lapg_head-4].sym)), ((char*)lapg_m[lapg_head-2].sym), ((char*)lapg_m[lapg_head-5].sym), ((char*)lapg_m[lapg_head-0].sym), ((int)lapg_m[lapg_head-1].sym) );
						delete[] ((char*)lapg_m[lapg_head-5].sym); delete[] ((char*)lapg_m[lapg_head-4].sym); delete[] ((char*)lapg_m[lapg_head-2].sym); delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
					case 25: {
						#line 112 "syntax"
						lb.lexem( gb.terminal(((char*)lapg_m[lapg_head-4].sym), ((char*)lapg_m[lapg_head-3].sym)), ((char*)lapg_m[lapg_head-1].sym), ((char*)lapg_m[lapg_head-4].sym), NULL, ((int)lapg_m[lapg_head-0].sym) );
						delete[] ((char*)lapg_m[lapg_head-4].sym); delete[] ((char*)lapg_m[lapg_head-3].sym); delete[] ((char*)lapg_m[lapg_head-1].sym);
					} break;
					case 31: {
						#line 121 "syntax"
						gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 0, NULL, gb.symbol( ((char*)lapg_m[lapg_head-2].sym), 0 ) );
						delete[] ((char*)lapg_m[lapg_head-2].sym);
						delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
					case 32: {
						#line 125 "syntax"
						gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 0, NULL, -1 );
						delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
					case 43: {
						#line 146 "syntax"
						if( !strcmp(((char*)lapg_m[lapg_head-0].sym),"left")) lapg_gg.sym = (void*)1;
						else if( !strcmp(((char*)lapg_m[lapg_head-0].sym),"right")) lapg_gg.sym = (void*)2;
						else if( !strcmp(((char*)lapg_m[lapg_head-0].sym),"nonassoc")) lapg_gg.sym = (void*)3;
						else { error(0,"wrong priority declaration: %%%s",((char*)lapg_m[lapg_head-0].sym));lapg_gg.sym = 0; }
						delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
					case 45: {
						#line 153 "syntax"
						 gb.addprio(((char*)lapg_m[lapg_head-0].sym),((int)lapg_m[lapg_head-1].sym),0); 
					} break;
					case 46: {
						#line 153 "syntax"
						 gb.addprio(((char*)lapg_m[lapg_head-0].sym),((int)lapg_m[lapg_head-2].sym),1); 
					} break;
					case 47: {
						#line 156 "syntax"
						 *(int *)&lapg_gg.sym = gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 0 ); delete[] ((char*)lapg_m[lapg_head-0].sym); 
					} break;
					case 48: {
						#line 157 "syntax"
						 *(int *)&lapg_gg.sym = -1; 
					} break;
					case 49: {
						#line 161 "syntax"
						gb.rule( length, ((int)lapg_m[lapg_head-1].sym), ((char*)lapg_m[lapg_head-0].sym), rule, lapg_gg.pos.line );
					} break;
					case 50: {
						#line 164 "syntax"
						gb.rule( length, ((int)lapg_m[lapg_head-0].sym), NULL, rule, lapg_gg.pos.line );
					} break;
					case 51: {
						#line 167 "syntax"
						gb.rule( 0, ((int)lapg_m[lapg_head-0].sym), ((char*)lapg_m[lapg_head-1].sym), rule, lapg_gg.pos.line );
					} break;
					case 52: {
						#line 170 "syntax"
						gb.rule( 0, ((int)lapg_m[lapg_head-0].sym), NULL, rule, lapg_gg.pos.line );
					} break;
					case 55: {
						#line 175 "syntax"
						if( ((char*)lapg_m[lapg_head-1].sym) ) {
							length += 2;
							rule[length] = gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 0 );
							rule[length-1] = gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 2, NULL, rule[length] );
							gb.rule( 0, -1, ((char*)lapg_m[lapg_head-1].sym), rule+length-1, lapg_m[lapg_head-2].pos.line );
						} else rule[++length] = gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 0 );
						delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
					case 56: {
						#line 184 "syntax"
						length = 0;
						if( ((char*)lapg_m[lapg_head-1].sym) ) {
							length += 2;
							rule[length] = gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 0 );
							rule[length-1] = gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 2, NULL, rule[length] );
							gb.rule( 0, -1, ((char*)lapg_m[lapg_head-1].sym), rule+length-1, lapg_m[lapg_head-1].pos.line );
						} else rule[++length] = gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 0 );
						delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
					case 57: {
						#line 196 "syntax"
						rule[0] = gb.symbol( ((char*)lapg_m[lapg_head-0].sym), 1 );
						delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
					case 58: {
						#line 200 "syntax"
						rule[0] = gb.symbol( ((char*)lapg_m[lapg_head-1].sym), 1, ((char*)lapg_m[lapg_head-0].sym) );
						delete[] ((char*)lapg_m[lapg_head-1].sym);
						delete[] ((char*)lapg_m[lapg_head-0].sym);
					} break;
				}
				lapg_head -= lapg_rlen[lapg_i];
				lapg_m[++lapg_head] = lapg_gg;
				lapg_m[lapg_head].state = lapg_state_sym( lapg_m[lapg_head-1].state, lapg_gg.lexem );
			} else if( lapg_i == -1 ) {
				lapg_m[++lapg_head] = lapg_n;
				lapg_m[lapg_head].state = lapg_state_sym( lapg_m[lapg_head-1].state, lapg_n.lexem );
				#ifdef DEBUG_syntax
					fprintf( stdout, "shift: %s (%s)\n", lapg_syms[lapg_n.lexem], token );
				#endif
			}

		} while( lapg_i >= 0 && lapg_m[lapg_head].state != -1 );

		if( (lapg_i == -2 || lapg_m[lapg_head].state == -1) && lapg_n.lexem != 0 ) {
			break;
		}

	} while( lapg_n.lexem );

	if( lapg_m[lapg_head].state == 91-1 ) lapg_i = 1; else lapg_i = 0;
	delete[] lapg_m;
	delete[] token;

	if( !lapg_i ) error( 0, "syntax error before line %i, column %i\n", lapg_n.pos.line, lapg_n.pos.column );
	return lapg_i;
}