playground/devtools/kdevelop4-extra-plugins

Andreas Pakulat apaku at gmx.de
Sun Jul 8 15:53:24 UTC 2007


SVN commit 685314 by apaku:

Adapt to the latest change in kdevelop-pg and provide char*
tokenText(std::size_t begin) functions in the ruby and python parsers.

Along the way updated the ruby parser to use the kdev-pg-* headers from
KDevelop-PG and also make the parser library a shared lib.

CC'ing kdevelop-devel to bring this to the attention of the SoC students
(although they should only need a rebuild).
CCMAIL:kdevelop-devel at kdevelop.org


 M  +8 -8      python/parser/decoder.h  
 M  +13 -4     python/parser/python.g  
 M  +4 -2      python/parser/python_io.cpp  
 M  +6 -1      ruby/CMakeLists.txt  
 M  +3 -4      ruby/parser/CMakeLists.txt  
 M  +8 -8      ruby/parser/decoder.h  
 D             ruby/parser/kdev-pg-allocator.h  
 D             ruby/parser/kdev-pg-list.h  
 D             ruby/parser/kdev-pg-location-table.h  
 D             ruby/parser/kdev-pg-memory-pool.h  
 D             ruby/parser/kdev-pg-replacement.h  
 D             ruby/parser/kdev-pg-token-stream.h  
 M  +1 -1      ruby/parser/main.cpp  
 M  +2 -2      ruby/parser/parsesession.h  
 M  +12 -0     ruby/parser/ruby.g  
 M  +1 -1      ruby/parser/ruby_io.cpp  
 A             ruby/parser/rubyparserexport.h   [License: GPL (v2+)]


--- trunk/playground/devtools/kdevelop4-extra-plugins/python/parser/decoder.h #685313:685314
@@ -34,28 +34,28 @@
 
 class decoder
 {
-  parser::token_stream_type *_M_token_stream;
+  parser* _M_parser;
 
 public:
-  decoder(parser::token_stream_type *token_stream)
-    : _M_token_stream(token_stream) {}
+  decoder(parser *parser)
+    : _M_parser(parser) {}
 
   int decode_kind(std::size_t index) const
   {
-    parser::token_type const &tk = _M_token_stream->token(index);
+    parser::token_type const &tk = _M_parser->token_stream->token(index);
     return tk.kind;
   }
 
   std::string decode_string(std::size_t index) const
   {
-    parser::token_type const &tk = _M_token_stream->token(index);
-    return std::string(&tk.text[tk.begin], tk.end - tk.begin);
+    parser::token_type const &tk = _M_parser->token_stream->token(index);
+    return std::string(_M_parser->tokenText(tk.begin), tk.end - tk.begin);
   }
 
   long decode_number(std::size_t index) const
   {
-    parser::token_type const &tk = _M_token_stream->token(index);
-    return ::strtol(&tk.text[tk.begin], 0, 0);
+    parser::token_type const &tk = _M_parser->token_stream->token(index);
+    return ::strtol(_M_parser->tokenText(tk.begin), 0, 0);
   }
 };
 
--- trunk/playground/devtools/kdevelop4-extra-plugins/python/parser/python.g #685313:685314
@@ -141,9 +141,15 @@
   };
   void report_problem( parser::problem_type type, const char* message );
   void report_problem( parser::problem_type type, std::string message );
+  char* tokenText(std::size_t begin);
 
 :]
 
+%parserclass (private declaration)
+[:
+   char* m_contents;
+:]
+
 -----------------------------------------------------------
 -- List of defined tokens
 -----------------------------------------------------------
@@ -579,6 +585,7 @@
 
 void parser::tokenize( char *contents )
 {
+    m_contents = contents;
     Lexer lexer( this, contents );
     int kind = parser::Token_EOF;
 
@@ -593,7 +600,6 @@
             t.kind = kind;
             t.begin = lexer.tokenBegin();
             t.end = lexer.tokenEnd();
-            t.text = contents;
             std::cerr<<t.kind<<std::endl;
             while(lexer.dedentationLevel()>1)
             {
@@ -601,7 +607,6 @@
                 t.kind = parser::Token_DEDENT;
                 t.begin = lexer.tokenBegin();
                 t.end = lexer.tokenEnd();
-                t.text = contents;
                 std::cerr<<t.kind<<std::endl;
                 lexer.setDedentationLevel(lexer.dedentationLevel()-1);
             }
@@ -616,7 +621,6 @@
             t.kind = kind;
             t.begin = lexer.tokenBegin();
             t.end = lexer.tokenEnd();
-            t.text = contents;
             std::cerr<<t.kind<<std::endl;
             kind = x;
         }
