From 96010ea9ae2d814e2322813028553a70ef6fb820 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 15 May 2010 00:43:55 -0400 Subject: [PATCH] lexer working with pcre, not building any objects for handling tokens yet --- .gitignore | 1 + tests/build/Makefile | 2 +- tests/build/itest.I | 11 ++++++++--- tests/build/main.cc | 16 ++++++++++++++++ tmpl/parser.cc | 12 +++++++++++- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 tests/build/main.cc diff --git a/.gitignore b/.gitignore index 5f8b7e8..bcf881b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ tags tmpl.* tests/*/itest.cc tests/*/itest.h +tests/*/test diff --git a/tests/build/Makefile b/tests/build/Makefile index 078fd11..158a360 100644 --- a/tests/build/Makefile +++ b/tests/build/Makefile @@ -6,7 +6,7 @@ LDFLAGS := -lpcre all: $(TARGET) -$(TARGET): +$(TARGET): $(shell which imbecile) imbecile $(I_SOURCE).I $(CXX) -o $@ *.cc $(LDFLAGS) diff --git a/tests/build/itest.I b/tests/build/itest.I index 9c99b75..986921e 100644 --- a/tests/build/itest.I +++ b/tests/build/itest.I @@ -1,9 +1,14 @@ [tokens] -ASSIGN := -DASSIGN :== -IDENTIFIER [a-zA-Z_][a-zA-Z_0-9]* +AND and +OR or +NOT not +LPAREN \( +RPAREN \) +WS \s+ +EQUALS = +IDENTIFIER [a-zA-Z_][a-zA-Z_0-9]* [rules] diff --git a/tests/build/main.cc b/tests/build/main.cc new file mode 100644 index 0000000..924c9f4 --- /dev/null +++ b/tests/build/main.cc @@ -0,0 +1,16 @@ + +#include +#include + +#include "itest.h" + +using namespace std; + +int main(int argc, char * argv[]) +{ + Parser p; + stringstream t(string( + "hi there (one and two and three and four) or (two = nine)" + )); + p.parse(t); +} diff --git a/tmpl/parser.cc b/tmpl/parser.cc index e03a327..3e15416 100644 --- a/tmpl/parser.cc +++ b/tmpl/parser.cc @@ -88,7 +88,7 @@ bool I_CLASSNAME::parse(istream & i) while (buff_pos < buff_size) { int longest_match_length = 0; - int longest_match_index; + int longest_match_index = -1; int longest_match_ovector[ovector_size]; for (int i = 0; i < sizeof(tokens)/sizeof(tokens[0]); i++) { @@ -108,6 +108,16 @@ bool I_CLASSNAME::parse(istream & i) } } } + if (longest_match_index >= 0) + { + cout << "Matched a " << tokens[longest_match_index].name << endl; + buff_pos += longest_match_length; + } + else + { + /* no pattern matched the input at the current position */ + return false; + } } }