1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream> //standard i/o streams
#include <fstream> //standard file i/o stream
#include <string> //basic_string(char) containter
#include <vector>
#include <regex>
using std::ostream;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::ifstream;
using std::string;
using std::vector;
using std::regex;
using std::regex_search;
using std::smatch;
class Token
{
string Type, Name;
int LineNo, LineCol;
public:
Token(string type, string name, int lineno, int linecol)
: Type(type), Name(name), LineNo(lineno), LineCol(linecol) {}
Token() {}
void SetType (string type) { Type = type; }
void SetName (string name) { Name = name; }
void SetLineNo (int lineno) { LineNo = lineno; }
void SetLineCol(int linecol) { LineCol = linecol; }
string GetType () { return Type; }
string GetName () { return Name; }
int GetLineNo () { return LineNo; }
int GetLineCol() { return LineCol; }
};
ostream& operator<<(ostream &out, Token token)
{
out<<"("<< token.GetType() <<", '"<< token.GetName() <<"', "<< token.GetLineNo() <<", "<< token.GetLineCol() <<")";
return out;
}
void ReadInFile (ifstream&, vector<string>&);
vector<Token> Lex (vector<string>&);
int main(int argc, char *argv[])
{
ifstream File(argv[1]);
vector<string> FileContents(1);
ReadInFile(File, FileContents);
vector<Token> TokenList = Lex(FileContents);
for(auto &Counter : TokenList)
cout<< Counter << endl;
}
void ReadInFile(ifstream &File, vector<string> &FileContents)
{
int Counter = 0;
while(getline(File, FileContents[Counter++]))
FileContents.resize(FileContents.size() + 1);
}
vector<Token> Lex(vector<string> &FileContents)
{
smatch Match;
vector<Token> TokenList;
int LineNo = 1;
for(auto &Counter : FileContents)
{
if(regex_search(Counter, Match, regex("function")))
TokenList.push_back(Token("FUNCTION", "function", LineNo, Match.position()));
LineNo++;
}
return TokenList;
}
|