Exemplo n.º 1
0
static xmlChar *
xmlPatScanNCName(xmlPatParserContextPtr ctxt) {
    const xmlChar *q, *cur;
    xmlChar *ret = NULL;
    int val, len;

    SKIP_BLANKS;

    cur = q = CUR_PTR;
    val = xmlStringCurrentChar(NULL, cur, &len);
    if (!IS_LETTER(val) && (val != '_'))
	return(NULL);

    while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
           (val == '.') || (val == '-') ||
	   (val == '_') ||
	   (IS_COMBINING(val)) ||
	   (IS_EXTENDER(val))) {
	cur += len;
	val = xmlStringCurrentChar(NULL, cur, &len);
    }
    ret = xmlStrndup(q, cur - q);
    CUR_PTR = cur;
    return(ret);
}
Exemplo n.º 2
0
QString Tellico::XML::elementName(const QString& name_) {
  QString name = name_;
  // change white space to dashes
  name.replace(QRegExp(QLatin1String("\\s+")), QLatin1String("-"));
  // first cut, if it passes, we're done
  if(XML::validXMLElementName(name)) {
    return name;
  }

  // next check first characters IS_DIGIT is defined in libxml/vali.d
  for(int i = 0; i < name.length() && (!IS_LETTER(name[i].unicode()) || name[i] == QLatin1Char('_')); ++i) {
    name = name.mid(1);
  }
  if(name.isEmpty() || XML::validXMLElementName(name)) {
    return name; // empty names are handled later
  }

  // now brute-force it, one character at a time
  int i = 0;
  while(i < name.length()) {
    if(!XML::validXMLElementName(name.left(i+1))) {
      name.remove(i, 1); // remember it's zero-indexed
    } else {
      // character is ok, increment i
      ++i;
    }
  }
  return name;
}
Exemplo n.º 3
0
/* Tokenise and stem a file */
static void
stemFile(FILE *file) {
  int character;
  int index;

  while (TRUE) {
    character = getc(file);

    if (character == EOF) {
      return;
    }

    if (IS_LETTER(character)) {
      index = 0;

      while (TRUE) {
        if (index == indexMax) {
          increaseValue();
        }

        character = tolower(character);

        value[index] = character;

        index++;

        character = getc(file);

        if (!IS_LETTER(character)) {
          ungetc(character, file);
          break;
        }
      }

      value[stem(value, 0, index - 1) + 1] = 0;

      /* The previous line calls the stemmer and
       * uses its result to zero-terminate the
       * string in `value`. */
      printf("%s", value);
    } else {
      putchar(character);
    }
  }
}
Exemplo n.º 4
0
/* try to match the next word. Returns a keyword if the next word is a keyword */
token_t lex_nextWord(LexState* ls, char c0)
{
	int cx = c0, i = 0;

	if (!IS_LETTER(cx)) {
		ungetc(cx, ls->src);
		return lex_error(ls, ERR_UNEXPECTED_INPUT);
	}

	ls->kf.name = (char*) (ls->kf.name ? realloc(ls->kf.name, NAME_SIZE_INIT) : malloc(NAME_SIZE_INIT));

	if (!ls->kf.name) {
		ls->error = ERR_OUT_OF_MEMORY;
		ls->kf.name_size = 0;
		return TK_NONE;
	}

	ls->kf.name_size = NAME_SIZE_INIT;

	do {
		ls->kf.name[i++] = cx;
		if (i >= ls->kf.name_size)
		{
			ls->kf.name = (char*)realloc(ls->kf.name, i+1);
			ls->kf.name_size = i+1;
		}
		cx = fgetc(ls->src);
	} while (cx != EOF && (IS_LETTER(cx) || IS_NUMBER(cx)));
	ungetc(cx, ls->src);
	ls->kf.name[i] = 0;
	int kw;
	for (kw=TOK_FIRST_KW; kw<=TOK_LAST_KW; kw++)
		if (strlen(lex_getKeywordString(kw)) == i && strncmp(ls->kf.name, lex_getKeywordString(kw), i) == 0)
			return kw;

	return TK_NAME;
}
Exemplo n.º 5
0
size_t uevent_split_base(const char *event, size_t event_len, char *props[], size_t size)
{
	size_t count = 0;
	const char *event_end = event + event_len;

	while (event < event_end && count < size) {
		if (IS_LETTER(*event)) {
			props[count++] = (char *) event;
		}

		while (*event++);
	}

	return count;
}
Exemplo n.º 6
0
int main()
{
	char *str = "  i  love  china , hello,wolrd ";
	char *p = str;
	int flag = F_SPACE;
	int counter = 0;
	
	for (p = str; '\0' != *p; p++) {
		if (IS_LETTER(*p)) {
			counter += flag;
			flag = F_LETTER;
		} else {
			flag = F_SPACE;
		}
	}

	printf("%d\n", counter);

	return 0;
}
Exemplo n.º 7
0
bool valid_email_addy( char * address )
{
    /* checks for simple email address, [email protected] valid, returns
       TRUE, otherwise, sends a message to ch, and stores it anyway, but
       ch->valid_email is set to false
    */

    char * checkme = address;
    bool valid = TRUE;

    for( ; *checkme != '\0'; checkme++ )
    {
        if (  ( IS_LETTER( *checkme ) )
            || ( *checkme == '1' )
            || ( *checkme == '2' )
            || ( *checkme == '3' )
            || ( *checkme == '4' )
            || ( *checkme == '5' )
            || ( *checkme == '6' )
            || ( *checkme == '7' )
            || ( *checkme == '8' )
            || ( *checkme == '9' )
            || ( *checkme == '0' )
            || ( *checkme == '.' )
            || ( *checkme == '@' )
            || ( *checkme == '-' )
            || ( *checkme == '_' )  )
            continue;
        else
        {
            valid = FALSE;
            break;
        }
    }
    return valid;
}
Exemplo n.º 8
0
static enum Status next_status(enum Status current_status, char ch)
{
  enum Status next_status;
  if ('\n' == ch)
    return STATUS_PUNCTUATION;
  else if (STATUS_COMMENTS == current_status)
    return STATUS_COMMENTS;

