|
/**
Copyright 2000 - 2003 LogicMatrix. All rights reserved.
This software is distributed under the LogicMatrix Free Software License. This software may be used for any purpose, personal or commercial. Redistributions in binary /source code form are permitted. Commercial redistribution of larger works derived from, or works which bundle this software requires a "Commercial Redistribution License" which can be purchased from LogicMatrix. Contact LogicMatrix for details
Redistributions qualify as Free and non-commercial under one of the following terms:
1) Redistributions are made at no charge beyond the reasonable cost of materials and delivery.
2) Redistributions in binary/source code form must reproduce this Copyright Notice,these license terms, and the disclaimer/limitation of liability set forth as below, in the documentation and/or other materials
provided with the distribution.
Disclaimer
==========
The Software is provided on an "AS IS" basis. No warranty is
provided that the Software is free of defects, or fit for a
particular purpose.
Limitation of Liability
=======================
LogicMatrix shall not be liable for any damages suffered by the Licensee or any third party resulting from use of the Software.
**/
/**
sql_lexer.cpp
Purpose: Defines a lexer for sql commands
Author: Vijay Mathew Pandyalakal
Date: 14/11/2003
Copyright: logicmatrix
**/
#include <string>
#include <vector>
#include <iostream>
using namespace std;
#include "mydate.h"
using namespace openutils;
#include "dsql_m_structs.h"
#include "text.h"
#include "sql_lexer.h"
using namespace dsqlm;
SQLLexer::SQLLexer(const char* sql) {
this->sql = sql;
}
void SQLLexer::setSql(const char* sql) {
this->sql = sql;
}
string SQLLexer::getSql() {
return sql;
}
vector<string> SQLLexer::tokenize() {
vector<string> ret;
int len = sql.length();
string tmp = "";
char c = ' ';
char pc = ' ';
bool in_chords = false;
/*char* buff = new char[len+1];
strcpy(buff,sql.c_str());
sql = "";*/
for(int i=0;i<len;i++) {
c = sql[i];
if(c == '>' || c == '<') {
if(!in_chords) {
if(i == (len-1)) {
throw DsqlMException("Invalid use of >");
}else {
char next = sql[i+1];
if(next == '=' || next == '>') {
ret.push_back(tmp);
tmp = "";
tmp += c;
tmp += next;
ret.push_back(tmp);
tmp = "";
i++;
continue;
}
}
}
}
if(isBreakableToken(i)) {
if(in_chords) {
tmp += c;
}else {
ret.push_back(tmp);
tmp = "";
if(c != ' ') {
tmp += c;
ret.push_back(tmp);
tmp = "";
}
}
}else if(c == '(' || c== ')' || c == ',') {
if(in_chords) {
tmp += c;
}else {
ret.push_back(tmp);
tmp = "";
tmp += c;
ret.push_back(tmp);
tmp = "";
}
}else if(c == '\'' || c == '\"') {
if(in_chords) {
if(pc == '\\') {
tmp += c;
}else {
tmp += c;
ret.push_back(tmp);
tmp = "";
in_chords = false;
}
}else {
ret.push_back(tmp);
tmp = "";
tmp += c;
in_chords = true;
}
}else if(c == '\\') {
if(!in_chords) {
tmp += c;
}
}else {
tmp += c;
}
pc = c;
}
/*sql = buff;
delete[] buff;*/
if(tmp != "") {
ret.push_back(tmp);
}
ret = trim(ret);
return ret;
}
vector<string> SQLLexer::trim(vector<string> vct) {
int sz = vct.size();
vector<string> ret;
string s = "";
Text text;
for(int i=0;i<sz;i++) {
s = vct[i];
s = text.trim(s);
if(s == "") continue;
ret.push_back(s);
}
return ret;
}
void SQLLexer::printTokens(vector<string> vct) {
for(int i=0;i<vct.size();i++) {
cout << vct[i] << endl;
}
}
bool SQLLexer::isBreakableToken(int i) {
char curr = sql[i];
if(i >= sql.size()) {
return false;
}
if(curr == ' ') return true;
if(curr == '=') return true;
if(curr == '<') return true;
if(curr == '>') return true;
return false;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.