lexer.l 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. %{
  22. #include "Ast.h"
  23. #include "parser.hpp"
  24. #define yylval qlow_parser_lval
  25. #define SET_TOKEN(t) (yylval.token = t)
  26. #define SET_STRING (yylval.string = new std::string(yytext, yyleng))
  27. extern "C" int yywrap()
  28. {
  29. return 1; /* do not continue on EOF */
  30. }
  31. /* */
  32. int commentDepth;
  33. size_t offset;
  34. extern QLOW_PARSER_LTYPE qlow_parser_lloc;
  35. extern const char* qlow_parser_filename;
  36. #define YY_USER_ACTION \
  37. do { \
  38. qlow_parser_lloc.first_line = yylineno; \
  39. qlow_parser_lloc.first_column = offset; \
  40. offset += yyleng; \
  41. qlow_parser_lloc.last_line = yylineno; \
  42. qlow_parser_lloc.last_column = offset; \
  43. qlow_parser_lloc.filename = qlow_parser_filename; \
  44. } while(0);
  45. %}
  46. %option prefix="qlow_parser_"
  47. %option yylineno
  48. %option stack
  49. %x COMMENT
  50. %x LINE_COMMENT
  51. %x STRING
  52. %s METHOD
  53. 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]
  54. /*UTF8CHAR [a-zA-Z0-9_]
  55. */
  56. %%
  57. <COMMENT>"/*" commentDepth++;
  58. <COMMENT>"*/" if ((--commentDepth) == 0) { BEGIN(INITIAL); };
  59. <COMMENT>\n ;
  60. <COMMENT>. ; // inside comment, ignore everything
  61. <LINE_COMMENT>\n yy_pop_state(); //yy_push_state(INITIAL);
  62. <LINE_COMMENT>. ; // inside comment, ignore everything
  63. <STRING>"\"" yy_pop_state();
  64. <STRING>[^\"^\n]* printf("%s\n", std::string(yytext, yyleng).c_str());
  65. <STRING>\n SET_STRING; return UNEXPECTED_SYMBOL;
  66. "/*" yy_push_state(COMMENT); commentDepth = 1;
  67. "//" yy_push_state(LINE_COMMENT);
  68. "\"" yy_push_state(STRING);
  69. [\t ] ; // Space or tab ignored
  70. <METHOD>\n offset = 0; return SET_TOKEN(NEW_LINE);
  71. \n offset = 0; //return SET_TOKEN(NEW_LINE);
  72. "class" return SET_TOKEN(CLASS);
  73. "do" yy_push_state(METHOD); return SET_TOKEN(DO);
  74. <METHOD>"end" yy_pop_state(); 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(ASSIGN);
  86. "." return SET_TOKEN(DOT);
  87. "==" return SET_TOKEN(EQUALS);
  88. "!=" return SET_TOKEN(NOT_EQUALS);
  89. "and" return SET_TOKEN(AND);
  90. "or" return SET_TOKEN(OR);
  91. "xor" return SET_TOKEN(XOR);
  92. "not" return SET_TOKEN(NOT);
  93. "+" return SET_TOKEN(PLUS);
  94. "-" return SET_TOKEN(MINUS);
  95. "*" return SET_TOKEN(ASTERISK);
  96. "/" return SET_TOKEN(SLASH);
  97. "(" return SET_TOKEN(ROUND_LEFT);
  98. ")" return SET_TOKEN(ROUND_RIGHT);
  99. "[" return SET_TOKEN(SQUARE_LEFT);
  100. "]" return SET_TOKEN(SQUARE_RIGHT);
  101. "false" return SET_TOKEN(FALSE);
  102. "true" return SET_TOKEN(TRUE);
  103. [0-9_]+ SET_STRING; return INT_LITERAL;
  104. 0x[0-9A-Fa-f]+ SET_STRING; return INT_LITERAL;
  105. [a-zA-Z_][a-zA-Z0-9_]* SET_STRING; return IDENTIFIER;
  106. . SET_STRING; return UNEXPECTED_SYMBOL; // printf("Unexpected symbol %s.\n", std::string(yytext, yyleng).c_str()); yyterminate();
  107. %%
  108. // [a-zA-Z_][a-zA-Z0-9_]* SET_STRING; return IDENTIFIER;
  109. /*
  110. QLOW_PARSER_LTYPE qlow_parser_lloc;
  111. const char* qlow_parser_filename;
  112. QLOW_PARSER_STYPE qlow_parser_lval;
  113. int main() {
  114. while (true) {
  115. yylex();
  116. auto& qpl = qlow_parser_lval;
  117. if (qpl.token > 0 && qpl.token < 1000) {
  118. printf("token %d\n", qpl.token);
  119. }
  120. else {
  121. printf("string %s\n", qpl.string->c_str());
  122. }
  123. }
  124. }
  125. */