@@ -630,7 +634,6 @@
         t.kind = kind;
         t.begin = lexer.tokenBegin();
         t.end = lexer.tokenEnd();
-        t.text = contents;
     }
     while ( kind != parser::Token_EOF );
 
@@ -638,6 +641,12 @@
 }
 
 
+char* parser::tokenText(std::size_t begin)
+{
+    return &m_contents[begin];
+}
+
+
 } // end of namespace cool
 
 :]
--- trunk/playground/devtools/kdevelop4-extra-plugins/python/parser/python_io.cpp #685313:685314
@@ -43,7 +43,7 @@
     if (done)
       return; // don't print with each call when going up the error path
 
-    decoder dec(parser->token_stream);
+    decoder dec(parser);
 
     int current_index = parser->token_stream->index() - 1;
     for (int i = current_index - 5; i < current_index + 5; i++)
@@ -115,7 +115,7 @@
     token_stream->start_position(index, &line, &col);
     size_t tokenLength = token.end - token.begin;
     char *tokenValue = new char[tokenLength+1];
-    strncpy(tokenValue, token.text + token.begin, tokenLength);
+    strncpy(tokenValue, tokenText(token.begin), tokenLength);
     tokenValue[tokenLength] = 0;
     std::stringstream s;
     s << " (current token: \"" << (token.kind != 0 ? tokenValue : "EOF") <<
@@ -127,6 +127,8 @@
         + "''" + s.str()
     );
 
+    delete tokenValue;
+
 }
 
 } // end of namespace python
--- trunk/playground/devtools/kdevelop4-extra-plugins/ruby/CMakeLists.txt #685313:685314
@@ -10,16 +10,21 @@
 endif( KDEVPG_DATA_DIR )
 
 find_package(KDE4 REQUIRED)
+find_package(KDevPlatform REQUIRED)
 
 add_subdirectory(parser)
 add_subdirectory(tests)
 
 include_directories(
-    ${KDEVPLATFORM_INCLUDES}
+    ${KDEVPLATFORM_INCLUDE_DIR}/interfaces
+    ${KDEVPLATFORM_INCLUDE_DIR}/language
+    ${KDEVPLATFORM_INCLUDE_DIR}/language/backgroundparser
+    ${KDEVPLATFORM_INCLUDE_DIR}/language/interfaces
     ${KDE4_INCLUDES}
     ${KDE4_INCLUDE_DIR}/threadweaver
     ${CMAKE_CURRENT_SOURCE_DIR}/parser
     ${CMAKE_CURRENT_BINARY_DIR}/parser
+    ${CMAKE_CURRENT_BINARY_DIR}
 )
 
 link_directories( ${KDE4_LIB_DIR} )
--- trunk/playground/devtools/kdevelop4-extra-plugins/ruby/parser/CMakeLists.txt #685313:685314
@@ -1,10 +1,8 @@
 
 find_package(KDevelop-PG)
 
-include_directories( ${KDEVPLATFORM_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} )
+include_directories( ${QT_INCLUDES} ${KDE4_INCLUDE_DIR} ${KDEVPLATFORM_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} )
 
