1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 07:50:09 -08:00

Add sanitization to TreeBuilder.

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

View File

@ -235,4 +235,16 @@ public class Pattern<T> {
public PatternNode<T> getHead() { public PatternNode<T> getHead() {
return head; 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.function.OperatorAssociativity;
import org.nwapw.abacus.lexing.Lexer; import org.nwapw.abacus.lexing.Lexer;
import org.nwapw.abacus.lexing.pattern.Match; import org.nwapw.abacus.lexing.pattern.Match;
import org.nwapw.abacus.lexing.pattern.Pattern;
import java.util.*; import java.util.*;
@ -48,7 +49,7 @@ public class TreeBuilder {
* @param function the function to register. * @param function the function to register.
*/ */
public void registerFunction(String function){ 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. * @param associativity the associativity of the operator.
*/ */
public void registerOperator(String operator, int precedence, OperatorAssociativity associativity){ 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); precedenceMap.put(operator, precedence);
associativityMap.put(operator, associativity); associativityMap.put(operator, associativity);
} }