From a5eb6486f46e2b45f372b75d281f185c4da7b6c9 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Sun, 26 Feb 2012 14:29:04 -0500 Subject: [PATCH] Refactored and fixed some memory leaks --- deps/cork | 2 +- example.dl | 120 ++++++------ rakefile.rb | 8 +- source/cork/cork.cpp | 183 ------------------ source/cork/cork.h | 18 -- source/dllexer/dllexer.cpp | 28 ++- source/dllexer/dllexer.h | 2 +- source/dlparser/dlparser.cpp | 49 ++--- source/dlparser/dlparser.h | 157 ++++++++------- source/dlparser/macro/macro.c | 5 + source/main.cpp | 15 +- source/parse_utils/lexer/ilexer.h | 2 +- source/parse_utils/lexer/token/token.cpp | 25 +++ source/parse_utils/lexer/token/token.h | 9 + .../parse_utils/parser/btparser/btparser.cpp | 12 +- source/parse_utils/parser/btparser/btparser.h | 6 +- source/parse_utils/parser/iparser.h | 2 +- .../parser/llkparser/llkparser.cpp | 13 +- .../parse_utils/parser/llkparser/llkparser.h | 6 +- 19 files changed, 255 insertions(+), 407 deletions(-) delete mode 100644 source/cork/cork.cpp delete mode 100644 source/cork/cork.h diff --git a/deps/cork b/deps/cork index 244613f..dbd1bb9 160000 --- a/deps/cork +++ b/deps/cork @@ -1 +1 @@ -Subproject commit 244613ffd2288f8d51ecab2c6743f1a1c28d6537 +Subproject commit dbd1bb9634cee2c271477bb32b8556f017be5559 diff --git a/example.dl b/example.dl index cb01400..785218f 100644 --- a/example.dl +++ b/example.dl @@ -2,81 +2,81 @@ # Macro Tests #------------------------------------------------------------------------------ # Macro Definitions -% if ( cond, branch1:Block ) { - exec_if( cond, branch1 ) -} - -% ifelse ( cond, branch1:Block, branch2:Block ) { - exec_if( cond, branch1, branch2 ) -} - -% foreach ( lst, fn:Block ) { - for_each(fn, lst) -} +#% if ( cond, branch1:Block ) { +# exec_if( cond, branch1 ) +#} +# +#% ifelse ( cond, branch1:Block, branch2:Block ) { +# exec_if( cond, branch1, branch2 ) +#} +# +#% foreach ( lst, fn:Block ) { +# for_each(fn, lst) +#} # Macro Uses -if (1 == 1) -{ - print("1 Hello, World!") -} - -if (1 == 0) -{ - print("Hello, World!") -} - -ifelse (1 == 1) -{ - print("2 Hello, World!") -}{ - error("Incorrect branch was taken.") -} - -ifelse (1 == 0) -{ - error("Incorrect branch was taken.") -}{ - print("3 Hello, World!") -} +#if (1 == 1) +#{ +# print("1 Hello, World!") +#} +# +#if (1 == 0) +#{ +# print("Hello, World!") +#} +# +#ifelse (1 == 1) +#{ +# print("2 Hello, World!") +#}{ +# error("Incorrect branch was taken.") +#} +# +#ifelse (1 == 0) +#{ +# error("Incorrect branch was taken.") +#}{ +# print("3 Hello, World!") +#} #------------------------------------------------------------------------------ # Collection Access and Iteration Tests #------------------------------------------------------------------------------ # Accessing elements of lists, vectors, and strings -lst1 = `(4,5,6) -print(lst1[0], " Hello, World!") - -vec1 = [4,5,6] -print(vec1[1], " Hello, World!") - -str1 = "456" -print(str1[2], " Hello, World!") - -# Iterating over lists, vectors, and strings -lst_foo = `(7,8,9,10) -foreach lst_foo { |a| - print(a," Hello, World!") -} - -vec_foo = [11,12,13,14,15] -foreach vec_foo { |a,b| - print(b," Hello, World!") -} - -str_foo = "6789" -foreach str_foo { |a| - print(1,a," Hello, World!") -} +#lst1 = `(4,5,6) +#print(lst1[0], " Hello, World!") +# +#vec1 = [4,5,6] +#print(vec1[1], " Hello, World!") +# +#str1 = "456" +#print(str1[2], " Hello, World!") +# +## Iterating over lists, vectors, and strings +#lst_foo = `(7,8,9,10) +#foreach lst_foo { |a| +# print(a," Hello, World!") +#} +# +#vec_foo = [11,12,13,14,15] +#foreach vec_foo { |a,b| +# print(b," Hello, World!") +#} +# +#str_foo = "6789" +#foreach str_foo { |a| +# print(1,a," Hello, World!") +#} #------------------------------------------------------------------------------ # Delayed Evaluation #------------------------------------------------------------------------------ % delay ( exp ) { - make_promise({ exp }) + make_promise({ exp }) } % force ( prom ) { - force( prom ) + force( prom ) } foo = delay nonexistent_var + 1 diff --git a/rakefile.rb b/rakefile.rb index 0b374aa..4348f0a 100644 --- a/rakefile.rb +++ b/rakefile.rb @@ -10,9 +10,13 @@ DLangParser = Binary.new({ :name => 'dlang', :output_dir => 'build/parser', :compiler_options => [ '-c', '-Wall', '-Werror', '-o' ], + :static_libs => [ './deps/cork/build/static/bin/libcork.a' ], :source_files => [ 'source/**/*.c*' ], - :include_dirs => [ 'source/**/' ], - :preprocessor_defines => [ 'DETECT_MEM_LEAKS' ] + :include_dirs => [ + 'source/**/', + 'deps/cork/source/**/' + ], + #:preprocessor_defines => [ 'DETECT_MEM_LEAKS' ] }) DLangParser.setup_default_rake_tasks() diff --git a/source/cork/cork.cpp b/source/cork/cork.cpp deleted file mode 100644 index 0a298fa..0000000 --- a/source/cork/cork.cpp +++ /dev/null @@ -1,183 +0,0 @@ -#include "cork.h" - -#ifdef DETECT_MEM_LEAKS - -// We want to use the real malloc and free in this file -#undef malloc -#undef free - -#include -#include // for std::bad_alloc -#include // for malloc() and free() -#include - -// Set the namespace -using namespace std; -/****************************************************************************** - * Typedefs - *****************************************************************************/ -typedef struct BlockTableEntry -{ - void * ptr; - unsigned int size; - const char* file; - int line; - void * next; -} BlockTableEntry_T; - -typedef struct BlockTable -{ - unsigned int size; - BlockTableEntry_T* blocks[TBL_SIZE]; -} BlockTable_T; - -/****************************************************************************** - * Prototypes - *****************************************************************************/ -void insert_record(void * ptr, BlockTable_T* entry); -void erase_record(void * ptr); - -/****************************************************************************** - * Globals - *****************************************************************************/ -unsigned int allocated; -static BlockTable_T Block_Table = { 0, {0} }; - -/****************************************************************************** - * Function Definitions - *****************************************************************************/ -void insert_record(void * ptr, BlockTableEntry_T* entry) -{ - unsigned int index = ((unsigned int)ptr) % TBL_SIZE; - BlockTableEntry_T* last = Block_Table.blocks[ index ]; - BlockTableEntry_T* curr = last; - - while (curr != NULL) - { - if ( curr->ptr == ptr ) - { - curr->size = entry->size; - free(entry); - break; - } - last = curr; - curr = (BlockTableEntry_T*)curr->next; - } - - if(curr == NULL) - { - if (last != NULL) - { - last->next = entry; - } - else - { - Block_Table.blocks[index] = entry; - } - Block_Table.size++; - } -} - -void erase_record(void * ptr) -{ - int depth = 0; - unsigned int index = ((unsigned int)ptr) % TBL_SIZE; - BlockTableEntry_T* last = Block_Table.blocks[ index ]; - BlockTableEntry_T* curr = last; - - while( curr != NULL ) - { - depth = 1; - if( curr->ptr == ptr ) - { - depth = 2; - if(last == curr) - { - depth = 3; - Block_Table.blocks[ index ] = (BlockTableEntry_T*)curr->next; - } - else - { - depth = 4; - last->next = curr->next; - } - free(curr); - Block_Table.size--; - break; - } - last = curr; - curr = (BlockTableEntry_T*)curr->next; - } -} - -void Cork_ReportMemoryLeaks(void) -{ - unsigned int index = 0; - cout << "-----------------------------------------------------------------" << endl; - cout << "Cork: Memory Allocation Analysis" << endl; - cout << "-----------------------------------------------------------------" << endl; - cout << "You have " << Block_Table.size << " Unclaimed objects." << endl; - - for(; index < TBL_SIZE; index++) - { - BlockTableEntry_T* entry = Block_Table.blocks[ index ]; - while(entry != NULL) - { - cout << "\t" << entry->size << "\t" << entry->ptr; - if( entry->file != NULL ) - { - cout << "\t" << entry->line << "\t" << entry->file; - } - cout << endl; - entry = (BlockTableEntry_T*)entry->next; - } - } -} - -void * operator new (size_t size, string file, unsigned int line) -{ - void * ptr = malloc(size); - char * fname = (char*)malloc(file.length()); - if(ptr == NULL) - { - throw bad_alloc(); - } - else - { - BlockTableEntry_T* entry = (BlockTableEntry_T*)malloc(sizeof(BlockTableEntry_T)); - strcpy( fname, file.c_str() ); - entry->ptr = ptr; - entry->size = size; - entry->file = fname; - entry->line = line; - entry->next = NULL; - insert_record(ptr,entry); - } - return ptr; -} - -void * operator new(size_t size) throw(bad_alloc) { - void * ptr = malloc(size); - if(ptr == NULL) - { - throw bad_alloc(); - } - else - { - BlockTableEntry_T* entry = (BlockTableEntry_T*)malloc(sizeof(BlockTableEntry_T)); - entry->ptr = ptr; - entry->size = size; - entry->file = NULL; - entry->line = 0; - entry->next = NULL; - insert_record(ptr,entry); - } - return ptr; -} - -void operator delete(void * p) { - free(p); - erase_record(p); -} - -#endif diff --git a/source/cork/cork.h b/source/cork/cork.h deleted file mode 100644 index 6af4f20..0000000 --- a/source/cork/cork.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef CORK_H -#define CORK_H - -#ifdef DETECT_MEM_LEAKS - #include - typedef unsigned int size_t; - - void Cork_ReportMemoryLeaks(void); - void * operator new (size_t size, std::string file, unsigned int line); - #define TBL_SIZE 512 - #define REPORT_LEAKS() Cork_ReportMemoryLeaks() - #define _new new (__FILE__,__LINE__) -#else - #define REPORT_LEAKS() - #define _new new -#endif - -#endif diff --git a/source/dllexer/dllexer.cpp b/source/dllexer/dllexer.cpp index 48f0773..83c9b3a 100644 --- a/source/dllexer/dllexer.cpp +++ b/source/dllexer/dllexer.cpp @@ -58,10 +58,11 @@ bool DLLexer::isStringChar(void) && (current != '\n')); } -Token* DLLexer::next(void) +Token DLLexer::next(void) { - Token* ret = NULL; - while ( (!input->eof()) && (ret == NULL) ) + Token ret; + Token* temp = NULL; + while ( (!input->eof()) && (temp == NULL) ) { if (isWhiteSpace()) { @@ -73,33 +74,40 @@ Token* DLLexer::next(void) } else if (isLetter()) { - ret = Id(); + temp = Id(); } else if( isOperator() ) { - ret = MultiCharOp(); + temp = MultiCharOp(); } else if (isDigit()) { - ret = Number(); + temp = Number(); } else if(current == '\'') { - ret = Char(); + temp = Char(); } else if(current == '"') { - ret = String(); + temp = String(); } else if(current == '$') { - ret = Symbol(); + temp = Symbol(); } else { - ret = SingleCharOp(); + temp = SingleCharOp(); } } + + if(temp != NULL) + { + ret = *(temp); + delete temp; + } + return ret; } diff --git a/source/dllexer/dllexer.h b/source/dllexer/dllexer.h index e27bfe8..e93e7ad 100644 --- a/source/dllexer/dllexer.h +++ b/source/dllexer/dllexer.h @@ -67,7 +67,7 @@ class DLLexer : public ILexer { void WS(void); void COMMENT(void); - Token* next(void); + Token next(void); Token* Id(void); Token* Number(void); Token* Decimal(std::ostringstream& oss); diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index f7f547d..7692b8d 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -2,24 +2,27 @@ #include "exception.h" #include "cork.h" -DLParser::DLParser() : BTParser(_new DLLexer()), ast(NULL) +DLParser::DLParser() : BTParser(_new DLLexer()) { } -void DLParser::parse(void) +DLParser::~DLParser() { - ast = Program(); + std::map::iterator iter; + for (iter = macros.begin(); iter != macros.end(); ++iter) { + delete (iter->second); + } } -AST* DLParser::getAST(void) +AST* DLParser::parse(void) { - return ast; + return Program(); } -bool DLParser::isMacro( Token* token ) +bool DLParser::isMacro( Token& token ) { bool ret = false; - if( (token->type() == ID) && (macros.find(token->text()) != macros.end()) ) + if( (token.type() == ID) && (macros.find(token.text()) != macros.end()) ) { ret = true; } @@ -40,10 +43,10 @@ AST* DLParser::parseMacroParam(Param* param) break; default: - Token* tok = lookaheadToken(1); + Token& tok = lookaheadToken(1); ostringstream oss; - oss << "Expected macro parameter type. Expected " << param->type() << ", received " << tok->type() << "."; - Exception ex( tok->line(), tok->column() ); + oss << "Expected macro parameter type. Expected " << param->type() << ", received " << tok.type() << "."; + Exception ex( tok.line(), tok.column() ); ex.setMessage(oss.str()); break; } @@ -65,7 +68,7 @@ AST* DLParser::Expression(void) AST* ret = NULL; if((lookaheadType(1) == ID) && (lookaheadType(2) == ASSIGN)) { - AST* id_node = _new AST( ID,(char*)(lookaheadToken(1)->text().c_str()) ); + AST* id_node = _new AST( ID,(char*)(lookaheadToken(1).text().c_str()) ); consume(); match(ASSIGN); ret = _new AST( ASSIGN, 2, id_node, Expression()); @@ -200,39 +203,39 @@ AST* DLParser::Literal(void) // Literal = ID case ID: - node = _new AST( ID, lookaheadToken(1)->text() ); + node = _new AST( ID, lookaheadToken(1).text() ); consume(); break; // Literal = NUM case NUM: - node = _new AST( NUM, lookaheadToken(1)->text() ); + node = _new AST( NUM, lookaheadToken(1).text() ); consume(); break; // Literal = CHAR case CHAR: - node = _new AST( CHAR, lookaheadToken(1)->text() ); + node = _new AST( CHAR, lookaheadToken(1).text() ); consume(); break; // Literal = STRING case STRING: - node = _new AST( STRING, lookaheadToken(1)->text() ); + node = _new AST( STRING, lookaheadToken(1).text() ); consume(); break; // Literal = SYMBOL case SYMBOL: - node = _new AST( SYMBOL, lookaheadToken(1)->text() ); + node = _new AST( SYMBOL, lookaheadToken(1).text() ); consume(); break; default: - Token* tok = lookaheadToken(1); + Token& tok = lookaheadToken(1); ostringstream oss; - oss << "Expected literal type, recieved type " << tok->type() << "."; - Exception ex( tok->line(), tok->column() ); + oss << "Expected literal type, recieved type " << tok.type() << "."; + Exception ex( tok.line(), tok.column() ); ex.setMessage(oss.str()); throw ex; } @@ -288,7 +291,7 @@ AST* DLParser::MacroDefinition(void) Macro* macro = NULL; match(MACRO); - id = _new AST( ID, lookaheadToken(1)->text() ); + id = _new AST( ID, lookaheadToken(1).text() ); consume(); match(LPAR); params = MacroParamList(); @@ -307,7 +310,7 @@ AST* DLParser::MacroDefinition(void) AST* DLParser::MacroExpansion(void) { AST* ret = NULL; - Macro* cur_macro = macros[ lookaheadToken(1)->text() ]; + Macro* cur_macro = macros[ lookaheadToken(1).text() ]; list::const_iterator it = cur_macro->params().begin(); consume(); @@ -336,12 +339,12 @@ AST* DLParser::MacroParamList(void) // MacroParam = ID (':' ID)? AST* DLParser::MacroParam(void) { - AST* ret = _new AST( ID, lookaheadToken(1)->text() ); + AST* ret = _new AST( ID, lookaheadToken(1).text() ); consume(); if( lookaheadType(1) == SEP ) { match(SEP); - AST* type = _new AST( ID, lookaheadToken(1)->text() ); + AST* type = _new AST( ID, lookaheadToken(1).text() ); consume(); ret = _new AST(SEP, 2, ret, type); } diff --git a/source/dlparser/dlparser.h b/source/dlparser/dlparser.h index d00d1dd..5ad2487 100644 --- a/source/dlparser/dlparser.h +++ b/source/dlparser/dlparser.h @@ -1,5 +1,5 @@ #ifndef DLPARSER_H -#define DLPARSER_H +#define DLPARSER_H #include #include "btparser.h" @@ -8,85 +8,84 @@ class DLParser : public BTParser { - private: - AST* ast; - std::map macros; - public: - DLParser(); - void parse(void); - AST* getAST(void); - bool isMacro(Token* token); - AST* parseMacroParam(Param* param); - - /********************************************************************** - * EBNF Syntax Grammar - *********************************************************************/ - // Program = Expression* - // - // Expression = ID '=' LogicalExpr - // | MacroDefinition - // | $MacroExpansion$ - // | LogicalExpr - // - // LogicalExpr = CompExpr (('&&' | '||') CompExpr)* - // - // CompExpr = AddSubExpr (('==' | '!=' | '<' | '>' | '<=' | '>=') AddSubExpr)* - // - // AddSubExpr = MulDivExpr (('+' | '-') MulDivExpr)* - // - // MulDivExpr = UnaryExpr (('*' | '/') UnaryExpr)* - // - // UnaryExpr = '!' GroupExpr - // | GroupExpr - // - // GroupExpr = '(' LogicalExpr ')' - // | Literal '(' ExpList ')' - // | Literal - // + private: + std::map macros; + public: + DLParser(); + ~DLParser(); + AST* parse(void); + bool isMacro(Token& token); + AST* parseMacroParam(Param* param); + + /********************************************************************** + * EBNF Syntax Grammar + *********************************************************************/ + // Program = Expression* + // + // Expression = ID '=' LogicalExpr + // | MacroDefinition + // | $MacroExpansion$ + // | LogicalExpr + // + // LogicalExpr = CompExpr (('&&' | '||') CompExpr)* + // + // CompExpr = AddSubExpr (('==' | '!=' | '<' | '>' | '<=' | '>=') AddSubExpr)* + // + // AddSubExpr = MulDivExpr (('+' | '-') MulDivExpr)* + // + // MulDivExpr = UnaryExpr (('*' | '/') UnaryExpr)* + // + // UnaryExpr = '!' GroupExpr + // | GroupExpr + // + // GroupExpr = '(' LogicalExpr ')' + // | Literal '(' ExpList ')' + // | Literal + // // Literal = VectorLiteral - // | ListLiteral - // | FuncLiteral - // | ID - // | NUM - // | CHAR - // | STRING - // | SYMBOL - // - // VectorLiteral = '[' ExpList ']' - // - // ListLiteral = '`' '(' ExpList ')' - // - // FuncLiteral = '{' ExpBlock '}' - // | '{' '|' ExpList '|' ExpBlock '}' - // - // MacroDefinition = '%' ID '(' MacroParamList ')' '{' Expression '}' - // - // MacroParamList = MacroParam (',' MacroParam)* - // - // MacroParam = ID (':' ID)? - // - // ExpList = (GroupExpr (',' GroupExpr)*)? - // - // ExpBlock = Expression* - private: - AST* Program(void); - AST* Expression(void); - AST* LogicalExpr(void); - AST* CompExpr(void); - AST* AddSubExpr(void); - AST* MulDivExpr(void); - AST* UnaryExpr(void); - AST* GroupExpr(void); - AST* Literal(void); - AST* VectorLiteral(void); - AST* ListLiteral(void); - AST* FuncLiteral(void); - AST* MacroDefinition(void); - AST* MacroExpansion(void); - AST* MacroParamList(void); - AST* MacroParam(void); - AST* ExpList(TokenType_T node_type, TokenType_T terminator); - AST* ExpBlock(TokenType_T node_type, TokenType_T terminator); + // | ListLiteral + // | FuncLiteral + // | ID + // | NUM + // | CHAR + // | STRING + // | SYMBOL + // + // VectorLiteral = '[' ExpList ']' + // + // ListLiteral = '`' '(' ExpList ')' + // + // FuncLiteral = '{' ExpBlock '}' + // | '{' '|' ExpList '|' ExpBlock '}' + // + // MacroDefinition = '%' ID '(' MacroParamList ')' '{' Expression '}' + // + // MacroParamList = MacroParam (',' MacroParam)* + // + // MacroParam = ID (':' ID)? + // + // ExpList = (GroupExpr (',' GroupExpr)*)? + // + // ExpBlock = Expression* + private: + AST* Program(void); + AST* Expression(void); + AST* LogicalExpr(void); + AST* CompExpr(void); + AST* AddSubExpr(void); + AST* MulDivExpr(void); + AST* UnaryExpr(void); + AST* GroupExpr(void); + AST* Literal(void); + AST* VectorLiteral(void); + AST* ListLiteral(void); + AST* FuncLiteral(void); + AST* MacroDefinition(void); + AST* MacroExpansion(void); + AST* MacroParamList(void); + AST* MacroParam(void); + AST* ExpList(TokenType_T node_type, TokenType_T terminator); + AST* ExpBlock(TokenType_T node_type, TokenType_T terminator); }; #endif diff --git a/source/dlparser/macro/macro.c b/source/dlparser/macro/macro.c index 0808661..a01f804 100644 --- a/source/dlparser/macro/macro.c +++ b/source/dlparser/macro/macro.c @@ -21,6 +21,11 @@ Macro::Macro(AST* macro_def) Macro::~Macro() { + + std::list::iterator iter; + for (iter = macro_params.begin(); iter != macro_params.end(); ++iter) { + delete *iter; + } delete macro_body; } diff --git a/source/main.cpp b/source/main.cpp index 240e115..507ef35 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -22,15 +22,19 @@ int main(int argc, char** argv) //string temp_fname = createTempFileName( input_fname ); //ifstream input(input_fname.c_str()); //DLLexer lexer; + //Token tok; //lexer.setInput(&input); - //lexer.next(); + //while( tok.type() != EOF ) + //{ + // tok = lexer.next(); + //} //(void)temp_fname; string input_fname(argv[1]); string temp_fname = createTempFileName( input_fname ); (void)temp_fname; DLParser parser; - Scheme* visitor = NULL; + //Scheme* visitor = NULL; AST* parse_tree = NULL; // Open the input and output files @@ -39,11 +43,10 @@ int main(int argc, char** argv) // Parse the file parser.setInput(&input); - parser.parse(); - parse_tree = parser.getAST(); + parse_tree = parser.parse(); // Translate the AST - visitor = _new Scheme( parse_tree ); + //visitor = _new Scheme( parse_tree ); //visitor->visit(); // Write to a temp file @@ -71,7 +74,7 @@ int main(int argc, char** argv) cerr << "Error: No input files." << endl; } - REPORT_LEAKS(); + Cork_ReportMemoryLeaks(); return ret; } diff --git a/source/parse_utils/lexer/ilexer.h b/source/parse_utils/lexer/ilexer.h index 3b04943..530c079 100644 --- a/source/parse_utils/lexer/ilexer.h +++ b/source/parse_utils/lexer/ilexer.h @@ -26,7 +26,7 @@ class ILexer void setInput(std::string& in); void setInput(std::istream* in); - virtual Token* next(void) = 0; + virtual Token next(void) = 0; }; #endif diff --git a/source/parse_utils/lexer/token/token.cpp b/source/parse_utils/lexer/token/token.cpp index dab99b7..ae8c245 100644 --- a/source/parse_utils/lexer/token/token.cpp +++ b/source/parse_utils/lexer/token/token.cpp @@ -1,5 +1,10 @@ +#include #include "token.h" +Token::Token() : tok_type(EOF), tok_text(""), tok_line(-1), tok_col(-1) +{ +} + Token::Token(TokenType_T ttype, std::string ttext, int line, int col) : tok_type(ttype), tok_text(ttext), tok_line(line), tok_col(col) { } @@ -8,21 +13,41 @@ Token::Token(TokenType_T ttype, int line, int col) : tok_type(ttype), tok_line(l { } +void Token::type(TokenType_T typ) +{ + tok_type = typ; +} + TokenType_T Token::type() { return tok_type; } +void Token::text(std::string txt) +{ + tok_text = txt; +} + std::string Token::text() { return tok_text; } +void Token::line(int ln) +{ + tok_line = ln; +} + int Token::line() { return tok_line; } +void Token::column(int col) +{ + tok_col = col; +} + int Token::column() { return tok_col; diff --git a/source/parse_utils/lexer/token/token.h b/source/parse_utils/lexer/token/token.h index 75f8a85..8c5f0b6 100644 --- a/source/parse_utils/lexer/token/token.h +++ b/source/parse_utils/lexer/token/token.h @@ -13,11 +13,20 @@ class Token int tok_line; int tok_col; public: + Token(); Token(TokenType_T ttype, int line, int col); Token(TokenType_T ttype, std::string ttext, int line, int col); + + void type(TokenType_T typ); TokenType_T type(); + + void text(std::string txt); std::string text(); + + void line(int ln); int line(); + + void column(int col); int column(); }; diff --git a/source/parse_utils/parser/btparser/btparser.cpp b/source/parse_utils/parser/btparser/btparser.cpp index 6aacba3..9a01d43 100644 --- a/source/parse_utils/parser/btparser/btparser.cpp +++ b/source/parse_utils/parser/btparser/btparser.cpp @@ -71,15 +71,15 @@ void BTParser::match(TokenType_T type) } else { - Token* tok = lookaheadToken(1); + Token& tok = lookaheadToken(1); ostringstream oss; - oss << "Expected token type. Expected " << type << ", received " << tok->type() << "."; - Exception ex( tok->line(), tok->column() ); + oss << "Expected token type. Expected " << type << ", received " << tok.type() << "."; + Exception ex( tok.line(), tok.column() ); ex.setMessage(oss.str()); } } -Token* BTParser::lookaheadToken(unsigned int i) +Token& BTParser::lookaheadToken(unsigned int i) { sync(i); return lookahead.at( current + i - 1 ); @@ -87,9 +87,7 @@ Token* BTParser::lookaheadToken(unsigned int i) TokenType_T BTParser::lookaheadType(unsigned int i) { - - Token* tok = lookaheadToken(i); - return (tok != NULL) ? tok->type() : EOF; + return lookaheadToken(i).type(); } unsigned int BTParser::mark(void) diff --git a/source/parse_utils/parser/btparser/btparser.h b/source/parse_utils/parser/btparser/btparser.h index ea77c39..5d94cb3 100644 --- a/source/parse_utils/parser/btparser/btparser.h +++ b/source/parse_utils/parser/btparser/btparser.h @@ -13,7 +13,7 @@ class BTParser : public IParser ILexer* lexer; unsigned int current; std::vector markers; - std::vector lookahead; + std::vector lookahead; public: BTParser(ILexer* lxer); ~BTParser(); @@ -26,14 +26,14 @@ class BTParser : public IParser void sync(unsigned int i); void fill(unsigned int n); void match(TokenType_T type); - Token* lookaheadToken(unsigned int i); + Token& lookaheadToken(unsigned int i); TokenType_T lookaheadType(unsigned int i); unsigned int mark(void); void release(void); void seek(unsigned int index); bool isSpeculating(void); - virtual void parse(void) = 0; + virtual AST* parse(void) = 0; }; #endif diff --git a/source/parse_utils/parser/iparser.h b/source/parse_utils/parser/iparser.h index 0c4d86c..47b75c1 100644 --- a/source/parse_utils/parser/iparser.h +++ b/source/parse_utils/parser/iparser.h @@ -30,7 +30,7 @@ class IParser { public: IParser(); virtual ~IParser(); - virtual void parse() = 0; + virtual AST* parse() = 0; void setInput(char* in); void setInput(string& in); diff --git a/source/parse_utils/parser/llkparser/llkparser.cpp b/source/parse_utils/parser/llkparser/llkparser.cpp index 3568726..4569dec 100644 --- a/source/parse_utils/parser/llkparser/llkparser.cpp +++ b/source/parse_utils/parser/llkparser/llkparser.cpp @@ -6,7 +6,7 @@ LLKParser::LLKParser(int k_val, ILexer* lxer) : k(k_val), next(0), lexer(lxer) { if ( lexer != NULL ) { - lookahead = new Token*[k]; + lookahead = new Token[k]; } else { @@ -79,13 +79,9 @@ void LLKParser::match(TokenType_T type) } } -Token* LLKParser::lookaheadToken(int i) +Token& LLKParser::lookaheadToken(int i) { - Token* ret = NULL; - if( lookahead != NULL ) - { - ret = lookahead[(next + i - 1) % k]; - } + Token& ret = lookahead[(next + i - 1) % k]; return ret; } @@ -94,8 +90,7 @@ TokenType_T LLKParser::lookaheadType(int i) TokenType_T ret = EOF; if( lookahead != NULL ) { - Token* tok = lookaheadToken(i); - ret = (tok != NULL) ? tok->type() : EOF; + ret = lookaheadToken(i).type(); } return ret; } diff --git a/source/parse_utils/parser/llkparser/llkparser.h b/source/parse_utils/parser/llkparser/llkparser.h index 2e403b7..8971cfa 100644 --- a/source/parse_utils/parser/llkparser/llkparser.h +++ b/source/parse_utils/parser/llkparser/llkparser.h @@ -12,7 +12,7 @@ class LLKParser : public IParser int k; int next; ILexer* lexer; - Token** lookahead; + Token* lookahead; public: LLKParser(int k_val, ILexer* lxer); ~LLKParser(); @@ -23,9 +23,9 @@ class LLKParser : public IParser void consume(void); void match(TokenType_T type); - Token* lookaheadToken(int i); + Token& lookaheadToken(int i); TokenType_T lookaheadType(int i); - virtual void parse(void) = 0; + virtual AST* parse(void) = 0; }; #endif -- 2.54.0