diff --git a/src/lexer.l b/src/lexer.l index bdc00f0..3e6e9c7 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -1,7 +1,7 @@ %option c++ %option noyywrap %option yyclass="Scanner" -%x STRING_DQ STRING_SQ STRING_TDQ STRING_TSQ +%x STRING_DQ STRING_SQ STRING_TDQ STRING_TSQ NEWLINE %{ #include "scanner.hpp" @@ -10,11 +10,15 @@ %} %% + +^[ \t]*\r?\n { /* Ignore lines containing whitespace only */ } +^[ \t]*#.*\n { /* Ignore comment-only lines */ } ^[ \t]*\r?\n { /* Ignore lines containing whitespace only */ } ^[ \t]*#.*\n { /* Ignore comment-only lines */ } -^[ \t]+ { +[ \t]+ { + BEGIN(INITIAL); int col = indent_stack.calculateColumn(yytext, yyleng); auto action = indent_stack.updateIndentation(col); @@ -22,14 +26,16 @@ if (action == IndentAction::Dedent) return yy::parser::token::TOK_DEDENT; } -^. { +. { + BEGIN(INITIAL); // Line starts with non-whitespace (column 0) auto action = indent_stack.updateIndentation(0); yyless(0); + if (action == IndentAction::Indent) return yy::parser::token::TOK_INDENT; if (action == IndentAction::Dedent) return yy::parser::token::TOK_DEDENT; } -\n { return yy::parser::token::TOK_NEWLINE; } +\n { BEGIN(NEWLINE);return yy::parser::token::TOK_NEWLINE; } def { return yy::parser::token::TOK_KW_DEF; } if { return yy::parser::token::TOK_KW_IF; }