예제 #1
0
파일: escape.c 프로젝트: vocho/openqnx
char *esc_line(char *tp) {
	register char *p2, *p1;
	char c;
	char *esc_char();

	for(p1 = p2 = tp ; *p2 != '\0' ;) {
		if((c = *p2++) == '\\')
			p2 = esc_char(p2, &c);
		*p1++ = c;
		}
	*p1 = '\0';
	return( tp );
	}
예제 #2
0
파일: tok.c 프로젝트: gaoxiaojun/neatcc
static void readstr(struct mem *mem)
{
	char *s = buf + cur;
	char *e = buf + len;
	int c;
	s++;
	while (s < e && *s != '"') {
		if (*s == '\\') {
			s += esc_char(&c, s);
			mem_putc(mem, c);
		} else {
			mem_putc(mem, (unsigned char) *s++);
		}
	}
	cur = s - buf + 1;
}
예제 #3
0
파일: tok.c 프로젝트: gaoxiaojun/neatcc
static void readnum(void)
{
	int base = 10;
	num_bt = 4 | BT_SIGNED;
	if (buf[cur] == '0' && tolower(buf[cur + 1]) == 'x') {
		num_bt &= ~BT_SIGNED;
		base = 16;
		cur += 2;
	}
	if (strchr(digs, tolower(buf[cur]))) {
		long result = 0;
		char *c;
		if (base == 10 && buf[cur] == '0')
			base = 8;
		while (cur < len && (c = strchr(digs, tolower(buf[cur])))) {
			result *= base;
			result += c - digs;
			cur++;
		}
		num = result;
		while (cur < len) {
			int c = tolower(buf[cur]);
			if (c != 'u' && c != 'l')
				break;
			if (c == 'u')
				num_bt &= ~BT_SIGNED;
			if (c == 'l')
				num_bt = (num_bt & BT_SIGNED) | LONGSZ;
			cur++;
		}
		return;
	}
	if (buf[cur] == '\'') {
		int ret;
		cur += 2 + esc_char(&ret, buf + cur + 1);
		num = ret;
		return;
	}
	num = -1;
}
예제 #4
0
파일: lexer.c 프로젝트: SemiSQ/OpenModelica
static int yylex0()
{
	int i, c, d, e, token;

	struct Token *val;

	static char ident[LEXER_IDENT_MAXLENGTH];
	static char string[LEXER_STRING_MAXLENGTH];


	if(creg >= MAX_COMMENTINFO-1)
		yyerror("!commentinfo-buffer overflow");

	if (tmp != yylineno)
	{
		tmp = yylineno;
		//printf("Curren row:%d \n",yylineno);
	}
	if(creg == 0)
	{ //initial bound
		struct CommentInfo cib;
		cib.bound = 1;
		cib.lastline = 1;
		cib.lastcol = 1;
		commentInfo[creg++] = cib;
	}
	else if(yylineno == specboundline + 2 && columnno == 0)
	{ //bound after result
		spreg = creg;
		createBound(1);
	}

	val = (struct Token *) malloc(sizeof(struct Token));
	if (!val)
		yyerror("!out of memory");
	memset(val, 0, sizeof(struct Token));

	yylval = (rml_t) val;

	c = get();

#ifdef LEXER_TOKEN_POSITION
	val->file      = yyThisFileName;
	val->firstline = yylineno;
	val->firstcol  = columnno;
	val->lastline  = yylineno;
	val->lastcol   = columnno;
#endif

	switch (c)
	{
	case '\n': case ' ': case '\t':
		return yylex0();

	case EOF: case 0:
		return EOF;

	case '=': /* '=' or '=>' or == or ==. */
		d = get();
		if (d == '>')
		{ token = YIELDS; break; }
		if (d == '=')
		{
			e = get();
			if (e == '.') { token = EQEQ_REAL; break; }
			unget();
			token = EQEQ_INT; break;
		}
		unget();
		token = EQUALS;
		break;

	case '<': /* < or <= or <=. or <. or <> or <>. */
		d = get();
		if (d == '.') { token = LT_REAL; break; }
		if (d == '=')
		{
			e = get();
			if (e = '.') { token = LE_REAL; break; }
			unget();
			token = LE_INT; break;
		}
		if (d == '>')
		{
			e = get();
			if (e = '.') { token = NE_REAL; break; }
			unget();
			token = NE_INT; break;
		}
		unget();
		token = LT_INT;
		break;

	case '>': /* > or >= or >=. or >. */
		d = get();
		if (d == '.') { token = GT_REAL; break; }
		if (d == '=')
		{
			e = get();
			if (e = '.') { token = GE_REAL; break; }
			unget();
			token = GE_INT; break;
		}
		unget();
		token = GT_INT;
		break;

	case '!': /* != or !=. */
		d = get();
		if (d == '=')
		{
			e = get();
			if (e = '.') { token = NE_REAL; break; }
			unget();
			token = NE_INT; break;
		}
		yyerror("invalid comparison operator!");
		break;


	case ':': /* ':' or '::' */
		d = get();
		if (d == ':')
		{ token = COLONCOLON; break; }
		unget();
		token = COLON;
		break;

	case '(': /* '(' or '(*...comment...*)' */
		d = get();
		if (d == '*')
		{
			struct CommentInfo ci;//added
			ci.firstline = yylineno;//added
			ci.firstcol = columnno;
			yyCommentLength = 0; //added

			int innercomments = 1;

			while (d != EOF)
			{
				d = get();
				if (d != '*' && d != '(')
				{
					if (yyCommentLength < LEXER_COMMENT_MAXLENGTH)
						ci.buffer[yyCommentLength++] = d;
					continue;
				}
				if (d == '(')
				{
					d = get();
					if (d == '*')
					{
						if (yyCommentLength < LEXER_COMMENT_MAXLENGTH)
							ci.buffer[yyCommentLength++] = '(';
						if (yyCommentLength < LEXER_COMMENT_MAXLENGTH)
							ci.buffer[yyCommentLength++] = '*';
						innercomments++;
					}
					else
					{
						if (yyCommentLength < LEXER_COMMENT_MAXLENGTH)
							ci.buffer[yyCommentLength++] = '(';
					}
					unget();
				    continue;
				}
				if (d == '*')
				{
					d = get();
					if (d == ')')
					{
						if (yyCommentLength < LEXER_COMMENT_MAXLENGTH)
							ci.buffer[yyCommentLength++] = '*';
						if (yyCommentLength < LEXER_COMMENT_MAXLENGTH)
							ci.buffer[yyCommentLength++] = ')';
						innercomments--;
						if (innercomments == 0)
						{
							yyCommentLength--; /* don't put the last ')' */
							break;
						}
					}
					else
					{
						if (yyCommentLength < LEXER_COMMENT_MAXLENGTH)
							ci.buffer[yyCommentLength++] = '*';
					}
					unget();
					continue;
				}
			}
			ci.buffer[yyCommentLength] = 0;
			ci.lastline  = yylineno; //added
			ci.lastcol   = columnno; //added
			ci.bound = 0;
			commentInfo[creg++] = ci;//added
			return yylex0(); /* comment: skip and return next token */
		}
		unget();
		iinner = 1;
		token = LPAR;
		break;

	case '-': /* '-' or horizontal bar */
		d = get();
		if (d == '-')
		{
			while (d == '-')
				d = get();
			unget();
			createBound(1);
			specboundline = yylineno;
			token = DASHES;
			break;
		}
		unget();
		token = MINUS;
		break;

	case '"': /* string constant */
		i = 0;
		while (1)
		{
			c = get();
			if (c == '"')
				break;
			if (c == '\\')
				c = esc_char(0);
			if (i < LEXER_STRING_MAXLENGTH-1)
				string[i++] = c;
			else
				yyerror("string buffer overflow");
		}
		string[i] = 0;
		token = SCON;
		val->u.string = strdup(string);
		//printf("String: %s", val->u.string);
		break;

	case '#':
		c = get();
		if (c != '"')
			yyerror("invalid character constant");
		c = get();
		if (c == '\\')
			c = esc_char(1);
		d = get();
		if (d != '"')
			yyerror("invalid character constant");
		val->u.number = c;
		token = CCON;
		break;

	case ')':
		iinner = 0;
		token = RPAR;
		break;
	case '.':
		token = DOT;
		break;
	case ',':
		token = COMMA;
		break;
	case '/':
		token = DIV;
		break;
	case '+':
		token = PLUS;
		break;
	case '*':
		specboundline = yylineno; //can be problem if an expression...
		if(!iinner)
			createBound(3);
		token = STAR;
		break;

	case '|':
		token = BAR;
		createBound(1);
		specboundline = yylineno;
		break;

	case '&':
		token = AMPERSAND;
		createBound(1);
		break;
	case '[':
		token = LBRACK;
		break;
	case ']':
		token = RBRACK;
		break;
	case '%':
		token = MOD_INT;
		break;

	default:
		if (isalpha(c) || c == '\'' || c == '_')
		{
			i = 0;
			do
			{
				if (i < 50)
				{
					ident[i++] = c;
#ifdef LEXER_TOKEN_POSITION
					val->lastline = yylineno;
					val->lastcol  = columnno;
#endif
				}
				c = get();
			}
			while (isalnum(c) || c == '\'' || c == '_');

			unget();
			ident[i] = 0;

			if (!keywords_sorted)
			{
				init_sort_keywords();
				keywords_sorted = 1;
			}

			for(i = 0; keywords[i].name; ++i) {
				if (!strcmp(ident, keywords[i].name)) {
					if((keywords[i].code == KW_RELATION) || (keywords[i].code == KW_RULE) ||
						(keywords[i].code == KW_END) || (keywords[i].code == KW_MODULE) || (keywords[i].code == KW_TYPE)
						|| (keywords[i].code == KW_VAL) || (keywords[i].code == KW_WITH)) {
							if(keywords[i].code == KW_END) {
								if(spreg != 0) {
									commentInfo[spreg].bound = 2;
									spreg = 0;
								}
							}
							createBound(1);
						}
					else if((keywords[i].code == KW_AXIOM) || (keywords[i].code == KW_DATATYPE)
						|| (keywords[i].code == KW_AND)){
							createBound(1);
							specboundline = yylineno;

						}
						return keywords[i].code;
				}
			}
			val->u.ident = strdup(ident);
			//printf("the string: %s \n\n",ident);
			return ident[0] == '\'' ? TYVARIDENT : IDENT;
		}
		else if (isdigit(c))
		{
			int is_float = 0;

			i = 0;

			for(val->u.number = 0; isdigit(c); c = get())
			{
				val->u.number = val->u.number*10 + c-'0';
				if (i < LEXER_IDENT_MAXLENGTH)
					ident[i++] = c;
			};
			if (c == '.')
			{
				do
				{
					if (i < LEXER_IDENT_MAXLENGTH)
						ident[i++] = c;
					c = get();
				} while (isdigit(c));
				is_float = 1;
			};
			if (c == 'e')
			{
				do
				{
					if (i < LEXER_IDENT_MAXLENGTH)
						ident[i++] = c;
					c = get();
				} while (isdigit(c) || c == '+' || c == '-');
				is_float = 1;
			};

			unget();

			if (is_float)
			{
				ident[i] = 0;
				if (sscanf(ident, "%lf", &(val->u.realnumber)) == 1)
				{
					//printf("Real value %f",val->u.realnumber);
					token = RCON;
					break;
				}
				yyerror("invalid real number");
			}

			token = ICON;
			break;
		}
		else
			yyerror("invalid input character");
	}

#ifdef LEXER_TOKEN_POSITION
	val->lastline = yylineno;
	val->lastcol  = columnno;
#endif
	return token;
}