diff --git a/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java b/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java index bef0247a9605e2e8c674debaf5e95b6ecf0d204d..15d28ff28f2f99ee186b119e74eda4a48b41908d 100644 --- a/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java +++ b/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java @@ -311,8 +311,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter } else { - macros.apply(file, "ParserTypedLinkedListInit", new String[] {getVariableName(node)}); - + boolean first = true; for(PListTerm listTerm : node.getListTerms()) { String typeName = lookupInternalTypeName(listTerm); @@ -324,12 +323,24 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter if(typeName.startsWith("L")) { - macros.apply(file, "ParserTypedLinkedListAddAll", new String[] {getVariableName(node), getVariableName(listTerm)}); + if(first) + { + macros.apply(file, "ParserTypedLinkedListInitAddAll", new String[] {getVariableName(node), getVariableName(listTerm)}); + } + else + { + macros.apply(file, "ParserTypedLinkedListAddAll", new String[] {getVariableName(node), getVariableName(listTerm)}); + } } else { + if(first) + { + macros.apply(file, "ParserTypedLinkedListInit", new String[] {getVariableName(node)}); + } macros.apply(file, "ParserTypedLinkedListAdd", new String[] {getVariableName(node), getVariableName(listTerm)}); } + first = false; } } diff --git a/src/main/java/org/sablecc/sablecc/parser/Parser.java b/src/main/java/org/sablecc/sablecc/parser/Parser.java index 5e7c14887c3ecc065ba217e53d7f5a99c0e06f49..b44a61ba034208cdc4c6030e5a91aead98a1092c 100644 --- a/src/main/java/org/sablecc/sablecc/parser/Parser.java +++ b/src/main/java/org/sablecc/sablecc/parser/Parser.java @@ -8031,14 +8031,12 @@ public class Parser implements IParser TPkgId tpkgidNode2; listNode1 = (List<TPkgId>)nodeArrayList1.get(0); tpkgidNode2 = (TPkgId)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(tpkgidNode2 != null) { @@ -8086,14 +8084,12 @@ public class Parser implements IParser PHelperDef phelperdefNode2; listNode1 = (List<PHelperDef>)nodeArrayList1.get(0); phelperdefNode2 = (PHelperDef)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(phelperdefNode2 != null) { @@ -8141,14 +8137,12 @@ public class Parser implements IParser TId tidNode2; listNode1 = (List<TId>)nodeArrayList1.get(0); tidNode2 = (TId)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(tidNode2 != null) { @@ -8196,14 +8190,12 @@ public class Parser implements IParser PTokenDef ptokendefNode2; listNode1 = (List<PTokenDef>)nodeArrayList1.get(0); ptokendefNode2 = (PTokenDef)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(ptokendefNode2 != null) { @@ -8251,14 +8243,12 @@ public class Parser implements IParser PStateListTail pstatelisttailNode2; listNode1 = (List<PStateListTail>)nodeArrayList1.get(0); pstatelisttailNode2 = (PStateListTail)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(pstatelisttailNode2 != null) { @@ -8306,14 +8296,12 @@ public class Parser implements IParser PConcat pconcatNode2; listNode1 = (List<PConcat>)nodeArrayList1.get(0); pconcatNode2 = (PConcat)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(pconcatNode2 != null) { @@ -8361,14 +8349,12 @@ public class Parser implements IParser PUnExp punexpNode2; listNode1 = (List<PUnExp>)nodeArrayList1.get(0); punexpNode2 = (PUnExp)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(punexpNode2 != null) { @@ -8416,14 +8402,12 @@ public class Parser implements IParser PProd pprodNode2; listNode1 = (List<PProd>)nodeArrayList1.get(0); pprodNode2 = (PProd)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(pprodNode2 != null) { @@ -8471,14 +8455,12 @@ public class Parser implements IParser PElem pelemNode2; listNode1 = (List<PElem>)nodeArrayList1.get(0); pelemNode2 = (PElem)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(pelemNode2 != null) { @@ -8526,14 +8508,12 @@ public class Parser implements IParser PAlt paltNode2; listNode1 = (List<PAlt>)nodeArrayList1.get(0); paltNode2 = (PAlt)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(paltNode2 != null) { @@ -8581,14 +8561,12 @@ public class Parser implements IParser PTerm ptermNode2; listNode1 = (List<PTerm>)nodeArrayList1.get(0); ptermNode2 = (PTerm)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(ptermNode2 != null) { @@ -8636,14 +8614,12 @@ public class Parser implements IParser PListTerm plisttermNode2; listNode1 = (List<PListTerm>)nodeArrayList1.get(0); plisttermNode2 = (PListTerm)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(plisttermNode2 != null) { @@ -8691,14 +8667,12 @@ public class Parser implements IParser PTerm ptermNode2; listNode1 = (List<PTerm>)nodeArrayList1.get(0); ptermNode2 = (PTerm)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(ptermNode2 != null) { @@ -8746,14 +8720,12 @@ public class Parser implements IParser PAstProd pastprodNode2; listNode1 = (List<PAstProd>)nodeArrayList1.get(0); pastprodNode2 = (PAstProd)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(pastprodNode2 != null) { @@ -8801,14 +8773,12 @@ public class Parser implements IParser PAstAlt pastaltNode2; listNode1 = (List<PAstAlt>)nodeArrayList1.get(0); pastaltNode2 = (PAstAlt)nodeArrayList2.get(0); - listNode3 = new LinkedList<>(); - if(listNode1 != null) - { - if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) { - listNode3 = listNode1; - } else { - listNode3.addAll(listNode1); - } + if (listNode1 instanceof LinkedList<?>) { + listNode3 = listNode1; + } else if (listNode1 == null) { + listNode3 = new LinkedList<>(); + } else { + listNode3 = new LinkedList<>(listNode1); } if(pastaltNode2 != null) { diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt index 951becb0a777c8ab3c622ddc51a72ada9ceff33b..3d1db583d18ae18a1798d6fdd519869911b80ede 100644 --- a/src/main/resources/org/sablecc/sablecc/parser.txt +++ b/src/main/resources/org/sablecc/sablecc/parser.txt @@ -373,6 +373,17 @@ Macro:ParserTypedLinkedListInit $ +Macro:ParserTypedLinkedListInitAddAll + if ($1$ instanceof LinkedList<?>) { + $0$ = $1$; + } else if ($1$ == null) { + $0$ = new LinkedList<>(); + } else { + $0$ = new LinkedList<>($1$); + } + +$ + Macro:ParserTypedLinkedListAdd if($1$ != null) {