📄 lex.cpp
字号:
#include "stdafx.h"
#include "Lex.h"
#include "string.h"
#include "Type.h"
#include "Token.h"
#include <ctype.h>
Lex::Lex(char *p)
{
this->sql=new char[strlen(p)+1];
strcpy(this->sql,p);
ptr=0;
end=strlen(this->sql);
token=new Token();
}
bool Lex::hasNext()
{
return(ptr<(end-1));
}
void Lex::back()
{
ptr=backp;
}
Token* Lex::getNextToken()
{
Type type1;
if(ptr>=end)
return NULL;
backp=ptr;
int i=0;
char temp[20];
char c;
c=getAt(this->sql,ptr);
if(c=='*')
{
temp[i++]=c;
token->type=type1.STAR;
ptr++;
temp[i]='\0';
if(c=getAt(this->sql,ptr)==' ')ptr++;
}
else if(c==',')
{
temp[i++]=c;
token->type=type1.DIVIDE;
ptr++;
temp[i]='\0';
if(c=getAt(this->sql,ptr)==' ')ptr++;
}
else if (isCompare(c)) {
temp[i++]=c;
token->type=type1.COMPARE;
ptr++;
temp[i]='\0';
if(c=getAt(this->sql,ptr)==' ')ptr++;
}
else if (isBracket(c)) {
temp[i++]=c;
token->type=type1.BRACKET;
ptr++;
temp[i]='\0';
if(c=getAt(this->sql,ptr)==' ')ptr++;
}
else {
if (c == '\'') {
c=getAt(this->sql,++ptr);
while (ptr <= end && c != '\'' && c != ' ') {
temp[i++]=c;
c=getAt(this->sql,++ptr);
}
if (c == '\'') {
token->type = type1.VARCHAR;
ptr++;
}
}
else if (isalpha(c)) {
temp[i++]=c;
c=getAt(this->sql,++ptr);
while (ptr <= end && isdigit(c)||isalpha(c)) {
temp[i++]=c;
c=getAt(this->sql,++ptr);
}
if(c == '.'){
temp[i++]=c;
c=getAt(this->sql,++ptr);
if(isalpha(c)){
while (ptr <= end && (isdigit(c)||isalpha(c))) {
temp[i++]=c;
c=getAt(this->sql,++ptr);
}
}
}
if(c=getAt(this->sql,ptr)==' ')ptr++;
temp[i]='\0';
char *tmp;
tmp=new char[i];
tmp=&temp[0];
if (strcmp(tmp,"and")==0 ||strcmp(tmp,"or")==0){
token->type = type1.LOGIC;
}
else if(strcmp(tmp,"select")==0 ||strcmp(tmp,"from")==0 ||strcmp(tmp,"where")==0){
token->type = type1.KEY;
}
else {
token->type = type1.ATTRI;
}
}
else if (isdigit(c)) {
temp[i++]=c;
c=getAt(this->sql,++ptr);
while (isdigit(c)) {
c=getAt(this->sql,ptr);
temp[i++]=c;
c=getAt(this->sql,++ptr);
}
if(c == '.'){
temp[i++]=c;
c=getAt(this->sql,++ptr);
if(isdigit(c)){
while (isdigit(c)) {
temp[i++]=c;
c=getAt(this->sql,++ptr);
}
}
token->type = type1.DOUBLE;
}
else{
token->type = type1.INTEGER;
}
temp[i]='\0';
if(c=getAt(this->sql,ptr)==' ')ptr++;
}
}
token->field=new char[i];
strcpy(token->field,temp);
return token;
}
bool Lex::isCompare(char c)
{
if(c=='='||c=='>'||c=='<')
return true;
else
return false;
}
bool Lex::isBracket(char c)
{
if(c=='('||c==')')
return true;
else
return false;
}
Token* Lex::getToken()
{
return token;
}
char Lex::getAt(char *p,int i)
{
for(int j=0;j<i;j++)
p++;
char c=*p;
return c;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -