コード例 #1
0
        PluralFormsNode MultiplicativeExpression()
        {
            PluralFormsNode p = PmExpression();

            if (p == null)
            {
                return(null);
            }
            PluralFormsNode n = p;

            if (Token.TokenType == PluralFormsToken.Type.Reminder)
            {
                PluralFormsNode qn = new PluralFormsNode(new PluralFormsToken(Token));
                if (!NextToken())
                {
                    return(null);
                }
                p = PmExpression();
                if (p == null)
                {
                    return(null);
                }
                qn.SetNode(1, p);
                qn.SetNode(0, n);
                return(qn);
            }
            return(n);
        }
コード例 #2
0
 public void SetNode(int i, PluralFormsNode n)
 {
     if (i >= 0 && i <= 2)
     {
         nodes[i] = n;
     }
 }
コード例 #3
0
        PluralFormsNode EqualityExpression()
        {
            PluralFormsNode p = RelationalExpression();

            if (p == null)
            {
                return(null);
            }
            PluralFormsNode n = p;

            if (Token.TokenType == PluralFormsToken.Type.Equal || Token.TokenType == PluralFormsToken.Type.NotEqual)
            {
                PluralFormsNode qn = new PluralFormsNode(new PluralFormsToken(Token));
                if (!NextToken())
                {
                    return(null);
                }
                p = RelationalExpression();
                if (p == null)
                {
                    return(null);
                }
                qn.SetNode(1, p);
                qn.SetNode(0, n);
                return(qn);
            }
            return(n);
        }
コード例 #4
0
        public PluralFormsNode ReleaseNode(int i)
        {
            PluralFormsNode node = nodes[i];

            nodes[i] = null;
            return(node);
        }
コード例 #5
0
        PluralFormsNode RelationalExpression()
        {
            PluralFormsNode p = MultiplicativeExpression();

            if (p == null)
            {
                return(null);
            }
            PluralFormsNode n = p;

            if (Token.TokenType == PluralFormsToken.Type.Greater ||
                Token.TokenType == PluralFormsToken.Type.Less ||
                Token.TokenType == PluralFormsToken.Type.GreaterOrEqual ||
                Token.TokenType == PluralFormsToken.Type.LessOrEqual)
            {
                PluralFormsNode qn = new PluralFormsNode(new PluralFormsToken(Token));
                if (!NextToken())
                {
                    return(null);
                }
                p = MultiplicativeExpression();
                if (p == null)
                {
                    return(null);
                }
                qn.SetNode(1, p);
                qn.SetNode(0, n);
                return(qn);
            }
            return(n);
        }
コード例 #6
0
 public static void IterateNodes(PluralFormsNode node, IterateNodesDelegate doBefore, IterateNodesDelegate doAfter)
 {
     doBefore(node);
     for (int i = 0; i < node.NodesCount; i++)
     {
         if (node.Nodes[i] != null)
         {
             IterateNodes(node.Nodes[i], doBefore, doAfter);
         }
     }
     doAfter(node);
 }
コード例 #7
0
        PluralFormsNode ParsePlural()
        {
            PluralFormsNode p = Expression();

            if (p == null)
            {
                return(null);
            }
            if (Token.TokenType != PluralFormsToken.Type.Semicolon)
            {
                return(null);
            }
            return(p);
        }
コード例 #8
0
        /// <summary>
        /// Evaluate the specified number, returns msgstr index.
        /// </summary>
        /// <param name='n'>
        /// Number to evaluate
        /// </param>
        /// <param name='traceToFile'>
        /// Debug purposes only. Trace to file an evaluation tree.
        /// </param>
        public long Evaluate(long n, bool traceToFile)
        {
            if (plural == null)
            {
                return(0);
            }
#if DEBUG
            RecursiveTracer tracer = new RecursiveTracer();
            tracer.Text.AppendFormat("Expression: {0}", expression);
            tracer.Text.AppendLine();
            tracer.Text.AppendFormat("Evaluate: {0}", n);
            tracer.Text.AppendLine();
            tracer.Text.AppendLine();
#endif
            long number = plural.Evaluate(n);
#if DEBUG
            PluralFormsNode.IterateNodes(
                plural,
                delegate(PluralFormsNode node)
            {
                tracer.Text.AppendFormat("{0}: ", tracer.Level++);
                tracer.Indent();
                if (node.Tracer != null)
                {
                    tracer.Text.AppendLine(node.Tracer.Text.ToString());
                }
            },
                delegate(PluralFormsNode node)
            {
                tracer.Level--;
            }
                );
            if (traceToFile)
            {
                tracer.SaveToFile("Evaluations.txt");
            }
#endif
            if (number < 0 || number > nplurals)
            {
                return(0);
            }
            return(number);
        }
