int get_token()
{
	int tok;
	char *temp;
	(token_type) = 0;
	tok = 0;
	temp = (token);

	if (*(prog) == '\0')
	{
		*(token) = 0;
		tok = FINISHED;
		return ((token_type) = DELIMITER);
	}
	while (iswhite(*(prog))) ++(prog);
	if (isdelim(*(prog)))
	{
		char t=*temp = *(prog);
		(prog)++;
		temp++;
		if ((t == '>' || t == '<' || t == '!') && (*prog) && (*prog == '='))
		{
			*temp = *(prog);
			(prog)++;
			temp++;
		}
		*temp = 0;
		return ((token_type) = DELIMITER);
	}
	if (isdigit(*(prog)))
	{
		while (!isdelim(*(prog)))
			*temp++=*(prog)++;
		*temp = '\0';
		return ((token_type) = NUMBER);
	}
	if (isalpha2(*(prog)))
	{
		while (!isdelim(*(prog)))
			*temp++=*(prog)++;
		(token_type) = VARIABLE;
	}
	*temp = '\0';
	if ((token_type) == VARIABLE)
	{
		tok = look_up((token));
		if (tok)
			(token_type) = FUNCTION;
	}
	return (token_type);
}
Exemple #2
0
static void
outwinname(Biobuf *b, Hist *h, char *ds, char *p)
{
	if(isdelim(p[0])) {
		// full rooted name
		zfile(b, ds, 3);	// leading "c:/"
		outzfile(b, p+1);
	} else {
		// relative name
		if(h->offset == 0 && pathname && pathname[1] == ':') {
			if(tolowerrune(ds[0]) == tolowerrune(pathname[0])) {
				// using current drive
				zfile(b, pathname, 3);	// leading "c:/"
				outzfile(b, pathname+3);
			} else {
				// using drive other then current,
				// we don't have any simple way to
				// determine current working directory
				// there, therefore will output name as is
				zfile(b, ds, 2);	// leading "c:"
			}
		}
		outzfile(b, p);
	}
}
Exemple #3
0
// ([0-9]|(~!@#$%^&*-+=\/)|[a-z])*[a-z]+([0-9]|(~!@#$%^&*-+=\/)|[a-z])
int read_symbol(FILE *in, struct value *val)
{
  char c;
  const char valid_chars[] = "~!@#$%^&*-+=/\\";
  unsigned int i, non_numerical_seen = 0;

  for (i = 0; i < MAX_SYMBOL_SIZE; i++) {
    c = fgetc(in);
    if (isdelim(c)) {
      break;
    } else if (!(isalnum(c) || strchr(valid_chars, c))) {
      val->symbol[i+1] = 0;
      ungets(val->symbol, in);
      return READ_INVALID_CHAR;
    } else if (!isdigit(c) && !non_numerical_seen) {
      non_numerical_seen = 1;
    }
    val->symbol[i] = c;
  }
  val->symbol[i] = 0;
   
  if (!val->symbol[0] || !non_numerical_seen) {
    ungets(val->symbol, in);
    return READ_FAIL;
  } else {
    val->type = SYMBOL;
    return READ_SUCCESS;
  }
}
Exemple #4
0
static void tokeniser_advance(Tokeniser* t)
{
    ASSERT(!tokeniser_eof(t), "next() on empty tokeniser");
    SET(t->tokenIndex, nil_make());

    skipSpace(t);
    if (*t->cursor == 0) {
        t->eof = 1;
        return;
    }

    const char* punctChars = "()'";
    for (int i = 0; punctChars[i] != 0; i++) {
        if (*t->cursor == punctChars[i]) {
            t->cursor++;
            SET(t->tokenIndex, makeToken(&punctChars[i], 1));
            return;
        }
    }

    if (*t->cursor == '"') {

        // Find out how long the escaped string is, and check it was terminated
        // properly.
        int length = 0;
        t->cursor++; // advance past the initial "
        const char* end = stringEscape(t->cursor, countChars, &length);
        if (*end != '"') {
            THROW("Expected \", got EOF");
        }

        // Now allocate a string and copy it in.
        Pointer ptr = string_alloc(length);
        char* dest = (char*) string_get(ptr);
        stringEscape(t->cursor, copyChars, &dest);
        *dest = 0;

        // Advance the cursor past the string.
        t->cursor = end + 1;

        SET(t->tokenIndex, ptr);
    }
    else {
        // Scan out a token
        const char* start = t->cursor;
        while (1) {
            char c = *t->cursor;
            if ((c == 0) || isspace(c) || isdelim(c)) {
                int length = t->cursor - start;
                SET(t->tokenIndex, makeToken(start, length));
                break;
            }
            // Advance the cursor now, not earlier.
            t->cursor++;
        }
    }
}
Exemple #5
0
static void
outhist(Biobuf *b)
{
	Hist *h;
	char *p, ds[] = {'c', ':', '/', 0};

	for(h = hist; h != H; h = h->link) {
		p = h->name;
		if(p) {
			if(windows) {
				// if windows variable is set, then, we know already,
				// pathname is started with windows drive specifier
				// and all '\' were replaced with '/' (see lex.c)
				if(isdelim(p[0]) && isdelim(p[1])) {
					// file name has network name in it, 
					// like \\server\share\dir\file.go
					zfile(b, "//", 2);	// leading "//"
					outzfile(b, p+2);
				} else if(p[1] == ':') {
					// file name has drive letter in it
					ds[0] = p[0];
					outwinname(b, h, ds, p+2);
				} else {
					// no drive letter in file name
					outwinname(b, h, pathname, p);
				}
			} else {
				if(p[0] == '/') {
					// full rooted name, like /home/rsc/dir/file.go
					zfile(b, "/", 1);	// leading "/"
					outzfile(b, p+1);
				} else {
					// relative name, like dir/file.go
					if(h->offset == 0 && pathname && pathname[0] == '/') {
						zfile(b, "/", 1);	// leading "/"
						outzfile(b, pathname+1);
					}
					outzfile(b, p);
				}
			}
		}
		zhist(b, h->line, h->offset);
	}
}
Exemple #6
0
/*
 * Find the next delimiter/non-delimiter within p
 *  Honor quotes and leading option characters
 */
