Balance() public static method

public static Balance ( string &str, int &i, char closeChar ) : bool
str string
i int
closeChar char
return bool
コード例 #1
0
ファイル: ExpressionTerm.cs プロジェクト: yadenisyur/KOS
        private List <String> splitByListIgnoreBracket(String input, ref List <String> operators, bool returnIfOneElement)
        {
            String        buffer = "";
            String        s;
            List <String> retList = new List <string>();

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == '(')
                {
                    int startI = i;
                    Utils.Balance(ref Text, ref i, ')');
                    buffer += Text.Substring(startI, i - startI + 1);
                }
                else if (input[i] == '"')
                {
                    int startI = i;
                    i       = Utils.FindEndOfString(Text, i + 1);
                    buffer += Text.Substring(startI, i - startI + 1);
                }
                else
                {
                    s = MatchAt(ref input, i, operators);

                    if (s != null)
                    {
                        // TODO: If buffer empty, syntax error

                        retList.Add(buffer);
                        retList.Add(s);
                        buffer = "";

                        i += s.Length - 1;
                    }
                    else
                    {
                        buffer += input[i];
                    }
                }
            }

            if (buffer.Trim() != "")
            {
                retList.Add(buffer);
            }

            if (returnIfOneElement)
            {
                return(retList);
            }
            else
            {
                return(retList.Count > 1 ? retList : null);
            }
        }
