CMDscan.l 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. %option yylineno nounput
  2. %{
  3. // flex --nounput -o CMDscan.cpp -P CMD CMDscan.l
  4. #define YYLMAX 4096
  5. #define YY_NO_UNISTD_H
  6. #include <stdio.h>
  7. #include "platform/platform.h"
  8. #include "core/stringTable.h"
  9. #include "console/console.h"
  10. #include "console/torquescript/compiler.h"
  11. #include "console/dynamicTypes.h"
  12. #include "core/strings/stringFunctions.h"
  13. template< typename T >
  14. struct Token
  15. {
  16. T value;
  17. S32 lineNumber;
  18. };
  19. // Can't have ctors in structs used in unions, so we have this.
  20. template< typename T >
  21. inline Token< T > MakeToken( T value, U32 lineNumber )
  22. {
  23. Token< T > result;
  24. result.value = value;
  25. result.lineNumber = lineNumber;
  26. return result;
  27. }
  28. #include "console/torquescript/CMDgram.h"
  29. // HACK: C++17 and beyond can't use register keyword
  30. #define register
  31. using namespace Compiler;
  32. #define YY_NEVER_INTERACTIVE 1
  33. // Some basic parsing primitives...
  34. static int Sc_ScanDocBlock();
  35. static int Sc_ScanString(int ret);
  36. static int Sc_ScanNum();
  37. static int Sc_ScanVar();
  38. static int Sc_ScanHex();
  39. static int Sc_ScanIdent();
  40. // Deal with debuggability of FLEX.
  41. #ifdef TORQUE_DEBUG
  42. #define FLEX_DEBUG 1
  43. #else
  44. #define FLEX_DEBUG 0
  45. #endif
  46. Vector<String> lines;
  47. // Install our own input code...
  48. #undef CMDgetc
  49. int CMDgetc();
  50. // Hack to make windows lex happy.
  51. #ifndef isatty
  52. inline int isatty(int) { return 0; }
  53. #endif
  54. static int yycolumn = 1;
  55. // Wrap our getc, so that lex doesn't try to do its own buffering/file IO.
  56. #define YY_INPUT(buf,result,max_size) \
  57. { \
  58. int c = '*', n; \
  59. for ( n = 0; n < max_size && \
  60. (c = CMDgetc()) != EOF && c != '\n'; ++n ) \
  61. buf[n] = (char) c; \
  62. if ( c == '\n' ) \
  63. buf[n++] = (char) c; yycolumn = 1;\
  64. result = n; \
  65. }
  66. #define YY_USER_ACTION do { \
  67. CMDlloc.first_line = CMDlloc.last_line = yylineno; \
  68. CMDlloc.first_column = yycolumn; CMDlloc.last_column = yycolumn + yyleng - 1; \
  69. yycolumn += yyleng; \
  70. } while(0);
  71. // File state
  72. void CMDSetScanBuffer(const char *sb, const char *fn);
  73. // Error reporting
  74. void CMDerror(const char * s, ...);
  75. // Reset the parser.
  76. void CMDrestart(FILE *in);
  77. %}
  78. DIGIT [0-9]
  79. INTEGER {DIGIT}+
  80. FLOAT ({INTEGER}?\.{INTEGER})|({INTEGER}(\.{INTEGER})?[eE][+-]?{INTEGER})
  81. LETTER [A-Za-z_]
  82. FILECHAR [A-Za-z_\.]
  83. VARMID [:A-Za-z0-9_]
  84. IDTAIL [A-Za-z0-9_]
  85. VARTAIL {VARMID}*{IDTAIL}
  86. VAR [$%]{LETTER}{VARTAIL}*
  87. ID {LETTER}{IDTAIL}*
  88. ILID [$%]{DIGIT}+{LETTER}{VARTAIL}*
  89. FILENAME {FILECHAR}+
  90. SPACE [ \t\v\f]
  91. HEXDIGIT [a-fA-F0-9]
  92. %%
  93. ;
  94. {SPACE}+ { }
  95. ("///"([^/\n\r][^\n\r]*)?[\n\r]+)+ { return(Sc_ScanDocBlock()); }
  96. "//"[^\n\r]* ;
  97. [\r] ;
  98. \n.* { yycolumn = 1;
  99. lines.push_back(String::ToString("%s", yytext+1));
  100. if (lines.size() > Con::getIntVariable("$scriptErrorLineCount", 10))
  101. lines.erase(lines.begin());
  102. yyless(1);
  103. }
  104. \"(\\.|[^\\"\n\r])*\" { return(Sc_ScanString(STRATOM)); }
  105. \'(\\.|[^\\'\n\r])*\' { return(Sc_ScanString(TAGATOM)); }
  106. "==" { CMDlval.i = MakeToken< int >( opEQ, yylineno ); return opEQ; }
  107. "!=" { CMDlval.i = MakeToken< int >( opNE, yylineno ); return opNE; }
  108. ">=" { CMDlval.i = MakeToken< int >( opGE, yylineno ); return opGE; }
  109. "<=" { CMDlval.i = MakeToken< int >( opLE, yylineno ); return opLE; }
  110. "&&" { CMDlval.i = MakeToken< int >( opAND, yylineno ); return opAND; }
  111. "||" { CMDlval.i = MakeToken< int >( opOR, yylineno ); return opOR; }
  112. "::" { CMDlval.i = MakeToken< int >( opCOLONCOLON, yylineno ); return opCOLONCOLON; }
  113. "--" { CMDlval.i = MakeToken< int >( opMINUSMINUS, yylineno ); return opMINUSMINUS; }
  114. "++" { CMDlval.i = MakeToken< int >( opPLUSPLUS, yylineno ); return opPLUSPLUS; }
  115. "$=" { CMDlval.i = MakeToken< int >( opSTREQ, yylineno ); return opSTREQ; }
  116. "!$=" { CMDlval.i = MakeToken< int >( opSTRNE, yylineno ); return opSTRNE; }
  117. "<<" { CMDlval.i = MakeToken< int >( opSHL, yylineno ); return opSHL; }
  118. ">>" { CMDlval.i = MakeToken< int >( opSHR, yylineno ); return opSHR; }
  119. "+=" { CMDlval.i = MakeToken< int >( opPLASN, yylineno ); return opPLASN; }
  120. "-=" { CMDlval.i = MakeToken< int >( opMIASN, yylineno ); return opMIASN; }
  121. "*=" { CMDlval.i = MakeToken< int >( opMLASN, yylineno ); return opMLASN; }
  122. "/=" { CMDlval.i = MakeToken< int >( opDVASN, yylineno ); return opDVASN; }
  123. "%=" { CMDlval.i = MakeToken< int >( opMODASN, yylineno ); return opMODASN; }
  124. "&=" { CMDlval.i = MakeToken< int >( opANDASN, yylineno ); return opANDASN; }
  125. "^=" { CMDlval.i = MakeToken< int >( opXORASN, yylineno ); return opXORASN; }
  126. "|=" { CMDlval.i = MakeToken< int >( opORASN, yylineno ); return opORASN; }
  127. "<<=" { CMDlval.i = MakeToken< int >( opSLASN, yylineno ); return opSLASN; }
  128. ">>=" { CMDlval.i = MakeToken< int >( opSRASN, yylineno ); return opSRASN; }
  129. "->" { CMDlval.i = MakeToken< int >( opINTNAME, yylineno ); return opINTNAME; }
  130. "-->" { CMDlval.i = MakeToken< int >( opINTNAMER, yylineno ); return opINTNAMER; }
  131. "NL" { CMDlval.i = MakeToken< int >( '\n', yylineno ); return '@'; }
  132. "TAB" { CMDlval.i = MakeToken< int >( '\t', yylineno ); return '@'; }
  133. "SPC" { CMDlval.i = MakeToken< int >( ' ', yylineno ); return '@'; }
  134. "@" { CMDlval.i = MakeToken< int >( 0, yylineno ); return '@'; }
  135. "/*" { /* this comment stops syntax highlighting from getting messed up when editing the lexer in TextPad */
  136. int c = 0, l;
  137. for ( ; ; )
  138. {
  139. l = c;
  140. c = yyinput();
  141. // Is this an open comment?
  142. if ( c == EOF )
  143. {
  144. CMDerror( "unexpected end of file found in comment" );
  145. break;
  146. }
  147. // Did we find the end of the comment?
  148. else if ( l == '*' && c == '/' )
  149. break;
  150. }
  151. }
  152. "?" |
  153. "[" |
  154. "]" |
  155. "(" |
  156. ")" |
  157. "+" |
  158. "-" |
  159. "*" |
  160. "/" |
  161. "<" |
  162. ">" |
  163. "|" |
  164. "." |
  165. "!" |
  166. ":" |
  167. ";" |
  168. "{" |
  169. "}" |
  170. "," |
  171. "&" |
  172. "%" |
  173. "^" |
  174. "~" |
  175. "=" { CMDlval.i = MakeToken< int >( CMDtext[ 0 ], yylineno ); return CMDtext[ 0 ]; }
  176. "in" { CMDlval.i = MakeToken< int >( rwIN, yylineno ); return(rwIN); }
  177. "or" { CMDlval.i = MakeToken< int >( rwCASEOR, yylineno ); return(rwCASEOR); }
  178. "break" { CMDlval.i = MakeToken< int >( rwBREAK, yylineno ); return(rwBREAK); }
  179. "return" { CMDlval.i = MakeToken< int >( rwRETURN, yylineno ); return(rwRETURN); }
  180. "else" { CMDlval.i = MakeToken< int >( rwELSE, yylineno ); return(rwELSE); }
  181. "assert" { CMDlval.i = MakeToken< int >( rwASSERT, yylineno ); return(rwASSERT); }
  182. "while" { CMDlval.i = MakeToken< int >( rwWHILE, yylineno ); return(rwWHILE); }
  183. "do" { CMDlval.i = MakeToken< int >( rwDO, yylineno ); return(rwDO); }
  184. "if" { CMDlval.i = MakeToken< int >( rwIF, yylineno ); return(rwIF); }
  185. "foreach$" { CMDlval.i = MakeToken< int >( rwFOREACHSTR, yylineno ); return(rwFOREACHSTR); }
  186. "foreach" { CMDlval.i = MakeToken< int >( rwFOREACH, yylineno ); return(rwFOREACH); }
  187. "for" { CMDlval.i = MakeToken< int >( rwFOR, yylineno ); return(rwFOR); }
  188. "continue" { CMDlval.i = MakeToken< int >( rwCONTINUE, yylineno ); return(rwCONTINUE); }
  189. "function" { CMDlval.i = MakeToken< int >( rwDEFINE, yylineno ); return(rwDEFINE); }
  190. "new" { CMDlval.i = MakeToken< int >( rwDECLARE, yylineno ); return(rwDECLARE); }
  191. "singleton" { CMDlval.i = MakeToken< int >( rwDECLARESINGLETON, yylineno ); return(rwDECLARESINGLETON); }
  192. "datablock" { CMDlval.i = MakeToken< int >( rwDATABLOCK, yylineno ); return(rwDATABLOCK); }
  193. "case" { CMDlval.i = MakeToken< int >( rwCASE, yylineno ); return(rwCASE); }
  194. "switch$" { CMDlval.i = MakeToken< int >( rwSWITCHSTR, yylineno ); return(rwSWITCHSTR); }
  195. "switch" { CMDlval.i = MakeToken< int >( rwSWITCH, yylineno ); return(rwSWITCH); }
  196. "default" { CMDlval.i = MakeToken< int >( rwDEFAULT, yylineno ); return(rwDEFAULT); }
  197. "package" { CMDlval.i = MakeToken< int >( rwPACKAGE, yylineno ); return(rwPACKAGE); }
  198. "namespace" { CMDlval.i = MakeToken< int >( rwNAMESPACE, yylineno ); return(rwNAMESPACE); }
  199. "true" { CMDlval.i = MakeToken< int >( 1, yylineno ); return INTCONST; }
  200. "false" { CMDlval.i = MakeToken< int >( 0, yylineno ); return INTCONST; }
  201. {VAR} { return(Sc_ScanVar()); }
  202. {ID} { return Sc_ScanIdent(); }
  203. 0[xX]{HEXDIGIT}+ return(Sc_ScanHex());
  204. {INTEGER} { CMDtext[CMDleng] = 0; CMDlval.i = MakeToken< int >( dAtoi(CMDtext), yylineno ); return INTCONST; }
  205. {FLOAT} return Sc_ScanNum();
  206. {ILID} return(ILLEGAL_TOKEN);
  207. . return(ILLEGAL_TOKEN);
  208. %%
  209. static const char *scanBuffer;
  210. static const char *fileName;
  211. static int scanIndex;
  212. extern YYLTYPE CMDlloc;
  213. const char * CMDGetCurrentFile()
  214. {
  215. return fileName;
  216. }
  217. int CMDGetCurrentLine()
  218. {
  219. return yylineno;
  220. }
  221. extern bool gConsoleSyntaxError;
  222. void CMDerror(const char *format, ...)
  223. {
  224. Compiler::gSyntaxError = true;
  225. const int BUFMAX = 1024;
  226. char tempBuf[BUFMAX];
  227. va_list args;
  228. va_start( args, format );
  229. #ifdef TORQUE_OS_WIN
  230. _vsnprintf( tempBuf, BUFMAX, format, args );
  231. #else
  232. vsnprintf( tempBuf, BUFMAX, format, args );
  233. #endif
  234. va_end(args);
  235. if(fileName)
  236. {
  237. Con::errorf(ConsoleLogEntry::Script, "%s Line: %d - %s", fileName, yylineno, tempBuf);
  238. // Update the script-visible error buffer.
  239. const char *prevStr = Con::getVariable("$ScriptError");
  240. if (prevStr[0])
  241. dSprintf(tempBuf, sizeof(tempBuf), "%s\n%s Line: %d - Syntax error.", prevStr, fileName, yylineno);
  242. else
  243. dSprintf(tempBuf, sizeof(tempBuf), "%s Line: %d - Syntax error.", fileName, yylineno);
  244. Con::setVariable("$ScriptError", tempBuf);
  245. // We also need to mark that we came up with a new error.
  246. static S32 sScriptErrorHash=1000;
  247. Con::setIntVariable("$ScriptErrorHash", sScriptErrorHash++);
  248. }
  249. else
  250. {
  251. Con::errorf(ConsoleLogEntry::Script, tempBuf);
  252. }
  253. }
  254. void CMDSetScanBuffer(const char *sb, const char *fn)
  255. {
  256. scanBuffer = sb;
  257. fileName = fn;
  258. scanIndex = 0;
  259. }
  260. int CMDgetc()
  261. {
  262. int ret = scanBuffer[scanIndex];
  263. if(ret)
  264. scanIndex++;
  265. else
  266. ret = -1;
  267. return ret;
  268. }
  269. int CMDwrap()
  270. {
  271. return 1;
  272. }
  273. static int Sc_ScanVar()
  274. {
  275. // Truncate the temp buffer...
  276. CMDtext[CMDleng] = 0;
  277. // Make it a stringtable string!
  278. CMDlval.s = MakeToken< StringTableEntry >( StringTable->insert(CMDtext), yylineno );
  279. return(VAR);
  280. }
  281. static int charConv(int in)
  282. {
  283. switch(in)
  284. {
  285. case 'r':
  286. return '\r';
  287. case 'n':
  288. return '\n';
  289. case 't':
  290. return '\t';
  291. default:
  292. return in;
  293. }
  294. }
  295. static int getHexDigit(char c)
  296. {
  297. if(c >= '0' && c <= '9')
  298. return c - '0';
  299. if(c >= 'A' && c <= 'F')
  300. return c - 'A' + 10;
  301. if(c >= 'a' && c <= 'f')
  302. return c - 'a' + 10;
  303. return -1;
  304. }
  305. static int Sc_ScanDocBlock()
  306. {
  307. S32 len = dStrlen(CMDtext);
  308. char* text = (char *) consoleAlloc(len + 1);
  309. S32 line = yylineno;
  310. for( S32 i = 0, j = 0; j <= len; j++ )
  311. {
  312. if( ( j <= (len - 2) ) && ( CMDtext[j] == '/' ) && ( CMDtext[j + 1] == '/' ) && ( CMDtext[j + 2] == '/' ) )
  313. {
  314. j += 2;
  315. continue;
  316. }
  317. if( CMDtext[j] == '\r' )
  318. continue;
  319. text[i++] = CMDtext[j];
  320. }
  321. CMDlval.str = MakeToken< char* >( text, line );
  322. return(DOCBLOCK);
  323. }
  324. static int Sc_ScanString(int ret)
  325. {
  326. CMDtext[CMDleng - 1] = 0;
  327. if(!collapseEscape(CMDtext+1))
  328. return -1;
  329. dsize_t bufferLen = dStrlen( CMDtext );
  330. char* buffer = ( char* ) consoleAlloc( bufferLen );
  331. dStrcpy( buffer, CMDtext + 1, bufferLen );
  332. CMDlval.str = MakeToken< char* >( buffer, yylineno );
  333. return ret;
  334. }
  335. static int Sc_ScanIdent()
  336. {
  337. ConsoleBaseType *type;
  338. CMDtext[CMDleng] = 0;
  339. if((type = ConsoleBaseType::getTypeByName(CMDtext)) != NULL)
  340. {
  341. /* It's a type */
  342. CMDlval.i = MakeToken< int >( type->getTypeID(), yylineno );
  343. return TYPEIDENT;
  344. }
  345. /* It's an identifier */
  346. CMDlval.s = MakeToken< StringTableEntry >( StringTable->insert(CMDtext), yylineno );
  347. return IDENT;
  348. }
  349. void expandEscape(char *dest, const char *src)
  350. {
  351. U8 c;
  352. while((c = (U8) *src++) != 0)
  353. {
  354. if(c == '\"')
  355. {
  356. *dest++ = '\\';
  357. *dest++ = '\"';
  358. }
  359. else if(c == '\\')
  360. {
  361. *dest++ = '\\';
  362. *dest++ = '\\';
  363. }
  364. else if(c == '\r')
  365. {
  366. *dest++ = '\\';
  367. *dest++ = 'r';
  368. }
  369. else if(c == '\n')
  370. {
  371. *dest++ = '\\';
  372. *dest++ = 'n';
  373. }
  374. else if(c == '\t')
  375. {
  376. *dest++ = '\\';
  377. *dest++ = 't';
  378. }
  379. else if(c == '\'')
  380. {
  381. *dest++ = '\\';
  382. *dest++ = '\'';
  383. }
  384. else if((c >= 1 && c <= 7) ||
  385. (c >= 11 && c <= 12) ||
  386. (c >= 14 && c <= 15))
  387. {
  388. /* Remap around: \b = 0x8, \t = 0x9, \n = 0xa, \r = 0xd */
  389. static U8 expandRemap[15] = { 0x0,
  390. 0x0,
  391. 0x1,
  392. 0x2,
  393. 0x3,
  394. 0x4,
  395. 0x5,
  396. 0x6,
  397. 0x0,
  398. 0x0,
  399. 0x0,
  400. 0x7,
  401. 0x8,
  402. 0x0,
  403. 0x9 };
  404. *dest++ = '\\';
  405. *dest++ = 'c';
  406. if(c == 15)
  407. *dest++ = 'r';
  408. else if(c == 16)
  409. *dest++ = 'p';
  410. else if(c == 17)
  411. *dest++ = 'o';
  412. else
  413. *dest++ = expandRemap[c] + '0';
  414. }
  415. else if(c < 32)
  416. {
  417. *dest++ = '\\';
  418. *dest++ = 'x';
  419. S32 dig1 = c >> 4;
  420. S32 dig2 = c & 0xf;
  421. if(dig1 < 10)
  422. dig1 += '0';
  423. else
  424. dig1 += 'A' - 10;
  425. if(dig2 < 10)
  426. dig2 += '0';
  427. else
  428. dig2 += 'A' - 10;
  429. *dest++ = dig1;
  430. *dest++ = dig2;
  431. }
  432. else
  433. *dest++ = c;
  434. }
  435. *dest = '\0';
  436. }
  437. bool collapseEscape(char *buf)
  438. {
  439. S32 len = dStrlen(buf) + 1;
  440. for(S32 i = 0; i < len;)
  441. {
  442. if(buf[i] == '\\')
  443. {
  444. if(buf[i+1] == 'x')
  445. {
  446. S32 dig1 = getHexDigit(buf[i+2]);
  447. if(dig1 == -1)
  448. return false;
  449. S32 dig2 = getHexDigit(buf[i+3]);
  450. if(dig2 == -1)
  451. return false;
  452. buf[i] = dig1 * 16 + dig2;
  453. dMemmove(buf + i + 1, buf + i + 4, len - i - 3);
  454. len -= 3;
  455. i++;
  456. }
  457. else if(buf[i+1] == 'c')
  458. {
  459. /* Remap around: \b = 0x8, \t = 0x9, \n = 0xa, \r = 0xd */
  460. static U8 collapseRemap[10] = { 0x1,
  461. 0x2,
  462. 0x3,
  463. 0x4,
  464. 0x5,
  465. 0x6,
  466. 0x7,
  467. 0xb,
  468. 0xc,
  469. 0xe };
  470. if(buf[i+2] == 'r')
  471. buf[i] = 15;
  472. else if(buf[i+2] == 'p')
  473. buf[i] = 16;
  474. else if(buf[i+2] == 'o')
  475. buf[i] = 17;
  476. else
  477. {
  478. int dig1 = buf[i+2] - '0';
  479. if(dig1 < 0 || dig1 > 9)
  480. return false;
  481. buf[i] = collapseRemap[dig1];
  482. }
  483. // Make sure we don't put 0x1 at the beginning of the string.
  484. if ((buf[i] == 0x1) && (i == 0))
  485. {
  486. buf[i] = 0x2;
  487. buf[i+1] = 0x1;
  488. dMemmove(buf + i + 2, buf + i + 3, len - i - 1);
  489. len -= 1;
  490. }
  491. else
  492. {
  493. dMemmove(buf + i + 1, buf + i + 3, len - i - 2);
  494. len -= 2;
  495. }
  496. i++;
  497. }
  498. else
  499. {
  500. buf[i] = charConv(buf[i+1]);
  501. dMemmove(buf + i + 1, buf + i + 2, len - i - 1);
  502. len--;
  503. i++;
  504. }
  505. }
  506. else
  507. i++;
  508. }
  509. return true;
  510. }
  511. static int Sc_ScanNum()
  512. {
  513. CMDtext[CMDleng] = 0;
  514. CMDlval.f = MakeToken< double >( dAtof(CMDtext), yylineno );
  515. return(FLTCONST);
  516. }
  517. static int Sc_ScanHex()
  518. {
  519. S32 val = 0;
  520. dSscanf(CMDtext, "%x", &val);
  521. CMDlval.i = MakeToken< int >( val, yylineno );
  522. return INTCONST;
  523. }
  524. void CMD_reset()
  525. {
  526. CMDrestart(NULL);
  527. }