From: <ibr...@us...> - 2011-05-13 14:38:46
|
Revision: 3961 http://tora.svn.sourceforge.net/tora/?rev=3961&view=rev Author: ibre5041 Date: 2011-05-13 14:38:39 +0000 (Fri, 13 May 2011) Log Message: ----------- code cleanup Removed Paths: ------------- branches/tora-trotl/sandbox/Makefile.antlr branches/tora-trotl/sandbox/input branches/tora-trotl/sandbox/main.c branches/tora-trotl/sandbox/main.cpp branches/tora-trotl/sandbox/mainwindow.cpp Deleted: branches/tora-trotl/sandbox/Makefile.antlr =================================================================== --- branches/tora-trotl/sandbox/Makefile.antlr 2011-05-13 14:37:45 UTC (rev 3960) +++ branches/tora-trotl/sandbox/Makefile.antlr 2011-05-13 14:38:39 UTC (rev 3961) @@ -1,94 +0,0 @@ -.PRECIOUS=.o - -ANTLRGRAMMARS ?= $(wildcard *.g) -HEADERS = $(wildcard *.h) $(wildcard *.tcc) -SOURCES = $(wildcard *.cpp) -GRAMMAROPTIONS=-Xconversiontimeout 1500000 - -INCLUDES= -I. -I/usr/include/qt4/QtCore \ - -I/usr/include/qt4/QtGui \ - -I/usr/include/qt4 \ - -I../../../devel/libantlr3c-3.2/include \ - -I../../../devel/libantlr3c-3.2 - -#-I/usr/share/qt4/mkspecs/linux-g++ \ - -LIBS = -L/usr/lib \ - -L/home/ibrezina/devel/libantlr3c-3.2/.libs \ - -Wl,-rpath=/home/ibrezina/devel/libantlr3c-3.2/.libs \ - -lantlr3c -lQtGui -lQtCore -lpthread - -CFLAGS=-ggdb -O0 -fno-inline - - -all: a.out - -a.out: main.c OracleSQL.tokens OracleSQLParser.o OracleSQLLexer.o main.c error_handler.o - g++ $(CFLAGS) $(INCLUDES) OracleSQLParser.o OracleSQLLexer.o error_handler.o main.c /home/ibrezina/devel/libantlr3c-3.2/.libs/libantlr3c.a - -t: tmain.c t.tokens tParser.o tLexer.o tmain.c error_handler.o - gcc $(CFLAGS) $(INCLUDES) tParser.o tLexer.o error_handler.o tmain.c /home/ibrezina/devel/libantlr3c-3.2/.libs/libantlr3c.a -o t - -tParser.o: tParser.c - gcc $(CFLAGS) $(INCLUDES) -c tParser.c -o $@ - -tLexer.o: tLexer.c - gcc $(CFLAGS) $(INCLUDES) -c tLexer.c -o $@ - -clean: - rm -f *.o a.out OracleSQLParser.[ch] OracleSQLLexer.[ch] - -%.u: %.g - @echo "Bulding dependencies for "$< - java -jar /home/ibrezina/workspace/SQL_Grammar/antlr-3.2.jar -depend $< > $@ - @grep ":" $@ |awk 'BEGIN {printf "ANTLRGENLIST := " }{printf " " $$1}END {print ""}' >> $@.tmp - @cat $@.tmp >> $@ - $(RM) $@.tmp - - -%.tokens %.c %Lexer.c %Parser.c %Lexer.h %Parser.h %.h: %.g - java -Xmx1500m -jar /home/ibrezina/workspace/SQL_Grammar/antlr-3.2.jar $(GRAMMAROPTIONS) $< - -OracleSQLParser.o: OracleSQLParser.c - g++ $(CFLAGS) $(INCLUDES) -c OracleSQLParser.c -o $@ - -OracleSQLLexer.o: OracleSQLLexer.c - g++ $(CFLAGS) $(INCLUDES) -c OracleSQLLexer.c -o $@ - -error_handler.o: error_handler.c error_handler.h - g++ $(CFLAGS) $(INCLUDES) -c error_handler.c -o $@ - -#QSTRINGParser.o: QSTRINGParser.c -# gcc $(CFLAGS) $(INCLUDES) -c QSTRINGParser.c -o $@ -# -#QSTRINGLexer.o: QSTRINGLexer.c -# gcc $(CFLAGS) $(INCLUDES) -c QSTRINGLexer.c -o $@ - -#ifneq ($(MAKECMDGOALS),clean) -ifneq ($(strip $(ANTLRGRAMMARS)),) --include $(ANTLRGRAMMARS:.g=.u) -endif -#endif - -TOBJS= treemodel.o \ - tsqlparseoracle.o \ - OracleSQLParser.o \ - OracleSQLLexer.o \ - error_handler.o \ - moc_treemodel.o - - -%.o: %.cpp $(HEADERS) - g++ $(CFLAGS) $(INCLUDES) -c $< -o $@ - -%.o: %.cxx %.h - g++ $(CFLAGS) $(INCLUDES) -c $< -o $@ - -%.cxx: %.h - moc-qt4 $(INCLUDES) $< -o moc_$@ - -moc_treemodel.cxx: treemodel.h - moc-qt4 $(INCLUDES) $< -o $@ - -t1: $(TOBJS) t1.o OracleSQLLexer.h - g++ $(TOBJS) t1.o $(LIBS) -o $@ Deleted: branches/tora-trotl/sandbox/input =================================================================== --- branches/tora-trotl/sandbox/input 2011-05-13 14:37:45 UTC (rev 3960) +++ branches/tora-trotl/sandbox/input 2011-05-13 14:38:39 UTC (rev 3961) @@ -1,28 +0,0 @@ -WITH -X1 AS (SELECT MAX(Y1) FROM KLM1), -X2 AS (SELECT MAX(Y2) FROM KLM2), -X3 AS (SELECT MAX(Y3) FROM KLM3), -X4 AS (SELECT MAX(Y4) FROM KLM4) -SELECT - DISTINCT - -1, - +1, - A + B * (A * D) AS, - T1.REGION_NAME, - T2.DIVISION_NAME, - T1.REGION_NAME AS A, - T2.DIVISION_NAME AS, - *, - A.*, - SUM(T3.AMOUNT), - SUM(COUNT(1)) + COUNT(*) - , SUM(1) + (SELECT COUNT(1) FROM DDD) A -FROM DUAL, FFF -WHERE A IS NULL -OR B IS NOT NULL -AND ( A LIKE 'D') -AND 1 = 0 -AND A.B.C IS A SET -UNION -SELECT A FROM DUAL - Deleted: branches/tora-trotl/sandbox/main.c =================================================================== --- branches/tora-trotl/sandbox/main.c 2011-05-13 14:37:45 UTC (rev 3960) +++ branches/tora-trotl/sandbox/main.c 2011-05-13 14:38:39 UTC (rev 3961) @@ -1,395 +0,0 @@ -// Example of a grammar for a trivial tree parser. -// Adapted from Java equivalent example, by Terence Parr -// Author: Jim Idle - April 2007 -// Permission is granted to use this example code in any way you want, so long as -// all the original authors are cited. -// - -// set ts=4,sw=4 -// Tab size is 4 chars, indent is 4 chars - -// Notes: Although all the examples provided are configured to be built -// by Visual Studio 2005, based on the custom build rules -// provided in $(ANTLRSRC)/code/antlr/main/runtime/C/vs2005/rulefiles/antlr3.rules -// there is no reason that this MUST be the case. Provided that you know how -// to run the antlr tool, then just compile the resulting .c files and this -// file together, using say gcc or whatever: gcc *.c -I. -o XXX -// The C code is generic and will compile and run on all platforms (please -// report any warnings or errors to the antlr-interest newsgroup (see www.antlr.org) -// so that they may be corrected for any platform that I have not specifically tested. -// -// The project settings such as additional library paths and include paths have been set -// relative to the place where this source code sits on the ANTLR perforce system. You -// may well need to change the settings to locate the includes and the lib files. UNIX -// people need -L path/to/antlr/libs -lantlr3c (release mode) or -lantlr3cd (debug) -// -// Jim Idle (jimi cut-this at idle ws) -// - -// You may adopt your own practices by all means, but in general it is best -// to create a single include for your project, that will include the ANTLR3 C -// runtime header files, the generated header files (all of which are safe to include -// multiple times) and your own project related header files. Use <> to include and -// -I on the compile line (which vs2005 now handles, where vs2003 did not). -// -#include "OracleSQLLexer.h" -#include "OracleSQLParser.h" - -void TreeWalk(pANTLR3_BASE_TREE p); -void TreeWalkPrinter(pANTLR3_BASE_TREE p); - -// Main entry point for this example -// -int ANTLR3_CDECL -main (int argc, char *argv[]) -{ - int retval; - - // Now we declare the ANTLR related local variables we need. - // Note that unless you are convinced you will never need thread safe - // versions for your project, then you should always create such things - // as instance variables for each invocation. - // ------------------- - - // Name of the input file. Note that we always use the abstract type pANTLR3_UINT8 - // for ASCII/8 bit strings - the runtime library guarantees that this will be - // good on all platforms. This is a general rule - always use the ANTLR3 supplied - // typedefs for pointers/types/etc. - // - pANTLR3_UINT8 fName; - - // The ANTLR3 character input stream, which abstracts the input source such that - // it is easy to provide input from different sources such as files, or - // memory strings. - // - // For an ASCII/latin-1 memory string use: - // input = antlr3NewAsciiStringInPlaceStream (stringtouse, (ANTLR3_UINT64) length, NULL); - // - // For a UCS2 (16 bit) memory string use: - // input = antlr3NewUCS2StringInPlaceStream (stringtouse, (ANTLR3_UINT64) length, NULL); - // - // For input from a file, see code below - // - // Note that this is essentially a pointer to a structure containing pointers to functions. - // You can create your own input stream type (copy one of the existing ones) and override any - // individual function by installing your own pointer after you have created the standard - // version. - // - pANTLR3_INPUT_STREAM input; - - // The lexer is of course generated by ANTLR, and so the lexer type is not upper case. - // The lexer is supplied with a pANTLR3_INPUT_STREAM from whence it consumes its - // input and generates a token stream as output. - // - pOracleSQLLexer lxr; - - // The token stream is produced by the ANTLR3 generated lexer. Again it is a structure based - // API/Object, which you can customise and override methods of as you wish. a Token stream is - // supplied to the generated parser, and you can write your own token stream and pass this in - // if you wish. - // - pANTLR3_COMMON_TOKEN_STREAM tstream; - - // The Lang parser is also generated by ANTLR and accepts a token stream as explained - // above. The token stream can be any source in fact, so long as it implements the - // ANTLR3_TOKEN_SOURCE interface. In this case the parser does not return anything - // but it can of course specify any kind of return type from the rule you invoke - // when calling it. - // - pOracleSQLParser psr; - - // The parser produces an AST, which is returned as a member of the return type of - // the starting rule (any rule can start first of course). This is a generated type - // based upon the rule we start with. - // - OracleSQLParser_start_rule_return langAST; - - - // The tree nodes are managed by a tree adaptor, which doles - // out the nodes upon request. You can make your own tree types and adaptors - // and override the built in versions. See runtime source for details and - // eventually the wiki entry for the C target. - // - pANTLR3_COMMON_TREE_NODE_STREAM nodes; - - // Finally, when the parser runs, it will produce an AST that can be traversed by the - // the tree parser: c.f. LangDumpDecl.g3t - // - ////pLangDumpDecl treePsr; - - // Create the input stream based upon the argument supplied to us on the command line - // for this example, the input will always default to ./input if there is no explicit - // argument. - // - if (argc < 2 || argv[1] == NULL) - { - fName =(pANTLR3_UINT8)"./input"; // Note in VS2005 debug, working directory must be configured - } - else - { - fName = (pANTLR3_UINT8)argv[1]; - } - - // Create the input stream using the supplied file name - // (Use antlr3AsciiFileStreamNew for UCS2/16bit input). - // - input = antlr3AsciiFileStreamNew(fName); - input->setUcaseLA(input, ANTLR3_TRUE); // ignore case - - // The input will be created successfully, providing that there is enough - // memory and the file exists etc - // - if (input == NULL) - { - fprintf(stderr, "Unable to open file %s\n", (char *)fName); - exit(ANTLR3_ERR_NOMEM); - } - - // Our input stream is now open and all set to go, so we can create a new instance of our - // lexer and set the lexer input to our input stream: - // (file | memory | ?) --> inputstream -> lexer --> tokenstream --> parser ( --> treeparser )? - // - lxr = OracleSQLLexerNew(input); // CLexerNew is generated by ANTLR - lxr->pLexer->input->charPositionInLine = 0; - - // Need to check for errors - // - if ( lxr == NULL ) - { - fprintf(stderr, "Unable to create the lexer due to malloc() failure1\n"); - exit(ANTLR3_ERR_NOMEM); - } - - // Our lexer is in place, so we can create the token stream from it - // NB: Nothing happens yet other than the file has been read. We are just - // connecting all these things together and they will be invoked when we - // call the parser rule. ANTLR3_SIZE_HINT can be left at the default usually - // unless you have a very large token stream/input. Each generated lexer - // provides a token source interface, which is the second argument to the - // token stream creator. - // Note tha even if you implement your own token structure, it will always - // contain a standard common token within it and this is the pointer that - // you pass around to everything else. A common token as a pointer within - // it that should point to your own outer token structure. - // - tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lxr)); - - if (tstream == NULL) - { - fprintf(stderr, "Out of memory trying to allocate token stream\n"); - exit(ANTLR3_ERR_NOMEM); - } - - // Finally, now that we have our lexer constructed, we can create the parser - // - psr = OracleSQLParserNew(tstream); // CParserNew is generated by ANTLR3 - - if (psr == NULL) - { - fprintf(stderr, "Out of memory trying to allocate parser\n"); - exit(ANTLR3_ERR_NOMEM); - } - - // We are all ready to go. Though that looked complicated at first glance, - // I am sure, you will see that in fact most of the code above is dealing - // with errors and there isn't really that much to do (isn't this always the - // case in C? ;-). - // - // So, we now invoke the parser. All elements of ANTLR3 generated C components - // as well as the ANTLR C runtime library itself are pseudo objects. This means - // that they are represented as pointers to structures, which contain any - // instance data they need, and a set of pointers to other interfaces or - // 'methods'. Note that in general, these few pointers we have created here are - // the only things you will ever explicitly free() as everything else is created - // via factories, that allocate memory efficiently and free() everything they use - // automatically when you close the parser/lexer/etc. - // - // Note that this means only that the methods are always called via the object - // pointer and the first argument to any method, is a pointer to the structure itself. - // It also has the side advantage, if you are using an IDE such as VS2005 that can do it - // that when you type ->, you will see a list of all the methods the object supports. - // - ////langAST = psr->decl(psr); - langAST = psr->start_rule(psr); - - // If the parser ran correctly, we will have a tree to parse. In general I recommend - // keeping your own flags as part of the error trapping, but here is how you can - // work out if there were errors if you are using the generic error messages - // - if (psr->pParser->rec->state->errorCount > 0) - { - fprintf(stderr, "The parser returned %d errors, tree walking aborted.\n", psr->pParser->rec->state->errorCount); - - } - else - { - nodes = antlr3CommonTreeNodeStreamNewTree(langAST.tree, ANTLR3_SIZE_HINT); // sIZE HINT WILL SOON BE DEPRECATED!! - - printf("Nodes: %s\n", langAST.tree->toStringTree(langAST.tree)->chars); - - // Tree parsers are given a common tree node stream (or your override) - // - ////treePsr = LangDumpDeclNew(nodes); - - ////treePsr->decl(treePsr); - nodes ->free (nodes); nodes = NULL; - ////treePsr ->free (treePsr); treePsr = NULL; - } - - // print tree elements with indent - TreeWalk(langAST.tree); - - -/* pANTLR3_VECTOR tokenVector = tstream->getTokens(tstream); */ -/* ANTLR3_UINT32 size = tokenVector->size(tokenVector); */ -/* ANTLR3_UINT32 i; */ -/* for (i=0; i < size; i++) { */ -/* pANTLR3_COMMON_TOKEN token = tokenVector->get(tokenVector, i); */ -/* printf("--%s-%d-\n", token->toString(token)->chars, token->getChannel(token)); */ -/* printf("[%d,%d,%d] '%s'\n", */ -/* token->getLine(token), */ -/* token->getCharPositionInLine(token), */ -/* token->getText(token)->len, */ -/* token->getText(token)->chars); */ -/* printf("--------------------------------------------------------------------------------\n"); */ -/* } */ - - - // We did not return anything from this parser rule, so we can finish. It only remains - // to close down our open objects, in the reverse order we created them - // - retval = psr->pParser->rec->state->errorCount; - - psr ->free (psr); psr = NULL; - tstream ->free (tstream); tstream = NULL; - lxr ->free (lxr); lxr = NULL; - input ->close (input); input = NULL; - - return retval; -}; - - -//----------------------------------------------------------------------------- -// Indent - print indent for screen dump / treewalk -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -void Indent(int Level) -{ - int i; - Level += 2; - for (i=0; i<Level; i++) printf(" "); -}; - -extern pANTLR3_UINT8 OracleSQLParserTokenNames[]; - -//----------------------------------------------------------------------------- -// TreeWalkWorker - -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -void TreeWalkWorker(pANTLR3_BASE_TREE p, int Level) -{ ANTLR3_UINT32 n, c; - - if (p->isNilNode(p) == ANTLR3_TRUE) { - printf("// nil-node\n"); - // return; - } - - n = p->getChildCount(p); - - for (c = 0; c<n ; c++) { - pANTLR3_BASE_TREE child; - child = (pANTLR3_BASE_TREE)p->getChild(p, c); - // printf(" %s\n",child->toString(child)->chars); - int ChildCount = child->getChildCount(child); - pANTLR3_COMMON_TOKEN Token; - if (child->getToken == NULL) { - printf("// getToken null\n"); - Token = 0; - } else { - Token = child->getToken(child); - } - - - ANTLR3_UINT32 TokenType = child->getType(child); - printf("//"); - Indent(Level); - uint8_t *s = child->toString(child)->chars; - printf("%s (%d, %s) [%d,%d] (%d)\n", - child->toString(child)->chars, - TokenType, - OracleSQLParserTokenNames[TokenType], - child->getLine(child), - child->getCharPositionInLine(child), - child->getChildCount(child) - ); - // printf("%s\n",child->toString(child)->chars); - if (ChildCount > 0) { - TreeWalkWorker(child, Level+1); - } - } -}; - -//----------------------------------------------------------------------------- -// TreeWalk - -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -void TreeWalk(pANTLR3_BASE_TREE p) -{ - TreeWalkWorker(p, 0); - TreeWalkPrinter(p); -}; - -void advance(unsigned line, unsigned pos, const uint8_t* str) -{ - static unsigned cline = 1, cpos=0; - - if(cline > line || (cline==line && cpos > pos)) - { - printf("Internal error [%d,%d],[%d,%d]\n", cline, cpos, line, pos); - exit(-2); - } - - while(cline < line) - { - printf("\n"); - cline++; - cpos = 0; - } - while(cpos < pos) - { - printf(" "); - cpos++; - } - - printf("%s", str); - cpos += strlen((const char*)str); -}; - -void TreeWalkPrinter(pANTLR3_BASE_TREE p) -{ - ANTLR3_UINT32 n, c; - - if (p->isNilNode(p) == ANTLR3_TRUE) { - //printf("// nil-node\n"); - //return; - } - - n = p->getChildCount(p); - - for (c = 0; c<n ; c++) { - pANTLR3_BASE_TREE child; - child = (pANTLR3_BASE_TREE)p->getChild(p, c); - int ChildCount = child->getChildCount(child); - - ANTLR3_UINT32 TokenType = child->getType(child); - if (ChildCount > 0) { - TreeWalkPrinter(child); - } else { - advance( child->getLine(child), - child->getCharPositionInLine(child), - child->toString(child)->chars - ); - } - } -}; - Deleted: branches/tora-trotl/sandbox/main.cpp =================================================================== --- branches/tora-trotl/sandbox/main.cpp 2011-05-13 14:37:45 UTC (rev 3960) +++ branches/tora-trotl/sandbox/main.cpp 2011-05-13 14:38:39 UTC (rev 3961) @@ -1,53 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt...@no...) -** -** This file is part of the example classes of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial Usage -** Licensees holding valid Qt Commercial licenses may use this file in -** accordance with the Qt Commercial License Agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Nokia. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-...@no.... -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "mainwindow.h" - -#include <QApplication> - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - MainWindow window; - window.resize(640, 512); - window.show(); - return app.exec(); -} Deleted: branches/tora-trotl/sandbox/mainwindow.cpp =================================================================== --- branches/tora-trotl/sandbox/mainwindow.cpp 2011-05-13 14:37:45 UTC (rev 3960) +++ branches/tora-trotl/sandbox/mainwindow.cpp 2011-05-13 14:38:39 UTC (rev 3961) @@ -1,441 +0,0 @@ -/**************************************************************************** - ** - ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). - ** Contact: Qt Software Information (qt...@no...) - ** - ** This file is part of the example classes of the Qt Toolkit. - ** - ** $QT_BEGIN_LICENSE:LGPL$ - ** Commercial Usage - ** Licensees holding valid Qt Commercial licenses may use this file in - ** accordance with the Qt Commercial License Agreement provided with the - ** Software or, alternatively, in accordance with the terms contained in - ** a written agreement between you and Nokia. - ** - ** GNU Lesser General Public License Usage - ** Alternatively, this file may be used under the terms of the GNU Lesser - ** General Public License version 2.1 as published by the Free Software - ** Foundation and appearing in the file LICENSE.LGPL included in the - ** packaging of this file. Please review the following information to - ** ensure the GNU Lesser General Public License version 2.1 requirements - ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. - ** - ** In addition, as a special exception, Nokia gives you certain - ** additional rights. These rights are described in the Nokia Qt LGPL - ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this - ** package. - ** - ** GNU General Public License Usage - ** Alternatively, this file may be used under the terms of the GNU - ** General Public License version 3.0 as published by the Free Software - ** Foundation and appearing in the file LICENSE.GPL included in the - ** packaging of this file. Please review the following information to - ** ensure the GNU General Public License version 3.0 requirements will be - ** met: http://www.gnu.org/copyleft/gpl.html. - ** - ** If you are unsure which license is appropriate for your use, please - ** contact the sales department at qt-...@no.... - ** $QT_END_LICENSE$ - ** - ****************************************************************************/ - -#include "OracleSQLLexer.h" -#include "OracleSQLParser.h" - -#include "tsqlparse.h" -#include "mainwindow.h" -#include "treemodel.h" - -#include <QtGui> -#include <QPointer> -#include <QTreeView> -#include <QTextCursor> -#include <iostream> - -using namespace SQLParser; - -//! [0] -MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), lastFormat(&keywordFormat) -{ - QFont mono("Monospace"); - mono.setStyleHint(QFont::TypeWriter); - - keywordFormat.setForeground(Qt::darkBlue); - keywordFormat.setFontWeight(QFont::Bold); - keywordFormat.setFont(mono); - keywordFormat.setFontPointSize(6); -/// identifierFormat.setFontWeight(QFont::Bold); - identifierFormat.setForeground(Qt::darkMagenta); - identifierFormat.setFont(mono); - identifierFormat.setFontPointSize(6); - singleLineCommentFormat.setForeground(Qt::red); - singleLineCommentFormat.setFont(mono); - singleLineCommentFormat.setFontPointSize(6); - multiLineCommentFormat.setForeground(Qt::red); - multiLineCommentFormat.setFont(mono); - multiLineCommentFormat.setFontPointSize(6); - quotationFormat.setForeground(Qt::darkGreen); - quotationFormat.setFont(mono); - quotationFormat.setFontPointSize(6); - functionFormat.setFontItalic(true); - functionFormat.setForeground(Qt::blue); - functionFormat.setFont(mono); - functionFormat.setFontPointSize(6); - - setupFileMenu(); - setupHelpMenu(); - - //setCentralWidget(editor); - QWidget *centralWidget = new QWidget(this); - QHBoxLayout *horizontalLayout = new QHBoxLayout(centralWidget); - horizontalLayout->setSpacing(6); - horizontalLayout->setMargin(11); - - tree = new QTreeView(centralWidget); - horizontalLayout->addWidget(tree); - editor = new QTextEdit(centralWidget); - horizontalLayout->addWidget(editor); - -// connect(tree, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), -// this, SLOT(lexemeSelect(const QItemSelection &, const QItemSelection &)) -// ); - - connect(tree, SIGNAL(activated ( const QModelIndex&)), - this, SLOT(lexemeSelect2(const QModelIndex&)) - ); - - - setupEditor(); - - setCentralWidget(centralWidget); - - setWindowTitle(tr("SQL Parse test")); - -} -//! [0] - -void MainWindow::lexemeSelect(const QItemSelection &selected, const QItemSelection &) -{ - QModelIndexList indexes = selected.indexes(); - - if (!indexes.isEmpty()) - return; - QModelIndex &index(indexes.first()); - QVariant data(index.data(Qt::UserRole)); - std::cout << "MainWindow::lexemeSelect()" << std::endl; -} - -void MainWindow::lexemeSelect2(const QModelIndex &index) -{ - QVariant data(index.data(Qt::UserRole)); - std::cout << "MainWindow::lexemeSelect2()" << std::endl; - ::SQLParser::Token *item = static_cast<SQLParser::Token*>(data.value<void*>()); - ::SQLParser::Position const &pos = item->getPosition(); - - QTextCursor cursor = editor->textCursor(); - cursor.movePosition(QTextCursor::Start); - cursor.movePosition(QTextCursor::QTextCursor::Down, QTextCursor::MoveAnchor, pos.getLine()-1); - cursor.movePosition(QTextCursor::QTextCursor::Right, QTextCursor::MoveAnchor, pos.getLinePos()); - std::cout << "Move: " << pos.getLine() -1 << "," << pos.getLinePos() << std::endl; - editor->setTextCursor(cursor); - editor->setFocus(Qt::OtherFocusReason); -} - -void MainWindow::about() -{ - QMessageBox::about(this, tr("SQL Parse test"), - tr("<p>The <b>Syntax Highlighter</b> example shows how " \ - "to perform simple syntax highlighting by subclassing " \ - "the QSyntaxHighlighter class and describing " \ - "highlighting rules using regular expressions.</p>")); -} - -void MainWindow::newFile() -{ - editor->clear(); -} - -void MainWindow::openFile(const QString &path) -{ - QString fileName = path; - - if (fileName.isNull()) - fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", "SQL Files (*.sql *.SQL)"); - - if (!fileName.isEmpty()) { - QFile file(fileName); - if (file.open(QFile::ReadOnly | QFile::Text)) - { - editor->clear(); - StatementFactOneParmSing::Instance().dump(); - this->stat = StatementFactOneParmSing::Instance().create("Oracle", QString(file.readAll())); - QTextCursor cursor = editor->textCursor(); - this->stat->print(cursor); - TreeModel *model = new TreeModel(this->stat->root()); - tree->setModel(model); - tree->show(); - } - } -} - -//! [1] -void MainWindow::setupEditor() -{ - QFont font; - font.setFamily("Courier"); - font.setFixedPitch(true); - font.setPointSize(6); - - //editor = new QTextEdit; - editor->setFont(font); - - QFile file("tests/condition02.sql"); - if (file.open(QFile::ReadOnly | QFile::Text)) - { - StatementFactOneParmSing::Instance().dump(); - this->stat = StatementFactOneParmSing::Instance().create("Oracle", QString(file.readAll())); - QTextCursor cursor = editor->textCursor(); - this->stat->print(cursor); - TreeModel *model = new TreeModel(this->stat->root()); - tree->setModel(model); - tree->show(); - } -} -//! [1] - -void MainWindow::setupFileMenu() -{ - QMenu *fileMenu = new QMenu(tr("&File"), this); - menuBar()->addMenu(fileMenu); - - fileMenu->addAction(tr("&New"), this, SLOT(newFile()), - QKeySequence(tr("Ctrl+N", - "File|New"))); - fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()), - QKeySequence(tr("Ctrl+O", - "File|Open"))); - fileMenu->addAction(tr("E&xit"), qApp, SLOT(quit()), - QKeySequence(tr("Ctrl+Q", - "File|Exit"))); -} - -void MainWindow::setupHelpMenu() -{ - QMenu *helpMenu = new QMenu(tr("&Help"), this); - menuBar()->addMenu(helpMenu); - - helpMenu->addAction(tr("&About"), this, SLOT(about())); - helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt())); -} - -// void MainWindow::loadAndParseSQLFile(QByteArray QBAinput) -// { -// pANTLR3_INPUT_STREAM input; -// pOracleSQLLexer lxr; -// pANTLR3_COMMON_TOKEN_STREAM tstream; -// pOracleSQLParser psr; -// OracleSQLParser_start_rule_return langAST; -// //pANTLR3_COMMON_TREE_NODE_STREAM nodes; - -// input = antlr3NewAsciiStringInPlaceStream( (uint8_t*) QBAinput.data(), (ANTLR3_UINT64) QBAinput.size(), NULL); -// input->setUcaseLA(input, ANTLR3_TRUE); // ignore case - -// if (input == NULL) -// { -// // TODO throw here -// exit(ANTLR3_ERR_NOMEM); -// } - -// // Our input stream is now open and all set to go, so we can create a new instance of our -// // lexer and set the lexer input to our input stream: -// // (file | memory | ?) --> inputstream -> lexer --> tokenstream --> parser ( --> treeparser )? -// // -// lxr = OracleSQLLexerNew(input); // CLexerNew is generated by ANTLR -// lxr->pLexer->input->charPositionInLine = 0; // fix off-by-one error for getCharPositionInLine for the 1st row - -// if ( lxr == NULL ) -// { -// // TODO throw here -// exit(ANTLR3_ERR_NOMEM); -// } - -// tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lxr)); - -// if (tstream == NULL) -// { -// // TODO throw here -// exit(ANTLR3_ERR_NOMEM); -// } - -// // Finally, now that we have our lexer constructed, we can create the parser -// // -// psr = OracleSQLParserNew(tstream); // CParserNew is generated by ANTLR3 - -// if (psr == NULL) -// { -// // TODO throw here -// exit(ANTLR3_ERR_NOMEM); -// } - -// langAST = psr->start_rule(psr); - -// if (psr->pParser->rec->state->errorCount > 0) -// { -// fprintf(stderr, "The parser returned %d errors, tree walking aborted.\n", psr->pParser->rec->state->errorCount); -// } -// // print tree elements with indent -// ////TreeWalk(langAST.tree); - -// QTextCursor cursor = editor->textCursor(); - -// this->tokenVector = tstream->getTokens(tstream); -// //ANTLR3_UINT32 size = tokenVector->count; -// ANTLR3_UINT32 i = 0; - -// TreeWalkPrinter(langAST.tree, i, cursor); - -// // for (i=0; i < size; i++) -// // { -// // pANTLR3_COMMON_TOKEN token = (pANTLR3_COMMON_TOKEN) tokenVector->get(tokenVector, i); -// // // printf("--%s-%d-\n", token->toString(token)->chars, token->getChannel(token)); -// // // printf("[%d,%d,%d] '%s'\n", -// // // token->getLine(token), -// // // token->getCharPositionInLine(token), -// // // token->getText(token)->len, -// // // token->getText(token)->chars); -// // // printf("--------------------------------------------------------------------------------\n"); -// // printf("|%d\t%d\t%s\n", token->getType(token), token->start, token->getText(token)->chars ); -// // switch(token->getType(token)) -// // { -// // case ID: -// // lastFormat = &identifierFormat; -// // break; -// // case SL_COMMENT: -// // lastFormat = &singleLineCommentFormat; -// // break; -// // case ML_COMMENT: -// // lastFormat = &multiLineCommentFormat; -// // break; -// // default: lastFormat = &identifierFormat; -// // } -// // // cursor.insertText((const char*)token->getText(token)->chars, *lastFormat); -// // } - -// psr ->free (psr); psr = NULL; -// tstream ->free (tstream); tstream = NULL; -// lxr ->free (lxr); lxr = NULL; -// input ->close (input); input = NULL; - -// this->stat->print(cursor); -// }; - -// void MainWindow::TreeWalkPrinter(ANTLR3_BASE_TREE *tree, ANTLR3_UINT32 &lastindex, QTextCursor &cur) -// { -// pANTLR3_BASE_TREE p = (ANTLR3_BASE_TREE*)tree; -// ANTLR3_UINT32 n, c; - -// if (p->isNilNode(p) == ANTLR3_TRUE) { -// //printf("// nil-node\n"); -// //return; -// } - -// n = p->getChildCount(p); - -// for (c = 0; c<n ; c++) { -// pANTLR3_BASE_TREE child; -// child = (pANTLR3_BASE_TREE) p->getChild(p, c); -// int ChildCount = child->getChildCount(child); - -// ANTLR3_UINT32 TokenType = child->getType(child); -// if (ChildCount > 0) { -// switch(TokenType) -// { -// case ID: -// lastFormat = &identifierFormat; -// break; -// case SL_COMMENT: -// lastFormat = &singleLineCommentFormat; -// break; -// case ML_COMMENT: -// lastFormat = &multiLineCommentFormat; -// break; -// default: -// ; -// lastFormat = &identifierFormat; -// }; -// TreeWalkPrinter(child, lastindex, cur); -// } else { -// // advance( child->getLine(child), -// // child->getCharPositionInLine(child), -// // child->toString(child)->chars -// // ); - -// pANTLR3_COMMON_TOKEN parser_token = child->getToken(child); -// ANTLR3_MARKER parser_token_start = parser_token->start; -// ANTLR3_UINT32 size = tokenVector->count; -// pANTLR3_COMMON_TOKEN lexer_token; -// while(lastindex<size) -// { -// lexer_token = (pANTLR3_COMMON_TOKEN) tokenVector->get(tokenVector, lastindex); -// //ANTLR3_UINT32 lexer_channel = lexer_token->getChannel(lexer_token); -// ANTLR3_MARKER lexer_token_start = lexer_token->start; -// if(lexer_token_start == parser_token_start) -// break; -// lastindex++; -// } - -// // printf(":%d\t%d\t%s:%s\n", -// // parser_token->getType(parser_token), -// // parser_token->start, -// // parser_token->getText(parser_token)->chars, -// // lexer_token->getText(lexer_token)->chars -// // ); -// cur.insertText ( (const char*) //child->toString(child)->chars -// lexer_token->getText(lexer_token)->chars -// // QString("\"%1\"[%2,%3,%4]") -// // .arg((const char*)child->toString(child)->chars ) -// // .arg(child->getLine(child)) -// // .arg(child->getCharPositionInLine(child)) -// // .arg(child->getType(child)) -// , *lastFormat -// ); -// lastindex++; -// while(lastindex<size) -// { -// lexer_token = (pANTLR3_COMMON_TOKEN) tokenVector->get(tokenVector, lastindex); - -// ANTLR3_UINT32 TokenType = lexer_token->getType(lexer_token); -// switch(TokenType) -// { -// case ID: -// lastFormat = &identifierFormat; -// break; -// case SL_COMMENT: -// lastFormat = &singleLineCommentFormat; -// break; -// case ML_COMMENT: -// lastFormat = &multiLineCommentFormat; -// break; -// default: -// ; -// lastFormat = &identifierFormat; -// }; - - -// ANTLR3_UINT32 lexer_channel = lexer_token->getChannel(lexer_token); -// //ANTLR3_MARKER lexer_token_start = lexer_token->start; -// if(lexer_channel != HIDDEN ) -// break; -// lastindex++; - -// cur.insertText ( (const char*) //child->toString(child)->chars -// lexer_token->getText(lexer_token)->chars -// , *lastFormat -// ); -// } - -// } -// } -// // cur.insertText ( "\n", *lastFormat); -// }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |