| 1 | /*************************************************************************** |
| 2 | A SymbolBrowser plugin for JuffEd editor. |
| 3 | |
| 4 | Copyright: 2010 Aleksander Sokoloff <[email protected]> |
| 5 | |
| 6 | Some of the code based on a ctags project http://ctags.sourceforge.net/ |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or |
| 9 | modify it under the terms of the GNU General Public License. |
| 10 | version 2 as published by the Free Software Foundation. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have rceived a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software Foundation, |
| 19 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <QDebug> |
| 24 | #include <QStringList> |
| 25 | #include "parser.h" |
| 26 | #include "../symbol.h" |
| 27 | |
| 28 | |
| 29 | /***************************************************************************** |
| 30 | *****************************************************************************/ |
| 31 | Parser::Parser(): QObject() { |
| 32 | mTextBuf = mTextBufEnd = NULL; |
| 33 | mNChar = mRChar = NULL; |
| 34 | mUngetChar = EOF; |
| 35 | mStopped = true; |
| 36 | mLineNum = 0; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /***************************************************************************** |
| 41 | *****************************************************************************/ |
| 42 | Parser::~Parser() { |
| 43 | |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /***************************************************************************** |
| 48 | *****************************************************************************/ |
| 49 | void Parser::run(char *textBuf, int textBufLen, Symbol *symbols) { |
| 50 | mSymbols = symbols; |
| 51 | mTextBuf = textBuf; |
| 52 | mTextBufEnd = mTextBuf; |
| 53 | mTextBufEnd += textBufLen; |
| 54 | resetText(); |
| 55 | |
| 56 | mStopped = false; |
| 57 | parse(); |
| 58 | } |
| 59 | |
| 60 | /***************************************************************************** |
| 61 | *****************************************************************************/ |
| 62 | void Parser::stop() { |
| 63 | mStopped = true; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /***************************************************************************** |
| 68 | This function reads a single character, performing newline |
| 69 | canonicalization. |
| 70 | To not mix use of fileReadLine () and fileGetc () for the same file. |
| 71 | *****************************************************************************/ |
| 72 | int Parser::fileGetc(){ |
| 73 | if (mStopped) |
| 74 | return EOF; |
| 75 | |
| 76 | if (mCurPos == mTextBufEnd) |
| 77 | return EOF; |
| 78 | |
| 79 | if (mUngetChar != EOF){ |
| 80 | int res = mUngetChar; |
| 81 | mUngetChar = EOF; |
| 82 | return res; |
| 83 | } |
| 84 | |
| 85 | char result = *mCurPos; |
| 86 | if (mCurPos>mTextBuf && *(mCurPos-1) == '\n') |
| 87 | mLineNum++; |
| 88 | |
| 89 | mCurPos++; |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /***************************************************************************** |
| 95 | An alternative interface to fileGetc (). Do not mix use of fileReadLine() |
| 96 | and fileGetc() for the same file. The returned string does not contain |
| 97 | the terminating newline. A NULL return value means that all lines in the |
| 98 | file have been read and we are at the end of file. |
| 99 | *****************************************************************************/ |
| 100 | const char *Parser::fileReadLine(){ |
| 101 | if (mStopped || mCurPos == mTextBufEnd) |
| 102 | return NULL; |
| 103 | |
| 104 | if (mRChar) *mRChar = '\r'; |
| 105 | if (mNChar) *mNChar = '\n'; |
| 106 | |
| 107 | char *start = mCurPos; |
| 108 | for(; mCurPos < mTextBufEnd; ++mCurPos) { |
| 109 | |
| 110 | if (char(*mCurPos) == '\n') { |
| 111 | mNChar = mCurPos; |
| 112 | mRChar = NULL; |
| 113 | *mCurPos = '\0'; |
| 114 | mCurPos++; |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | if (char(*mCurPos) == '\r') { |
| 119 | mRChar = mCurPos; |
| 120 | mNChar = NULL; |
| 121 | *mCurPos = '\0'; |
| 122 | mCurPos += 2; |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | // For first text line mLineNum already have 0. |
| 129 | if (mCurPos > mTextBuf) ++mLineNum; |
| 130 | |
| 131 | return start; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /***************************************************************************** |
| 136 | *****************************************************************************/ |
| 137 | void Parser::fileUngetc(int c){ |
| 138 | mUngetChar = c; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /***************************************************************************** |
| 143 | *****************************************************************************/ |
| 144 | unsigned long Parser::getSourceLineNumber(){ |
| 145 | return mLineNum; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /***************************************************************************** |
| 150 | *****************************************************************************/ |
| 151 | unsigned long Parser::getInputLineNumber(){ |
| 152 | return mLineNum; |
| 153 | } |
| 154 | |
| 155 | /************************************************ |
| 156 | * |
| 157 | ************************************************/ |
| 158 | void Parser::resetText() { |
| 159 | mCurPos = mTextBuf; |
| 160 | if (mRChar) *mRChar = '\r'; |
| 161 | if (mNChar) *mNChar = '\n'; |
| 162 | mRChar = NULL; |
| 163 | mNChar = NULL; |
| 164 | } |
| 165 |