-add_definitions(-fPIC)
-
 ########### next target ###############
 
 set(kdevrubyparser_STAT_SRCS
@@ -154,7 +152,8 @@
 
 
 kde4_automoc(${kdevrubyparser_STAT_SRCS})
-kde4_add_library(kdevrubyparser STATIC ${kdevrubyparser_STAT_SRCS})
+kde4_add_library(kdevrubyparser SHARED ${kdevrubyparser_STAT_SRCS})
+target_link_libraries(kdevrubyparser ${QT_QTCORE_LIBRARY})
 
 # generate the command-line parser
 add_executable(ruby-parser main.cpp)
--- trunk/playground/devtools/kdevelop4-extra-plugins/ruby/parser/decoder.h #685313:685314
@@ -34,28 +34,28 @@
 
 class decoder
 {
-  parser::token_stream_type *_M_token_stream;
+  parser *_M_parser;
 
 public:
-  decoder(parser::token_stream_type *token_stream)
-    : _M_token_stream(token_stream) {}
+  decoder(parser *parser)
+    : _M_parser(parser) {}
 
   int decode_kind(std::size_t index) const
   {
-    parser::token_type const &tk = _M_token_stream->token(index);
+    parser::token_type const &tk = _M_parser->token_stream->token(index);
     return tk.kind;
   }
 
   std::string decode_string(std::size_t index) const
   {
-    parser::token_type const &tk = _M_token_stream->token(index);
-    return std::string(&tk.text[tk.begin], tk.end - tk.begin);
+    parser::token_type const &tk = _M_parser->token_stream->token(index);
+    return std::string(_M_parser->tokenText(tk.begin), tk.end - tk.begin);
   }
 
   long decode_number(std::size_t index) const
   {
-    parser::token_type const &tk = _M_token_stream->token(index);
-    return ::strtol(&tk.text[tk.begin], 0, 0);
+    parser::token_type const &tk = _M_parser->token_stream->token(index);
+    return ::strtol(_M_parser->tokenText(tk.begin), 0, 0);
   }
 };
 
--- trunk/playground/devtools/kdevelop4-extra-plugins/ruby/parser/main.cpp #685313:685314
@@ -44,7 +44,7 @@
     if (done)
       return; // don't print with each call when going up the error path
 
-    decoder dec(parser->token_stream);
+    decoder dec(parser);
 
     std::size_t current_index = parser->token_stream->index() - 1;
     for (std::size_t i = current_index - 5; i < current_index + 5; i++)
--- trunk/playground/devtools/kdevelop4-extra-plugins/ruby/parser/parsesession.h #685313:685314
@@ -25,14 +25,14 @@
 #include <QtCore/QByteArray>
 
 #include "ruby_parser.h"
+#include "rubyparserexport.h"
 
-
 namespace ruby
 {
 
 /// Contains everything needed to keep an AST useful once the rest of the parser
 /// has gone away.
-class ParseSession
+class KDEVRUBYPARSER_EXPORT ParseSession
 {
 public:
   ParseSession();
--- trunk/playground/devtools/kdevelop4-extra-plugins/ruby/parser/ruby.g #685313:685314
@@ -56,6 +56,9 @@
 ------------------------------------------------------------
 
 [:
+
+#include "rubyparserexport.h"
+
 namespace ruby
 {
   class Lexer;
@@ -84,6 +87,7 @@
 
   void report_problem( parser::problem_type type, const char* message );
   void report_problem( parser::problem_type type, std::string message );
+  char* tokenText(std::size_t begin);
 :]
 
 
@@ -106,6 +110,7 @@
   bool expect_array_or_block_arguments;
 
   Lexer *m_lexer;
+  char* m_contents;
 :]
 
 
@@ -1062,6 +1067,7 @@
 
 void parser::tokenize( char *contents )
 {
+    m_contents = contents;
     m_lexer = new Lexer( this, contents );
 
     int kind = parser::Token_EOF;
@@ -1098,6 +1104,12 @@
     _M_state.ltCounter = state->ltCounter;
 }
 
+
+char* parser::tokenText(std::size_t begin)
+{
+    return &m_contents[begin];
+}
+
 } // end of namespace ruby
 
 :]
--- trunk/playground/devtools/kdevelop4-extra-plugins/ruby/parser/ruby_io.cpp #685313:685314
@@ -72,7 +72,7 @@
     token_stream->start_position(index, &line, &col);
     size_t tokenLength = token.end - token.begin;
     char *tokenValue = new char[tokenLength+1];
-    strncpy(tokenValue, token.text + token.begin, tokenLength);
+    strncpy(tokenValue, tokenText(token.begin), tokenLength);
     tokenValue[tokenLength] = 0;
     std::stringstream s;
     s << " (current token: \"" << (token.kind != 0 ? tokenValue : "EOF") <<




More information about the KDevelop-devel mailing list