static char *find(char *p, int delim)
{ int ch, quote;
  int isopt;

  assert(p);

#define isdelim(ch) (isopt? isoptdelim(ch): isargdelim(ch))

  if((isopt = isoption(p)) != 0 && delim) {
    /* Assume the following example:
        p == "arg/opt"
      first find("arg/opt", 1) == find next delimiter
      returns pointer to "/opt"
      Now the process would call skip delimiters to reach the
      next argument --> find("/opt", 0) == find next non-delimiter
      will return "/opt", because '/' is delimiter only when
      searching for delimiters.
      Now the process would call skip non-delimiters to reach the
      end of the arg --> find("/opt", 1) == find next delimiter
      This time the leading optch's are part of the argument.
      ===> This is performed in this branch.

      If the string would be "/opt1/opt2", the call of
      find("/opt1/opt2", 1) == find next delimiter must stop
      at the second '/'.
    */
    while((ch = *++p) != 0 && isoptch(ch));
  }

  quote = 0;
  while((ch = *p++) != '\0' && (quote
   || (delim && !(isdelim(ch) || isoptch(ch)))
   || (!delim && isdelim(ch))))
    if(quote == ch)
      quote = 0;
    else if(strchr(QUOTE_STR, ch))
      quote = ch;

  return p - 1;
#undef isdelim
}
Exemple #7
0
void get_token(void)
{
	register char *temp;
	
	tok_type=0;
	temp=token;
	*temp='\0';
	
	if(!*p) return;
	
	while(isspace(*p)) ++p;
	
	if(isalpha(*p)) {
		while(!isdelim(*p)) *temp++=*p++;
		tok_type=STRING;
	} else if(isdigit(*p) || *p=='-') {
		while(!isdelim(*p)) *temp++=*p++;
		tok_type= NUMBER;
	}

	*temp='\0';	

}
Exemple #8
0
static UINT32 suck_number(const UINT8 **psrc)
{
	const UINT8 *src = *psrc;
	UINT32 value = 0;

	/* skip delimiters */
	while (isdelim(*src))
		src++;

	/* loop over and accumulate digits */
	while (isdigit(*src))
	{
		value = value * 10 + *src - '0';
		src++;
	}

	/* return a pointer to the string afterwards */
	*psrc = src;
	return value;
}
Exemple #9
0
static int Parse()
{
   char* t;

   type = 0;
   t = token;
   while( iswhite( *expression ) )
      expression++;
   if( isdelim( *expression ) )
   {
      type = DEL;
      *t++ = *expression++;
   }
   else if( isnumer( *expression ) )
   {
      type = NUM;
      while( isnumer( *expression ) )
         *t++ = *expression++;
   }
   else if( isalpha( *expression ) )
   {
      type = VAR;
      while( isalpha( *expression ) )
        *t++ = *expression++;
      token[VARLEN] = 0;
   }
   else if( *expression )
   {
      *t++ = *expression++;
      *t = 0;
      return E_SYNTAX ;
   }
   *t = 0;
   while( iswhite( *expression ) )
      expression++;

   return E_OK;
}
Exemple #10
0
/* (+|-|/0)[0-9]+ where /0 is a null token */
int read_fixnum(FILE *in, struct value *val)
{
  char c;
  char buf[BUF_LEN] = {0};
  unsigned int i = 0, neg = 0;

  c = fgetc(in);

  if ('-' == c) {
    neg = 1;
  } else if (isdigit(c)) {
    buf[i++] = c;
  } else if ((EOF == c) || (c != '+')) {
    ungetc(c, in);
    return READ_FAIL;
  }
    
  while ((EOF != (c = fgetc(in))) && (i < BUF_LEN)) {
    if (isdigit(c)) { 
      buf[i] = c;
    } else if (isdelim(c)) {
      ungetc(c, in);
      break;
    } else {
      ungets(buf, in);
      return READ_INVALID_CHAR;
    }
    i++;
  }

  if (buf[0] != 0) {
    val->type = FIXNUM;
    val->fixnum = atoi(buf) * (neg ? -1 : 1);
    return READ_SUCCESS;
  } else {
    return READ_FAIL;
  }
}
Exemple #11
0
static void Parse(void)
{
   char* t;

   type = 0;
   t = (char*)token;
   while( iswhite( *expression ) )
      expression++;
   if( isdelim( *expression ) )
   {
      type = DEL;
      *t++ = *expression++;
   }
   else if( isnumer( *expression ) )
   {
      type = NUM;
      while( isnumer( *expression ) )
         *t++ = *expression++;
   }
   else if( isalpha( *expression ) )
   {
      type = VAR;
      while( isalpha( *expression ) )
        *t++ = *expression++;
      token[VARLEN] = 0;
   }
   else if( *expression )
   {
      *t++ = *expression++;
      *t = 0;
      ERR( E_SYNTAX );
   }
   *t = 0;
   while( iswhite( *expression ) )
      expression++;
}
Exemple #12
0
int jed_parse(const void *data, size_t length, jed_data *result)
{
	const UINT8 *cursrc = (const UINT8 *)data;
	const UINT8 *srcend = cursrc + length;
	const UINT8 *scan;
	jed_parse_info pinfo;
	UINT16 checksum;
	int i;

	/* initialize the output and the intermediate info struct */
	memset(result, 0, sizeof(*result));
	memset(&pinfo, 0, sizeof(pinfo));

	/* first scan for the STX character; ignore anything prior */
	while (cursrc < srcend && *cursrc != 0x02)
		cursrc++;
	if (cursrc >= srcend)
		return JEDERR_INVALID_DATA;

	/* then scan to see if we have an ETX */
	scan = cursrc;
	checksum = 0;
	while (scan < srcend && *scan != 0x03)
		checksum += *scan++ & 0x7f;
	if (scan >= srcend)
		return JEDERR_INVALID_DATA;

	/* see if there is a transmission checksum at the end */
	checksum += *scan;
	if (scan + 4 < srcend && ishex(scan[1]) && ishex(scan[2]) && ishex(scan[3]) && ishex(scan[4]))
	{
		UINT16 dessum = (hexval(scan[1]) << 12) | (hexval(scan[2]) << 8) | (hexval(scan[3]) << 4) | hexval(scan[4] << 0);
		if (dessum != 0 && dessum != checksum)
			return JEDERR_BAD_XMIT_SUM;
	}

	/* the ETX becomes the real srcend */
	srcend = scan;

	/* blast through the comment field */
	cursrc++;
	while (cursrc < srcend && *cursrc != '*')
		cursrc++;

	/* now loop over fields and decide which ones go in the file output */
	cursrc++;
	while (cursrc < srcend)
	{
		/* skip over delimiters */
		while (cursrc < srcend && isdelim(*cursrc))
			cursrc++;
		if (cursrc >= srcend)
			break;

		/* end of field is an asterisk -- find it */
		scan = cursrc;
		while (scan < srcend && *scan != '*')
			scan++;
		if (scan >= srcend)
			return JEDERR_INVALID_DATA;

		/* process the field */
		process_field(result, cursrc, scan, &pinfo);

		/* advance past it */
		cursrc = scan + 1;
	}

	/* if we got an explicit fuse count, override our computed count */
	if (pinfo.explicit_numfuses != 0)
		result->numfuses = pinfo.explicit_numfuses;

	/* clear out leftover bits */
	if (result->numfuses % 8 != 0)
		result->fusemap[result->numfuses / 8] &= (1 << (result->numfuses % 8)) - 1;
	memset(&result->fusemap[(result->numfuses + 7) / 8], 0, sizeof(result->fusemap) - (result->numfuses + 7) / 8);

	/* validate the checksum */
	checksum = 0;
	for (i = 0; i < (result->numfuses + 7) / 8; i++)
		checksum += result->fusemap[i];
	if (pinfo.checksum != 0 && checksum != pinfo.checksum)
		return JEDERR_BAD_FUSE_SUM;

	return JEDERR_NONE;
}
Exemple #13
0
/* Get a bas_token. */
int get_token(){

   char *temp;

  bas_token_type=0; bas_tok=0;
  temp=bas_token;

  //    printf( "gettoken: <%c>\n", *prog );

  if(*prog=='\0') { /* end of file */
    *bas_token=0;
    bas_tok = FINISHED;
    return(bas_token_type=DELIMITER);
  }

  while(iswhite(*prog)) ++prog;  /* skip over white space */
  /*
  if(*prog=='\r') { /
    ++prog; ++prog;
    bas_tok = EOL; *bas_token='\r';
    bas_token[1]='\n'; bas_token[2]=0;
    return (bas_token_type = DELIMITER);
  }

*/
    if(*prog=='\n') { // lf 
    ++prog;
    //currentline++; // just for debug
    bas_tok = EOL; *bas_token='\n';
    bas_token[1]=0; 
    // printf("...EOL found ...%s" , "");
    return (bas_token_type = DELIMITER);
  }


  if(*prog=='#') { /* lf */
    ++prog;
    bas_tok = COMMENT; *bas_token='#';
    bas_token[1]=0;
    return (bas_token_type = DELIMITER);
  }



 
  if(strchr("!+-*^/%=;(),><&|", *prog)){ /* delimiter */
    *temp=*prog;

    //if there is NOT double delimiter
    if(strchr("+-*^/%;(),", *prog)){ /*  delimiter */
      // if  (strchr("(", *prog)){ printf("delim found %c \n", *prog); }
      //  if  (strchr(")", *prog)){ printf("delim found %c \n", *prog); }
      prog++; /* advance to next position */
      temp++;
      *temp=0; 
       return (bas_token_type=DELIMITER);
    }// was single delimiter
 




    /*  DOUBLE DELIMITERS WILL BE SINGLFIED.......HERE
     *
     */
    // printf("case of dd may come <%c> <%c>\n", *prog, prog[1] );

    if((strchr("=", *prog))&&(strchr("=",prog[1]) )){ /* double delimiter   == is ~  */
      bas_token[0]='~'; bas_token[1]='\0';
      prog++; /* advance to next position */
      temp++;
        prog++; /* advance to next position */
      temp++;
    *temp=0; 
    return (bas_token_type=DELIMITER);
  }

    if((strchr("!", *prog))&&(strchr("=",prog[1]) )){ /* double delimiter   != is !  */
      bas_token[0]='!'; bas_token[1]='\0';
      prog++; /* advance to next position */
      temp++;
       prog++; /* advance to next position */
      temp++;
    *temp=0; 
    return (bas_token_type=DELIMITER);
   }

    if((strchr("&", *prog))&&(strchr("&",prog[1]) )){ /* double delimiter && is @ */
      bas_token[0]='@'; bas_token[1]='\0';
      prog++; /* advance to next position */
      temp++;
      prog++; /* advance to next position */
      temp++; 
    *temp=0; 
    return (bas_token_type=DELIMITER);
    }

    if((strchr("|", *prog))&&(strchr("|",prog[1]) )){ /* double delimiter  || is $*/
      bas_token[0]='$'; bas_token[1]='\0';
      prog++; /* advance to next position */
      temp++;
        prog++; /* advance to next position */
      temp++;
    *temp=0; 
    return (bas_token_type=DELIMITER);
  }

    if((strchr(">", *prog))&&(strchr("=",prog[1]) )){ /* double delimiter  >= is :*/
      bas_token[0]=':'; bas_token[1]='\0';
      prog++; /* advance to next position */
      temp++;
        prog++; /* advance to next position */
      temp++;
    *temp=0; 
    return (bas_token_type=DELIMITER);
  }

    if((strchr("<", *prog))&&(strchr("=",prog[1]) )){ /* double delimiter  <= is _ */
      bas_token[0]='_'; bas_token[1]='\0';
      prog++; /* advance to next position */
      temp++;
        prog++; /* advance to next position */
      temp++;
    *temp=0; 
    return (bas_token_type=DELIMITER);
  }


    
      prog++; /* advance to next position */
      temp++;
    *temp=0; 
    return (bas_token_type=DELIMITER);
  }// DELIMITERS - DOUBLE DELIMITERS-------------------END-----DOUBLE
    


  /*   20100419 - now  |  and  &  are unabiguous
   *
   */

    //if there is NOT double delimiter
    if(strchr("|&", *prog)){ /*  delimiter */
      // if  (strchr("(", *prog)){ printf("delim found %c \n", *prog); }
      //  if  (strchr(")", *prog)){ printf("delim found %c \n", *prog); }
      prog++; /* advance to next position */
      temp++;
      *temp=0; 
       return (bas_token_type=DELIMITER);
    }// was single delimiter
 







  if(*prog=='"') { /* quoted string */
    prog++;
    while(*prog!='"'&& *prog!='\n') *temp++=*prog++;
    if(*prog=='\n') serror(1);
    prog++;*temp=0;
    return(bas_token_type=QUOTE);
  }
  

  if(isdigit(*prog)) { /* number */
    while(!isdelim(*prog)) *temp++=*prog++;
    *temp = '\0';
    // printf("temp=%s\n", bas_token);
    return(bas_token_type = NUMBER);
  }


  if(isalpha(*prog)) { /* var or command */
    while(!isdelim(*prog)) *temp++=*prog++;
    bas_token_type=STRING;
  }

  *temp = '\0';

  /* see if a string is a command or a variable */
  if(bas_token_type==STRING) {
    bas_tok=look_up(bas_token); /* convert to internal rep */
    if(!bas_tok) bas_token_type = VARIABLE;
    else bas_token_type = COMMAND; /* is a command */
  }
  return bas_token_type;
}//------------------------get_token
Exemple #14
0
static void
outhist(Biobuf *b)
{
	Hist *h;
	char *p, ds[] = {'c', ':', '/', 0};
	char *tofree;
	int n;
	static int first = 1;
	static char *goroot, *goroot_final;

	if(first) {
		// Decide whether we need to rewrite paths from $GOROOT to $GOROOT_FINAL.
		first = 0;
		goroot = getenv("GOROOT");
		goroot_final = getenv("GOROOT_FINAL");
		if(goroot == nil)
			goroot = "";
		if(goroot_final == nil)
			goroot_final = goroot;
		if(strcmp(goroot, goroot_final) == 0) {
			goroot = nil;
			goroot_final = nil;
		}
	}

	tofree = nil;
	for(h = hist; h != H; h = h->link) {
		p = h->name;
		if(p) {
			if(goroot != nil) {
				n = strlen(goroot);
				if(strncmp(p, goroot, strlen(goroot)) == 0 && p[n] == '/') {
					tofree = smprint("%s%s", goroot_final, p+n);
					p = tofree;
				}
			}
			if(windows) {
				// if windows variable is set, then, we know already,
				// pathname is started with windows drive specifier
				// and all '\' were replaced with '/' (see lex.c)
				if(isdelim(p[0]) && isdelim(p[1])) {
					// file name has network name in it, 
					// like \\server\share\dir\file.go
					zfile(b, "//", 2);	// leading "//"
					outzfile(b, p+2);
				} else if(p[1] == ':') {
					// file name has drive letter in it
					ds[0] = p[0];
					outwinname(b, h, ds, p+2);
				} else {
					// no drive letter in file name
					outwinname(b, h, pathname, p);
				}
			} else {
				if(p[0] == '/') {
					// full rooted name, like /home/rsc/dir/file.go
					zfile(b, "/", 1);	// leading "/"
					outzfile(b, p+1);
				} else {
					// relative name, like dir/file.go
					if(h->offset >= 0 && pathname && pathname[0] == '/') {
						zfile(b, "/", 1);	// leading "/"
						outzfile(b, pathname+1);
					}
					outzfile(b, p);
				}
			}
		}
		zhist(b, h->line, h->offset);
		if(tofree) {
			free(tofree);
			tofree = nil;
		}
	}
}
Exemple #15
0
Token Tokenizer::getNextToken()
{
	Token result;
	if (position == LEN)
	{
		result.type = Token::TOKEN_TYPE::FINISHED;
		result.stringRepresentation = "EOL";
		return result;
	}
	do
	{
		//skip over whitespace characters
		while (position < LEN && std::isspace(CUR))
			position++;
		//skip comments
		if (CUR == '/')
		{
			if (NEXT == '*')
			{
				position+=2;
				do
				{
					while (CUR != '*')
						position++;
					position++;
				} while (CUR != '/');
				position++;
			}
			else if (NEXT == '/')
			{
				position += 2;
				while (CUR != '\n' && CUR != '\r')//new line
					position++;
			}
		}
		//skip over whitespace characters after comments
		while (position < LEN && std::isspace(CUR))
			position++;
	} while (CUR == '/' && (NEXT == '/' || NEXT == '*'));
	//end of file
	if (position == LEN )
	{
		result.type = Token::TOKEN_TYPE::FINISHED;
		result.stringRepresentation = "";
		return result;
	}
	//operators
	std::vector<string> operators{	"+", "-", "*", "/", "%", "++", "--", "==",
									"!=", ">", "<", ">=", "<=", "&&", "||", "!",
									"&", "|", "^", "~", "<<", ">>", "=", "+=",
									"-=", "*=", "/=", "%=", ">>=", "<<=", "&=",
									"^=", "|=", };//using initializer lists
	int ind = 0;
	string op = "";
	//while there is a longer operator keep going
	do
	{
		op += CUR;
		position++;

	} while ((std::find(operators.begin(), operators.end(), op) != operators.end()));
	op.pop_back();//remove last character
	position--;
	if (op.length() > 0)
	{
		result.type = Token::TOKEN_TYPE::OPERATOR;
		result.stringRepresentation = op;
		return result;
	}
	//delimiters
	if (std::strchr("(),;{}[].:", CUR))
	{
		result.type = Token::TOKEN_TYPE::DELIMITER;
		result.stringRepresentation = "";
		result.stringRepresentation += CUR;
		position++;
		return result;

	}
	string ligit_escape = "0abfnrtv\\\"?";
	//string literal
	if (CUR == '"')
	{
		string s = "";
		position++;
		bool escaped = false;
				
		while (CUR != '"' || escaped)
		{
			if (CUR == '\n' || CUR == '\r')
			{
				result.type = Token::TOKEN_TYPE::ERROR;
				result.stringRepresentation = generateErrorMessage("New line in string literal");
				return result;
			}
			if (escaped && std::strchr(ligit_escape.c_str(), CUR)==nullptr)
			{
				result.type = Token::TOKEN_TYPE::ERROR;
				result.stringRepresentation = generateErrorMessage("Illegal escape character");
				return result;
			}
			s += CUR;
			if (CUR == '\\')
				escaped = true;
			else
				escaped = false;
			position++;
		}
		position++;
		result.type = Token::TOKEN_TYPE::STRING_LITERAL;
		result.stringRepresentation = s;
		return result;
	}
	//char literal
	if (CUR == '\'')
	{
		position++;
		if (CUR == '\\')
		{
			position++;
		}
		if (NEXT == '\'')
		{
			if (CUR == '\n' || CUR == '\r')
			{
				result.type = Token::TOKEN_TYPE::ERROR;
				result.stringRepresentation = generateErrorMessage("New line in character literal");
				return result;
			}
			else
			{
				result.type = Token::TOKEN_TYPE::CHAR_LITERAL;
				result.stringRepresentation = "";
				result.stringRepresentation += CUR;
				return result;
				position += 2;
			}
		}
		else
		{
			result.type = Token::TOKEN_TYPE::ERROR;
			result.stringRepresentation = generateErrorMessage("Character literal too long");
			return result;
		}
	}
	//number literal
	if (std::isdigit(CUR))
	{
		string n;
		n += CUR;
		position++;
		while (std::isdigit(CUR))
		{
			n += CUR;
			position++;
		}
		result.type = Token::TOKEN_TYPE::NUMBER_LITERAL;
		result.stringRepresentation = n;
		return result;
	}
	//keywords and identifiers
	string word = "";
	if (std::isalpha(CUR) || CUR == '_')
	{
		while (!isdelim(CUR))
		{
			word += CUR;
			position++;
		}
	}
	std::vector<string> keywords{ "auto", "break", "case", "char", "const", "continue", "default",
								"do", "double", "else", "enum", "extern", "float", "for", "goto",
								"if", "int", "long", "register", "return", "short", "signed", "sizeof",
								"static", "struct", "switch", "typedef", "union", "unsigned", "void",
								"volatile", "while" };
	if (word.length() > 0)
	{
		if ((std::find(keywords.begin(), keywords.end(), word) != keywords.end()))//its a keyword
		{
			result.type = Token::TOKEN_TYPE::KEYWORD;
			result.stringRepresentation = word;
			return result;
		}
		else
		{
			result.type = Token::TOKEN_TYPE::IDENTIFIER;
			result.stringRepresentation = word;
			return result;
		}
	}
	result.type = Token::TOKEN_TYPE::ERROR;
	result.stringRepresentation = generateErrorMessage("Unexpected character");
	return result;
}
Exemple #16
0
// Get a token. 
tok_types get_token()
{

	char *temp;

	token_type = UNDEFTT; tok = UNDEFTOK;

	temp = token;
	*temp = '\0';

	// Skip over white space. 
	while (isspace(*prog) && *prog) ++prog;

	// Skip over newline. 
	while (*prog == '\r') {
		++prog;
		++prog;
		// Again, skip over white space. 
		while (isspace(*prog) && *prog) ++prog;
	}

	// Check for end of program. 
	if (*prog == '\0') {
		*token = '\0';
		tok = END;
		return (token_type = DELIMITER);
	}

	// Check for block delimiters. 
	if (strchr("{}", *prog)) {
		*temp = *prog;
		temp++;
		*temp = '\0';
		prog++;
		return (token_type = BLOCK);
	}

	// Look for comments. 
	if (*prog == '/')
		if (*(prog + 1) == '*') { // is a /* comment 
			prog += 2;
			do { // find end of comment 
				while (*prog != '*') prog++;
				prog++;
			} while (*prog != '/');
			prog++;
			return (token_type = DELIMITER);
		}
		else if (*(prog + 1) == '/') { // is a // comment 
			prog += 2;
			// Find end of comment. 
			while (*prog != '\r' && *prog != '\0') prog++;
			if (*prog == '\r') prog += 2;
			return (token_type = DELIMITER);
		}

		// Check for double-ops. 
		if (strchr("!<>=+-", *prog)) {
			switch (*prog) {
			case '=':
				if (*(prog + 1) == '=') {
					prog++; prog++;
					*temp = EQ;
					temp++; *temp = EQ; temp++;
					*temp = '\0';
				}
				break;
			case '!':
				if (*(prog + 1) == '=') {
					prog++; prog++;
					*temp = NE;
					temp++; *temp = NE; temp++;
					*temp = '\0';
				}
				break;
			case '<':
				if (*(prog + 1) == '=') {
					prog++; prog++;
					*temp = LE; temp++; *temp = LE;
				}
				else if (*(prog + 1) == '<') {
					prog++; prog++;
					*temp = LS; temp++; *temp = LS;
				}
				else {
					prog++;
					*temp = LT;
				}
				temp++;
				*temp = '\0';
				break;
			case '>':
				if (*(prog + 1) == '=') {
					prog++; prog++;
					*temp = GE; temp++; *temp = GE;
				}
				else if (*(prog + 1) == '>') {
					prog++; prog++;
					*temp = RS; temp++; *temp = RS;
				}
				else {
					prog++;
					*temp = GT;
				}
				temp++;
				*temp = '\0';
				break;
			case '+':
				if (*(prog + 1) == '+') {
					prog++; prog++;
					*temp = INC; temp++; *temp = INC;
					temp++;
					*temp = '\0';
				}
				break;
			case '-':
				if (*(prog + 1) == '-') {
					prog++; prog++;
					*temp = DEC; temp++; *temp = DEC;
					temp++;
					*temp = '\0';
				}
				break;
			}

			if (*token) return(token_type = DELIMITER);
		}

		// Check for other delimiters. 
		if (strchr("+-*^/%=;:(),'", *prog)) {
			*temp = *prog;
			prog++;
			temp++;
			*temp = '\0';
			return (token_type = DELIMITER);
		}

		// Read a quoted string. 
		if (*prog == '"') {
			prog++;
			while (*prog != '"' && *prog != '\r' && *prog) {
				// Check for \n escape sequence. 
				if (*prog == '\\') {
					if (*(prog + 1) == 'n') {
						prog++;
						*temp++ = '\n';
					}
				}
				else if ((temp - token) < MAX_T_LEN)
					*temp++ = *prog;

				prog++;
			}
			if (*prog == '\r' || *prog == 0)
				throw InterpExc(SYNTAX);
			prog++; *temp = '\0';
			return (token_type = STRING);
		}

		// Read an integer number. 
		if (isdigit(*prog)) {
			while (!isdelim(*prog)) {
				if ((temp - token) < MAX_ID_LEN)
					*temp++ = *prog;
				prog++;
			}
			*temp = '\0';
			return (token_type = NUMBER);
		}

		// Read identifier or keyword. 
		if (isalpha(*prog)) {
			while (!isdelim(*prog)) {
				if ((temp - token) < MAX_ID_LEN)
					*temp++ = *prog;
				prog++;
			}
			token_type = TEMP;
		}

		*temp = '\0';

		// Determine if token is a keyword or identifier. 
		if (token_type == TEMP) {
			tok = look_up(token); // convert to internal form 
			if (tok) token_type = KEYWORD; // is a keyword 
			else token_type = IDENTIFIER;
		}

		// Check for unidentified character in file. 
		if (token_type == UNDEFTT)
			throw InterpExc(SYNTAX);

		return token_type;
}
Exemple #17
0
static int
argcv_scan (struct argcv_info *ap)
{
  int i = 0;
  int len = ap->len;
  const char *command = ap->command;
  const char *delim = ap->delim;
  const char *comment = ap->comment;
  
  for (;;)
    {
      i = ap->save;

      if (i >= len)
	return i + 1;

      /* Skip initial whitespace */
      while (i < len && isws (command[i]))
	i++;
      ap->start = i;

      if (!isdelim (command[i], delim))
	{
	  while (i < len)
	    {
	      if (command[i] == '\\')
		{
		  if (++i == len)
		    break;
		  i++;
		  continue;
		}
	      
	      if (command[i] == '\'' || command[i] == '"')
		{
		  int j;
		  for (j = i + 1; j < len && command[j] != command[i]; j++)
		    if (command[j] == '\\')
		      j++;
		  if (j < len)
		    i = j + 1;
		  else
		    i++;
		}
	      else if (isws (command[i]) || isdelim (command[i], delim))
		break;
	      else
		i++; /* skip the escaped character */
	    }
	  i--;
	}
      else if (!(ap->flags & DICO_ARGCV_RETURN_DELIMS))
	{
	  while (i < len && isdelim (command[i], delim))
	    i++;
	  ap->save = i;
	  continue;
	}
      

      ap->end = i;
      ap->save = ap->finish_pos = i + 1;

      /* If we have a token, and it starts with a comment character, skip
         to the newline and restart the token search. */
      if (ap->save <= len)
	{
	  if (strchr (comment, command[ap->start]) != NULL)
	    {
	      ap->finish_pos = ap->start;
	      i = ap->save;
	      while (i < len && command[i] != '\n')
		i++;

	      ap->save = i;
	      continue;
	    }
	}
      break;
    }
  return ap->save;
}
Exemple #18
0
int
parse(int *argc, char ***argv, char *str)
{
    int ac;
    char *val, *p, *q, *copy = NULL;
    size_t i = 0;
    char token, tmp, quote, *buf;
    enum { STR, VAR, WHITE } state;

    ac = *argc = 0;
    quote = 0;
    if (!str || (p = copy = backslash(str)) == NULL)
	return 1;

    /* Initialize vector and state */
    clean();
    state = STR;
    buf = (char *)alloc(PARSE_BUFSIZE);
    token = 0;

    /* And awaaaaaaaaay we go! */
    while (*p) {
	switch (state) {
	case STR:
	    if ((*p == '\\') && p[1]) {
		p++;
		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
		buf[i++] = *p++;
	    } else if (isquote(*p)) {
		quote = quote ? 0 : *p;
		++p;
	    }
	    else if (isspace(*p) && !quote) {
		state = WHITE;
		if (i) {
		    buf[i] = '\0';
		    PARSE_FAIL(insert(&ac, buf));
		    i = 0;
		}
		++p;
	    } else if (*p == '$') {
		token = isdelim(*(p + 1));
		if (token)
		    p += 2;
		else
		    ++p;
		state = VAR;
	    } else {
		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
		buf[i++] = *p++;
	    }
	    break;

	case WHITE:
	    if (isspace(*p))
		++p;
	    else
		state = STR;
	    break;

	case VAR:
	    if (token) {
		PARSE_FAIL((q = strchr(p, token)) == NULL);
	    } else {
		q = p;
		while (*q && !isspace(*q))
		    ++q;
	    }
	    tmp = *q;
	    *q = '\0';
	    if ((val = variable_lookup(p)) != NULL) {
		size_t len = strlen(val);

		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
		i += min(len, PARSE_BUFSIZE - 1);
	    }
	    *q = tmp;	/* restore value */
	    p = q + (token ? 1 : 0);
	    state = STR;
	    break;
	}
    }
    /* If at end of token, add it */
    if (i && state == STR) {
	buf[i] = '\0';
	PARSE_FAIL(insert(&ac, buf));
    }
    args[ac] = NULL;
    *argc = ac;
    *argv = (char **)alloc((sizeof(char *) * ac + 1));
    bcopy(args, *argv, sizeof(char *) * ac + 1);
    free(buf);
    free(copy);
    return 0;
}
Exemple #19
0
int ps_parse_get_token (FILE *stream, int *type,  char *buffer, int bufflen)
{
  int i = 0;
  char ch;
  int unbalanced = 0, escape = 0; /* for STRING type */
  int have_digit = 0, have_exp = 0; /* for INTEGER/REAL type */
  
  if (type == NULL || buffer == NULL || bufflen < 1) {
    fprintf(stderr, "no space allocated for input buffer");
    return PS_PARSE_BUFF_ERROR;
  }

  /* skip whitespaces, comments */
  while ((ch = fgetc(stream)) != EOF) {
    if (ch == '%') {
      skip_line (stream);
    } else if (!isspace(ch)) {
      break;
    }
  }
  if (ch == EOF) {
    *type = PS_UNKNOWN;
    return PS_PARSE_END;
  }

  switch (ch) {
  case '/':
    *type = PS_NAME;
    break;
  case '(':
    *type = PS_STRING;
    break;
  case '<': /* hex or a85 or dict */
    ch = fgetc(stream);
    if (ch == '<') {
      *type = PS_BEGIN_DICT;
      return PS_PARSE_OK;
    } else if (ch == '~') {
      *type = PS_STRING_A85;
    } else if (isxdigit(ch)) {
      *type = PS_STRING_AHX;
      buffer[i++] = ch;
    } else {
      return PS_PARSE_INVALID;
    }
    break;
  case '[':
    *type = PS_BEGIN_ARRAY;
    return PS_PARSE_OK;
    break;
  case ']':
    *type = PS_END_ARRAY;
    return PS_PARSE_OK;
    break;
  case '{':
    *type = PS_BEGIN_PROC;
    return PS_PARSE_OK;
    break;
  case '}':
    *type = PS_END_PROC;
    return PS_PARSE_OK;
    break;
  case '>':
    if (fgetc(stream) == '>') {
      *type = PS_END_DICT;
      return PS_PARSE_OK;
    } else {
      if (verbose)
	fprintf(stderr, "unexpected character `>'\n");
      *type = PS_UNKNOWN;
      return PS_PARSE_INVALID;
    }
    break;
  case ')':
    if (verbose)
      fprintf(stderr, "unexpected character `%c'\n", ch);
    *type = PS_UNKNOWN;
    return PS_PARSE_INVALID;
    break;
  default:
    if (isdigit(ch)) {
      have_digit = 1;
      *type = PS_INTEGER; /* actually undecided (maybe TOKEN/REAL/RADIX) */
    } else if (ch == '-' || ch == '+') {
      *type = PS_INTEGER;
    } else if (ch == '.') {
      *type = PS_REAL;
    } else {
      *type = PS_TOKEN;
    }
    buffer[i++] = ch;
    break;
  }

#define isdelim(ch) \
ch == '(' || ch == '<' || ch == '[' || ch == ']' \
|| ch == '{' || ch == '}' || ch == '/' || ch == '%'

  while ((ch = fgetc(stream)) != EOF) {
    if (*type == PS_TOKEN || *type == PS_NAME) {
      if (isspace(ch)) {
	break; /* not strict */
      } else if (isdelim(ch)) {
	ungetc(ch, stream);
	break;
      }
    } else if (*type == PS_INTEGER) {
      if (isspace(ch)) {
	break;
      } else if (isdelim(ch)) {
	ungetc(ch, stream);
	break;
      } else if (ch == '#') {
	/* check base (2-36) here */
	*type = PS_RADIX;
      } else if (ch == '.' && !have_exp) {
	*type = PS_REAL;
      } else if (ch == '+' || ch == '-') {
	if (i < 1 || (buffer[i-1] != 'e' && buffer[i-1] != 'E')) {
	  *type = PS_TOKEN;
	}
      } else if ((ch == 'e' || ch == 'E') && have_digit ) {
	have_exp = 1;
      } else if (isdigit(ch)) {
	have_digit = 1;
      } else {
	*type = PS_TOKEN; /* not a number */
      }
    } else if (*type == PS_REAL) {
      if (isspace(ch)) {
	break;
      } else if (isdelim(ch)) {
	ungetc(ch, stream);
	break;
      } else if (ch == '+' || ch == '-') {
	if (i < 1 || (buffer[i-1] != 'e' && buffer[i-1] != 'E')) {
	  *type = PS_TOKEN;
	}
      } else if ((ch == 'e' || ch == 'E') && have_digit ) {
	have_exp = 1;
      } else if (isdigit(ch)) {
	have_digit = 1;
      } else {
	*type = PS_TOKEN; /* not a number */
      }
    } else if (*type == PS_RADIX) {
      if (isspace(ch)) {
	break;
      } else if (isdelim(ch)) {
	ungetc(ch, stream);
	break;
      } else if (!(isalpha(ch) || isdigit(ch))) {
	*type = PS_TOKEN;
      }
      /* string is somewhat special */
    } else if (*type == PS_STRING) {
      if (escape) {
	escape = 0;
      } else {
	if (ch == '(') {
	  unbalanced++;
	} else if (ch == ')') {
	  if (!unbalanced)
	    break;
	  unbalanced--;
	} else if (ch == '\\') {
	  escape = 1;
	}
      }
    } else if (*type == PS_STRING_AHX) {
      if (ch == '>') {
	break;
      } else if (isspace(ch)) {
	continue; /* next */
      } else if (!isxdigit(ch)) {
	if (verbose)
	  fprintf(stderr, "non hex digit appears in <...>\n");
	*type = PS_UNKNOWN;
	return PS_PARSE_INVALID;
      }
      /* not strictly checking string */
    } else if (*type == PS_STRING_A85) {
      if (ch == '~' && fgetc(stream) == '>') {
	break;
      } else if (ch < '!' || (ch > 'u' && ch != 'z')) {
	*type = PS_UNKNOWN;
	return PS_PARSE_INVALID;
      }
    }

    if (i >= bufflen) { /* error */
      *type = PS_UNKNOWN;
      return PS_PARSE_BUFF_ERROR;
    }
    buffer[i++] = ch;
  }
  buffer[i] = 0;

  if (ch == EOF) {
    if (i == 0) {
      return PS_PARSE_END;
    } else {
      *type = PS_UNKNOWN;
      return PS_PARSE_INVALID;
    }
  }

  /*
  if (*type == PS_RADIX && check_radix(buffer) < 0)
    *type = TOKEN;
  */

  if ((*type == PS_REAL || *type == PS_INTEGER) && !have_digit)
    *type = PS_TOKEN;

  return PS_PARSE_OK;
}
Exemple #20
0
static bool uri_parse(const char *str, size_t len, struct uri_info *uri)
{
	const char *b, *p = str, *e = str+len, *x;
#define NEXT(I) ({ \
		b = p += (I); \
		STRSEARCH(p, e-p, isdelim(*p)); \
	})

	NEXT(0);

	if (p < e && p[0] == ':' && !(
				(x = strndigit(p+1, MIN(e-p-1,PORT_LEN))) > p+1 && 
				(x == e || isdelim(*x))))
	{
		uri->scheme = b;
		uri->scheme_len = p-b;
		NEXT(1);
	}
	else
	{
		uri->scheme = NULL;
		uri->scheme_len = 0;
	}

	if (p < e-1 && p == b && p[0] == '/' && p[1] == '/')
		NEXT(2);

	if (p < e-1 && *p == '@')
		NEXT(1);

	uri->host = b;
	uri->host_len = p-b;
	uri->port = -1;

	while (p < e-1 && p[0] == ':')
	{
		char portbuf[8];
		unsigned portlen = sizeof(portbuf)-1;

		NEXT(1);

		if (p-b < portlen)
			portlen = p-b;
		memcpy(portbuf, b, portlen);
		portbuf[portlen] = 0;

		uri->port = strtoul(portbuf, (char **)&x, 10);
		if (!*x)
			break;

		uri->host_len = p-uri->host;
		uri->port = -1;
	}

	if (p < e)
	{
		uri->path = p;
		uri->path_len = e-p;
	}
	else
	{
		uri->path = NULL;
		uri->path_len = 0;
	}

