Skip to content

Commit

Permalink
add helpers for rest-api
Browse files Browse the repository at this point in the history
  • Loading branch information
mesut146 committed Oct 22, 2021
1 parent 85b4284 commit c1d78ad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
22 changes: 19 additions & 3 deletions src/main/java/mesut/parserx/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import mesut.parserx.parser.AstBuilder;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class Utils {

public static Tree makeTokenLessTree(String grammar) {
public static Tree makeTokenLessTree(String grammar, final boolean fromRegex) {
try {
final Tree tree = AstBuilder.makeTree(grammar);
new SimpleTransformer(tree) {
int count = 1;
Map<String, TokenDecl> newTokens = new HashMap<>();

@Override
public Node transformName(Name node, Node parent) {
Expand All @@ -28,11 +31,24 @@ public Node transformName(Name node, Node parent) {
public Node transformString(StringNode node, Node parent) {
TokenDecl decl = tree.getTokenByValue(node.value);
if (decl == null) {
decl = new TokenDecl("T" + count++, node);
tree.addToken(decl);
if (fromRegex) {
return node;
}
else {
decl = new TokenDecl("T" + count++, node);
newTokens.put(decl.name, decl);
}
}
return decl.ref();
}

@Override
public void transformAll() {
super.transformAll();
for (TokenDecl decl : newTokens.values()) {
tree.addToken(decl);
}
}
}.transformAll();
return tree;
} catch (Exception e) {
Expand Down
17 changes: 12 additions & 5 deletions src/test/java/common/Env.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package common;

import mesut.parserx.nodes.Node;
import mesut.parserx.nodes.TokenDecl;
import mesut.parserx.nodes.Tree;
import mesut.parserx.regex.RegexFromStr;
import mesut.parserx.utils.Utils;
import org.junit.Test;

Expand Down Expand Up @@ -35,16 +38,20 @@ public static File getResFile(String name) throws IOException {
return new File(url.getPath());
}

public static Tree makeRule(String grammar) {
return Utils.makeTokenLessTree(grammar);
}

public static Tree tree(String res) throws IOException {
return Tree.makeTree(getResFile(res));
}

@Test
public void tokenLessTest() {
System.out.println(Utils.makeTokenLessTree("A: \"asd\" \"a\"*;"));
System.out.println(Utils.makeTokenLessTree("A: \"asd\" \"a\"*;", false));
}

@Test
public void tokenLessTest2() {
Node rhs = RegexFromStr.build("a*b");
Tree tree = new Tree();
tree.addToken(new TokenDecl("START", rhs));
System.out.println(Utils.makeTokenLessTree(tree.toString(), true));
}
}
3 changes: 2 additions & 1 deletion src/test/java/regex/FactorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import mesut.parserx.gen.transform.FactorLoop;
import mesut.parserx.gen.transform.Recursion;
import mesut.parserx.nodes.*;
import mesut.parserx.utils.Utils;
import org.junit.Ignore;
import org.junit.Test;

public class FactorTest {

@Test
public void helper() {
Tree tree = Env.makeRule("A: (a | b y)* c | (a | d)+ e | c c;");
Tree tree = Utils.makeTokenLessTree("A: (a | b y)* c | (a | d)+ e | c c;", false);
RuleDecl decl = tree.rules.get(0);
System.out.println(Helper.firstMap(decl.rhs, tree));
}
Expand Down

0 comments on commit c1d78ad

Please sign in to comment.