indent fix
This commit is contained in:
parent
5062a9b588
commit
4def48d206
3 changed files with 37 additions and 50 deletions
|
|
@ -13,17 +13,17 @@ public:
|
|||
{
|
||||
int current = indent_stack.top();
|
||||
|
||||
if (spaces == current)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (spaces > current)
|
||||
{
|
||||
indent_stack.push(spaces);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (spaces == current)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
pending_dedents = 0;
|
||||
while (indent_stack.top() > spaces)
|
||||
{
|
||||
|
|
@ -31,35 +31,23 @@ public:
|
|||
pending_dedents++;
|
||||
}
|
||||
|
||||
if (indent_stack.top() != spaces)
|
||||
{
|
||||
throw std::runtime_error("Indentation error");
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool hasPendingDedents() const
|
||||
{
|
||||
return pending_dedents > 0;
|
||||
}
|
||||
|
||||
void consumePendingDedent()
|
||||
{
|
||||
if (pending_dedents > 0)
|
||||
{
|
||||
pending_dedents--;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int flushAllDedents()
|
||||
{
|
||||
int total_dedents = 0;
|
||||
while (indent_stack.size() > 1)
|
||||
{
|
||||
indent_stack.pop();
|
||||
total_dedents++;
|
||||
return 0;
|
||||
}
|
||||
return total_dedents;
|
||||
|
||||
// returns false on failure
|
||||
bool consumeDedent()
|
||||
{
|
||||
if (pending_dedents <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
pending_dedents--;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
26
src/lexer.l
26
src/lexer.l
|
|
@ -7,18 +7,13 @@
|
|||
#include "scanner.hpp"
|
||||
#include "indent_helper.hpp"
|
||||
|
||||
IndentHelper indent_helper;
|
||||
bool at_line_start = true;
|
||||
%}
|
||||
|
||||
%%
|
||||
^[ \t]*\r?\n { /* Ignore lines containing whitespace only */ }
|
||||
^[ \t]*#.*\n { /* Ignore comment-only lines */ }
|
||||
|
||||
^[ \t]* {
|
||||
if (!at_line_start) {
|
||||
REJECT;
|
||||
}
|
||||
at_line_start = false;
|
||||
|
||||
int spaces = 0;
|
||||
for (int i = 0; i < yyleng; i++)
|
||||
spaces += (yytext[i] == '\t') ? 8 : 1;
|
||||
|
|
@ -28,10 +23,7 @@ bool at_line_start = true;
|
|||
if (result < 0) return yy::parser::token::TOK_DEDENT;
|
||||
}
|
||||
|
||||
\n {
|
||||
at_line_start = true;
|
||||
return yy::parser::token::TOK_NEWLINE;
|
||||
}
|
||||
\n { return yy::parser::token::TOK_NEWLINE; }
|
||||
|
||||
def { return yy::parser::token::TOK_KW_DEF; }
|
||||
if { return yy::parser::token::TOK_KW_IF; }
|
||||
|
|
@ -54,15 +46,5 @@ in { return yy::parser::token::TOK_KW_IN; }
|
|||
[0-9]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_NUMBER; }
|
||||
[A-Za-z_][A-Za-z0-9_]* { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_IDENTIFIER; }
|
||||
|
||||
|
||||
<<EOF>> {
|
||||
if (indent_helper.hasPendingDedents()) {
|
||||
indent_helper.consumeDedent();
|
||||
return yy::parser::token::TOK_DEDENT;
|
||||
}
|
||||
int d;
|
||||
if ((d = indent_helper.flushDedent()) != 0) return yy::parser::token::TOK_DEDENT;
|
||||
return yy::parser::token::TOK_EOF;
|
||||
}
|
||||
|
||||
<<EOF>> { return yy::parser::token::TOK_EOF; }
|
||||
. { /* ignore other characters */ }
|
||||
|
|
|
|||
|
|
@ -5,21 +5,38 @@
|
|||
#endif
|
||||
|
||||
#include "parser.hpp"
|
||||
#include "indent_helper.hpp"
|
||||
#include <string>
|
||||
|
||||
class Scanner : public yyFlexLexer
|
||||
{
|
||||
public:
|
||||
IndentHelper indent_helper;
|
||||
Scanner(std::istream &in) : yyFlexLexer(&in), yylval(nullptr) {}
|
||||
|
||||
int lex(yy::parser::semantic_type *lval)
|
||||
{
|
||||
if (indent_helper.consumeDedent())
|
||||
{
|
||||
return yy::parser::token::TOK_DEDENT;
|
||||
}
|
||||
this->yylval = lval;
|
||||
return yylex();
|
||||
int token = yylex();
|
||||
|
||||
if (token == yy::parser::token::TOK_EOF)
|
||||
{
|
||||
int result = indent_helper.processLine(0);
|
||||
|
||||
if (result == -1)
|
||||
{
|
||||
return yy::parser::token::TOK_DEDENT;
|
||||
}
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
private:
|
||||
// implemented by flex
|
||||
virtual int yylex() override;
|
||||
yy::parser::semantic_type *yylval;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue