Skip to content

Commit

Permalink
Fix Bugs:
Browse files Browse the repository at this point in the history
- Token and Function semantic can contain space and other special character
- Detect invalid token or function
- Add debug mode to parser
  • Loading branch information
Borjianamin98 authored and IYP-Programer-Yeah committed Feb 1, 2020
1 parent 7f42519 commit 29c5664
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 59 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ir.ac.sbu</groupId>
<artifactId>PGen</artifactId>
<version>2.0</version>
<version>2.1</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ir.ac.sbu.controller;

import ir.ac.sbu.utility.CheckUtility;
import ir.ac.sbu.utility.DialogUtility;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
Expand Down Expand Up @@ -34,12 +35,14 @@ public void init(EdgeModel edge) {
}

public void apply(ActionEvent actionEvent) {
if (tokenText.getText().trim().isEmpty()) {
DialogUtility.showErrorDialog("Token can not be empty");
} else {
try {
CheckUtility.checkTokenName(tokenText.getText());
CheckUtility.checkFunctionName(funcText.getText());
Stage stage = (Stage) applyBtn.getScene().getWindow();
CommandManager.getInstance().applyCommand(new ChangeEdgeCmd(edge, tokenText.getText(), funcText.getText(), graphChk.isSelected(), globalChk.isSelected()));
stage.close();
} catch (IllegalArgumentException e) {
DialogUtility.showErrorDialog(e.getMessage());
}
}
}
21 changes: 12 additions & 9 deletions src/main/java/ir/ac/sbu/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ir.ac.sbu.parser.LLParserGenerator;
import ir.ac.sbu.service.ExportService;
import ir.ac.sbu.service.SaveLoadService;
import ir.ac.sbu.utility.CheckUtility;
import ir.ac.sbu.utility.DialogUtility;
import ir.ac.sbu.utility.GenerateUID;
import ir.ac.sbu.utility.ResourceUtility;
Expand Down Expand Up @@ -103,22 +104,24 @@ protected void updateItem(GraphModel item, boolean empty) {
renameBtn.setOnAction(event -> {
Optional<String> newValue = DialogUtility.showInputDialog(cell.getItem().getName(), "Rename Graph");
if (newValue.isPresent()) {
if (!newValue.get().trim().isEmpty()) {
try {
CheckUtility.checkGraphName(newValue.get());
cell.getItem().setName(newValue.get());
} else {
DialogUtility.showErrorDialog("Name of graph can not be empty");
} catch (IllegalArgumentException e) {
DialogUtility.showErrorDialog(e.getMessage());
}
}
});
duplicateBtn.setOnAction(event -> {
Optional<String> result = DialogUtility.showInputDialog(cell.getItem().getName(), "New Graph");
if (result.isPresent()) {
if (!result.get().trim().isEmpty()) {
try {
CheckUtility.checkGraphName(result.get());
GraphModel currentGraph = cell.getItem();
GraphModel duplicateGraph = currentGraph.createCopy(result.get());
graphs.add(duplicateGraph);
} else {
DialogUtility.showErrorDialog("Name of graph can not be empty");
} catch (IllegalArgumentException e) {
DialogUtility.showErrorDialog(e.getMessage());
}
}
});
Expand Down Expand Up @@ -172,9 +175,9 @@ public void exportFullParser(ActionEvent actionEvent) {
File directorySelected = DialogUtility.showDirectoryDialog(pane.getScene().getWindow());
try {
if (directorySelected != null) {
InputStream parserSource = ResourceUtility.getResourceAsStream("parser/Parser.code");
InputStream lexicalSource = ResourceUtility.getResourceAsStream("parser/Lexical.code");
InputStream codeGeneratorSource = ResourceUtility.getResourceAsStream("parser/CodeGenerator.code");
InputStream parserSource = ResourceUtility.getResourceAsStream("parser/Parser.java");
InputStream lexicalSource = ResourceUtility.getResourceAsStream("parser/Lexical.java");
InputStream codeGeneratorSource = ResourceUtility.getResourceAsStream("parser/CodeGenerator.java");
Path destination = Paths.get(directorySelected.getPath());

LLParserGenerator parser = new LLParserGenerator(graphs);
Expand Down
63 changes: 46 additions & 17 deletions src/main/java/ir/ac/sbu/parser/LLParserGenerator.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package ir.ac.sbu.parser;

import ir.ac.sbu.command.ChangeEdgeCmd;
import ir.ac.sbu.command.CommandManager;
import ir.ac.sbu.exception.TableException;
import ir.ac.sbu.parser.builder.Action;
import ir.ac.sbu.parser.builder.LLCell;
import ir.ac.sbu.utility.CheckUtility;
import ir.ac.sbu.utility.DialogUtility;
import ir.ac.sbu.wagu.Block;
import ir.ac.sbu.wagu.Board;
import ir.ac.sbu.wagu.Table;
import javafx.stage.Stage;
import javafx.util.Pair;
import ir.ac.sbu.model.EdgeModel;
import ir.ac.sbu.model.GraphModel;
Expand Down Expand Up @@ -51,7 +55,7 @@ public LLParserGenerator(List<GraphModel> graphs) throws TableException {
checkGraphs(graphs);
checkTokens();

variableGraph = graphs.stream().collect(Collectors.toMap(GraphModel::getName, Function.identity(), (o, o2) -> o));
variableGraph = graphs.stream().collect(Collectors.toMap(GraphModel::getName, Function.identity(), (o, v) -> o));
allNodes = graphs.stream().flatMap(graphModel -> graphModel.getNodes().stream())
.collect(Collectors.toList());
allEdges = graphs.stream()
Expand All @@ -68,11 +72,25 @@ public LLParserGenerator(List<GraphModel> graphs) throws TableException {
}

private void checkEdges() throws TableException {
List<String> messages = new ArrayList<>();

for (EdgeModel edge : allEdges) {
try {
CheckUtility.checkFunctionName(edge.getFunction());
} catch (IllegalArgumentException e) {
messages.add(e.getMessage());
}
}

if (allEdges.stream()
.filter(EdgeModel::isGraph)
.map(EdgeModel::getToken)
.anyMatch(token -> token.equals("MAIN"))) {
throw new TableException(Collections.singletonList("Graph MAIN should not used in graphs"));
messages.add("Graph MAIN should not used in graphs.");
}

if (!messages.isEmpty()) {
throw new TableException(messages);
}
}

Expand All @@ -82,18 +100,25 @@ private void checkTokens() throws TableException {
tokenAsInt.put("$", 0);
int tokenUID = 1;
for (String token : tokens) {
if (token.startsWith("$")) {
messages.add("All string starting with $ are predefined tokens");
try {
CheckUtility.checkTokenName(token);
tokenAsInt.put(token, tokenUID);
tokenUID++;
} catch (IllegalArgumentException e) {
messages.add(e.getMessage());
}
tokenAsInt.put(token, tokenUID);
tokenUID++;
}

for (String variable : variables) {
if (tokenAsInt.put(variable, tokenUID) != null) {
messages.add(String.format("%s Should be either a token or a graph", variable));
try {
CheckUtility.checkGraphName(variable);
if (tokenAsInt.put(variable, tokenUID) != null) {
messages.add(String.format("%s Should be either a token or a graph.", variable));
}
tokenUID++;
} catch (IllegalArgumentException e) {
messages.add(e.getMessage());
}
tokenUID++;
}

tokensSortedById = tokenAsInt.keySet().stream()
Expand All @@ -119,10 +144,15 @@ private void checkGraphs(List<GraphModel> graphs) throws TableException {
.forEach(s -> messages.add(String.format("Duplicate graph %s exist", s)));

List<GraphModel> graphWithNoFinalNode = graphs.stream().
filter(graph -> graph.getNodes().stream().noneMatch(NodeModel::isFinalNode)).collect(Collectors.toList());
graphWithNoFinalNode.forEach(graph -> messages.add(String.format("Graph %s doesn't have final node", graph.getName())));
List<GraphModel> graphWithNoStartNode = graphs.stream().filter(graph -> graph.getStart() == null).collect(Collectors.toList());
graphWithNoStartNode.forEach(graph -> messages.add(String.format("Graph %s doesn't have start node", graph.getName())));
filter(graph -> graph.getNodes().stream().noneMatch(NodeModel::isFinalNode))
.collect(Collectors.toList());
graphWithNoFinalNode.forEach(graph ->
messages.add(String.format("Graph %s doesn't have final node", graph.getName())));
List<GraphModel> graphWithNoStartNode = graphs.stream()
.filter(graph -> graph.getStart() == null)
.collect(Collectors.toList());
graphWithNoStartNode.forEach(graph ->
messages.add(String.format("Graph %s doesn't have start node", graph.getName())));

if (!messages.isEmpty()) {
throw new TableException(messages);
Expand Down Expand Up @@ -213,11 +243,10 @@ public void buildTable(File file) throws TableException {
writer.println(allNodes.size() + " " + table[0].length);
writer.println(startNode);

writer.println(String.join(" ", tokensSortedById));
writer.println(String.join(CheckUtility.DELIMITER, tokensSortedById));
for (LLCell[] cellOfNode : table) {
for (LLCell cell : cellOfNode) {
writer.print(cell + " ");
}
writer.print(Arrays.stream(cellOfNode).map(LLCell::toString)
.collect(Collectors.joining(CheckUtility.DELIMITER)));
writer.println();
}
writer.println();
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/ir/ac/sbu/utility/CheckUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ir.ac.sbu.utility;

public class CheckUtility {
public static String DELIMITER = ",";

private CheckUtility() {
}

public static void checkGraphName(String graphName) {
if (graphName.trim().isEmpty()) {
throw new IllegalArgumentException("Graph name can not be empty.");
} else if (graphName.contains(DELIMITER)) {
throw new IllegalArgumentException("Graph can not contain '" + DELIMITER + "' character.");
}
}

public static void checkTokenName(String tokenName) {
if (tokenName.startsWith("$")) {
throw new IllegalArgumentException("All string starting with $ are predefined tokens.");
} else if (tokenName.trim().isEmpty()) {
throw new IllegalArgumentException("Token can not be empty.");
} else if (tokenName.contains(DELIMITER)) {
throw new IllegalArgumentException("Token can not contain '" + DELIMITER + "' character.");
}
}

public static void checkFunctionName(String functionName) {
if (functionName.contains(DELIMITER)) {
throw new IllegalArgumentException("Token can not contain '" + DELIMITER + "' character.");
} else if (functionName.contains(" ")) {
throw new IllegalArgumentException("Token can not contain ' ' character.");
}
}
}
File renamed without changes.
Loading

0 comments on commit 29c5664

Please sign in to comment.