alec

Abstraction Layer for Escape Codes
git clone git://git.dimitrijedobrota.com/alec.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |

lexer.l (2121B)


1 %{ 2 #include <stack> 3 #include "driver.hpp" 4 #include "parser.hpp" 5 6 using namespace alec; 7 8 #undef YY_DECL 9 #define YY_DECL int driver::yylex(parser::semantic_type* yylval, location_t *const lloc) 10 11 #define YY_USER_INIT m_yylloc = lloc; 12 #define YY_USER_ACTION copy_location(); 13 14 std::stack<int> mode_st; 15 16 #define BEGIN_MODE(mode) do { \ 17 if(yy_flex_debug) std::cerr<<"Starting mode: "<<mode<<std::endl; \ 18 mode_st.push(YY_START); \ 19 BEGIN((mode)); \ 20 } while(0); 21 22 #define END_MODE() do { \ 23 if(yy_flex_debug) std::cerr<<"Returning to mode: "<<mode_st.top()<<std::endl; \ 24 BEGIN(mode_st.top()); \ 25 mode_st.pop(); \ 26 } while(0); 27 %} 28 29 %option c++ noyywrap debug nodefault 30 %option yyclass = "driver" 31 %option prefix = "yy_alec_" 32 33 34 LINE_END (\n|\r|\r\n) 35 SWITCH_BEGIN "/\*%%\*//\*"{LINE_END} 36 SWITCH_END "\*//\*%%\*/"{LINE_END} 37 38 %x GEN LAST 39 40 %% 41 42 %{ 43 using Token = parser::token; 44 %} 45 46 47 {SWITCH_BEGIN} { BEGIN_MODE(GEN); return Token::SWITCH; } 48 .*{LINE_END} { 49 yylval->emplace<std::string>(yytext); 50 return Token::PROLOGUE; 51 } 52 53 <GEN>{LINE_END} { return Token::EOL; } 54 <GEN>^[\t ]*{LINE_END} { return Token::EOL; } 55 <GEN>^[\t ]*\|*[\t ]*{LINE_END} { return Token::EMPTY; } 56 57 <GEN>^[\t ]*"//".* { 58 yylval->emplace<std::string>(yytext); 59 return Token::COMMENT; 60 } 61 62 <GEN>, { return Token::COMMA; } 63 64 <GEN>[^,\n]* { 65 char *p = yytext + strlen(yytext) - 1; 66 while(isspace(*p)) *p-- = '\0'; 67 while(*yytext && isspace(*yytext)) yytext++; 68 yylval->emplace<std::string>(yytext); 69 return Token::LITERAL; 70 } 71 72 <GEN>{SWITCH_END} { 73 BEGIN_MODE(LAST); 74 return Token::SWITCH; 75 } 76 77 <LAST>.*{LINE_END} { 78 yylval->emplace<std::string>(yytext); 79 return Token::EPILOGUE; 80 } 81 82 %% 83