예제 #1
0
bool parser::parse_atom(utf8str::iterator& it, utf8str::iterator& it_end, shared_ptr<expr>& result)
{
	if (it != it_end)
	{
		uint32_t ch = utf8::peek_next(it, it_end);		
		if (is_atom_char(ch))
		{
			utf8::unchecked::next(it);
			shared_ptr<utf8str> atom(new utf8str());
			atom->append(ch);
			while (it != it_end)
			{
				ch = utf8::peek_next(it, it_end);
				if (is_atom_char(ch))
				{
					utf8::unchecked::next(it);
					atom->append(ch);
				}
				else
				{					
					break;
				}
			}
			try
			{
				cout << "Trying to coerce value to integer" << std::endl;
				int intValue = boost::lexical_cast<int>(atom->get_string());
				result = shared_ptr<expr>(new expr(intValue));				
			}
			catch(boost::bad_lexical_cast& e)
			{
				cout << "Not an integer, than symbol" << std::endl;
				result = shared_ptr<expr>(new expr(atom));				
			}
			return true;
		}
		else
		{			
			return false;
		}
	}
	return false;
}
예제 #2
0
static BOOL is_atom_match(LPSTR lpMem, LPSTR *lpBegin, LPSTR *lpEnd,
                          LPSTR *lpPrev, LPREPLACEKEY rk, WORD wAtomLen)
{
    if (rk->rx)
    {
        LPSTR lpBAtom = *lpBegin;
        LPSTR lpEAtom = *lpEnd;
        
        while (lpBAtom > lpMem && is_atom_char(lpBAtom[-1]))
            lpBAtom--;
        if (lpBAtom == *lpPrev)
            return FALSE;

        while (lpEAtom > *lpBegin && is_atom_char(lpEAtom[-1]))
            lpEAtom--;
        if (lpEAtom != *lpBegin)
        {
            char e;
            BOOL bMatch;
            
            lpEAtom = *lpBegin;
            while (is_atom_char(lpEAtom[0])) lpEAtom++;
            e = lpEAtom[0];
            lpEAtom[0] = '\0';
            bMatch = KppRegexec(rk->rx, lpBAtom, lpBegin, lpEnd);
            lpEAtom[0] = e;
            
            if (bMatch)
                *lpPrev = lpBAtom;
            
            return bMatch;
        }
        else
            *lpPrev = lpBAtom;
        
        return TRUE;
    }
    else
        return (*lpBegin == lpMem ||
                *lpBegin > lpMem && !is_atom_char((*lpBegin)[-1])) &&
                !is_atom_char((*lpBegin)[wAtomLen]);
}
예제 #3
0
static enum label_classification
classify_label(struct ntb_key *key)
{
        enum label_classification classification = LABEL_NONE;
        const char *p;

        if (key == NULL)
                return LABEL_NONE;

        for (p = key->label; *p; p++) {
                if (*p < ' ' || *p > 127 ||
                    *p == '"' || *p == '\\')
                        return LABEL_ENCODE;
                else if (is_atom_char(*p)) {
                        if (classification == LABEL_NONE)
                                classification = LABEL_RAW;
                } else {
                        classification = LABEL_QUOTES;
                }
        }

        return classification;
}
예제 #4
0
파일: token.c 프로젝트: 74AC153/paren
void token_chomp(tok_state_t *state)
{
	size_t len;
	int64_t lit;
	bool in_comment = false;
	char *cursor, *test_end;

	/* prime the stream (no-op if stream end) */
	if(state->nextch == -1) {
		state->in_string = false;
		advance_tok_stream(state);
	}

	/* string-mode shortcut */
	if(state->in_string) {
		if(state->nextch == '"') {
			state->in_string = false;
			state->type = TOK_RPAREN;
			goto finish;
		}

		state->type = TOK_LIT;

		if(state->nextch != '\\') {
			state->u.lit = state->nextch;
			goto finish;
		}
		
		/* \n    0x0A newline
		   \r    0x0D carriage return
		   \t    0x09 tab
		   \###  octal value
		   \anything else -> anything else */
		advance_tok_stream(state); // consume '\'
		switch(state->nextch) {
		case -1:
			state->u.lit = '\\';
			state->in_string = false;
			break;
		case 'n': state->u.lit = 0xA; break;
		case 'r': state->u.lit = 0xD; break;
		case 't': state->u.lit = 0x9; break;
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
			state->u.lit = state->nextch - '0';
			advance_tok_stream(state);
			while(state->nextch >= '0' && state->nextch <= '7') {
				state->u.lit = (state->u.lit << 3) | (state->nextch - '0');
				advance_tok_stream(state); //consume next digit
			}
			return; // early return because we've been doing lookahead
		default: state->u.lit = state->nextch; break;
		}
		goto finish;
	}

	/* skip leading whitespace and comments */
	while(true) {
		if(state->nextch == '#') {
			in_comment = true;
		} else if(state->nextch == '\n') {
			in_comment = false;
		}
		if(! isspace_custom(state->nextch) && ! in_comment) {
			break;
		}
		advance_tok_stream(state);
	}

	switch(state->nextch) {
	case -1: /* end of input */
		state->type = TOK_END;
		break;
	case '"':
		state->type = TOK_LPAREN;
		state->in_string = true;
		break;
	case '(':
		state->type = TOK_LPAREN;
		break;
	case ')':
		state->type = TOK_RPAREN;
		break;
	case '.':
		state->type = TOK_DOT;
		break;
	default:
		/* read stream into symbol buffer and null-terminate */
		for(cursor = state->u.sym;
		    (ssize_t)(cursor-state->u.sym) < (ssize_t)(sizeof(state->u.sym)-1)
		     && is_atom_char(state->nextch);
		    cursor++) {
			if(state->nextch == -1) break;
			*cursor = (unsigned char) state->nextch;
			advance_tok_stream(state);
		}
		*cursor = 0; /* null terminate u.sym */
		/* discard rest of token that doesn't fit in symbol buffer */
		if(cursor - state->u.sym == (sizeof(state->u.sym) - 1)) {
			while(is_atom_char(state->nextch)) {
				advance_tok_stream(state);
			}
		}

		/* try to convert symbol buffer to a numeric literal */
		lit = strtoll_custom((char *) state->u.sym, (char **) &test_end);

		if(test_end == cursor) {
			/* whole string consumed -> numeric literal */
			state->type = TOK_LIT;
			state->u.lit = lit;
		} else {
			/* not numeric -> symbol */
			state->type = TOK_SYM;
		}
		return; // early return because we've been doing lookahead
	}
finish:
	advance_tok_stream(state);
}