2 #include "ScriptParser.h"
4 CScriptParser::CScriptParser(void):
6 m_pScriptSection(NULL),
7 m_pLastScriptSection(NULL),
12 CScriptParser::~CScriptParser(void) {
16 void CScriptParser::ClearBuffer(void) {
25 m_pScriptSection = NULL;
26 m_pLastScriptSection = NULL;
27 memset(m_breakChars, 0, sizeof(m_breakChars));
30 const char* CScriptParser::MakeToken(const char* pToken) {
40 int len = static_cast<int>(strlen(pToken));
42 m_pToken = new char[len + 1];
44 strcpy(m_pToken, pToken);
49 #define MAX_TOKEN_STRING 1024
50 // Should NEVER return NULL
51 const char* CScriptParser::GetToken(bool bAllowLinebreaks) {
53 char token[MAX_TOKEN_STRING];
54 bool bNewLines = false;
56 m_pLastScriptSection = m_pScriptSection;
61 if(!m_pScript || !m_pScriptSection) {
62 return MakeToken(token);
66 SkipWhitespace( &bNewLines );
67 if ( !*m_pScriptSection ) {
68 return MakeToken(token);
70 if ( bNewLines && !bAllowLinebreaks ) {
71 return MakeToken(token);
74 c = *m_pScriptSection;
76 if ( c == '/' && m_pScriptSection[1] == '/' ) { // C style comments
77 m_pScriptSection += 2;
78 while (*m_pScriptSection && *m_pScriptSection != '\n') {
81 } else if ( c=='/' && m_pScriptSection[1] == '*' ) { // C++ style comments
82 m_pScriptSection += 2;
83 while ( *m_pScriptSection && ( *m_pScriptSection != '*' || m_pScriptSection[1] != '/' ) ) {
86 if ( *m_pScriptSection ) {
87 m_pScriptSection += 2;
97 c = *m_pScriptSection++;
100 return MakeToken(token);
102 if (len < MAX_TOKEN_STRING) {
110 if(len > 0 && IsBreakChar(*m_pScriptSection)) {
114 if (len < MAX_TOKEN_STRING) {
124 c = *m_pScriptSection;
127 if (len == MAX_TOKEN_STRING) {
132 return MakeToken(token);
135 void CScriptParser::SkipWhitespace(bool* pbNewLines) {
138 if(!m_pScript || !m_pScriptSection) {
142 while( (c = *m_pScriptSection) <= ' ') {
153 void CScriptParser::SkipBracedSection(void) {
159 token = GetToken( true );
160 if( token[1] == 0 ) {
161 if( *token == '{' ) {
163 } else if( *token == '}' ) {
167 } while( depth && *m_pScriptSection );
170 void CScriptParser::SkipRestOfLine(void) {
174 p = m_pScriptSection;
175 while ( (c = *p++) != 0 ) {
180 m_pScriptSection = p;
183 void CScriptParser::UndoGetToken(void) {
184 if(!m_pLastScriptSection) {
187 m_pScriptSection = m_pLastScriptSection;
188 m_pLastScriptSection = NULL;
191 void CScriptParser::ResetParseSession(void) {
196 m_pScriptSection = m_pScript;
197 m_pLastScriptSection = NULL;
200 char* CScriptParser::GetBufferCopy(void) {
205 int len = static_cast<int>(strlen(m_pScript));
206 char* pBuffer = new char[len + 1];
207 strcpy(pBuffer, m_pScript);
211 int CScriptParser::GetTokenOffset(void) {
212 if(!m_pScript || !m_pScriptSection) {
216 return static_cast<int>(m_pScriptSection - m_pScript);
219 void CScriptParser::LoadScript(const char* pScript) {
222 int len = static_cast<int>(strlen(pScript));
227 m_pScript = new char[len + 1];
228 m_pScript[len] = '\0';
230 strcpy(m_pScript, pScript);
231 m_pScriptSection = m_pScript;
234 void CScriptParser::AddBreakChar(char c) {
235 for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {
236 if(!m_breakChars[i]) {
242 // TODO: Error: max break chars hit
245 bool CScriptParser::IsBreakChar(char c) {
246 for(int i = 0; i < SP_MAX_BREAKCHARS; i++) {
247 if(!m_breakChars[i]) {
250 if(m_breakChars[i] == c) {
257 void CScriptParser::SetScript(char* pScript) {
260 int len = static_cast<int>(strlen(pScript));
266 m_pScriptSection = m_pScript;