From 25dc7c4595d913fafbb3e457739bfe1163d005d6 Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Thu, 20 Jul 2023 22:38:05 +0200 Subject: [PATCH] Continue refactoring type handling in alternative code generation --- .../GenerateAlternativeCodeForParser.java | 25 +++++++++-------- .../resources/org/sablecc/sablecc/parser.txt | 28 ++++++++----------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java b/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java index af30d93..d350a2c 100644 --- a/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java +++ b/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java @@ -76,7 +76,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter } } - private static String variablePrefixFromInternalType(String typeName) + private static String getVariableName(String typeName, int position) { if("null".equals(typeName)) { @@ -84,11 +84,11 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter } else if(typeName.startsWith("L")) { - return "list"; + return "listNode" + position; } else { - return typeName.toLowerCase(); + return typeName.toLowerCase() + "Node" + position; } } @@ -114,7 +114,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter if(typeName.startsWith("L")) { - macros.apply(file, "ParserListVariableDeclaration", new String[] {"" + position}); + macros.apply(file, "ParserListVariableDeclaration", new String[] {getVariableName(typeName, position)}); } else if(typeName.equals("null")) { @@ -122,7 +122,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter } else { - macros.apply(file, "ParserSimpleVariableDeclaration", new String[] {typeName, variablePrefixFromInternalType(typeName), "" + position}); + macros.apply(file, "ParserSimpleVariableDeclaration", new String[] {typeName, getVariableName(typeName, position)}); } } @@ -162,7 +162,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter } else { - macros.apply(file, "ParserNewBodyListAdd", new String[] {variablePrefixFromInternalType(type_name), "" + position}); + macros.apply(file, "ParserNewBodyListAdd", new String[] {getVariableName(type_name, position)}); } } @@ -237,7 +237,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter macros.apply(file, "ParserSimpleTerm", new String[] { - variablePrefixFromInternalType(type_name), ""+position, + getVariableName(type_name, position), variableTypeFromInternalType(type_name), ""+elemPosition, ""+positionMap } ); @@ -296,6 +296,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter { try { + String listTypeName = lookupInternalTypeName(node); int listPosition = CG.getTermNumbers().get(node); for(PListTerm listTerm : node.getListTerms()) @@ -307,11 +308,11 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter { if(type_name.startsWith("L")) { - macros.apply(file, "ParserTypedLinkedListAddAll", new String[] {"list", ""+listPosition, "list", ""+ position}); + macros.apply(file, "ParserTypedLinkedListAddAll", new String[] {getVariableName(listTypeName, listPosition), getVariableName(type_name, position)}); } else { - macros.apply(file, "ParserTypedLinkedListAdd", new String[] {"list", ""+listPosition, variablePrefixFromInternalType(type_name), ""+ position}); + macros.apply(file, "ParserTypedLinkedListAdd", new String[] {getVariableName(listTypeName, listPosition), getVariableName(type_name, position)}); } } } @@ -332,7 +333,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter try { - macros.apply(file, "ParserNewBodyNew", new String[] {variablePrefixFromInternalType(type_name), ""+position, newAltName}); + macros.apply(file, "ParserNewBodyNew", new String[] {getVariableName(type_name, position), newAltName}); if(!params.isEmpty()) { @@ -345,11 +346,11 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter if(type_name.equals("null")) { - macros.apply(file, "ParserNew&ListBodyParamsNull", new String[] {isNotTheFirstParam+"null"}); + macros.apply(file, "ParserNewBodyParam", new String[] {isNotTheFirstParam+"null"}); } else { - macros.apply(file, "ParserNew&ListBodyParams", new String[] {isNotTheFirstParam + variablePrefixFromInternalType(type_name), ""+position}); + macros.apply(file, "ParserNewBodyParam", new String[] {isNotTheFirstParam + getVariableName(type_name, position)}); } isNotTheFirstParam = ", "; diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt index 2375146..82811fe 100644 --- a/src/main/resources/org/sablecc/sablecc/parser.txt +++ b/src/main/resources/org/sablecc/sablecc/parser.txt @@ -333,30 +333,26 @@ Macro:ParserBraceClosing $ Macro:ParserSimpleVariableDeclaration - $0$ $1$Node$2$; + $0$ $1$; $ Macro:ParserListVariableDeclaration - LinkedList listNode$0$ = new LinkedList(); + LinkedList $0$ = new LinkedList(); $ Macro:ParserSimpleTerm - $0$Node$1$ = ($2$)nodeArrayList$3$.get($4$); + $0$ = ($1$)nodeArrayList$2$.get($3$); $ Macro:ParserNewBodyNew - $0$Node$1$ = new $2$( + $0$ = new $1$( $ -Macro:ParserNew&ListBodyParams -$0$Node$1$ -$ - -Macro:ParserNew&ListBodyParamsNull +Macro:ParserNewBodyParam $0$ $ @@ -366,27 +362,27 @@ Macro:ParserNewBodyNewTail $ Macro:ParserTypedLinkedListAdd - if($2$Node$3$ != null) + if($1$ != null) { - $0$Node$1$.add($2$Node$3$); + $0$.add($1$); } $ Macro:ParserTypedLinkedListAddAll - if($2$Node$3$ != null) + if($1$ != null) { - if(!$0$Node$1$.isEmpty()){ - $0$Node$1$.addAll($2$Node$3$); + if(!$0$.isEmpty()){ + $0$.addAll($1$); }else{ - $0$Node$1$ = $2$Node$3$; + $0$ = $1$; } } $ Macro:ParserNewBodyListAdd - nodeList.add($0$Node$1$); + nodeList.add($0$); $ -- GitLab