25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scenesetup-scanner.l 4.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. %{
  2. //
  3. // Lol Engine
  4. //
  5. // Copyright: (c) 2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. // (c) 2013 Sam Hocevar <sam@hocevar.net>
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the Do What The Fuck You Want To
  9. // Public License, Version 2, as published by Sam Hocevar. See
  10. // http://www.wtfpl.net/ for more details.
  11. //
  12. #if defined HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <cstdlib>
  16. using std::exit;
  17. using std::malloc;
  18. using std::realloc;
  19. using std::free;
  20. #include "core.h"
  21. #include "../scenesetup.h"
  22. #include "../scenesetup-compiler.h"
  23. typedef lol::SceneSetupParser::token token;
  24. typedef lol::SceneSetupParser::token_type token_type;
  25. #ifndef YY_DECL
  26. # define YY_DECL lol::SceneSetupParser::token_type \
  27. lol::SceneSetupScanner::lex(lol::SceneSetupParser::semantic_type* yylval, \
  28. lol::SceneSetupParser::location_type* yylloc)
  29. #endif
  30. #define yyterminate() return token::T_END
  31. #define YY_NO_UNISTD_H
  32. #define YY_USER_ACTION yylloc->columns(yyleng);
  33. %}
  34. %option c++ prefix="SceneSetup"
  35. %option batch yywrap nounput stack
  36. %%
  37. %{ /* reset location at the beginning of yylex() */
  38. yylloc->step();
  39. %}
  40. addlight { return token::T_ADDLIGHT; }
  41. position { return token::T_OBJPOSITION; }
  42. lookat { return token::T_OBJLOOKAT; }
  43. color { return token::T_OBJCOLOR; }
  44. custom { return token::T_CUSTOMCMD; }
  45. %{ /* ======= BASE COLOR TYPES ========================================= */ %}
  46. %{ /* COLOR */ %}
  47. #[0-9a-fA-F]{3} {
  48. uint32_t tmp = std::strtol(yytext + 1, nullptr, 16);
  49. yylval->u32val = 0x11000000u * (tmp >> 8)
  50. | 0x00110000u * ((tmp >> 4) & 0xf)
  51. | 0x00001100u * (tmp & 0xf)
  52. | 0x000000ffu;
  53. return token::COLOR; }
  54. #[0-9a-fA-F]{4} {
  55. uint32_t tmp = std::strtol(yytext + 1, nullptr, 16);
  56. yylval->u32val = 0x11000000u * (tmp >> 12)
  57. | 0x00110000u * ((tmp >> 8) & 0xf)
  58. | 0x00001100u * ((tmp >> 4) & 0xf)
  59. | 0x00000011u * (tmp & 0xf);
  60. return token::COLOR; }
  61. #[0-9a-fA-F]{6} {
  62. yylval->u32val = 0xffu
  63. | 0x100u * (uint32_t)std::strtol(yytext + 1, nullptr, 16);
  64. return token::COLOR; }
  65. #[0-9a-fA-F]{8} {
  66. yylval->u32val = (uint32_t)std::strtol(yytext + 1, nullptr, 16);
  67. return token::COLOR; }
  68. %{ /* ======= BASE DATA TYPES ========================================= */ %}
  69. %{ /* BOOL */ %}
  70. true { yylval->bval = true; return token::BOOLEAN; }
  71. false { yylval->bval = false; return token::BOOLEAN; }
  72. %{ /* FLOAT */ %}
  73. [-+]?[0-9]*\.[0-9]+([eE][-+]?[0-9]+)? {
  74. yylval->fval = (float)std::atof(yytext); return token::F_NUMBER; }
  75. %{ /* INT */ %}
  76. [-+]?[0-9]+ {
  77. yylval->ival = std::atoi(yytext); return token::I_NUMBER; }
  78. %{ /* STRING */ %}
  79. [\"][^\"]*[\"] {
  80. yylval->sval = strdup(yytext); return token::STRING; }
  81. %{ /* STRING VAR */ %}
  82. [a-zA-Z][a-zA-Z_\-]+ {
  83. yylval->svval = strdup(yytext); return token::STRING_VAR; }
  84. %{ /* ======= COMMENTS ======= */ %}
  85. "//"[^\n\r]*[\n\r] { /* ignore this */ }
  86. %{ /* Semantics tokens */ %}
  87. "-" { return token_type('-'); }
  88. "(" { return token_type('('); }
  89. ")" { return token_type(')'); }
  90. [ ,] { /* ignore this */ }
  91. [\n] { /* ignore this */ }
  92. . { return token::T_ERROR; }
  93. %%
  94. lol::SceneSetupScanner::SceneSetupScanner(char const *command)
  95. : SceneSetupFlexLexer(0, 0),
  96. m_input(command)
  97. {
  98. }
  99. lol::SceneSetupScanner::~SceneSetupScanner()
  100. {
  101. }
  102. int lol::SceneSetupScanner::LexerInput(char* buf, int max_size)
  103. {
  104. (void)max_size; /* unused for now */
  105. buf[0] = m_input[0];
  106. if (buf[0])
  107. ++m_input;
  108. return buf[0] ? 1 : 0;
  109. }
  110. #ifdef yylex
  111. #undef yylex
  112. #endif
  113. int SceneSetupFlexLexer::yylex()
  114. {
  115. std::cerr << "in SceneSetupFlexLexer::yylex() !" << std::endl;
  116. return 0;
  117. }
  118. int SceneSetupFlexLexer::yywrap()
  119. {
  120. return 1;
  121. }