コード例 #2
0
ファイル: ExpressionTerm.cs プロジェクト: yadenisyur/KOS
        private void processSymbols()
        {
            // Is the input empty?
            if (String.IsNullOrEmpty(Text))
            {
                return;
            }

            // HEADING.. BY is now deprecated in favor of HEADING(x,y), but here it is if you're using it still
            Text = Regex.Replace(Text, "HEADING ([ :@A-Za-z0-9\\.\\-\\+\\*/]+) BY ([ :@A-Za-z0-9\\.\\-\\+\\*/]+)", "HEADING($2,$1)", RegexOptions.IgnoreCase);

            //enables scientific notation eg 6.6E-11 -> 6.6*10^-11
            Text = Regex.Replace(Text, "(\\d)E([-+]{1}[0-9]+)", "$1*10^$2");

            // Resource tags are now deprecated in favor of SHIP:ResourceName
            Text = Regex.Replace(Text, "(\\s|^)<([a-zA-Z]+)>(\\s|$)", " SHIP:$2 ", RegexOptions.IgnoreCase);

            // Is this JUST a matched symbol?
            String s = MatchAt(ref Text, 0, allSymbols);

            if (s != null && Text.Length == s.Length)
            {
                if (mathSymbols.Contains(s))
                {
                    Type = TermTypes.MATH_OPERATOR;
                }
                else if (comparisonSymbols.Contains(s))
                {
                    Type = TermTypes.COMPARISON_OPERATOR;
                }
                else if (booleanSymbols.Contains(s))
                {
                    Type = TermTypes.BOOLEAN_OPERATOR;
                }

                return;
            }

            SubTerms = new List <Term>();

            // If this is a parameter list, grab the parameters
            if (Type == TermTypes.PARAMETER_LIST)
            {
                var parameterList = parseParameters(Text);
                if (parameterList != null)
                {
                    foreach (String param in parameterList)
                    {
                        SubTerms.Add(new Term(param));
                    }
                }

                return;
            }

            // Does this thing contain a boolean operation?
            var booleanElements = splitByListIgnoreBracket(Text, ref booleanSymbols);

            if (booleanElements != null)
            {
                Type = TermTypes.BOOLEAN;

                foreach (String element in booleanElements)
                {
                    if (booleanSymbols.Contains(element))
                    {
                        SubTerms.Add(new Term(element, TermTypes.BOOLEAN_OPERATOR));
                    }
                    else
                    {
                        SubTerms.Add(new Term(element));
                    }
                }

                return;
            }

            // Does this thing contain a comparison?
            var comparisonElements = splitByListIgnoreBracket(Text, ref comparisonSymbols);

            if (comparisonElements != null)
            {
                Type = TermTypes.COMPARISON;

                foreach (String element in comparisonElements)
                {
                    SubTerms.Add(new Term(element));
                }

                return;
            }

            // Does this thing contain an Index?
            var listElements = splitByListIgnoreBracket(Text, ref listaccessSymbols);

            if (listElements != null)
            {
                Type = TermTypes.INDEX;

                foreach (var element in listElements)
                {
                    if (listaccessSymbols.Contains(element))
                    {
                        SubTerms.Add(new Term(element, TermTypes.INDEX_OPERATOR));
                    }
                    else
                    {
                        SubTerms.Add(new Term(element));
                    }
                }

                return;
            }


            // Parse this as a normal term
            String buffer = "";

            for (int i = 0; i < Text.Length; i++)
            {
                s = MatchAt(ref Text, i, allSymbols);

                if (s == null)
                {
                    buffer += Text[i];
                }
                else if (s == "(")
                {
                    int startI = i;
                    Utils.Balance(ref Text, ref i, ')');

                    if (buffer.Trim() != "")
                    {
                        string functionName = buffer.Trim();
                        buffer = "";

                        Term bracketTerm  = new Term(Text.Substring(startI + 1, i - startI - 1), TermTypes.PARAMETER_LIST);
                        Term functionTerm = Merge(new Term(functionName), bracketTerm);
                        functionTerm.Type = TermTypes.FUNCTION;

                        SubTerms.Add(functionTerm);
                    }
                    else
                    {
                        SubTerms.Add(new Term(Text.Substring(startI + 1, i - startI - 1)));
                    }
                }
                else if (s == "\"")
                {
                    int startI = i;
                    i       = Utils.FindEndOfString(Text, i + 1);
                    buffer += Text.Substring(startI, i - startI + 1);
                }
                else if (s == ":")
                {
                    int    end        = findEndOfSuffix(Text, i + 1);
                    String suffixName = Text.Substring(i + 1, end - i);
                    i += end - i;

                    if (buffer.Trim() != "")
                    {
                        SubTerms.Add(new Term(buffer.Trim()));
                        buffer = "";
                    }

                    if (SubTerms.Count > 0)
                    {
                        Term last = SubTerms.Last();
                        SubTerms.Remove(last);

                        Term structureTerm = Merge(last, new Term(suffixName, TermTypes.SUFFIX));
                        structureTerm.Type = TermTypes.STRUCTURE;
                        SubTerms.Add(structureTerm);
                    }
                }
                else if (s == "-")
                {
                    if (buffer.Trim() != "" ||
                        (SubTerms.Count > 0 && SubTerms.Last().Type != TermTypes.MATH_OPERATOR &&
                         SubTerms.Last().Type != TermTypes.COMPARISON_OPERATOR))
                    {
                        // Not a sign, treat as operator
                        if (buffer.Trim() != "")
                        {
                            SubTerms.Add(new Term(buffer.Trim()));
                        }
                        SubTerms.Add(new Term(s));

                        buffer = "";
                        i     += s.Length - 1;
                    }
                    else
                    {
                        buffer += Text[i];
                    }
                }
                else
                {
                    if (buffer.Trim() != "")
                    {
                        SubTerms.Add(new Term(buffer.Trim()));
                    }
                    SubTerms.Add(new Term(s));

                    buffer = "";
                    i     += s.Length - 1;
                }
            }

            // If there's only one term, we're done!
            if (SubTerms.Count == 0)
            {
                Type = TermTypes.FINAL;
                return;
            }

            if (buffer.Trim() != "")
            {
                SubTerms.Add(new Term(buffer));
            }

            // If I end up with exactly one subTerm, then I AM that subterm. Exception: If I already have a special type
            if (SubTerms.Count == 1 && this.Type == TermTypes.REGULAR)
            {
                Term child = SubTerms[0];
                SubTerms.Clear();

                CopyFrom(ref child);
            }
        }