コード例 #9
0
        PluralFormsNode Expression()
        {
            PluralFormsNode p = LogicalOrExpression();

            if (p == null)
            {
                return(null);
            }
            PluralFormsNode n = p;

            if (Token.TokenType == PluralFormsToken.Type.Question)
            {
                PluralFormsNode qn = new PluralFormsNode(new PluralFormsToken(Token));
                if (!NextToken())
                {
                    return(null);
                }
                p = Expression();
                if (p == null)
                {
                    return(null);
                }
                qn.SetNode(1, p);
                if (Token.TokenType != PluralFormsToken.Type.Colon)
                {
                    return(null);
                }
                if (!NextToken())
                {
                    return(null);
                }
                p = Expression();
                if (p == null)
                {
                    return(null);
                }
                qn.SetNode(2, p);
                qn.SetNode(0, n);
                return(qn);
            }
            return(n);
        }
コード例 #10
0
        PluralFormsNode LogicalAndExpression()
        {
            PluralFormsNode p = EqualityExpression();

            if (p == null)
            {
                return(null);
            }
            PluralFormsNode ln = p;             // left

            if (Token.TokenType == PluralFormsToken.Type.LogicalAnd)
            {
                PluralFormsNode un = new PluralFormsNode(new PluralFormsToken(Token));                 // up
                if (!NextToken())
                {
                    return(null);
                }
                p = LogicalAndExpression();
                if (p == null)
                {
                    return(null);
                }
                PluralFormsNode rn = p;                 // right
                if (rn.Token.TokenType == PluralFormsToken.Type.LogicalAnd)
                {
                    // transform 1 && (2 && 3) -> (1 && 2) && 3
                    //     u                  r
                    // l       r     ->   u      3
                    //       2   3      l   2
                    un.SetNode(0, ln);
                    un.SetNode(1, rn.ReleaseNode(0));
                    rn.SetNode(0, un);
                    return(rn);
                }

                un.SetNode(0, ln);
                un.SetNode(1, rn);
                return(un);
            }
            return(ln);
        }
