File indexing completed on 2024-05-05 04:37:08

0001 
0002 #include "coolparser.h"
0003 #include "cooldefaultvisitor.h"
0004 #include "decoder.h"
0005 
0006 #include <cstdlib>
0007 #include <iostream>
0008 #include <fstream>
0009 
0010 using namespace cool;
0011 
0012 static void usage(char const* argv0);
0013 static bool parse_file(char const* filename);
0014 
0015 
0016 void print_token_environment(Parser* parser)
0017 {
0018     static bool done = false;
0019     if (done)
0020       return; // don't print with each call when going up the error path
0021 
0022     decoder dec(parser->tokenStream, parser);
0023 
0024     std::size_t current_index = parser->tokenStream->index() - 1;
0025     for (std::size_t i = current_index - 5; i < current_index + 5; ++i)
0026       {
0027         if (i < 0 || i >= parser->tokenStream->size())
0028           continue;
0029 
0030         if (i == current_index)
0031           std::cerr << ">>";
0032 
0033         std::cerr << qPrintable(dec.decode_id(i)); // print out currently processed token
0034 
0035         if (i == current_index)
0036           std::cerr << "<<";
0037 
0038         std::cerr << " ";
0039       }
0040     std::cerr << std::endl;
0041 
0042     done = true;
0043 }
0044 
0045 
0046 int main(int argc, char *argv[])
0047 {
0048   if (argc == 1)
0049     {
0050       usage(argv[0]);
0051       exit(EXIT_FAILURE);
0052     }
0053   while (char const *arg = *++argv)
0054     {
0055       /*if (!strncmp(arg, "--option=", 9))
0056         {
0057           char const* option = arg + 9;
0058 
0059           std::cerr << "--option=" << option
0060                     << " has been given!" << std::endl;
0061         }
0062       else */
0063       if (!strncmp(arg, "--", 2))
0064         {
0065           std::cerr << "Unknown option: " << arg << std::endl;
0066           usage(argv[0]);
0067           exit(EXIT_FAILURE);
0068         }
0069       else if(!parse_file(arg))
0070         {
0071           exit(EXIT_FAILURE);
0072         }
0073     }
0074 
0075   return EXIT_SUCCESS;
0076 }
0077 
0078 bool parse_file(char const *filename)
0079 {
0080   char *contents;
0081   std::ifstream filestr(filename);
0082 
0083   if (filestr.is_open())
0084     {
0085       std::filebuf *pbuf;
0086       long size;
0087 
0088       // get pointer to associated buffer object
0089       pbuf = filestr.rdbuf();
0090 
0091       // get file size using buffer's members
0092       size = pbuf->pubseekoff(0,std::ios::end,std::ios::in);
0093       pbuf->pubseekpos(0,std::ios::in);
0094 
0095       // allocate memory to contain file data
0096       contents = new char[size+1];
0097 
0098       // get file data
0099       pbuf->sgetn(contents, size);
0100 
0101       contents[size] = '\0';
0102 
0103       filestr.close();
0104     }
0105   else
0106     {
0107       std::cerr << filename << ": file not found" << std::endl;
0108       return false;
0109     }
0110 
0111   KDevPG::TokenStream token_stream;
0112   Parser::memoryPoolType memory_pool;
0113 
0114   // 0) setup
0115   Parser cool_parser;
0116   cool_parser.setTokenStream(&token_stream);
0117   cool_parser.setMemoryPool(&memory_pool);
0118 
0119   // 1) tokenize
0120   cool_parser.tokenize(contents);
0121 
0122   // 2) parse
0123   ProgramAst *ast = 0;
0124   bool matched = cool_parser.parseProgram(&ast);
0125   if (matched)
0126     {
0127       DefaultVisitor v;
0128       v.visitNode(ast);
0129     }
0130   else
0131     {
0132       cool_parser.expectedSymbol(AstNode::ProgramKind, "program"); // ### remove me
0133     }
0134 
0135   delete[] contents;
0136 
0137   return matched;
0138 }
0139 
0140 static void usage(char const* argv0)
0141 {
0142   std::cerr << "usage: " << argv0 /*<< " [options]"*/ << " file1.cl [file2.cl...]"
0143     << std::endl; /*<< std::endl
0144     << "Options:" << std::endl
0145     << "  --option=BLA: Do BLAH while parsing." << std::endl
0146     << "                BLAH is one of FOO, BAR or KUNG, default is FOO."
0147     << std::endl;
0148     */
0149 }