lexer.l 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* =============================================================================
  2. //
  3. // This file is part of the qlow compiler.
  4. //
  5. // Copyright (C) 2014-2015 Nicolas Winkler
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  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 received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. // ===========================================================================*/
  21. %option reentrant bison-bridge bison-locations
  22. %option prefix="qlow_parser_"
  23. %option yylineno nounput noinput
  24. %option stack
  25. %option 8bit
  26. %option header-file="lexer.h"
  27. %{
  28. #include "Parser.h"
  29. #include "syntax.hpp"
  30. #define yylval qlow_parser_lval
  31. #define YY_EXTRA_TYPE size_t
  32. #define SET_TOKEN(t) (yylval_param->token = t)
  33. #define SET_STRING (yylval_param->string = new std::string(yytext, yyleng))
  34. extern "C" int qlow_parser_wrap(yyscan_t s);
  35. // TODO rewrite
  36. #define YY_USER_ACTION \
  37. do { \
  38. yylloc_param->first_line = yylineno; \
  39. yylloc_param->first_column = yyg->yyextra_r; \
  40. yyg->yyextra_r += yyleng; \
  41. yylloc_param->last_line = yylineno; \
  42. yylloc_param->last_column = yyg->yyextra_r; \
  43. yylloc_param->filename = nullptr; \
  44. } while(0);
  45. #undef YY_USER_ACTION
  46. #define YY_USER_ACTION ;
  47. %}
  48. %x COMMENT
  49. %x LINE_COMMENT
  50. %x STRING
  51. %s METHOD
  52. UTF8CHAR [\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec][\x80-\xbf][\x80-\xbf]|\xed[\x80-\x9f][\x80-\xbf]|[\xee\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf3][\x80-\xbf][\x80-\xbf][\x80-\xbf]|\xf4[\x80-\x8f][\x80-\xbf][\x80-\xbf]
  53. /*UTF8CHAR [a-zA-Z0-9_]
  54. */
  55. %%
  56. int commentDepth = 0;
  57. <COMMENT>"/*" commentDepth++;
  58. <COMMENT>"*/" if ((--commentDepth) == 0) { BEGIN(INITIAL); };
  59. <COMMENT>\n yyg->yyextra_r = 0;
  60. <COMMENT>. ; // inside comment, ignore everything
  61. <LINE_COMMENT>\n yyg->yyextra_r = 0; yy_pop_state(yyscanner); //yy_push_state(INITIAL);
  62. <LINE_COMMENT>. ; // inside comment, ignore everything
  63. <STRING>"\"" yy_pop_state(yyscanner);
  64. <STRING>[^\"^\n]* printf("%s\n", std::string(yytext, yyleng).c_str());
  65. <STRING>\n yyg->yyextra_r = 0; SET_STRING; return UNEXPECTED_SYMBOL;
  66. "/*" yy_push_state(COMMENT, yyscanner); commentDepth = 1;
  67. "//" yy_push_state(LINE_COMMENT, yyscanner);
  68. "\"" yy_push_state(STRING, yyscanner);
  69. [\t ] ; // Space or tab ignored
  70. <METHOD>\n yyg->yyextra_r = 0; return SET_TOKEN(NEW_LINE);
  71. \n yyg->yyextra_r = 0; //return SET_TOKEN(NEW_LINE);
  72. "class" return SET_TOKEN(CLASS);
  73. "do" yy_push_state(METHOD, yyscanner); return SET_TOKEN(DO);
  74. <METHOD>"end" yy_pop_state(yyscanner); return SET_TOKEN(END);
  75. <INITIAL>"end" return SET_TOKEN(END);
  76. "if" return SET_TOKEN(IF);
  77. "while" return SET_TOKEN(WHILE);
  78. "else" return SET_TOKEN(ELSE);
  79. "return" return SET_TOKEN(RETURN);
  80. "new" return SET_TOKEN(NEW);
  81. "extern" return SET_TOKEN(EXTERN);
  82. ":" return SET_TOKEN(COLON);
  83. ";" return SET_TOKEN(SEMICOLON);
  84. "," return SET_TOKEN(COMMA);
  85. "." return SET_TOKEN(DOT);
  86. "&" return SET_TOKEN(AMPERSAND);
  87. ":=" SET_STRING; return ASSIGN;
  88. "==" SET_STRING; return EQUALS;
  89. "!=" SET_STRING; return NOT_EQUALS;
  90. "and" SET_STRING; return AND;
  91. "or" SET_STRING; return OR;
  92. "xor" SET_STRING; return XOR;
  93. "not" SET_STRING; return NOT;
  94. "as" return SET_TOKEN(AS);
  95. "+" SET_STRING; return PLUS;
  96. "-" SET_STRING; return MINUS;
  97. "*" SET_STRING; return ASTERISK;
  98. "/" SET_STRING; return SLASH;
  99. [\+\-\*\/=!<>]+ SET_STRING; return CUSTOM_OPERATOR;
  100. "(" return SET_TOKEN(ROUND_LEFT);
  101. ")" return SET_TOKEN(ROUND_RIGHT);
  102. "[" return SET_TOKEN(SQUARE_LEFT);
  103. "]" return SET_TOKEN(SQUARE_RIGHT);
  104. "false" return SET_TOKEN(FALSE);
  105. "true" return SET_TOKEN(TRUE);
  106. [0-9_]+ SET_STRING; return INT_LITERAL;
  107. 0x[0-9A-Fa-f]+ SET_STRING; return INT_LITERAL;
  108. [a-zA-Z_][a-zA-Z0-9_]* SET_STRING; return IDENTIFIER;
  109. . SET_STRING; return UNEXPECTED_SYMBOL; // printf("Unexpected symbol %s.\n", std::string(yytext, yyleng).c_str()); yyterminate();
  110. %%
  111. // [a-zA-Z_][a-zA-Z0-9_]* SET_STRING; return IDENTIFIER;
  112. /*
  113. QLOW_PARSER_LTYPE qlow_parser_lloc;
  114. const char* qlow_parser_filename;
  115. QLOW_PARSER_STYPE qlow_parser_lval;
  116. int main() {
  117. while (true) {
  118. yylex();
  119. auto& qpl = qlow_parser_lval;
  120. if (qpl.token > 0 && qpl.token < 1000) {
  121. printf("token %d\n", qpl.token);
  122. }
  123. else {
  124. printf("string %s\n", qpl.string->c_str());
  125. }
  126. }
  127. }
  128. */