コード例 #11
0
        PluralFormsNode PmExpression()
        {
            PluralFormsNode n;

            if (Token.TokenType == PluralFormsToken.Type.N || Token.TokenType == PluralFormsToken.Type.Number)
            {
                n = new PluralFormsNode(new PluralFormsToken(Token));
                if (!NextToken())
                {
                    return(null);
                }
            }
            else if (Token.TokenType == PluralFormsToken.Type.LeftBracket)
            {
                if (!NextToken())
                {
                    return(null);
                }
                PluralFormsNode p = Expression();
                if (p == null)
                {
                    return(null);
                }
                n = p;
                if (Token.TokenType != PluralFormsToken.Type.RightBracket)
                {
                    return(null);
                }
                if (!NextToken())
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
            return(n);
        }
コード例 #12
0
        PluralFormsNode LogicalOrExpression()
        {
            PluralFormsNode p = LogicalAndExpression();

            if (p == null)
            {
                return(null);
            }
            PluralFormsNode ln = p;

            if (Token.TokenType == PluralFormsToken.Type.LogicalOr)
            {
                PluralFormsNode un = new PluralFormsNode(new PluralFormsToken(Token));
                if (!NextToken())
                {
                    return(null);
                }
                p = LogicalOrExpression();
                if (p == null)
                {
                    return(null);
                }
                PluralFormsNode rn = p;                 // right
                if (rn.Token.TokenType == PluralFormsToken.Type.LogicalOr)
                {
                    // see logicalAndExpression comment
                    un.SetNode(0, ln);
                    un.SetNode(1, rn.ReleaseNode(0));
                    rn.SetNode(0, un);
                    return(rn);
                }

                un.SetNode(0, ln);
                un.SetNode(1, rn);
                return(un);
            }
            return(ln);
        }
コード例 #13
0
        public void DumpNodes(string fileName)
        {
            if (plural != null)
            {
                RecursiveTracer tracer = new RecursiveTracer();
                tracer.Text.Append(expression);
                tracer.Text.AppendLine();

                PluralFormsNode.IterateNodes(
                    plural,
                    delegate(PluralFormsNode node)
                {
                    tracer.Text.AppendFormat("{0}: ", tracer.Level++);
                    tracer.Indent();
                    tracer.Text.AppendLine(node.ToString());
                },
                    delegate(PluralFormsNode node)
                {
                    tracer.Level--;
                }
                    );
                tracer.SaveToFile(fileName);
            }
        }
コード例 #14
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 PluralFormsNode Expression()
 {
     PluralFormsNode p = LogicalOrExpression();
     if (p == null)
         return null;
     PluralFormsNode n = p;
     if (Token.TokenType == PluralFormsToken.Type.Question) {
         PluralFormsNode qn = new PluralFormsNode(new PluralFormsToken(Token));
         if (! NextToken()) {
             return null;
         }
         p = Expression();
         if (p == null) {
             return null;
         }
         qn.SetNode(1, p);
         if (Token.TokenType != PluralFormsToken.Type.Colon) {
             return null;
         }
         if (! NextToken()) {
             return null;
         }
         p = Expression();
         if (p == null) {
             return null;
         }
         qn.SetNode(2, p);
         qn.SetNode(0, n);
         return qn;
     }
     return n;
 }
コード例 #15
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 PluralFormsNode EqualityExpression()
 {
     PluralFormsNode p = RelationalExpression();
     if (p == null)
         return null;
     PluralFormsNode n = p;
     if (Token.TokenType == PluralFormsToken.Type.Equal || Token.TokenType == PluralFormsToken.Type.NotEqual) {
         PluralFormsNode qn = new PluralFormsNode(new PluralFormsToken(Token));
         if (! NextToken()) {
             return null;
         }
         p = RelationalExpression();
         if (p == null) {
             return null;
         }
         qn.SetNode(1, p);
         qn.SetNode(0, n);
         return qn;
     }
     return n;
 }
コード例 #16
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 public void SetNode(int i, PluralFormsNode n)
 {
     if (i >= 0 && i <= 2)
         nodes[i] = n;
 }
コード例 #17
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 public static void IterateNodes(PluralFormsNode node, IterateNodesDelegate doBefore, IterateNodesDelegate doAfter)
 {
     doBefore(node);
     for(int i = 0; i < node.NodesCount; i++)
     {
         if (node.Nodes[i] != null)
             IterateNodes(node.Nodes[i], doBefore, doAfter);
     }
     doAfter(node);
 }
コード例 #18
0
        public bool Parse(PluralFormsCalculator calculator)
        {
            if (Token.TokenType != PluralFormsToken.Type.Nplurals)
            {
                return(false);
            }
            if (!NextToken())
            {
                return(false);
            }
            if (Token.TokenType != PluralFormsToken.Type.Assign)
            {
                return(false);
            }
            if (!NextToken())
            {
                return(false);
            }
            if (Token.TokenType != PluralFormsToken.Type.Number)
            {
                return(false);
            }
            int nplurals = Token.Number;

            if (!NextToken())
            {
                return(false);
            }
            if (Token.TokenType != PluralFormsToken.Type.Semicolon)
            {
                return(false);
            }
            if (!NextToken())
            {
                return(false);
            }
            if (Token.TokenType != PluralFormsToken.Type.Plural)
            {
                return(false);
            }
            if (!NextToken())
            {
                return(false);
            }
            if (Token.TokenType != PluralFormsToken.Type.Assign)
            {
                return(false);
            }
            if (!NextToken())
            {
                return(false);
            }
            PluralFormsNode plural = ParsePlural();

            if (plural == null)
            {
                return(false);
            }
            if (Token.TokenType != PluralFormsToken.Type.Semicolon)
            {
                return(false);
            }
            if (!NextToken())
            {
                return(false);
            }
            if (Token.TokenType != PluralFormsToken.Type.Eof)
            {
                return(false);
            }
            calculator.Init(nplurals, plural);
            return(true);
        }
コード例 #19
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 PluralFormsNode PmExpression()
 {
     PluralFormsNode n;
     if (Token.TokenType == PluralFormsToken.Type.N || Token.TokenType == PluralFormsToken.Type.Number) {
         n = new PluralFormsNode(new PluralFormsToken(Token));
         if (! NextToken()) {
             return null;
         }
     }
     else if (Token.TokenType == PluralFormsToken.Type.LeftBracket) {
         if (! NextToken()) {
             return null;
         }
         PluralFormsNode p = Expression();
         if (p == null) {
             return null;
         }
         n = p;
         if (Token.TokenType != PluralFormsToken.Type.RightBracket) {
             return null;
         }
         if (! NextToken()) {
             return null;
         }
     }
     else {
         return null;
     }
     return n;
 }
コード例 #20
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 public PluralFormsCalculator(string expression)
 {
     nplurals = 0;
     plural = null;
     this.expression = expression;
 }
コード例 #21
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
        PluralFormsNode LogicalAndExpression()
        {
            PluralFormsNode p = EqualityExpression();
            if (p == null)
                return null;
            PluralFormsNode ln = p; // left
            if (Token.TokenType == PluralFormsToken.Type.LogicalAnd) {
                PluralFormsNode un = new PluralFormsNode(new PluralFormsToken(Token)); // up
                if (! NextToken()) {
                    return null;
                }
                p = LogicalAndExpression();
                if (p == null) {
                    return null;
                }
                PluralFormsNode rn = p; // right
                if (rn.Token.TokenType == PluralFormsToken.Type.LogicalAnd) {
                    // transform 1 && (2 && 3) -> (1 && 2) && 3
                    //     u                  r
                    // l       r     ->   u      3
                    //       2   3      l   2
                    un.SetNode(0, ln);
                    un.SetNode(1, rn.ReleaseNode(0));
                    rn.SetNode(0, un);
                    return rn;
                }

                un.SetNode(0, ln);
                un.SetNode(1, rn);
                return un;
            }
            return ln;
        }
コード例 #22
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
        PluralFormsNode LogicalOrExpression()
        {
            PluralFormsNode p = LogicalAndExpression();
            if (p == null)
                return null;
            PluralFormsNode ln = p;
            if (Token.TokenType == PluralFormsToken.Type.LogicalOr) {
                PluralFormsNode un = new PluralFormsNode(new PluralFormsToken(Token));
                if (! NextToken()) {
                    return null;
                }
                p = LogicalOrExpression();
                if (p == null) {
                    return null;
                }
                PluralFormsNode rn = p; // right
                if (rn.Token.TokenType == PluralFormsToken.Type.LogicalOr) {
                    // see logicalAndExpression comment
                    un.SetNode(0, ln);
                    un.SetNode(1, rn.ReleaseNode(0));
                    rn.SetNode(0, un);
                    return rn;
                }

                un.SetNode(0, ln);
                un.SetNode(1, rn);
                return un;
            }
            return ln;
        }
コード例 #23
0
 public PluralFormsCalculator(string expression)
 {
     nplurals        = 0;
     plural          = null;
     this.expression = expression;
 }
コード例 #24
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 PluralFormsNode MultiplicativeExpression()
 {
     PluralFormsNode p = PmExpression();
     if (p == null) {
         return null;
     }
     PluralFormsNode n = p;
     if (Token.TokenType == PluralFormsToken.Type.Reminder) {
         PluralFormsNode qn = new PluralFormsNode(new PluralFormsToken(Token));
         if (! NextToken()) {
             return null;
         }
         p = PmExpression();
         if (p == null) {
             return null;
         }
         qn.SetNode(1, p);
         qn.SetNode(0, n);
         return qn;
     }
     return n;
 }
コード例 #25
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 internal void Init(int nplurals, PluralFormsNode plural)
 {
     this.nplurals = nplurals;
     this.plural = plural;
 }
コード例 #26
0
ファイル: PluralForms.cs プロジェクト: rzaitov/GetTextNet
 PluralFormsNode RelationalExpression()
 {
     PluralFormsNode p = MultiplicativeExpression();
     if (p == null)
         return null;
     PluralFormsNode n = p;
     if (Token.TokenType == PluralFormsToken.Type.Greater
         || Token.TokenType == PluralFormsToken.Type.Less
         || Token.TokenType == PluralFormsToken.Type.GreaterOrEqual
         || Token.TokenType == PluralFormsToken.Type.LessOrEqual) {
         PluralFormsNode qn = new PluralFormsNode(new PluralFormsToken(Token));
         if (! NextToken()) {
             return null;
         }
         p = MultiplicativeExpression();
         if (p == null) {
             return null;
         }
         qn.SetNode(1, p);
         qn.SetNode(0, n);
         return qn;
     }
     return n;
 }
コード例 #27
0
 internal void Init(int nplurals, PluralFormsNode plural)
 {
     this.nplurals = nplurals;
     this.plural   = plural;
 }