  if (IS_LETTER(ch))
    next_status = STATUS_LETTER;
  else if ('.' == ch)
    next_status = STATUS_PRAGMA;
  else if (IS_PUNCTUATION(ch))
    next_status = STATUS_PUNCTUATION;
  else if (IS_NUMBER(ch))
    next_status = STATUS_NUMBER;
  else if (IS_BLANK(ch))
    next_status = STATUS_BLANK;
  else if (IS_COMMENTS(ch))
    next_status = STATUS_COMMENTS;
  else
    next_status = STATUS_INVALID;
  return next_status;
}
Exemplo n.º 9
0
//CreateFile,open a device or file given it's name.
static __COMMON_OBJECT* _CreateFile(__COMMON_OBJECT* lpThis,  //IOManager object.
									LPSTR            lpszFileName,
									DWORD            dwAccessMode,
									DWORD            dwShareMode,
									LPVOID           lpReserved)
{
	CHAR FileName[512];
	__COMMON_OBJECT*    pFileHandle = NULL;

	if(NULL == lpszFileName)
	{
		return NULL;
	}
	if(StrLen(lpszFileName) > 511)  //File name too long.
	{
		return NULL;
	}
	if((lpszFileName[0] == 0) || 
	   (lpszFileName[1] == 0) ||
	   (lpszFileName[2] == 0)) //Target file name should has
		                       //at lease 3 characters.
	{
		return NULL;
	}

	//strcpy(FileName,lpszFileName);
	StrCpy(lpszFileName,FileName);
	ToCapital(FileName);  //Convert to capital.
	if(IS_LETTER(FileName[0]))  //Maybe a file object.
	{
		if(FileName[1] != ':')  //Invalid file system name.
		{
			return NULL;
		}
		if(FileName[2] != '\\') //Third character should specify root directory.
		{
			return NULL;
		}
		//A valid file name specified,so try to open it.
		pFileHandle = __OpenFile(lpThis,FileName,dwAccessMode,dwShareMode);
		if(NULL == pFileHandle)
		{
			if(FILE_OPEN_ALWAYS & dwAccessMode)  //Try to create one.
			{
				if(CreateNewFile(lpThis,FileName))  //Can create it.
				{
					pFileHandle = __OpenFile(lpThis,FileName,dwAccessMode,dwShareMode); //Try to open again.
				}
			}
		}
		return pFileHandle;
	}
	//The target name is not a file name,check if a device name.
	if((FileName[0] == '\\') && //For device name,the first 2 character should be '\'.
	   (FileName[1] == '\\') &&
	   (FileName[2] == '.' ))   //The third character should be a dot.
	{
		if(FileName[3] != '\\') //The 4th character also must be '\'.
		{
			return NULL;
		}
		//The name is a device name,try to open ti.
		return __OpenDevice(lpThis,FileName,dwAccessMode,dwShareMode);
	}
	return NULL;
}
void read_next_token(void) {
	if (token_has_been_peeked) {
		token_has_been_peeked = false;
		return;
	}

	uint32_t k = 0;
	do {
		if (buff[cursor] == 0) {
			cursor = 0;
			const char* res = fgets(buff, buff_size, input_source);
			if (res == 0) {
				*token = 0;
				next_token_type = END;
				return;
			}
		}
		bool is_quoted_string = false;
		while (buff[cursor] != 0) {
			if (IS_SPACE(buff[cursor]) && !is_quoted_string) {
				cursor += 1;
				break;
			}
			if (buff[cursor] == '\"') { // a " character is next
				is_quoted_string = !is_quoted_string;
				cursor += 1;
			} else if (buff[cursor] == '\\') { // a \ character is next
				switch(buff[cursor+1]) {
				case 'n':
					token[k] = '\n'; k += 1;
					break;
				case 't':
					token[k] = '\t'; k += 1;
					break;
				case '\\':
					token[k] = '\\'; k += 1;
					break;
				default:
					break;
				}
				cursor += 2;
			} else {
				token[k] = buff[cursor];
				k += 1;
				cursor += 1;
			}
		}
		token[k] = 0;
	} while (k == 0);

	while (buff[cursor] != 0 && IS_SPACE(buff[cursor])) {
		cursor += 1;
	}

	/* now, set the various parsing flags */
	if (IS_NUMBER(token[0])) {
		next_token_type = NUMBER;
		token_number_value = atoi(token);
	} else if (! IS_LETTER(token[0])) {
		next_token_type = SYMBOL;
	} else {
		next_token_type = NAME;
	}
}
Exemplo n.º 11
0
static void
xsltNumberFormatTokenize(xmlChar *format,
			 xsltFormatPtr tokens)
{
    int index = 0;
    int j;

    default_token.token = DEFAULT_TOKEN;
    default_token.width = 1;
    default_token.separator = BAD_CAST(DEFAULT_SEPARATOR);


    tokens->start = NULL;
    tokens->tokens[0].separator = NULL;
    tokens->end = NULL;

    /*
     * Insert initial non-alphanumeric token.
     * There is always such a token in the list, even if NULL
     */
    while (! (IS_LETTER(format[index]) || IS_DIGIT(format[index]))) {
	if (format[index] == 0)
	    break; /* while */
	index++;
    }
    if (index > 0)
	tokens->start = xmlStrndup(format, index);


    for (tokens->nTokens = 0; tokens->nTokens < MAX_TOKENS;
	 tokens->nTokens++) {
	if (format[index] == 0)
	    break; /* for */

	/*
	 * separator has already been parsed (except for the first
	 * number) in tokens->end, recover it.
	 */
	if (tokens->nTokens > 0) {
	    tokens->tokens[tokens->nTokens].separator = tokens->end;
	    tokens->end = NULL;
	}

	if (IS_DIGIT_ONE(format[index]) ||
		 IS_DIGIT_ZERO(format[index])) {
	    tokens->tokens[tokens->nTokens].width = 1;
	    while (IS_DIGIT_ZERO(format[index])) {
		tokens->tokens[tokens->nTokens].width++;
		index++;
	    }
	    if (IS_DIGIT_ONE(format[index])) {
		tokens->tokens[tokens->nTokens].token = format[index] - 1;
		index++;
	    }
	} else if (format[index] == (xmlChar)'A') {
	    tokens->tokens[tokens->nTokens].token = format[index];
	    index++;
	} else if (format[index] == (xmlChar)'a') {
	    tokens->tokens[tokens->nTokens].token = format[index];
	    index++;
	} else if (format[index] == (xmlChar)'I') {
	    tokens->tokens[tokens->nTokens].token = format[index];
	    index++;
	} else if (format[index] == (xmlChar)'i') {
	    tokens->tokens[tokens->nTokens].token = format[index];
	    index++;
	} else {
	    /* XSLT section 7.7
	     * "Any other format token indicates a numbering sequence
	     *  that starts with that token. If an implementation does
	     *  not support a numbering sequence that starts with that
	     *  token, it must use a format token of 1."
	     */
	    tokens->tokens[tokens->nTokens].token = (xmlChar)'0';
	    tokens->tokens[tokens->nTokens].width = 1;
	}
	/*
	 * Skip over remaining alphanumeric characters from the Nd
	 * (Number, decimal digit), Nl (Number, letter), No (Number,
	 * other), Lu (Letter, uppercase), Ll (Letter, lowercase), Lt
	 * (Letters, titlecase), Lm (Letters, modifiers), and Lo
	 * (Letters, other (uncased)) Unicode categories. This happens
	 * to correspond to the Letter and Digit classes from XML (and
	 * one wonders why XSLT doesn't refer to these instead).
	 */
	while (IS_LETTER(format[index]) || IS_DIGIT(format[index]))
	    index++;

	/*
	 * Insert temporary non-alphanumeric final tooken.
	 */
	j = index;
	while (! (IS_LETTER(format[index]) || IS_DIGIT(format[index]))) {
	    if (format[index] == 0)
		break; /* while */
	    index++;
	}
	if (index > j)
	    tokens->end = xmlStrndup(&format[j], index - j);
    }
}
Exemplo n.º 12
0
std::string read_user_format_number(std::ifstream &ifs, UserFormatColumn<DATA_T> &ufc,
                                    std::string &line, bool &need_readline, encode_result &encres, char* argv[] = 0)
{
    std::string ret, fm_buffer;
    DATA_T val = 0;
    char* c;

    ret.clear();

    while (std::getline(ifs, line)) {

        encres.line_nr++;
        encres.line_in_blk++;
        c = const_cast<char*>(line.c_str());
        fm_buffer.clear();

        if (IS_COMMENT(line))
            continue;

        /* end of FORMATs of a user block */
        if (IS_BLANK(line)) {

            need_readline = false;
            break;
        }

        while (!IS_EOL(*c) && !IS_EOS(*c)) {

            /* Meeting the next FORMAT, which means this format is done */
            if (IS_LETTER(*c)) {

                need_readline = false;
                break;

            } else if (IS_PLACEHOLD(*c)) {

                if (!TEMPLATE_MODE)
                    throw E2promValueException("Placehold should be used in template file",
                                               to_string<int>(encres.line_nr), usage_allowed_user_format_type);

                E2promMsg("Reading parameter for user "+to_string<uint16_t>(ufc.uheader.ID)+
                          " @ line "+to_string<int>(encres.line_nr));

                read_user_parameters(fm_buffer, ufc.uheader.ID, encres, argv);
                break;

            } else if (IS_DIGIT(*c) || IS_PERIOD(*c)) {

                fm_buffer += *c++;

            } else if (IS_SPACE(*c) || (IS_COMMENT_SIGN(*c))) {

                break;

            } else {

                throw E2promValueException(
                    "Unexpected characters found for type",
                    to_string<int>(encres.line_nr),
                    usage_allowed_user_format_type
                );
            }
        } /* end of read-char while-loop */

//		if (TEMPLATE_MODE)
//			process_fm_buffer(fm_buffer, ufc.uheader.ID);

        if (!fm_buffer.empty()) {

            val = to_digits<DATA_T>(fm_buffer);
            ufc += val;
        }

        if (!IS_LETTER(*c) && !IS_BLANK(line)) {

            need_readline = true;

        } else {

            break;
        }
    } /* end of read-line while-loop */

//		ret += strize_formats<DATA_T>(ufc);
    std::stringstream ss;
//	ss.width(decres.GLOBAL_ALIGNMENT);
    ss << ufc;

    return ss.str();
}
Exemplo n.º 13
0
static int
resToClassNew(char *resReq, struct resVal *resVal, struct lsInfo *lsInfo)
{
    int i, s, t, len, entry, hasFunction = FALSE, hasQuote;
    char res[MAXLSFNAMELEN], val[MAXLSFNAMELEN];
    char tmpbuf[MAXLSFNAMELEN*2];
    char *sp, *op;

    len = strlen(resReq);

    sp = resVal->selectStr;
    strcpy(sp,"expr ");
    s = 0;
    t = strlen(sp);

    while (s < len) {
        if (t >= (resVal->selectStrSize - MAXLSFNAMELEN))
            return PARSE_BAD_EXP;


        if (resReq[s] == ' ') {
            s++;
            continue;
        }

        if ( resReq[s] == '(' || resReq[s] == ')' || resReq[s] == '=' ||
             resReq[s] == '!' || resReq[s] == '>' || resReq[s] == '<' ||
             resReq[s] == '|' || resReq[s] == '&' || resReq[s] == '+' ||
             resReq[s] == '-' || resReq[s] == '*' || resReq[s] == '/' ||
             resReq[s] == '.' || IS_DIGIT(resReq[s])) {

            sp[t++] = resReq[s++];
            continue;
        }

        if (IS_LETTER(resReq[s])) {

            i = 0;
            while(IS_LETTER(resReq[s]) || IS_DIGIT(resReq[s]) ||
                  IS_VALID_OTHER(resReq[s]))
                res[i++] = resReq[s++];
            res[i] = '\0';

            entry = getResEntry(res);

            if (entry < 0) {
                if (strncmp ("defined", res, strlen (res)) == 0) {
                    while (resReq[s] == ' ')
                        s++;
                    if (resReq[s] != '(')
                        return (PARSE_BAD_EXP);
                    i = 0;
		    s++;
                    while (IS_LETTER(resReq[s]) || IS_DIGIT(resReq[s]) ||
                           IS_VALID_OTHER(resReq[s]))
                        res[i++] = resReq[s++];
                    res[i] = '\0';
                    entry = getResEntry(res);
                    if (entry < 0)
                        return PARSE_BAD_NAME;
		    sprintf(tmpbuf,"[%s \"%s\" ]",
                            "defined", lsInfo->resTable[entry].name);
                    sp[t] = '\0';
                    strcat(sp,tmpbuf);
                    t += strlen(tmpbuf);
                    while (resReq[s] == ' ')
                        s++;
                    if (resReq[s] != ')')
                        return (PARSE_BAD_EXP);
                    s++;
                    continue;
                }
                return PARSE_BAD_NAME;
            }
            switch(lsInfo->resTable[entry].valueType) {
                case LS_NUMERIC:
                case LS_BOOLEAN:
                    strcat(res,"()");
                    sp[t] = '\0';
                    strcat(sp,res);
                    t += strlen(res);
                    break;
                case LS_STRING:

                    while(resReq[s] == ' ')
                        s++;


                    if (resReq[s] == '\0' || resReq[s+1] == '\0')
                        return(PARSE_BAD_EXP);


                    op = NULL;
                    if (resReq[s] == '=' && resReq[s+1] == '=') {
                        op = "eq";
                        s += 2;
                    } else if (resReq[s] == '!' && resReq[s+1] == '=') {
                        op = "ne";
                        s += 2;
                    } else if (resReq[s] == '>' && resReq[s+1] == '=') {
                        op = "ge";
                        s += 2;
                    } else if (resReq[s] == '<' && resReq[s+1] == '=') {
                        op = "le";
                        s += 2;
                    } else if (resReq[s] == '<') {
                        op = "lt";
                        s += 1;
                    } else if (resReq[s] == '>') {
                        op = "gt";
                        s += 1;
                    } else {
                        return -1;
                    }

                    while(resReq[s] == ' ')
                        s++;

                    if (resReq[s] == '\''){
                        hasQuote = TRUE;
                        s++;
                    } else
                        hasQuote = FALSE;
                    i = 0;
                    if (!hasQuote){
                        while(IS_LETTER(resReq[s]) || IS_DIGIT(resReq[s]) ||
                              IS_VALID_OTHER(resReq[s]))
                            val[i++] = resReq[s++];
                    } else {
                        while(resReq[s] && resReq[s] != '\'' && i < MAXLSFNAMELEN)
                            val[i++] = resReq[s++];
                        if (i-1 == MAXLSFNAMELEN)
                            return(PARSE_BAD_VAL);
                        if (resReq[s] == '\'')
                            s++;
                    }
                    val[i] = '\0';
                    if (i == 0) {
                        return(PARSE_BAD_VAL);
                    }

                    if (validValue(val, lsInfo, entry) < 0) {
                        return(PARSE_BAD_VAL);
                    }

                    sprintf(tmpbuf,"[%s \"%s\" \"%s\"]",lsInfo->resTable[entry].name
                            ,op,val);
                    sp[t] = '\0';
                    strcat(sp,tmpbuf);
                    t += strlen(tmpbuf);
                default:
                    break;
            }
	    hasFunction = FALSE;
        } else {
            return (PARSE_BAD_EXP);
        }

    }
    sp[t] = '\0';
    resVal->options |= PR_SELECT;
    return(PARSE_OK);
}
Exemplo n.º 14
0
//********************************************************************
Bool WordsControl(
		long reason	// См. enum BROWSE_REASON
		)
{
// Слежение за словами и строками
// Вызывается после записи символа или конца строки
// или после иной обработки

	switch(reason)
		{
		case BROWSE_PARAGRAPH_START:
			// Начало абзаца
			gLastEOL = NULL;
			gBegWord = NULL;
			gDefis = NULL;
			gCharBack = NULL;
			break;

		case BROWSE_PARAGRAPH_END:
			// Конец абзаца
			// Обработать конец слова если есть
			if ( gBegWord && !WordEnd() )
				return FALSE;

			gLastEOL = NULL;
			gBegWord = NULL;
			gDefis = NULL;
			gCharBack = NULL;
			break;

		case BROWSE_LINE_END:
			// Конец строки
			// Запоминается адрес до записи конца строки
			// или пробела в начале следующей строки
			if ( gCharBack && IsEOL(*gCharBack) )
				gLastEOL = gCharBack;
			else
				gLastEOL = gMemCur;
			break;

		case BROWSE_LINE_START:
			// Начало строки
			{
			Byte c = *(gMemCur-1);	// Предыдущий символ
			if (gLastEOL &&			// Предыдущая строка
				c &&				// Не кончается нулем
				!IsEOL(c) &&		// Нет конца строки текста
				!(gFormat==ROUT_FMT_HTML && // Нет конца строки HTML
				  !memcmp("<br>",(char*)gMemCur-4,4)
				 ) &&
				c != ' '			// На конце не пробел
				)
				{
				// Вставить пробел перед новой строкой
				*gMemCur++ = ' ';
				}
			}
			break;

		case BROWSE_CHAR:
			// Символ

			// Буква
			if (IS_LETTER(gCharCode))
				{
				// Начало слова
				if(!gBegWord)
					gBegWord = gCharBack;
				}
			// Не буква. А слово есть.
			else if (gBegWord)
				{
				// Дефис
				if (gCharCode == '-')
					{
					Byte c = *(gMemCur-2);	// Предыдущий символ
					if ( c == '-' )
						{
						// Два дефиса подряд это тире
						if ( gDefis == gMemCur-2 )
							gDefis = 0;
						}
					else
						{
						// Скрытый дефис в "мягкой" строке может быть удален
						if ( gEdCharHidden &&
							!(gPreserveLineBreaks || gEdLineHardBreak)
						   )
 							gDefis = gCharBack;
						}
					}

				// Конец слова
				else
					{
					// Обработать конец слова
					if ( !WordEnd() )
						return FALSE;
					}
				}

			break;

		}

	return TRUE;
}
Exemplo n.º 15
0
int tokenize(struct token_list* tk_list, char* file_buffer)
{
  enum Status status;
   line_num;
  size_t token_begin, token_end;
  token_begin = 0, token_end  = 0;
  status = STATUS_INVALID;
  str_toupper(file_buffer);
  /*
   * Careful: it seems an error to let "i <= len", 
   * but we need one more execution to flush the last token into token list.
   */
  size_t line_num = 1;
  for (size_t i = 0; ; ++i) {
    struct token_node* tok_node;
    switch (status) {
    case STATUS_LETTER:
      if (!IS_LETTER(file_buffer[i]) && !IS_DIGIT(file_buffer[i])) {
        token_end = i;
        tok_node = create_token(TOKEN_LABEL, file_buffer + token_begin, token_end - token_begin);
        tok_node->type = letter_type(tok_node->liter, tok_node->len);
        token_append(tk_list, tok_node);
        token_begin = i;
        status = next_status(status, file_buffer[i]);
      }
      break;
    case STATUS_PRAGMA:
      if (!IS_LETTER(file_buffer[i])) {
        int type;
        token_end = i;
        type = pragma_type(file_buffer + token_begin, token_end - token_begin);
        if (type < 0) {
          error("invalid pragma ad line %d\n", line_num);
          return -4;
        }
        tok_node = create_token(type, file_buffer + token_begin, token_end - token_begin);
        token_append(tk_list, tok_node);
        token_begin = i;
        status = next_status(status, file_buffer[i]);
      }
      break;
    case STATUS_PUNCTUATION:
      token_end = i;
      tok_node = create_token(file_buffer[token_begin], file_buffer + token_begin, token_end - token_begin);
      token_append(tk_list, tok_node);
      token_begin = i;
      status = next_status(status, file_buffer[i]);
      break;
    case STATUS_NUMBER:
      if (!IS_NUMBER(file_buffer[i])) {
        token_end = i;
        if (!check_number(file_buffer + token_begin, token_end - token_begin)) {
          error("invalid number format at line %d\n", line_num);
          return -2;
        }
        tok_node = create_token(TOKEN_NUMBER, file_buffer + token_begin, token_end - token_begin);
        tok_node->data = parse_number(tok_node->liter);
        token_append(tk_list, tok_node);
        token_begin = i;
        status = next_status(status, file_buffer[i]);
      }
      break;
    case STATUS_BLANK:
      if (!IS_BLANK(file_buffer[i])) {
        token_begin = i;
        status = next_status(status, file_buffer[i]);
      }
      break;
    case STATUS_COMMENTS:
      //once status is in comments, it will always be in comments
      if ('\n' == file_buffer[i]) {
        token_begin = i;
        status = next_status(status, file_buffer[i]);
      }
      break;
    case STATUS_INVALID:
      token_begin = i;
      status = next_status(status, file_buffer[i]);
      if (STATUS_INVALID == status && 0 != file_buffer[i]) {
        error("invalid format at line %d\n", line_num);
        return -3;
      }
      break;
    }
    if (0 == file_buffer[i])
      break;
    else if ('\n' == file_buffer[i])
      ++line_num;
  }
  return 0;
}
Exemplo n.º 16
0
// this function takes a string containing an arithmetic 
// expression in infix notation and compiles it into an 
// internal reverse polish notation representation.
// which can then easily be evaluated (and re-evaluated)
// This is done by first tokenizing the string 
// and then parse the tokens, converting 
// the infix notation to rpn.
bool Expression::compile(const char *string) 
{
	const char *src = string;
	Vector<Token> tokens;

	// tokenize it and parse and convert numbers.
	while(*src) 
	{
		if(IS_WHITESPACE(*src)) 
		{
			src++;
			continue;
		}
		switch(*src) 
		{
		case '(': tokens.add(Token(LPAREN)); break;
		case ')': tokens.add(Token(RPAREN)); break;
		case '-': tokens.add(Token(MINUS)); break;
		case '+': tokens.add(Token(PLUS)); break;
		case '*': tokens.add(Token(MUL)); break;
		case '/': tokens.add(Token(DIV)); break;
		default:
			if(IS_NUMBER(*src)) 
				{
					char num[32];
					num[0] = *src;
					src++;
					char *num_dest = num+1;
					int numDots = 0;
					while(IS_NUMBER(*src)||*src=='.')
					{
						if(*src=='.')
						{
							numDots++;
							if(numDots>1)
							{
								// error
								printf("wrong amount of dots in constant number.");
								return false;
							}
						}

						*num_dest = *src;
						num_dest++;
						src++;
					}
					*num_dest = 0;
					float i = atof(num);
					tokens.add(Token(NUMBER, i));
					continue;
				}
			else if(IS_LETTER(*src)) 
			{
				char litteral[255];
				litteral[0] = *src;
				src++;
				char *litteral_dest = litteral+1;
				while(IS_LETTER(*src)||IS_NUMBER(*src)) 
				{
					*litteral_dest = *src;
					litteral_dest++;
					src++;
				}
				*litteral_dest = 0;

				while(IS_WHITESPACE(*src)) src++;

				if(*src=='(') 
				{
					tokens.add(Token(FUNCTION, scope.getFunction(litteral)));
					src++;
				} 
				else 
				{
					tokens.add(Token(VARIABLE, scope.getVariable(litteral)));
				}

				continue;					
			}
			else return false;
		}
		src++;
	}

	// compile! this is done recursivly 
	rpn.clear();
	_compile(0, tokens, rpn);

	rpn.add(Token(END));
	return true;
}