Add sanitization to TreeBuilder.

This commit is contained in:
Danila Fedorin 2017-07-27 15:26:02 -07:00
parent a7c2084254
commit b0ae3f90fc
2 changed files with 15 additions and 2 deletions

View File

@ -235,4 +235,16 @@ public class Pattern<T> {
public PatternNode<T> getHead() {
return head;
}
public static String sanitize(String from){
Pattern<Integer> pattern = new Pattern<>("", 0);
from = from.replace(".", "\\.");
from = from.replace("|", "\\|");
from = from.replace("(", "\\(");
from = from.replace(")", "\\)");
for(Character key : pattern.operations.keySet()){
from = from.replace("" + key, "\\" + key);
}
return from;
}
}

View File

@ -3,6 +3,7 @@ package org.nwapw.abacus.tree;
import org.nwapw.abacus.function.OperatorAssociativity;
import org.nwapw.abacus.lexing.Lexer;
import org.nwapw.abacus.lexing.pattern.Match;
import org.nwapw.abacus.lexing.pattern.Pattern;
import java.util.*;
@ -48,7 +49,7 @@ public class TreeBuilder {
* @param function the function to register.
*/
public void registerFunction(String function){
lexer.register(function, TokenType.FUNCTION);
lexer.register(Pattern.sanitize(function), TokenType.FUNCTION);
}
/**
@ -58,7 +59,7 @@ public class TreeBuilder {
* @param associativity the associativity of the operator.
*/
public void registerOperator(String operator, int precedence, OperatorAssociativity associativity){
lexer.register(operator, TokenType.OP);
lexer.register(Pattern.sanitize(operator), TokenType.OP);
precedenceMap.put(operator, precedence);
associativityMap.put(operator, associativity);
}