Пример #1
0
accept_t lexer_read_variable(struct lexer_book * bk, struct token * name)
{
    lexer_discard_white_space(bk);

    if(lexer_next_peek(bk) == '=') {
        lexer_next_char(bk);
        lexer_add_to_lexeme(bk, '=');
    } else {
        int c = lexer_next_char(bk);
        if(lexer_next_peek(bk) != '=')
            return NO;
        lexer_add_to_lexeme(bk, c);
        lexer_next_char(bk);	/* Jump = */
    }

    lexer_push_token(bk, lexer_pack_token(bk, VARIABLE));
    lexer_push_token(bk, name);

    lexer_discard_white_space(bk);

    lexer_read_expandable(bk, '\n');
    lexer_roll_back(bk, 1);	//Recover '\n'

    lexer_discard_white_space(bk);

    if(lexer_next_char(bk) != '\n')
        return NO;

    return YES;
}
Пример #2
0
int lexer_read_variable(struct lexer *lx, struct token *name)
{
	lexer_discard_white_space(lx);

	if(lexer_next_peek(lx) == '=') {
		lexer_next_char(lx);
		lexer_add_to_lexeme(lx, '=');
	} else {
		int c = lexer_next_char(lx);
		if(lexer_next_peek(lx) != '=')
			lexer_report_error(lx, "Missing = in variable definition.");
		lexer_add_to_lexeme(lx, c);
		lexer_next_char(lx);	/* Jump = */
	}

	lexer_push_token(lx, lexer_pack_token(lx, TOKEN_VARIABLE));
	lexer_push_token(lx, name);

	lexer_discard_white_space(lx);

	//Read variable value
	lexer_push_token(lx, lexer_read_expandable(lx, '\n'));
	lexer_roll_back(lx, 1);	//Recover '\n'

	lexer_discard_white_space(lx);

	if(lexer_next_char(lx) != '\n')
		lexer_report_error(lx, "Missing newline at end of variable definition.");

	return 1;
}
Пример #3
0
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;
	}
}
Пример #4
0
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;
	}
}
Пример #5
0
void lexer_append_all_tokens(struct lexer_book *bk, struct lexer_book *bk_s)
{
	struct token *head_s;
	
	bk_s->substitution_mode = bk->substitution_mode;
	while( !bk_s->eof )
	{
		if(lexer_next_peek(bk_s) == CHAR_EOF)
		{
			/* Found end of string while completing command */
			bk_s->lexeme_end++;
			bk_s->eof = 1;
		}
		else
		{
			switch(bk_s->substitution_mode)
			{
			case CHAR_EOF:
			case COMMAND:
				head_s = lexer_read_command_argument(bk_s);
				break;
			case FILES:
				head_s = lexer_read_file(bk_s);
				break;
			case SYNTAX:
				lexer_read_expandable(bk_s, CHAR_EOF);
				head_s = lexer_pack_token(bk_s, LITERAL);
				break;
			default:
				lexer_read_line(bk_s);
				continue;
				break;
			}

			if(head_s)
				lexer_push_token(bk_s, head_s);
		}
	}

	while( (head_s = list_pop_tail(bk_s->token_queue)) != NULL )
		list_push_head(bk->token_queue, head_s);
}