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 c1d78ad commit cc55848
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
95 changes: 57 additions & 38 deletions src/main/java/mesut/parserx/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,77 @@

import mesut.parserx.nodes.*;
import mesut.parserx.parser.AstBuilder;
import mesut.parserx.regex.RegexFromStr;

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

public class Utils {

public static Tree makeTokenLessTree(String grammar, final boolean fromRegex) {
public static Tree fromRegex(String regex) {
Node rhs = RegexFromStr.build(regex);
Tree tree = new Tree();
tree.addToken(new TokenDecl("START", rhs));
makeTokens(tree, true);
return tree;
}

public static Tree fromGrammar(String grammar) throws IOException {
Tree tree = AstBuilder.makeTree(grammar);
makeTokens(tree, false);
return tree;
}

public static Tree makeTokenLessTree(String grammar, 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) {
//if it is not a rule then must be a token
if (tree.getRule(node) == null) {
//add fake token
node.isToken = true;
tree.tokens.add(new TokenDecl(node.name, new StringNode(node.name)));
}
return node;
Tree tree = AstBuilder.makeTree(grammar);
makeTokens(tree, fromRegex);
return tree;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static void makeTokens(final Tree tree, final boolean fromRegex) {
new SimpleTransformer(tree) {
int count = 1;
Map<String, TokenDecl> newTokens = new HashMap<>();

@Override
public Node transformName(Name node, Node parent) {
//if it is not a rule then must be a token
if (tree.getRule(node) == null) {
//add fake token
node.isToken = true;
tree.tokens.add(new TokenDecl(node.name, new StringNode(node.name)));
}
return node;
}

@Override
public Node transformString(StringNode node, Node parent) {
TokenDecl decl = tree.getTokenByValue(node.value);
if (decl == null) {
if (fromRegex) {
return node;
}
else {
decl = new TokenDecl("T" + count++, node);
newTokens.put(decl.name, decl);
}
@Override
public Node transformString(StringNode node, Node parent) {
TokenDecl decl = tree.getTokenByValue(node.value);
if (decl == null) {
if (fromRegex) {
return node;
}
else {
decl = new TokenDecl("T" + count++, node);
newTokens.put(decl.name, decl);
}
return decl.ref();
}
return decl.ref();
}

@Override
public void transformAll() {
super.transformAll();
for (TokenDecl decl : newTokens.values()) {
tree.addToken(decl);
}
@Override
public void transformAll() {
super.transformAll();
for (TokenDecl decl : newTokens.values()) {
tree.addToken(decl);
}
}.transformAll();
return tree;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}.transformAll();
}

public static String camel(String s) {
Expand Down
9 changes: 3 additions & 6 deletions src/test/java/common/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ public static Tree tree(String res) throws IOException {
}

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

@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));
System.out.println(Utils.fromRegex("a*b"));
}
}

0 comments on commit cc55848

Please sign in to comment.