Skip to content
Snippets Groups Projects
Commit c10d247c authored by dgelessus's avatar dgelessus
Browse files

Simplify creation of lists returned by generated Parser new methods

This also avoids allocating a full ArrayList if just one node is pushed
(which is by far the most common case).
parent 9b64a778
No related branches found
No related tags found
No related merge requests found
...@@ -130,20 +130,32 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter ...@@ -130,20 +130,32 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
{ {
try try
{ {
PTerm firstTerm = null; if(node.getTerms().isEmpty())
for(PTerm term : node.getTerms())
{
if(firstTerm == null)
{ {
firstTerm = term; macros.apply(file, "ParserNewTailEmpty", null);
} }
macros.apply(file, "ParserNewBodyListAdd", new String[] {getVariableName(term)}); else
} {
if (firstTerm != null && popCount > 0) { PTerm firstTerm = node.getTerms().get(0);
if (popCount > 0) {
// The nodeArrayList variables are numbered starting at 1, so the first popped variable has the number popCount and not popCount-1. // The nodeArrayList variables are numbered starting at 1, so the first popped variable has the number popCount and not popCount-1.
macros.apply(file, "ParserNewCheck", new String[] {getVariableName(firstTerm), String.valueOf(popCount)}); macros.apply(file, "ParserNewCheck", new String[] {getVariableName(firstTerm), String.valueOf(popCount)});
} }
macros.apply(file, "ParserNewTail");
if(node.getTerms().size() == 1)
{
macros.apply(file, "ParserNewTailSingle", new String[] {getVariableName(firstTerm)});
}
else
{
macros.apply(file, "ParserNewTailMultiHead");
for(PTerm term : node.getTerms())
{
macros.apply(file, "ParserNewTailMultiElement", new String[] {getVariableName(term)});
}
macros.apply(file, "ParserNewTailMultiTail");
}
}
} }
catch(IOException e) catch(IOException e)
{ {
......
This diff is collapsed.
...@@ -15,6 +15,7 @@ import java.io.BufferedInputStream; ...@@ -15,6 +15,7 @@ import java.io.BufferedInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -314,8 +315,6 @@ Macro:ParserNewHeader ...@@ -314,8 +315,6 @@ Macro:ParserNewHeader
private List<Object> new$0$() /* reduce $1$ */ private List<Object> new$0$() /* reduce $1$ */
{ {
List<Object> nodeList = new ArrayList<>();
$ $
...@@ -390,18 +389,35 @@ Macro:ParserTypedLinkedListAddAll ...@@ -390,18 +389,35 @@ Macro:ParserTypedLinkedListAddAll
$ $
Macro:ParserNewBodyListAdd Macro:ParserNewCheck
nodeList.add($0$); checkResult($0$, nodeArrayList1, nodeArrayList$1$);
$ $
Macro:ParserNewCheck Macro:ParserNewTailEmpty
checkResult($0$, nodeArrayList1, nodeArrayList$1$); return Collections.emptyList();
}
$
Macro:ParserNewTailSingle
return Collections.<Object>singletonList($0$);
}
$
Macro:ParserNewTailMultiHead
return Arrays.asList(new Object[] {
$
Macro:ParserNewTailMultiElement
$0$,
$ $
Macro:ParserNewTail Macro:ParserNewTailMultiTail
return nodeList; });
} }
$ $
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment