lexer.l 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. %option extra-type="std::string"
  28. %{
  29. #include "Parser.h"
  30. #include "syntax.hpp"
  31. #define register
  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. #ifdef _WIN32
  36. #define YY_NO_UNISTD_H
  37. #endif
  38. #define YY_USER_ACTION \
  39. do { \
  40. yylloc_param->first_line = yylineno; \
  41. yylloc_param->first_column = yycolumn; \
  42. yycolumn += yyleng; \
  43. yylloc_param->last_line = yylineno; \
  44. yylloc_param->last_column = yycolumn; \
  45. yylloc_param->filename = yyextra; \
  46. } while(0);
  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) { yy_pop_state(yyscanner); };
  59. <COMMENT>\n yycolumn = 0;
  60. <COMMENT>. ; // inside comment, ignore everything
  61. <LINE_COMMENT>"\n" yycolumn = 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 yycolumn = 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 yycolumn = 0; return SET_TOKEN(NEW_LINE);
  71. \n yycolumn = 0; //return SET_TOKEN(NEW_LINE);
  72. "class" return SET_TOKEN(CLASS);
  73. "struct" return SET_TOKEN(STRUCT);
  74. "do" yy_push_state(METHOD, yyscanner); return SET_TOKEN(DO);
  75. <METHOD>"end" yy_pop_state(yyscanner); return SET_TOKEN(END);
  76. <INITIAL>"end" return SET_TOKEN(END);
  77. "if" return SET_TOKEN(IF);
  78. "while" return SET_TOKEN(WHILE);
  79. "else" return SET_TOKEN(ELSE);
  80. "return" return SET_TOKEN(RETURN);
  81. "new" return SET_TOKEN(NEW);
  82. "extern" return SET_TOKEN(EXTERN);
  83. ":" return SET_TOKEN(COLON);
  84. ";" return SET_TOKEN(SEMICOLON);
  85. "," return SET_TOKEN(COMMA);
  86. "." return SET_TOKEN(DOT);
  87. "&" return SET_TOKEN(AMPERSAND);
  88. ":=" SET_STRING; return ASSIGN;
  89. "==" SET_STRING; return EQUALS;
  90. "!=" SET_STRING; return NOT_EQUALS;
  91. "and" SET_STRING; return AND;
  92. "or" SET_STRING; return OR;
  93. "xor" SET_STRING; return XOR;
  94. "not" SET_STRING; return NOT;
  95. "as" return SET_TOKEN(AS);
  96. "+" SET_STRING; return PLUS;
  97. "-" SET_STRING; return MINUS;
  98. "*" SET_STRING; return ASTERISK;
  99. "/" SET_STRING; return SLASH;
  100. [\+\-\*\/=!<>]+ SET_STRING; return CUSTOM_OPERATOR;
  101. "(" return SET_TOKEN(ROUND_LEFT);
  102. ")" return SET_TOKEN(ROUND_RIGHT);
  103. "[" return SET_TOKEN(SQUARE_LEFT);
  104. "]" return SET_TOKEN(SQUARE_RIGHT);
  105. "false" return SET_TOKEN(FALSE);
  106. "true" return SET_TOKEN(TRUE);
  107. [0-9_]+ SET_STRING; return INT_LITERAL;
  108. 0x[0-9A-Fa-f]+ SET_STRING; return INT_LITERAL;
  109. [a-zA-Z_][a-zA-Z0-9_]* SET_STRING; return IDENTIFIER;
  110. . SET_STRING; return UNEXPECTED_SYMBOL; // printf("Unexpected symbol %s.\n", std::string(yytext, yyleng).c_str()); yyterminate();
  111. %%
  112. // [a-zA-Z_][a-zA-Z0-9_]* SET_STRING; return IDENTIFIER;
  113. /*
  114. QLOW_PARSER_LTYPE qlow_parser_lloc;
  115. const char* qlow_parser_filename;
  116. QLOW_PARSER_STYPE qlow_parser_lval;
  117. int main() {
  118. while (true) {
  119. yylex();
  120. auto& qpl = qlow_parser_lval;
  121. if (qpl.token > 0 && qpl.token < 1000) {
  122. printf("token %d\n", qpl.token);
  123. }
  124. else {
  125. printf("string %s\n", qpl.string->c_str());
  126. }
  127. }
  128. }
  129. */