Added a few more rules trying to get a feeling for how this will play out

This commit is contained in:
Jonathan Turner 2009-06-24 03:36:11 +00:00
parent f4efd62e65
commit 1abe806178
2 changed files with 86 additions and 7 deletions

View File

@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 2.6)
project(chaioop)
SET (CMAKE_BUILD_TYPE gdb)
SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb")
SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb")
SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb -O3")
SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb -O3")
include_directories(../langkit ../dispatchkit)

View File

@ -1,5 +1,10 @@
#include <iostream>
#include <vector>
#include <fstream>
#include <stdexcept>
#include <stdlib.h>
#include <string.h>
namespace langkit {
class Token {
@ -46,6 +51,24 @@ namespace langkit {
return retval;
}
bool Str(const char *s) {
bool retval = false;
int len = strlen(s);
if ((input_end - input_pos) >= len) {
std::string::iterator tmp = input_pos;
for (int i = 0; i < len; ++i) {
if (*tmp != s[i]) {
return false;
}
}
retval = true;
input_pos = tmp;
}
return retval;
}
bool Arg_List() {
bool retval = Id() || Int();
while (retval && Char(',')) {
@ -54,21 +77,77 @@ namespace langkit {
return retval;
}
bool Fun_Call() {
bool retval = false;
std::string::iterator prev = input_pos;
if (Id() && Char('(')) {
Arg_List();
retval = Char(')');
}
if (!retval) { input_pos = prev; }
return retval;
}
bool Eol() {
bool retval = false;
if ((input_pos != input_end) && (Str("\r\n") || Char('\n'))) {
retval = true;
}
return retval;
}
bool Fun_Calls() {
bool retval = false;
while ((Fun_Call() && Eol()) || (Eol())) { }
if (input_pos == input_end) {
retval = true;
}
return retval;
}
bool parse(std::string input) {
input_pos = input.begin();
input_end = input.end();
return Id() && Char('(') && Arg_List() && Char(')');
return Fun_Calls();
}
};
};
int main() {
std::string load_file(const char *filename) {
std::ifstream infile (filename, std::ios::in | std::ios::ate);
if (!infile.is_open()) {
std::string fname = filename;
throw std::runtime_error("Can not open: " + fname);
}
std::streampos size = infile.tellg();
infile.seekg(0, std::ios::beg);
std::vector<char> v(size);
infile.read(&v[0], size);
std::string ret_val (v.empty() ? std::string() : std::string (v.begin(), v.end()).c_str());
return ret_val;
}
int main(int argc, char *argv[]) {
langkit::Parser parser;
std::cout << "Hello, world" << std::endl;
std::cout << parser.parse("e(x,10)") << std::endl;
if (argc > 1) {
std::cout << parser.parse(load_file(argv[1])) << std::endl;
}
return 0;
}