license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxsl / lexer.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Scripting Language Interpreter
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: lexer.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25 #include "parser.tab.hpp"
26
27
28 //
29 // Constructor for our lexer class
30 //
31
32 NXSL_Lexer::NXSL_Lexer(NXSL_Compiler *pCompiler, const TCHAR *pszCode)
33 {
34 #ifdef UNICODE
35 m_nSourceSize = wcslen(pszCode);
36 m_pszSourceCode = (char *)malloc(m_nSourceSize + 1);
37 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, pszCode,
38 -1, m_pszSourceCode, m_nSourceSize + 1, NULL, NULL);
39 #else
40 m_pszSourceCode = strdup(pszCode);
41 m_nSourceSize = (int)strlen(pszCode);
42 #endif
43 m_nCurrLine = 1;
44 m_nSourcePos = 0;
45 m_pCompiler = pCompiler;
46 m_bErrorState = FALSE;
47 }
48
49
50 //
51 // Destructor
52 //
53
54 NXSL_Lexer::~NXSL_Lexer()
55 {
56 safe_free(m_pszSourceCode);
57 }
58
59
60 //
61 // Alternative input method
62 //
63
64 int NXSL_Lexer::lexerInput(char *pBuffer, int nMaxSize)
65 {
66 int nBytes;
67
68 if (m_nSourcePos < m_nSourceSize)
69 {
70 nBytes = min(nMaxSize, m_nSourceSize - m_nSourcePos);
71 memcpy(pBuffer, &m_pszSourceCode[m_nSourcePos], nBytes);
72 m_nSourcePos += nBytes;
73 }
74 else
75 {
76 nBytes = 0; // EOF
77 }
78 return nBytes;
79 }
80
81
82 //
83 // Report error
84 //
85
86 void NXSL_Lexer::error(const char *pszText)
87 {
88 m_pCompiler->error(pszText);
89 }