示例#1
0
文件: lexer.c 项目: brenden/cctools
struct token *lexer_read_command_argument(struct lexer_book *bk)
{
	int c = lexer_next_peek(bk);

	switch(c)
	{
	case CHAR_EOF:
		/* Found end of file while completing command */
		bk->lexeme_end++;
		bk->eof = 1;

		if( bk->stream )
			return lexer_pack_token(bk, NEWLINE);
		else
			return NULL;
		break;
	case '\n' :
		lexer_next_char(bk);           /* Jump \n */
		lexer_add_to_lexeme(bk, c);
		return lexer_pack_token(bk, NEWLINE);
		break;
	case '#' :
		lexer_discard_comments(bk);
		lexer_add_to_lexeme(bk, '\n');
		return lexer_pack_token(bk, NEWLINE);
	case ' ' :
	case '\t':
		return lexer_read_white_space(bk);
		break;
	case '$'  :
		return lexer_read_substitution(bk);
		break;
	case '"' :
		return lexer_read_expandable(bk, '"');
		break;
	case '<' :
	case '>' :
		lexer_next_char(bk);           /* Jump <, > */
		lexer_add_to_lexeme(bk, c);
		return lexer_pack_token(bk, IO_REDIRECT);
		break;
	case '\'':
		lexer_add_to_lexeme(bk, '\'');
		lexer_read_literal(bk);
		lexer_add_to_lexeme(bk, '\'');
		return lexer_pack_token(bk, LITERAL);
		break;
	default:
		lexer_read_literal(bk);
		return lexer_pack_token(bk, LITERAL);
		break;
	}
}
示例#2
0
文件: lexer.c 项目: NeilB879/cctools
struct token *lexer_read_command_argument(struct lexer *lx)
{
	int c = lexer_next_peek(lx);

	switch (c) {
	case CHAR_EOF:
		/* Found end of file while completing command */
		lx->lexeme_end++;
		lx->eof = 1;

		if(lx->depth == 0)
			lexer_report_error(lx, "Found end of file while completing command.\n");
		return NULL;
		break;
	case '\n':
		lexer_next_char(lx);	/* Jump \n */
		lexer_add_to_lexeme(lx, c);
		return lexer_pack_token(lx, TOKEN_NEWLINE);
		break;
	case '#':
		lexer_discard_comments(lx);
		lexer_add_to_lexeme(lx, '\n');
		return lexer_pack_token(lx, TOKEN_NEWLINE);
	case ' ':
	case '\t':
		return lexer_read_white_space(lx);
		break;
	case '$':
		return lexer_read_substitution(lx);
		break;
	case '"':
		return lexer_read_expandable(lx, '"');
		break;
	case '<':
	case '>':
		lexer_next_char(lx);	/* Jump <, > */
		lexer_add_to_lexeme(lx, c);
		return lexer_pack_token(lx, TOKEN_IO_REDIRECT);
		break;
	case '\'':
		lexer_add_to_lexeme(lx, '\'');
		lexer_read_literal(lx);
		lexer_add_to_lexeme(lx, '\'');
		return lexer_pack_token(lx, TOKEN_LITERAL);
		break;
	default:
		lexer_read_literal(lx);
		return lexer_pack_token(lx, TOKEN_LITERAL);
		break;
	}
}
示例#3
0
int lexer_read_expandable_recursive(struct lexer_book *bk, char end_marker)
{
    int count = 0;
    lexer_discard_white_space(bk);

    while(!bk->eof) {
        int c = lexer_next_peek(bk);

        if(c == '$') {
            count++;
            lexer_push_token(bk, lexer_read_substitution(bk));
        }
        if(c == '\'') {
            lexer_read_literal(bk);
            lexer_push_token(bk, lexer_pack_token(bk, LITERAL));
        } else if(c == end_marker) {
            lexer_next_char(bk);	/* Jump end_marker */
            return count;
        } else if(c == '"')
            count += lexer_read_expandable_recursive(bk, '"');
        else if(c == '#' && end_marker != '"') {
            lexer_discard_comments(bk);
            return count;
        } else {
            count++;
            lexer_push_token(bk, lexer_read_literal_in_expandable_until(bk, end_marker));
        }
    }
    /* Found eof before end_marker */
    abort();
}
示例#4
0
文件: lexer.c 项目: NeilB879/cctools
//opened tracks whether it is the opening (opened = 0) or closing (opened = 1) double quote we encounter.
struct list *lexer_read_expandable_recursive(struct lexer *lx, char end_marker, int opened)
{
	lexer_discard_white_space(lx);

	struct list *tokens = list_create();

	while(!lx->eof) {
		int c = lexer_next_peek(lx);

		if(c == '$') {
			list_push_tail(tokens, lexer_read_substitution(lx));
		}

		if(c == '\'') {
			lexer_read_literal(lx);
			list_push_tail(tokens, lexer_pack_token(lx, TOKEN_LITERAL));
		} else if(c == '"' && opened == 0) {
				lexer_add_to_lexeme(lx, lexer_next_char(lx));
				list_push_tail(tokens, lexer_pack_token(lx, TOKEN_LITERAL));     // Add first "
				tokens = list_splice(tokens, lexer_read_expandable_recursive(lx, '"', 1));
				lexer_add_to_lexeme(lx, '"');
				list_push_tail(tokens, lexer_pack_token(lx, TOKEN_LITERAL));     // Add closing "
				if(end_marker == '"')
					return tokens;
		} else if(c == '#' && end_marker != '"') {
			lexer_discard_comments(lx);
		} else if(c == end_marker) {
			lexer_next_char(lx);	/* Jump end_marker */
			return tokens;
		} else {
			list_push_tail(tokens, lexer_read_literal_in_expandable_until(lx, end_marker));
		}
	}

	lexer_report_error(lx, "Found EOF before end marker: %c.\n", end_marker);

	return NULL;
}