#undef NEXT

	return true;
}
Exemple #21
0
int mailesmtp_parse_ehlo(mailsmtp * session)
{
  char * response;

  /* restore data */
  session->esmtp = MAILSMTP_ESMTP;
  session->auth = MAILSMTP_AUTH_CHECKED;

  response = session->response;

  /* ESMTP supported extensions :
     DSN
     EXPN
     8BITMIME
     SIZE [<n>]
     ETRN
     STARTTLS
     AUTH <mechanisms...>
  */
  while (response != NULL) {
    if (!strncasecmp(response, "EXPN", 4) && isdelim(response[4])) {
      session->esmtp |= MAILSMTP_ESMTP_EXPN;
    }
    else if (!strncasecmp(response, "ETRN", 4) && isdelim(response[4])) {
      session->esmtp |= MAILSMTP_ESMTP_ETRN;
    }
    else if (!strncasecmp(response, "DSN", 3) && isdelim(response[3])) {
      session->esmtp |= MAILSMTP_ESMTP_DSN;
    }
    else if (!strncasecmp(response, "8BITMIME", 8) && isdelim(response[8])) {
      session->esmtp |= MAILSMTP_ESMTP_8BITMIME;
    }
    else if (!strncasecmp(response, "STARTTLS", 8) && isdelim(response[8])) {
      session->esmtp |= MAILSMTP_ESMTP_STARTTLS;
    }
    else if (!strncasecmp(response, "SIZE", 4) && isdelim(response[4])) {
      session->esmtp |= MAILSMTP_ESMTP_SIZE;
      if ((response[4] == ' ') || (response[4] == '\t')) {
        session->smtp_max_msg_size = strtoul(response + 4, NULL, 10);
      }
      /* TODO: grab optionnal max size */
    }
    else if (!strncasecmp(response, "PIPELINING", 10) && isdelim(response[10])) {
      session->esmtp |= MAILSMTP_ESMTP_PIPELINING;
    }
    else if (!strncasecmp(response, "AUTH ", 5)) {
      response += 5;       /* remove "AUTH " */
      while (response[0] != '\n' && response[0] != '\0') {
        while (response[0] == ' ') response++;
        if (strncasecmp(response, "LOGIN", 5) == 0) {
          session->auth |= MAILSMTP_AUTH_LOGIN;
          response += 5;
        } else if (strncasecmp(response, "CRAM-MD5", 8) == 0) {
          session->auth |= MAILSMTP_AUTH_CRAM_MD5;
          response += 8;
        } else if (strncasecmp(response, "PLAIN", 5) == 0) {
          session->auth |= MAILSMTP_AUTH_PLAIN;
          response += 5;
        } else if (strncasecmp(response, "DIGEST-MD5", 10) == 0) {
          session->auth |= MAILSMTP_AUTH_DIGEST_MD5;
          response += 10;
        } else if (strncasecmp(response, "GSSAPI", 6) == 0) {
          session->auth |= MAILSMTP_AUTH_GSSAPI;
          response += 6;
        } else if (strncasecmp(response, "SRP", 3) == 0) {
          session->auth |= MAILSMTP_AUTH_SRP;
          response += 3;
        } else if (strncasecmp(response, "NTLM", 4) == 0) {
          session->auth |= MAILSMTP_AUTH_NTLM;
          response += 4;
        } else if (strncasecmp(response, "KERBEROS_V4", 11) == 0) {
          session->auth |= MAILSMTP_AUTH_KERBEROS_V4;
          response += 11;
        } else {
          /* unknown auth method - jump to next word or eol */
          while (!isdelim(response[0]) || response[0] == '\r')
            response++;
        }
      }
    }
    // for broken servers
    else if (!strncasecmp(response, "AUTH=", 5)) {
      response += 5;       /* remove "AUTH=" */
      while (response[0] != '\n' && response[0] != '\0') {
        while (response[0] == ' ') response++;
        if (strncasecmp(response, "LOGIN", 5) == 0) {
          session->auth |= MAILSMTP_AUTH_LOGIN;
          response += 5;
        } else if (strncasecmp(response, "CRAM-MD5", 8) == 0) {
          session->auth |= MAILSMTP_AUTH_CRAM_MD5;
          response += 8;
        } else if (strncasecmp(response, "PLAIN", 5) == 0) {
          session->auth |= MAILSMTP_AUTH_PLAIN;
          response += 5;
        } else if (strncasecmp(response, "DIGEST-MD5", 10) == 0) {
          session->auth |= MAILSMTP_AUTH_DIGEST_MD5;
          response += 10;
        } else if (strncasecmp(response, "GSSAPI", 6) == 0) {
          session->auth |= MAILSMTP_AUTH_GSSAPI;
          response += 6;
        } else if (strncasecmp(response, "SRP", 3) == 0) {
          session->auth |= MAILSMTP_AUTH_SRP;
          response += 3;
        } else if (strncasecmp(response, "NTLM", 4) == 0) {
          session->auth |= MAILSMTP_AUTH_NTLM;
          response += 4;
        } else if (strncasecmp(response, "KERBEROS_V4", 11) == 0) {
          session->auth |= MAILSMTP_AUTH_KERBEROS_V4;
          response += 11;
        } else {
          /* unknown auth method - jump to next word or eol */
          while (!isdelim(response[0]) || response[0] == '\r')
            response++;
        }
      }
    }
    response = strpbrk(response, "\n");
    if (response != NULL)
      response++;
  }

  return MAILSMTP_NO_ERROR;
}
static char *
next_token(char *p, char *end, char **new_p, int *delim)
{
   char *tok_start = NULL, *tok_end = NULL, *tok = NULL, *sa_start = NULL;
   int in_tok = 0;
   int in_quote = 0;
   int in_parens = 0;
   int in_comment_ss  = 0;
   int in_comment_cpp = 0;
   int in_comment_sa  = 0;
   int had_quote = 0;
   int is_escaped = 0;
   char *cpp_token_line = NULL;
   char *cpp_token_file = NULL;

   *delim = 0;
   if (p >= end) return NULL;
   while (p < end)
     {
	if (*p == '\n')
	  {
	     in_comment_ss = 0;
	     in_comment_cpp = 0;
	     cpp_token_line = NULL;
	     cpp_token_file = NULL;
	     line++;
	  }
	if ((!in_comment_ss) && (!in_comment_sa))
	  {
	     if ((!in_quote) && (*p == '/') && (p < (end - 1)) && (*(p + 1) == '/'))
	       in_comment_ss = 1;
	     if ((!in_quote) && (*p == '#'))
	       in_comment_cpp = 1;
	     if ((!in_quote) && (*p == '/') && (p < (end - 1)) && (*(p + 1) == '*'))
	       {
		  in_comment_sa = 1;
		  sa_start = p;
	       }
	  }
	if ((in_comment_cpp) && (*p == '#'))
	  {
	     char *pp, fl[4096];
	     char *tmpstr = NULL;
	     int   l, nm;

	     /* handle cpp comments */
	     /* their line format is
	      * # <line no. of next line> <filename from next line on> [??]
	      */
	     cpp_token_line = NULL;
	     cpp_token_file = NULL;

	     pp = p;
	     while ((pp < end) && (*pp != '\n'))
	       {
		  pp++;
	       }
	     l = pp - p;
	     tmpstr = alloca(l + 1);
	     if (!tmpstr)
	       {
		  ERR("%s: Error. %s:%i malloc %i bytes failed",
		      progname, file_in, line - 1, l + 1);
		  exit(-1);
	       }
	     strncpy(tmpstr, p, l);
	     tmpstr[l] = 0;
	     l = sscanf(tmpstr, "%*s %i \"%[^\"]\"", &nm, fl);
	     if (l == 2)
	       {
		  strcpy(file_buf, fl);
		  line = nm;
		  file_in = file_buf;
	       }
	  }
	else if ((!in_comment_ss) && (!in_comment_sa) && (!in_comment_cpp))
	  {
	     if (!in_tok)
	       {
		  if (!in_quote)
		    {
		       if (!isspace(*p))
			 {
			    if (*p == '"')
			      {
				 in_quote = 1;
				 had_quote = 1;
			      }
			    else if (*p == '(')
			      in_parens++;

			    in_tok = 1;
			    tok_start = p;
			    if (isdelim(*p)) *delim = 1;
			 }
		    }
	       }
	     else
	       {
		  if (in_quote)
		    {
		       if ((*p) == '\\')
			 is_escaped = !is_escaped;
		       else if (((*p) == '"') && (!is_escaped))
			 {
			    in_quote = 0;
			    had_quote = 1;
			 }
		       else if (is_escaped)
			 is_escaped = 0;
		    }
		  else if (in_parens)
		    {
		       if (((*p) == ')') && (!is_escaped))
			 in_parens--;
		    }
		  else
		    {
		       if (*p == '"')
			 {
			    in_quote = 1;
			    had_quote = 1;
			 }
		       else if (*p == '(')
			 in_parens++;

		       /* check for end-of-token */
		       if (
			   (isspace(*p)) ||
			   ((*delim) && (!isdelim(*p))) ||
			   (isdelim(*p))
			   )
			 {
			    in_tok = 0;
			    tok_end = p - 1;
			    if (*p == '\n') line--;
			    goto done;
			 }
		    }
	       }
	  }
	if (in_comment_sa)
	  {
	     if ((*p == '/') && (*(p - 1) == '*') && ((p - sa_start) > 2))
	       in_comment_sa = 0;
	  }
	p++;
     }
   if (!in_tok) return NULL;
   tok_end = p - 1;

   done:
   *new_p = p;

   tok = mem_alloc(tok_end - tok_start + 2);
   strncpy(tok, tok_start, tok_end - tok_start + 1);
   tok[tok_end - tok_start + 1] = 0;

   if (had_quote)
     {
	is_escaped = 0;
	p = tok;

	while (*p)
	  {
	     if ((*p == '\"') && (!is_escaped))
	       {
		  memmove(p, p + 1, strlen(p));
	       }
	     else if ((*p == '\\') && (*(p + 1) == 'n'))
	       {
		  memmove(p, p + 1, strlen(p));
		  *p = '\n';
	       }
	     else if ((*p == '\\') && (*(p + 1) == 't'))
	       {
		  memmove(p, p + 1, strlen(p));
		  *p = '\t';
	       }
	     else if (*p == '\\')
	       {
		  memmove(p, p + 1, strlen(p));
		  if (*p == '\\') p++;
		  else is_escaped = 1;
	       }
	     else
	       {
		  if (is_escaped) is_escaped = 0;
		  p++;
	       }
	  }
     }
   else if ((tok) && (*tok == '('))
     {
	char *tmp;
	tmp = tok;
	tok = perform_math(tok);
	free(tmp);
     }

   return tok;
}
Exemple #23
0
/* Get a token. */
char get_token(void)
{

	register char *temp;

	token_type = 0; tok = 0;

	temp = token;
	*temp = '\0';

	/* skip over white space */
	while (iswhite(*prog) && *prog) ++prog;

	/* Handle Windows and Mac newlines */
	if (*prog == '\r') {
		++prog;
		/* Only skip \n if it exists (if it doesn't, we are running on mac) */
		if (*prog == '\n') {
			++prog;
		}
		/* skip over white space */
		while (iswhite(*prog) && *prog) ++prog;
	}

	/* Handle Unix newlines */
	if (*prog == '\n') {
		++prog;
		/* skip over white space */
		while (iswhite(*prog) && *prog) ++prog;
	}

	if (*prog == '\0') { /* end of file */
		*token = '\0';
		tok = FINISHED;
		return (token_type = DELIMITER);
	}

	if (strchr("{}", *prog)) { /* block delimiters */
		*temp = *prog;
		temp++;
		*temp = '\0';
		prog++;
		return (token_type = BLOCK);
	}

	/* look for comments */
	if (*prog == '/')
		if (*(prog + 1) == '*') { /* is a comment */
			prog += 2;
			do { /* find end of comment */
				while (*prog != '*' && *prog != '\0') prog++;
				if (*prog == '\0') {
					prog--;
					break;
				}
				prog++;
			} while (*prog != '/');
			prog++;
		}

	/* look for C++ style comments */
	if (*prog == '/')
		if (*(prog + 1) == '/') { /* is a comment */
			prog += 2;
			/* find end of line */
			while (*prog != '\r' && *prog != '\n' && *prog != '\0') prog++;
			if (*prog == '\r' && *(prog + 1) == '\n') {
				prog++;
			}
		}

	/* look for the end of file after a comment */
	if (*prog == '\0') { /* end of file */
		*token = '\0';
		tok = FINISHED;
		return (token_type = DELIMITER);
	}

	if (strchr("!<>=", *prog)) { /* is or might be
								   a relational operator */
		switch (*prog) {
		case '=': if (*(prog + 1) == '=') {
			prog++; prog++;
			*temp = EQ;
			temp++; *temp = EQ; temp++;
			*temp = '\0';
		}
				  break;
		case '!': if (*(prog + 1) == '=') {
			prog++; prog++;
			*temp = NE;
			temp++; *temp = NE; temp++;
			*temp = '\0';
		}
				  break;
		case '<': if (*(prog + 1) == '=') {
			prog++; prog++;
			*temp = LE; temp++; *temp = LE;
		}
				  else {
					  prog++;
					  *temp = LT;
				  }
				  temp++;
				  *temp = '\0';
				  break;
		case '>': if (*(prog + 1) == '=') {
			prog++; prog++;
			*temp = GE; temp++; *temp = GE;
		}
				  else {
					  prog++;
					  *temp = GT;
				  }
				  temp++;
				  *temp = '\0';
				  break;
		}
		if (*token) return(token_type = DELIMITER);
	}

	if (strchr("+-*^/%=;(),'", *prog)) { /* delimiter */
		*temp = *prog;
		prog++; /* advance to next position */
		temp++;
		*temp = '\0';
		return (token_type = DELIMITER);
	}

	if (*prog == '"') { /* quoted string */
		prog++;
		while ((*prog != '"' && *prog != '\r' && *prog != '\n' && *prog != '\0') || (*prog == '"' && *(prog - 1) == '\\')) *temp++ = *prog++;
		if (*prog == '\r' || *prog == '\n' || *prog == '\0') sntx_err(SYNTAX);
		prog++; *temp = '\0';
		str_replace(token, "\\a", "\a");
		str_replace(token, "\\b", "\b");
		str_replace(token, "\\f", "\f");
		str_replace(token, "\\n", "\n");
		str_replace(token, "\\r", "\r");
		str_replace(token, "\\t", "\t");
		str_replace(token, "\\v", "\v");
		str_replace(token, "\\\\", "\\");
		str_replace(token, "\\\'", "\'");
		str_replace(token, "\\\"", "\"");
		return (token_type = STRING);
	}

	if (isdigit((int)*prog)) { /* number */
		while (!isdelim(*prog)) *temp++ = *prog++;
		*temp = '\0';
		return (token_type = NUMBER);
	}

	if (isalpha((int)*prog)) { /* var or command */
		while (!isdelim(*prog)) *temp++ = *prog++;
		token_type = TEMP;
	}

	*temp = '\0';

	/* see if a string is a command or a variable */
	if (token_type == TEMP) {
		tok = look_up(token); /* convert to internal rep */
		if (tok) token_type = KEYWORD; /* is a keyword */
		else token_type = IDENTIFIER;
	}
	return token_type;
}
Exemple #24
0
static int
argcv_scan (int len, const char *command, const char *delim, const char *cmnt,
	    int *start, int *end, int *save)
{
  int i = 0;
  
  for (;;)
    {
      i = *save;

      if (i >= len)
	return i + 1;

      /* Skip initial whitespace */
      while (i < len && isws (command[i]))
	i++;
      *start = i;

      if (!isdelim (command[i], delim))
	{
	  while (i < len)
	    {
	      if (command[i] == '\\')
		{
		  if (++i == len)
		    break;
		  i++;
		  continue;
		}
	      
	      if (command[i] == '\'' || command[i] == '"')
		{
		  int j;
		  for (j = i+1; j < len && command[j] != command[i]; j++)
		    if (command[j] == '\\')
		      j++;
		  if (j < len)
		    i = j+1;
		  else
		    i++;
		}
	      else if (isws (command[i]) || isdelim (command[i], delim))
		break;
	      else
		i++; /* skip the escaped character */
	    }
	  i--;
	}

      *end = i;
      *save = i + 1;

      /* If we have a token, and it starts with a comment character, skip
         to the newline and restart the token search. */
      if (*save <= len)
	{
	  if (strchr (cmnt, command[*start]) != NULL)
	    {
	      i = *save;
	      while (i < len && command[i] != '\n')
		i++;

	      *save = i;
	      continue;
	    }
	}
      break;
    }
  return *save;
}
Exemple #25
0
int get_token()
{
    char *temp;
    token_type = 0;
    tok = 0;
    temp = token;
    *temp = '\0';
    while(iswhite(*prog) && *prog) 
        ++prog; 
    if(*prog=='\n') { 
        ++prog;
        while(iswhite(*prog) && *prog) 
            ++prog;
    }
    if(*prog=='\0') {
        *token = '\0';
        tok = FINISHED;
        return(token_type=DELIMITER);
    }
    if(strchr1("{}", *prog)) {
        *temp = *prog;
        temp++;
        *temp = '\0';
        prog++;
        return (token_type = BLOCK);
    }
    if(*prog=='/') {
        if(*(prog+1)=='*') {
            prog += 2;
            do { 
                while(*prog!='*') 
                    prog++;
                prog++;
            } while (*prog!='/');
            prog++;
        }
    }
    if(strchr1("!<>=", *prog)) {
        switch(*prog) {
            case '=':
                if(*(prog+1)=='=') {
                    prog++; prog++;
                    *temp = EQ;
                    temp++; *temp = EQ; temp++;
                    *temp = '\0';
                }
                break;
            case '!':
                if(*(prog+1)=='=') {
                    prog++; prog++;
                    *temp = NE;
                    temp++; *temp = NE; temp++;
                    *temp = '\0';
                }
                break;
            case '<':
                if(*(prog+1)=='=') {
                    prog++; prog++;
                    *temp = LE; temp++; *temp = LE;
                } else {
                    prog++;
                    *temp = LT;
                }
                temp++;
                *temp = '\0';
                break;
            case '>':
                if(*(prog+1)=='=') {
                    prog++; prog++;
                    *temp = GE; temp++; *temp = GE;
                } else {
                    prog++;
                    *temp = GT;
                }
                temp++;
                *temp = '\0';
                break;
        }
        if(*token) 
            return(token_type = DELIMITER);
    }
    if(strchr1("+-*^&|/%=;(),'", *prog)){
        *temp = *prog;
        prog++;
        temp++;
        *temp = '\0'; 
        return (token_type=DELIMITER);
    }
    if(*prog=='"') { 
        prog++;
        while(*prog!='"'&& *prog!='\n') 
            *temp++ = *prog++;
        if(*prog=='\n') 
            sntx_err(SYNTAX);
        prog++; *temp = '\0';
        return(token_type=STRING);
    }
    if(isdigit(*prog)) { 
        while(!isdelim(*prog)) 
            *temp++ = *prog++;
        *temp = '\0';
        return(token_type = NUMBER);
    }
    if(isalph(*prog)) { 
        while(!isdelim(*prog)) 
            *temp++ = *prog++;
        token_type=TEMP;
    }
    *temp = '\0';
    if(token_type==TEMP) {
        tok = look_up(token);
        if(tok) 
            token_type = KEYWORD; 
        else 
            token_type = IDENTIFIER;
    }
    return token_type;
}
Exemple #26
0
BOOL parse_cmd(char *cmd)
{
  unsigned int i, rc;
  int Argc;                // argument count
  #define MAXARGS 32
  char *Argv[MAXARGS];      // argument pointers
  char *pszTemp;

  if (strlen(cmd)==0)
  {
    return FALSE;
  }

  if (strlen(cmd)==2)
    if (cmd[1]==':')
    {
      if (QueryDriveReady(cmd[0]-'a'+1))
        DosSetDefaultDisk(cmd[0]-'a'+1);
      return FALSE;
    }

  while (( *cmd == ' ' ) || ( *cmd == '\t' ))
          cmd++;

  // loop through arguments
  for ( Argc = 0; (( Argc < MAXARGS ) && ( *cmd )); Argc++ )  {

          pszTemp = cmd;              // save start of argument

          while ( isdelim( *cmd ) == 0 ) {
                  if ( *cmd )
                          cmd++;
          }

          // check flag for terminators
          if (Argc == 0) { // '\0' after program name only
                  if (( *cmd != '\0' ))
                          *cmd++ = '\0';
          }

          // update the Argv pointer
          Argv[ Argc ] = pszTemp;
          log("Argv[%d]=%s\n", Argc, pszTemp);

          // skip delimiters for Argv[2] onwards
          while (( *cmd ) && ( isdelim( *cmd )))
                  cmd++;
  }

  while (*cmd && isdelim(*cmd))
    cmd--;

  // change the first trailing delimiter to '\0'
  cmd++;
  *cmd = '\0'; /* second NULL terminator */

  Argv[Argc] = NULL;
  log("Argv[%d]=%s\n", Argc, 0);

  if (!strcmp(Argv[0], "exit"))
  {
    return TRUE;
  }

  for (i = 0; commands[i]; i ++)
  {
    if (!strcmp(Argv[0], commands[i]->cmdname))
    {
      (*(commands[i]->func))(Argc, Argv);
      return FALSE;
    }
  }

  //log("argc=%d\n", Argc);

  //for (i = 0; i < Argc; i++)
  //  log("argv[%d]=%s\n", i, Argv[i]);

  execute_external(Argc, Argv);

  return FALSE;
}
/* Чтение лексемы */
char get_token()
{
	register char *temp;
	token_type = 0; tok = 0;
	temp = token;

	if( *prog == '\0' )
	{
		/* Конец файла */
		*token = 0;
		tok = FINISHED;
		return( token_type = DELIMITER );
	}
	while( iswhite(*prog) ) ++prog; /* Пропуск пробелов */

	if( *prog == '\r' )
	{
		/* конец строки программы */
		++prog; ++prog;
		tok = EOL; *token = '\r';
		token[1] = '\n'; token[2] = 0;

		return (token_type = DELIMITER );
	}

	if( strchr("+-*^/%=;(),<>", *prog ) )
	{
		/* Разделитель */
		*temp = *prog;
		prog++; /* Переход на следующу позицию */
		temp++;
		*temp = 0;
		return ( token_type = DELIMITER );
	}

	if ( *prog == '"' )
	{
		/* строка кавычек */
		prog ++;
		while( *prog != '"' && *prog != '\r' ) *temp++ = *prog++;
		if( *prog == '\r' ) serror(1);
		prog++; *temp = 0;
		return( token_type = QUOTE );
	}

	if( isdigit(*prog) )
	{
		/* число */
		while( !isdelim( *prog ) ) *temp++ = *prog++;
		*temp = '\0';

		return( token_type = NUMBER );
	}

	if( isalpha( *prog ) )
	{
		/* переменная или команда */
		while( !isdelim( *prog ) ) *temp++ = *prog++;
		token_type = STRING;
	}

	*temp = '\0';
/* просматривается, если строка - переменная или команда */
	if ( token_type == STRING )
	{
		tok = look_up( token ); /* Преобразование во внутренний формат */ //Пока что таблица table не заполнена, посему look_up === 0
		if ( !tok ) token_type = VARIABLE;
		else token_type = COMMAND; /* это команда */
	}
	return token_type;
}
static inline int isregular(int ch)
{
	return !isdelim(ch) && !iswhite(ch) && ch != EOF;
}
Exemple #29
0
/* Get a token. */
get_token(void)
{
  register char *temp;
  token_type = 0; tok = 0;
  temp = token;
  *temp = '\0';
 /* skip over white space */
  while(iswhite(*prog) && *prog) ++prog;
  if(*prog==0x0a) {
    ++prog;
//    ++prog;
    /* skip over white space */
    while(iswhite(*prog) && *prog) ++prog;
  }
  if(*prog=='\0') { /* end of file */
    *token = '\0';
    tok = FINISHED;
    return(token_type=DELIMITER);
  }
  if(strchr("{}", *prog)) { /* block delimiters */
    *temp = *prog;
    temp++;
    *temp = '\0';
    prog++;
    return (token_type = BLOCK);
  }
  /* look for comments */
  if(*prog=='/')
    if(*(prog+1)=='*') { /* is a comment */
      prog += 2;
      do { /* find end of comment */
        while(*prog!='*') prog++;
        prog++;
      } while (*prog!='/');
      prog++;
    }
  if(strchr("!<>=", *prog)) { /* is or might be
               a relation operator */
    switch(*prog) {
      case '=': if(*(prog+1)=='=') {
     prog++; prog++;
          *temp = EQ;
          temp++; *temp = EQ; temp++;
     *temp = '\0';
     }
   break;
      case '!': if(*(prog+1)=='=') {
     prog++; prog++;
          *temp = NE;
          temp++; *temp = NE; temp++;
     *temp = '\0';
   }
   break;
      case '<': if(*(prog+1)=='=') {
     prog++; prog++;
          *temp = LE; temp++; *temp = LE;
   }
   else {
     prog++;
     *temp = LT;
   }
        temp++;
   *temp = '\0';
   break;
      case '>': if(*(prog+1)=='=') {
     prog++; prog++;
          *temp = GE; temp++; *temp = GE;
   }
   else {
     prog++;
     *temp = GT;
   }
        temp++;
   *temp = '\0';
   break;
    }
    if(*token) return(token_type = DELIMITER);
  }
  if(strchr("+-*^/%=;(),'", *prog)){ /* delimiter */
    *temp = *prog;
    prog++; /* advance to next position */
    temp++;
    *temp = '\0';
    return (token_type=DELIMITER);
  }
  if(*prog=='"') { /* quoted string */
    prog++;
    while(*prog!='"'&& *prog!=0x0a) *temp++ = *prog++;
    if(*prog==0x0a) sntx_err(SYNTAX);
    prog++; *temp = '\0';
    return(token_type=STRING);
  }
  if(isdigit(*prog)) { /* number */
    while(!isdelim(*prog)) *temp++ = *prog++;
    *temp = '\0';
    return(token_type = NUMBER);
  }
  if(isalpha(*prog)) { /* var or command */
    while(!isdelim(*prog)) *temp++ = *prog++;
    token_type=TEMP;
  }
  *temp = '\0';
  /* see if a string is a command or a variable */
  if(token_type==TEMP) {
    tok = look_up(token); /* convert to internal rep */
    if(tok) token_type = KEYWORD; /* is a keyword */
    else token_type = IDENTIFIER;
  }
  return token_type;
}
/* Считывание лексемы из входного потока. */
int get_token(void) {
	register char *temp;
	token_type = 0;
	tok = 0;
	temp = token;
	*temp = '\0';
	/* пропуск пробелов, символов табуляции и пустой строки */
	while(iswhite(*prog) && *prog) {
		++prog;
	}
	while(*prog == '\r' || *prog == '\n') {
		++prog;
		// COMPILER_SPECIFIC
		while(iswhite(*prog) && *prog) {
			++prog;
		}
	}
	/* конец файла */
	if(*prog == '\0') {
		*token = '\0';
		tok = FINISHED;
		return (token_type = DELIMITER);
	}
	/* блок кода */
	if(strchr("{}", *prog)) { /* ограничение блока */
		*temp = *prog;
		temp++;
		*temp = '\0';
		prog++;
		return (token_type = BLOCK);
	}
	/* массив */
	if(strchr("[]", *prog)) {
		*temp = *prog;
		temp++;
		*temp = '\0';
		prog++;
		return (token_type = ARRAY);
	}
	/* комментарий */
	if(*prog == '/') {
		if(*(prog+1) == '*') {
			prog += 2;
			do {
				while(*prog != '*') {
					prog++;
				}
				prog++;
			} while(*prog != '/');
			prog++;
		}
	}
	/* оператор сравнения */
	if(strchr("!<>=", *prog)) {
		switch(*prog) {
		case '=':
			if(*(prog+1) == '=') { // ==
				prog++;
				prog++;
				*temp = EQ;
				temp++;
				*temp = EQ;
				temp++;
				*temp = '\0';
			}
			break;
		case '!':
			if(*(prog+1) == '=') { // !=
				prog++;
				prog++;
				*temp = NE;
				temp++;
				*temp = NE;
				temp++;
				*temp = '\0';
			}
			break;
		case '<':
			if(*(prog+1) == '=') { // <=
				prog++;
				prog++;
				*temp = LE;
				temp++;
				*temp = LE;
			} else {               // <
				prog++;
				*temp = LT;
			}
			temp++;
			*temp = '\0';
			break;
		case '>':
			if(*(prog+1) == '=') { // >=
				prog++;
				prog++;
				*temp = GE;
				temp++;
				*temp = GE;
			} else {              // >
				prog++;
				*temp = GT;
			}
			temp++;
			*temp = '\0';
			break;
		}
		if(*token) {
			return(token_type = DELIMITER);
		}
	}
	/* разделитель */
	if(strchr("+-*^/%=;(),'", *prog)) {
		*temp = *prog;
		prog++; /* продвижение на следующую позицию */
		temp++;
		*temp = '\0';
		return (token_type = DELIMITER);
	}
	/* строка в кавычках */
	if(*prog=='"') {
		prog++;
		while(*prog != '"' && *prog != '\r') {
			*temp++ = *prog++;
		}
		if(*prog == '\r') {
			sntx_err(SYNTAX);
		}
		prog++;
		*temp = '\0';
		return (token_type = STRING);
	}
	/* число */
	if(isdigit(*prog)) {
		while(!isdelim(*prog)) {
			*temp++ = *prog++;
		}
		*temp = '\0';
		return (token_type = NUMBER);
	}
	/* переменная или оператор */
	if(isalpha(*prog)) {
		while(!isdelim(*prog)) {
			*temp++ = *prog++;
		}
		token_type = TEMP;
	}
	*temp = '\0';
	/* ключевое слово или имя */
	if(token_type==TEMP) {
		tok = look_up(token); /* преобразовать во внутренее представление */
		if(tok) {
			token_type = KEYWORD; /* это зарезервированное слово */
		} else {
			token_type = IDENTIFIER;
		}
	}
	return token_type;
}