From f9083efadc4fcfe0887bb81854d047edd4f37cb2 Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Fri, 21 Jul 2023 18:59:51 +0200 Subject: [PATCH] Remove support for Parser.filter This was already only available for parsers that have no AST transformations and don't rely on inlining. My last optimizations to the token lists on the stack have broken this feature (because the new lists are no longer mutable), but we don't use it anyway, so just remove it. Even the original SableCC thesis says "We do not recommend this usage [the Parser.filter method], because it is error prone." --- .../java/org/sablecc/sablecc/GenParser.java | 45 ++------------- .../java/org/sablecc/sablecc/SableCC.java | 7 +-- .../org/sablecc/sablecc/parser/Parser.java | 4 +- .../resources/org/sablecc/sablecc/parser.txt | 55 ++----------------- 4 files changed, 11 insertions(+), 100 deletions(-) diff --git a/src/main/java/org/sablecc/sablecc/GenParser.java b/src/main/java/org/sablecc/sablecc/GenParser.java index 20592f2..98bcc81 100644 --- a/src/main/java/org/sablecc/sablecc/GenParser.java +++ b/src/main/java/org/sablecc/sablecc/GenParser.java @@ -69,11 +69,6 @@ public class GenParser extends DepthFirstAdapter private String firstProductionName; private boolean processInlining; private boolean prettyPrinting; - private boolean grammarHasTransformations; - - // This boolean is used to check weither the filter() method in class Parser.java - // should be present or not. - private boolean activateFilter = true; //This tree-walker field generate the code of parsing and construction of the AST. GenerateAlternativeCodeForParser aParsedAltAdapter; @@ -89,15 +84,13 @@ public class GenParser extends DepthFirstAdapter private Map<String, Node> alts; public GenParser(ResolveIds ids, ResolveAltIds altIds, ResolveTransformIds transformIds, - String firstProductionName, boolean processInlining, boolean prettyPrinting, - boolean grammarHasTransformations) + String firstProductionName, boolean processInlining, boolean prettyPrinting) { this.ids = ids; this.altIds = altIds; this.transformIds = transformIds; this.processInlining = processInlining; this.prettyPrinting = prettyPrinting; - this.grammarHasTransformations = grammarHasTransformations; AET = new AlternativeElementTypes(ids); CG = new ComputeCGNomenclature(ids, transformIds.getProdTransformIds()); @@ -232,11 +225,6 @@ public class GenParser extends DepthFirstAdapter } catch(ConflictException ce) { - if(activateFilter) - { - activateFilter = false; - } - //Here, we are trying to inline the grammar with production imply in the conflict. if(processInlining) { @@ -375,37 +363,14 @@ public class GenParser extends DepthFirstAdapter macros.apply(file, "ParserHeader", new String[] {ids.pkgNameDot}); - if(activateFilter && !grammarHasTransformations) - { - macros.apply(file, "ParserNoInliningPushHeader"); - macros.apply(file, "ParserCommon", new String[] {", true", ", false"}); - } - else - { - macros.apply(file, "ParserInliningPushHeader"); - macros.apply(file, "ParserCommon", new String[] {"", ""}); - } - //this loop generates the code for all possible reductions and the type of //the node needed to be created at a local point. for(int i = 0; i < (productions.length - 1); i++) { - if(activateFilter && !grammarHasTransformations) - { - macros.apply(file, "ParserNoInliningReduce", new String[] { - "" + productions[i].index, - "" + productions[i].leftside, - "" + (productions[i].name.startsWith("ANonTerminal$") || - productions[i].name.startsWith("ATerminal$")), - productions[i].name}); - } - else - { - macros.apply(file, "ParserInliningReduce", new String[] { - "" + productions[i].index, - "" + productions[i].leftside, - productions[i].name}); - } + macros.apply(file, "ParserParseReduce", new String[] { + "" + productions[i].index, + "" + productions[i].leftside, + productions[i].name}); } macros.apply(file, "ParserParseTail", new String[] {firstProductionName}); diff --git a/src/main/java/org/sablecc/sablecc/SableCC.java b/src/main/java/org/sablecc/sablecc/SableCC.java index be66d89..9524c41 100644 --- a/src/main/java/org/sablecc/sablecc/SableCC.java +++ b/src/main/java/org/sablecc/sablecc/SableCC.java @@ -197,15 +197,11 @@ public class SableCC { tree = new Parser(new Lexer(reader)).parse(); } - boolean hasTransformations = false; - if (((AGrammar) tree.getPGrammar()).getAst() == null) { System.out .println("Adding productions and alternative of section AST."); // AddAstProductions astProductions = new AddAstProductions(); tree.apply(new AddAstProductions()); - } else { - hasTransformations = true; } System.out.println("Verifying identifiers."); @@ -269,8 +265,7 @@ public class SableCC { try { System.out.println("Generating the parser."); tree.apply(new GenParser(ids, alt_ids, transform_ids, ast_ids - .getFirstAstProduction(), processInlining, prettyPrinting, - hasTransformations)); + .getFirstAstProduction(), processInlining, prettyPrinting)); } catch (RuntimeException e) { System.out.println(e.getMessage()); throw e; diff --git a/src/main/java/org/sablecc/sablecc/parser/Parser.java b/src/main/java/org/sablecc/sablecc/parser/Parser.java index 6610247..195cd55 100644 --- a/src/main/java/org/sablecc/sablecc/parser/Parser.java +++ b/src/main/java/org/sablecc/sablecc/parser/Parser.java @@ -23,8 +23,6 @@ import org.sablecc.sablecc.node.*; @SuppressWarnings({"unchecked", "unused"}) public class Parser implements IParser { - protected List<Object> nodeList; - private final Lexer lexer; private final ListIterator<State> stack = new LinkedList<State>().listIterator(); private final TokenIndex converter = new TokenIndex(); @@ -105,7 +103,7 @@ public class Parser implements IParser return (PositionedNode)last; } - private void push(int numstate, List<Object> listNode) throws ParserException, LexerException, IOException + private void push(int numstate, List<Object> listNode) { if(!this.stack.hasNext()) { diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt index e436a95..274a2d7 100644 --- a/src/main/resources/org/sablecc/sablecc/parser.txt +++ b/src/main/resources/org/sablecc/sablecc/parser.txt @@ -31,8 +31,6 @@ import $0$node.*; @SuppressWarnings({"unchecked", "unused"}) public class Parser implements IParser { - protected List<Object> nodeList; - private final Lexer lexer; private final ListIterator<State> stack = new LinkedList<State>().listIterator(); private final TokenIndex converter = new TokenIndex(); @@ -113,11 +111,7 @@ public class Parser implements IParser return (PositionedNode)last; } -$ - -Macro:ParserInliningPushHeader - - private void push(int numstate, List<Object> listNode) throws ParserException, LexerException, IOException + private void push(int numstate, List<Object> listNode) { if(!this.stack.hasNext()) { @@ -130,37 +124,6 @@ Macro:ParserInliningPushHeader s.nodes = listNode; } - -$ - -Macro:ParserNoInliningPushHeader - protected void filter() throws ParserException, LexerException, IOException - {} - - private void push(int numstate, List<Object> listNode, boolean hidden) throws ParserException, LexerException, IOException - { - this.nodeList = listNode; - - if(!hidden) - { - filter(); - } - - if(!this.stack.hasNext()) - { - this.stack.add(new State(numstate, this.nodeList)); - return; - } - - State s = this.stack.next(); - s.state = numstate; - s.nodes = this.nodeList; - } - - -$ - -Macro:ParserCommon private int goTo(int index) { int state = state(); @@ -213,7 +176,7 @@ Macro:ParserCommon public Start parse() throws ParserException, LexerException, IOException { - push(0, null$0$); + push(0, null); while(true) { Token lastToken = this.lexer.peek(); @@ -259,7 +222,7 @@ Macro:ParserCommon case SHIFT: { List<Object> list = Collections.<Object>singletonList(this.lexer.next()); - push(destination, list$1$); + push(destination, list); } break; case REDUCE: @@ -268,7 +231,7 @@ Macro:ParserCommon $ -Macro:ParserInliningReduce +Macro:ParserParseReduce case $0$: /* reduce $2$ */ { List<Object> list = new$0$(); @@ -278,16 +241,6 @@ Macro:ParserInliningReduce $ -Macro:ParserNoInliningReduce - case $0$: /* reduce $3$ */ - { - List<Object> list = new$0$(); - push(goTo($1$), list, $2$); - } - break; - -$ - Macro:ParserParseTail } break; -- GitLab