object EvaluateWhereClause(Runtime.PassiveExpression expr, Runtime.Pattern lastPattern, ScriptThread thread) { // instantiate where-clause pattern var patt = Pattern.Instantiate(thread); patt.CopyBoundVariables(lastPattern); // perform matching var result = patt.Match(expr); if (result) { // store last recognized pattern as a local variable thread.SetLastPattern(patt); // match succeeded, return result expression if (ResultExpression != null) { return(ResultExpression.Evaluate(thread)); } // match succeeded, evaluate more conditions if (MoreConditions != null) { return(MoreConditions.Evaluate(thread)); } } // matching failed, return nothing return(null); }
public void Symbol_DontMatchAnotherSymbol() { var pattern = new Pattern(123); // 123 var expr = PassiveExpression.Build('X'); // 'X' var result = pattern.Match(expr); Assert.IsFalse(result); }
public void SymbolChain_MatchesSameSymbolChain() { var pattern = new Pattern(123, 'T', 'e', 's', 't'); // 123 'Test' var expr = PassiveExpression.Build(123, 'T', 'e', 's', 't'); // 123 'Test' var result = pattern.Match(expr); Assert.IsTrue(result); }
public void SymbolChain_DontMatchDifferentSymbolChain() { var pattern = new Pattern(123, 'T', 'e', 's', 't'); // 123 'Test' var expr = PassiveExpression.Build(123, 'T', 'E', 'S', 'T'); // 123 'TEST' var result = pattern.Match(expr); Assert.IsFalse(result); }
public void Symbol_MatchesSameSymbol() { var pattern = new Pattern(123); // 123 var expr = PassiveExpression.Build(123); // 123 var result = pattern.Match(expr); Assert.IsTrue(result); }
public void SymbolVariable_MatchesAnySymbol() { var pattern = new Pattern(new SymbolVariable("s.1")); // s.1 var expr = PassiveExpression.Build(123); // 123 var result = pattern.Match(expr); Assert.IsTrue(result); Assert.IsTrue(pattern.Variables.ContainsKey("s.1")); Assert.AreEqual(123, pattern.Variables["s.1"].Value); expr = PassiveExpression.Build('A'); // 'A' result = pattern.Match(expr); Assert.IsTrue(result); Assert.IsTrue(pattern.Variables.ContainsKey("s.1")); Assert.AreEqual('A', pattern.Variables["s.1"].Value); }
public void CopyBoundVariables(Pattern pattern) { foreach (string name in pattern.Variables.Keys) { if (Variables.ContainsKey(name)) { Variable var = Variables[name]; var.Value = pattern.GetVariable(name); // first occurance of the variable is in another pattern var.FirstOccurance = -1; } else { // copy bound variable from another pattern Variable var = pattern.Variables[name]; Variables[name] = var; var.FirstOccurance = -1; } } }
void EvaluateWhereClause(Runtime.Pattern lastPattern, EvaluationContext context, AstMode mode) { // instantiate where-clause pattern var patt = Pattern.Instantiate(context, mode); patt.CopyBoundVariables(lastPattern); // perform matching var expr = context.Data.Pop() as Runtime.PassiveExpression; var result = patt.Match(expr); if (result) { // store last recognized pattern as a local variable context.SetLastPattern(patt); // match succeeded, return true if (ResultExpression != null) { ResultExpression.Evaluate(context, AstMode.Read); context.Data.Push(true); return; } // match succeeded? depends on more conditions if (MoreConditions != null) { // return true or false MoreConditions.Evaluate(context, AstMode.Read); return; } } // match failed, return false context.Data.Push(false); }
public void ComplexPattern_MatchesExpression2() { var pattern = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new SymbolVariable("s.3"), new ClosingBrace(), new OpeningBrace(), new ExpressionVariable("e.2"), '0', new ClosingBrace()); // (e.1 s.3) (e.2 '0') var expr = PassiveExpression.Build(new OpeningBrace(), '0', '1', new ClosingBrace(), new OpeningBrace(), '0', new ClosingBrace()); // ('01') ('0') var result = pattern.Match(expr); Assert.IsTrue(result); Assert.IsTrue(pattern.Variables.ContainsKey("e.1")); Assert.IsTrue(pattern.Variables.ContainsKey("e.2")); Assert.IsTrue(pattern.Variables.ContainsKey("s.3")); Assert.AreEqual(PassiveExpression.Build('0'), pattern.Variables["e.1"].Value); Assert.AreEqual(PassiveExpression.Build(), pattern.Variables["e.2"].Value); Assert.AreEqual('1', pattern.Variables["s.3"].Value); }
public void SymbolVariable_DontMatchBraces() { var pattern = new Pattern(new SymbolVariable("s.1")); // s.1 var expr = PassiveExpression.Build(new OpeningBrace()); // ( var result = pattern.Match(expr); Assert.IsFalse(result); }
// extract arguments specified as <Function t.1 e.2> void GetArguments(PassiveExpression expression, out object arg1, out object arg2) { var p = new Pattern(new TermVariable("t.1"), new ExpressionVariable("e.2")); if (p.Match(expression)) { arg1 = p.GetVariable("t.1"); arg2 = p.GetVariable("e.2"); return; } // can't find match throw new RecognitionImpossibleException(); }
private static PassiveExpression _Lex(PassiveExpression expression) { Pattern pattern10 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "(".ToCharArray(), new ExpressionVariable("e.2")); if (pattern10.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), new OpeningBrace(), pattern10.GetVariable("e.1"), new ClosingBrace(), new ClosingBrace(), pattern10.GetVariable("e.2")))); }; Pattern pattern11 = new Pattern(new OpeningBrace(), new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new ExpressionVariable("e.2"), new ClosingBrace(), ")".ToCharArray(), new ExpressionVariable("e.3")); if (pattern11.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern11.GetVariable("e.1"), new OpeningBrace(), pattern11.GetVariable("e.2"), new ClosingBrace(), new ClosingBrace(), pattern11.GetVariable("e.3")))); }; Pattern pattern12 = new Pattern(new OpeningBrace(), "$".ToCharArray(), new ExpressionVariable("e.1"), new ClosingBrace(), ")".ToCharArray(), new ExpressionVariable("e.3")); if (pattern12.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("ERROR: Unpaired right parenthsis:".ToCharArray())), _Prout(PassiveExpression.Build(pattern12.GetVariable("e.1"), ")".ToCharArray()))); }; Pattern pattern13 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), " ".ToCharArray(), new ExpressionVariable("e.2")); if (pattern13.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern13.GetVariable("e.1"), new ClosingBrace(), pattern13.GetVariable("e.2")))); }; Pattern pattern14 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "\\t".ToCharArray(), new ExpressionVariable("e.2")); if (pattern14.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern14.GetVariable("e.1"), new ClosingBrace(), pattern14.GetVariable("e.2")))); }; Pattern pattern15 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "\'".ToCharArray(), new ExpressionVariable("e.2")); if (pattern15.Match(expression)) { expression = PassiveExpression.Build(_Quotes(PassiveExpression.Build("\'".ToCharArray(), new OpeningBrace(), new ClosingBrace(), pattern15.GetVariable("e.2")))); { Pattern pattern16 = new Pattern(new OpeningBrace(), new ClosingBrace(), new ExpressionVariable("e.3")); pattern16.CopyBoundVariables(pattern15); if (pattern16.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("ERROR: Unpaired quote \'".ToCharArray(), pattern16.GetVariable("e.2")))); }; Pattern pattern17 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.0"), new ClosingBrace(), new ExpressionVariable("e.3")); pattern17.CopyBoundVariables(pattern15); if (pattern17.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern17.GetVariable("e.1"), pattern17.GetVariable("e.0"), new ClosingBrace(), pattern17.GetVariable("e.2")))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); } }; Pattern pattern19 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new SymbolVariable("s.A"), new ExpressionVariable("e.2")); if (pattern19.Match(expression)) { expression = PassiveExpression.Build(_Type(PassiveExpression.Build(pattern19.GetVariable("s.A")))); { Pattern pattern20 = new Pattern("L".ToCharArray(), new ExpressionVariable("e.A1")); pattern20.CopyBoundVariables(pattern19); if (pattern20.Match(expression)) { expression = PassiveExpression.Build(_Id__tail(PassiveExpression.Build(new OpeningBrace(), pattern20.GetVariable("s.A"), new ClosingBrace(), pattern20.GetVariable("e.2")))); Pattern pattern21 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.Id"), new ClosingBrace(), new ExpressionVariable("e.3")); pattern21.CopyBoundVariables(pattern20); if (pattern21.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern21.GetVariable("e.1"), _Implode(PassiveExpression.Build(pattern21.GetVariable("e.Id"))), new ClosingBrace(), pattern21.GetVariable("e.3")))); } }; Pattern pattern22 = new Pattern("D".ToCharArray(), new ExpressionVariable("e.A1")); pattern22.CopyBoundVariables(pattern19); if (pattern22.Match(expression)) { expression = PassiveExpression.Build(_D__string(PassiveExpression.Build(new OpeningBrace(), pattern22.GetVariable("s.A"), new ClosingBrace(), pattern22.GetVariable("e.2")))); Pattern pattern23 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.D-Str"), new ClosingBrace(), new ExpressionVariable("e.3")); pattern23.CopyBoundVariables(pattern22); if (pattern23.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern23.GetVariable("e.1"), _Numb(PassiveExpression.Build(pattern23.GetVariable("e.D-Str"))), new ClosingBrace(), pattern23.GetVariable("e.3")))); } }; Pattern pattern24 = new Pattern(new SymbolVariable("s.T"), new ExpressionVariable("e.A1")); pattern24.CopyBoundVariables(pattern19); if (pattern24.Match(expression)) { return PassiveExpression.Build(_Lex(PassiveExpression.Build(new OpeningBrace(), pattern24.GetVariable("e.1"), pattern24.GetVariable("s.A"), new ClosingBrace(), pattern24.GetVariable("e.2")))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); } }; Pattern pattern26 = new Pattern(new OpeningBrace(), "$".ToCharArray(), new ExpressionVariable("e.1"), new ClosingBrace()); if (pattern26.Match(expression)) { return PassiveExpression.Build(pattern26.GetVariable("e.1")); }; Pattern pattern27 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.M"), new ClosingBrace()); if (pattern27.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("ERROR: Unpaired left parentheses".ToCharArray())), _Pr__lmb(PassiveExpression.Build(pattern27.GetVariable("e.M")))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public void SymbolVariable_DontMatchSymbolChain() { var pattern = new Pattern(new SymbolVariable("s.1")); // s.1 var expr = PassiveExpression.Build(123, 321); // 123 321 var result = pattern.Match(expr); Assert.IsFalse(result); }
private static PassiveExpression _Lookm(PassiveExpression expression) { Pattern pattern13 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "[".ToCharArray(), new ExpressionVariable("e.2")); if (pattern13.Match(expression)) { return(PassiveExpression.Build(new OpeningBrace(), pattern13.GetVariable("e.1"), new ClosingBrace(), "[".ToCharArray(), pattern13.GetVariable("e.2"))); } ; Pattern pattern14 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "\"".ToCharArray(), new ExpressionVariable("e.2")); if (pattern14.Match(expression)) { expression = PassiveExpression.Build(_Quotes(PassiveExpression.Build("\"".ToCharArray(), new OpeningBrace(), new ClosingBrace(), pattern14.GetVariable("e.2")))); Pattern pattern15 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.0"), new ClosingBrace(), new ExpressionVariable("e.3")); pattern15.CopyBoundVariables(pattern14); if (pattern15.Match(expression)) { return(PassiveExpression.Build(_Lookm(PassiveExpression.Build(new OpeningBrace(), pattern15.GetVariable("e.1"), pattern15.GetVariable("e.0"), new ClosingBrace(), pattern15.GetVariable("e.3"))))); } } ; Pattern pattern16 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "\'".ToCharArray(), new ExpressionVariable("e.2")); if (pattern16.Match(expression)) { expression = PassiveExpression.Build(_Quotes(PassiveExpression.Build("\'".ToCharArray(), new OpeningBrace(), new ClosingBrace(), pattern16.GetVariable("e.2")))); Pattern pattern17 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.0"), new ClosingBrace(), new ExpressionVariable("e.3")); pattern17.CopyBoundVariables(pattern16); if (pattern17.Match(expression)) { return(PassiveExpression.Build(_Lookm(PassiveExpression.Build(new OpeningBrace(), pattern17.GetVariable("e.1"), pattern17.GetVariable("e.0"), new ClosingBrace(), pattern17.GetVariable("e.3"))))); } } ; Pattern pattern18 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), "/*".ToCharArray(), new ExpressionVariable("e.2"), "*/".ToCharArray(), new ExpressionVariable("e.3")); if (pattern18.Match(expression)) { return(PassiveExpression.Build(_Lookm(PassiveExpression.Build(new OpeningBrace(), pattern18.GetVariable("e.1"), "/*".ToCharArray(), pattern18.GetVariable("e.2"), "*/".ToCharArray(), new ClosingBrace(), pattern18.GetVariable("e.3"))))); } ; Pattern pattern19 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace(), new TermVariable("t.A"), new ExpressionVariable("e.2")); if (pattern19.Match(expression)) { return(PassiveExpression.Build(_Lookm(PassiveExpression.Build(new OpeningBrace(), pattern19.GetVariable("e.1"), pattern19.GetVariable("t.A"), new ClosingBrace(), pattern19.GetVariable("e.2"))))); } ; Pattern pattern20 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace()); if (pattern20.Match(expression)) { return(PassiveExpression.Build(pattern20.GetVariable("e.1"))); } ; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
private static PassiveExpression _Parse(PassiveExpression expression) { Pattern pattern48 = new Pattern(new ExpressionVariable("e.Exp")); if (pattern48.Match(expression)) { expression = PassiveExpression.Build(_Last1(PassiveExpression.Build(new OpeningBrace(), "+-".ToCharArray(), new ClosingBrace(), pattern48.GetVariable("e.Exp"), new OpeningBrace(), new ClosingBrace()))); Pattern pattern49 = new Pattern(new ExpressionVariable("e.1"), new SymbolVariable("s.Op"), new OpeningBrace(), new ExpressionVariable("e.2"), new ClosingBrace()); pattern49.CopyBoundVariables(pattern48); if (pattern49.Match(expression)) { return PassiveExpression.Build(pattern49.GetVariable("s.Op"), new OpeningBrace(), _Parse(PassiveExpression.Build(pattern49.GetVariable("e.1"))), new ClosingBrace(), _Parse(PassiveExpression.Build(pattern49.GetVariable("e.2")))); } }; Pattern pattern50 = new Pattern(new ExpressionVariable("e.Exp")); if (pattern50.Match(expression)) { expression = PassiveExpression.Build(_Last1(PassiveExpression.Build(new OpeningBrace(), "*/".ToCharArray(), new ClosingBrace(), pattern50.GetVariable("e.Exp"), new OpeningBrace(), new ClosingBrace()))); Pattern pattern51 = new Pattern(new ExpressionVariable("e.1"), new SymbolVariable("s.Op"), new OpeningBrace(), new ExpressionVariable("e.2"), new ClosingBrace()); pattern51.CopyBoundVariables(pattern50); if (pattern51.Match(expression)) { return PassiveExpression.Build(pattern51.GetVariable("s.Op"), new OpeningBrace(), _Parse(PassiveExpression.Build(pattern51.GetVariable("e.1"))), new ClosingBrace(), _Parse(PassiveExpression.Build(pattern51.GetVariable("e.2")))); } }; Pattern pattern52 = new Pattern(new ExpressionVariable("e.1"), "^".ToCharArray(), new ExpressionVariable("e.2")); if (pattern52.Match(expression)) { return PassiveExpression.Build("^".ToCharArray(), new OpeningBrace(), _Parse(PassiveExpression.Build(pattern52.GetVariable("e.1"))), new ClosingBrace(), _Parse(PassiveExpression.Build(pattern52.GetVariable("e.2")))); }; Pattern pattern53 = new Pattern(new SymbolVariable("s.Symb")); if (pattern53.Match(expression)) { expression = PassiveExpression.Build(_Type(PassiveExpression.Build(pattern53.GetVariable("s.Symb")))); Pattern pattern54 = new Pattern("Wi".ToCharArray(), new ExpressionVariable("e.S")); pattern54.CopyBoundVariables(pattern53); if (pattern54.Match(expression)) { return PassiveExpression.Build(pattern54.GetVariable("s.Symb")); } }; Pattern pattern55 = new Pattern(new SymbolVariable("s.Symb")); if (pattern55.Match(expression)) { expression = PassiveExpression.Build(_Type(PassiveExpression.Build(pattern55.GetVariable("s.Symb")))); Pattern pattern56 = new Pattern("N".ToCharArray(), new ExpressionVariable("e.S")); pattern56.CopyBoundVariables(pattern55); if (pattern56.Match(expression)) { return PassiveExpression.Build(pattern56.GetVariable("s.Symb")); } }; Pattern pattern57 = new Pattern(new OpeningBrace(), new ExpressionVariable("e.Exp"), new ClosingBrace()); if (pattern57.Match(expression)) { return PassiveExpression.Build(_Parse(PassiveExpression.Build(pattern57.GetVariable("e.Exp")))); }; Pattern pattern58 = new Pattern(); if (pattern58.Match(expression)) { return PassiveExpression.Build(); }; Pattern pattern59 = new Pattern(new ExpressionVariable("e.Exp")); if (pattern59.Match(expression)) { return PassiveExpression.Build(_Prout(PassiveExpression.Build("Syntax error. Cannot parse ".ToCharArray(), pattern59.GetVariable("e.Exp"))), _Br(PassiveExpression.Build("compl=fail".ToCharArray()))); }; throw new RecognitionImpossibleException("Recognition impossible. Last expression: " + expression.ToString()); }
public void TermVariable_MatchesNonEmptyExpressionInBraces() { var pattern = new Pattern(new TermVariable("t.1")); // t.1 var expr = PassiveExpression.Build(new OpeningBrace(), "Hello", new ClosingBrace()); // () var result = pattern.Match(expr); Assert.IsTrue(result); Assert.IsTrue(pattern.Variables.ContainsKey("t.1")); Assert.AreEqual(PassiveExpression.Build(new OpeningBrace(), "Hello", new ClosingBrace()), pattern.Variables["t.1"].Value); }
public void ExpressionVariable_MatchesEmptyExpression() { var pattern = new Pattern(new ExpressionVariable("e.1")); // e.1 var expr = PassiveExpression.Build(); // var result = pattern.Match(expr); Assert.IsTrue(result); Assert.IsTrue(pattern.Variables.ContainsKey("e.1")); }
public PassiveExpression Br(PassiveExpression expression) { // <Br e.N '=' e.Expr>, where e.N is expression which does not // include '=' on the upper level of the bracket's structure Pattern pattern = new Pattern(new ExpressionVariable("Key"), '=', new ExpressionVariable("Value")); if (pattern.Match(expression)) { var key = (PassiveExpression)pattern.GetVariable("Key"); var value = (PassiveExpression)pattern.GetVariable("Value"); var strKey = key.ToString(); BuriedKeys[strKey] = key; BuriedValues[strKey] = value; return PassiveExpression.Build(); } throw new RecognitionImpossibleException("<Br e.N '=' e.Expr>: unexpected arguments"); }
public void ExpressionVariableInBraces_MatchesNonEmptyBraces() { var pattern = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace()); // (e.1) var expr = PassiveExpression.Build(new OpeningBrace(), 123, new ClosingBrace()); // (123) var result = pattern.Match(expr); Assert.IsTrue(result); Assert.IsTrue(pattern.Variables.ContainsKey("e.1")); Assert.AreEqual(PassiveExpression.Build(123), pattern.Variables["e.1"].Value); }
public void ExpressionVariableInBraces_DontMatchEmptyExpression() { var pattern = new Pattern(new OpeningBrace(), new ExpressionVariable("e.1"), new ClosingBrace()); // (e.1) var expr = PassiveExpression.Build(); // var result = pattern.Match(expr); Assert.IsFalse(result); }