Skip to content

Commit

Permalink
Grammar: Emit DEDENT tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
sanssecours committed May 4, 2018
1 parent 71fb52e commit 40ee717
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Grammar/Test.g4
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ unique_ptr<CommonToken> commonToken(int type, string text, size_t start,
return move(token);
}

unique_ptr<CommonToken> dedent() {
unique_ptr<CommonToken> token{new CommonToken{TestParser::DEDENT}};
return move(token);
}

public:

void emit(unique_ptr<Token> token) override {
Expand All @@ -55,7 +60,7 @@ unique_ptr<Token> nextToken() override {
}

nodes : node+ EOF ;
node : INDENT? ID NEWLINE;
node : INDENT? ID NEWLINE DEDENT* ;

NEWLINE : ( '\r'? '\n' ) SPACES? {{
string newLine = regex_replace(this->getText(), regex("[^\r\n]"), "");
Expand All @@ -70,6 +75,13 @@ NEWLINE : ( '\r'? '\n' ) SPACES? {{
indents.push(indentation);
emit(commonToken(TestParser::INDENT, spaces, last - spaces.length() + 1,
last));
} else if (indentation < previous) {
while (!indents.empty() && indents.top() > indentation) {
indents.pop();
emit(dedent());
}
} else {
skip();
}
}};
ID : [a-zA-Z0-9]+ ;
Expand Down

0 comments on commit 40ee717

Please sign in to comment.