From 96b09fdf2e99863f079e6bc843d56583dc7c810c Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 27 Jul 2017 15:26:02 -0700 Subject: [PATCH] Add sanitization to TreeBuilder. --- src/org/nwapw/abacus/lexing/pattern/Pattern.java | 12 ++++++++++++ src/org/nwapw/abacus/tree/TreeBuilder.java | 5 +++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/org/nwapw/abacus/lexing/pattern/Pattern.java b/src/org/nwapw/abacus/lexing/pattern/Pattern.java index 2057a8c..5a7f6cb 100644 --- a/src/org/nwapw/abacus/lexing/pattern/Pattern.java +++ b/src/org/nwapw/abacus/lexing/pattern/Pattern.java @@ -235,4 +235,16 @@ public class Pattern { public PatternNode getHead() { return head; } + + public static String sanitize(String from){ + Pattern 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; + } } diff --git a/src/org/nwapw/abacus/tree/TreeBuilder.java b/src/org/nwapw/abacus/tree/TreeBuilder.java index de1a8ee..30e9a5d 100644 --- a/src/org/nwapw/abacus/tree/TreeBuilder.java +++ b/src/org/nwapw/abacus/tree/TreeBuilder.java @@ -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); }