[gnome-calculator] GCalc: fix variable starting with 'i' parsing



commit c5481791d6052e74bb6f822249bacd56afa2917c
Author: Daniel Espinosa <esodan gmail com>
Date:   Sun Nov 3 22:03:55 2019 -0600

    GCalc: fix variable starting with 'i' parsing

 gcalc/gcalc-parser.vala  |  7 ++++++-
 tests/gcalc-parsing.vala | 28 ++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)
---
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index 2668e84c..7ba75262 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -27,6 +27,7 @@ public class GCalc.Parser : Object {
   MathExpression current_parent = null;
   MathExpression top_parent = null;
   Equation eq = null;
+  Regex rg;
   bool enable_parameter = false;
   Gee.ArrayList<TokenType> expected = new Gee.ArrayList<TokenType> ();
   GLib.Scanner scanner;
@@ -44,6 +45,11 @@ public class GCalc.Parser : Object {
     scanner.config.scan_hex = false;
     scanner.config.scan_hex_dollar = false;
     scanner.config.numbers_2_int = false;
+    try {
+      rg = new Regex ("^i\\d", RegexCompileFlags.ANCHORED, RegexMatchFlags.ANCHORED);
+    } catch (GLib.Error e) {
+      message ("Error on compile regular expression: %s", e.message);
+    }
   }
   /**
    * Creates a {@link MathEquation} and adds it to given
@@ -69,7 +75,6 @@ public class GCalc.Parser : Object {
       }
       switch (token) {
         case TokenType.IDENTIFIER:
-          Regex rg = new Regex ("i[0-9]*.*", RegexCompileFlags.ANCHORED, RegexMatchFlags.ANCHORED);
           if (rg.match (n, RegexMatchFlags.ANCHORED, null)) {
             string cxn = n.replace ("i", "");
             double v = double.parse (cxn);
diff --git a/tests/gcalc-parsing.vala b/tests/gcalc-parsing.vala
index ec88e908..6cf0f5d0 100644
--- a/tests/gcalc-parsing.vala
+++ b/tests/gcalc-parsing.vala
@@ -460,6 +460,34 @@ class Tests {
         warning ("Error: %s", error.message);
       }
     });
+    Test.add_func ("/gcalc/parser/term/variable/i-start",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new EquationManager ();
+        parser.parse ("id*3", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        assert (eq.expressions.get_n_items () == 1);
+        var p = eq.expressions.get_item (0) as MathPolynomial;
+        assert (p != null);
+        message ("Terms: %u", p.expressions.get_n_items ());
+        assert (p.expressions.get_n_items () == 1);
+        var t1 = p.expressions.get_item (0) as MathTerm;
+        assert (t1 != null);
+        assert (t1.expressions.get_n_items () == 3);
+        message ("T: %s", t1.expressions.get_item (0).get_type ().name ());
+        var c1 = t1.expressions.get_item (0) as MathVariable;
+        assert (c1 != null);
+        var m = t1.expressions.get_item (1) as MathMultiply;
+        assert (m != null);
+        var c2 = t1.expressions.get_item (2) as MathConstant;
+        assert (c2 != null);
+      } catch (GLib.Error error) {
+        warning ("Error: %s", error.message);
+      }
+    });
     Test.add_func ("/gcalc/parser/term/complex/multiply-division/constant-variable",
     ()=>{
       try {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]