Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary variable 'state' in LexerImpl by using Stack #450

Merged
merged 2 commits into from
Jul 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -70,9 +72,7 @@ public final class LexerImpl implements Lexer {
* The state of the lexer is important so that we know what to expect next and to help discover
* errors in the template (ex. unclosed comments).
*/
private State state;

private LinkedList<State> states;
private Deque<State> states;

private enum State {
DATA, EXECUTE, PRINT, COMMENT, STRING, STRING_INTERPOLATION
Expand Down Expand Up @@ -153,24 +153,25 @@ public TokenStream tokenize(Reader reader, String name) {
throw new ParserException(e, "Can not convert template Reader into a String", 0, name);
}

/*
* Start in a DATA state. This state basically means that we are NOT in
* between a pair of meaningful delimiters.
*/
this.state = State.DATA;

this.tokens = new ArrayList<>();
this.states = new LinkedList<>();
this.states = new ArrayDeque<>();
this.brackets = new LinkedList<>();

/*
* Start in a DATA state by pushing it to the state stack. This state basically means that we are NOT in
* between a pair of meaningful delimiters.
*/
this.pushState(State.DATA);

/*
* loop through the entire source and apply different lexing methods
* depending on what kind of state we are in at the time.
*
* This will always start on lexData();
*/
while (this.source.length() > 0) {
switch (this.state) {
switch (this.states.peek()) {
case DATA:
this.lexData();
break;
Expand All @@ -196,6 +197,7 @@ public TokenStream tokenize(Reader reader, String name) {

// end of file token
this.pushToken(Token.Type.EOF);
this.popState();

// make sure that all brackets have been closed, else throw an error
if (!this.brackets.isEmpty()) {
Expand Down Expand Up @@ -620,20 +622,17 @@ private Token pushToken(Token.Type type, String value) {
}

/**
* Pushes the current state onto the stack and then updates the current state to the new state.
*
* @param state The new state to use as the current state
* Updates the current state to the new state by pushing the current state onto the stack.
*/
private void pushState(State state) {
this.states.push(this.state);
this.state = state;
this.states.push(state);
}

/**
* Pop state from the stack
*/
private void popState() {
this.state = this.states.pop();
this.states.pop();
}

/**
Expand Down