PAN-64 Refactored ParserService.
This commit is contained in:
@@ -39,10 +39,8 @@ class ParserService {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void recurse(final Node current)
|
public void recurse(final Node current)
|
||||||
{
|
{
|
||||||
int startIndex;
|
|
||||||
int endIndex;
|
|
||||||
final String statement;
|
final String statement;
|
||||||
|
|
||||||
if (current != null) {
|
if (current != null) {
|
||||||
@@ -51,60 +49,80 @@ class ParserService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String nextStatement;
|
|
||||||
|
|
||||||
if(statement.contains("let"))
|
if(statement.contains("let"))
|
||||||
{
|
{
|
||||||
current.setLeft(new Node("let", current));
|
separateByLet(current, statement);
|
||||||
|
|
||||||
startIndex = statement.indexOf("let")+"let".length();
|
|
||||||
|
|
||||||
if(statement.contains("if")){
|
|
||||||
endIndex = statement.indexOf("if");
|
|
||||||
} else if(statement.contains("then")){
|
|
||||||
endIndex = statement.indexOf("then");
|
|
||||||
} else {
|
|
||||||
endIndex = statement.length();
|
|
||||||
}
|
|
||||||
|
|
||||||
nextStatement = statement.substring(startIndex, endIndex);
|
|
||||||
|
|
||||||
current.getLeft().setCenter(new Node(nextStatement, current.getLeft()));
|
|
||||||
recurse(current.getLeft().getCenter());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(statement.contains("if"))
|
if(statement.contains("if"))
|
||||||
{
|
{
|
||||||
current.setCenter(new Node("if", current));
|
separateByIf(current, statement);
|
||||||
|
|
||||||
startIndex = statement.indexOf("if")+"if".length();
|
|
||||||
endIndex = (statement.contains("then") ? statement.indexOf("then") : statement.length());
|
|
||||||
nextStatement = statement.substring(startIndex, endIndex);
|
|
||||||
|
|
||||||
current.getCenter().setCenter(new Node(nextStatement, current.getCenter()));
|
|
||||||
recurse(current.getCenter().getCenter());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(current.getStatement().contains("then"))
|
if(current.getStatement().contains("then"))
|
||||||
{
|
{
|
||||||
current.setRight(new Node("then", current));
|
separateByThen(current, statement);
|
||||||
|
|
||||||
startIndex = statement.indexOf("then")+"then".length();
|
|
||||||
nextStatement = statement.substring(startIndex);
|
|
||||||
|
|
||||||
current.getRight().setCenter(new Node(nextStatement, current.getRight()));
|
|
||||||
recurse(current.getRight().getCenter());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void separateByLet(final Node current, final String statement){
|
||||||
|
final int startIndex;
|
||||||
|
final int endIndex;
|
||||||
|
final String nextStatement;
|
||||||
|
|
||||||
|
current.setLeft(new Node("let", current));
|
||||||
|
|
||||||
|
startIndex = statement.indexOf("let")+"let".length();
|
||||||
|
|
||||||
|
if(statement.contains("if")){
|
||||||
|
endIndex = statement.indexOf("if");
|
||||||
|
} else if(statement.contains("then")){
|
||||||
|
endIndex = statement.indexOf("then");
|
||||||
|
} else {
|
||||||
|
endIndex = statement.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
nextStatement = statement.substring(startIndex, endIndex);
|
||||||
|
|
||||||
|
current.getLeft().setCenter(new Node(nextStatement, current.getLeft()));
|
||||||
|
recurse(current.getLeft().getCenter());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void separateByIf(final Node current, final String statement){
|
||||||
|
final int startIndex;
|
||||||
|
final int endIndex;
|
||||||
|
final String nextStatement;
|
||||||
|
|
||||||
|
current.setCenter(new Node("if", current));
|
||||||
|
|
||||||
|
startIndex = statement.indexOf("if")+"if".length();
|
||||||
|
endIndex = (statement.contains("then") ? statement.indexOf("then") : statement.length());
|
||||||
|
nextStatement = statement.substring(startIndex, endIndex);
|
||||||
|
|
||||||
|
current.getCenter().setCenter(new Node(nextStatement, current.getCenter()));
|
||||||
|
recurse(current.getCenter().getCenter());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void separateByThen(final Node current, final String statement){
|
||||||
|
final int startIndex;
|
||||||
|
final String nextStatement;
|
||||||
|
|
||||||
|
current.setRight(new Node("then", current));
|
||||||
|
|
||||||
|
startIndex = statement.indexOf("then")+"then".length();
|
||||||
|
nextStatement = statement.substring(startIndex);
|
||||||
|
|
||||||
|
current.getRight().setCenter(new Node(nextStatement, current.getRight()));
|
||||||
|
recurse(current.getRight().getCenter());
|
||||||
|
}
|
||||||
|
|
||||||
public List<String> retrieveStatements(final Node parsedTree)
|
public List<String> retrieveStatements(final Node parsedTree)
|
||||||
{
|
{
|
||||||
return populateStatementList(parsedTree, new ArrayList<>());
|
return populateStatementList(parsedTree, new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<String> populateStatementList(final Node node, final ArrayList<String> statementList)
|
public ArrayList<String> populateStatementList(final Node node, final ArrayList<String> statementList)
|
||||||
{
|
{
|
||||||
if(node == null)
|
if(node == null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user