Ejemplo n.º 1
0
        /**
         * Collects the array of <c>Ptg</c> Tokens for the specified tree.
         */
        public static Ptg[] ToTokenArray(ParseNode rootNode)
        {
            TokenCollector temp = new TokenCollector(rootNode.TokenCount);

            rootNode.CollectPtgs(temp);
            return(temp.GetResult());
        }
Ejemplo n.º 2
0
        public void Parse(string text, TokenCollector collect, int position = 0, bool exploreValues = true, CancellationTokenSource cts = null)
        {
            void Collect(Group match, int style, int offset = 0)
            {
                CollectDynamicStyle(match, x => style, offset);
            }

            void CollectDynamicStyle(Group match, Func <string, int> style, int offset = 0)
            {
                if (!match.Success)
                {
                    return;
                }
                collect(match.Value, position + offset + match.Index, style(match.Value));
            }

            // multi line comment     \s*(?'mcomment'\/\*[\w\W]*?\*\/) |

            var matches = Regex.Matches(text, @"
(?:
    \s*(?'comment';.*) |
    \s*(?'section'\[[^\]\r\n]+\])\s*(?'comment';.*)? |
    \s*(?'key'[^;=\r\n]+)\s*=\s*(?'value'[^;\r\n]+)?(?'comment';.*)? |
    \s*(?'unknown'.+)
)
", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);

            foreach (Match m in matches)
            {
                if (cts != null && cts.IsCancellationRequested)
                {
                    return;
                }
                Collect(m.Groups["mcomment"], StyleComment);
                Collect(m.Groups["comment"], StyleComment);
                Collect(m.Groups["section"], StyleSection);
                Collect(m.Groups["key"], StyleKey);
                Collect(m.Groups["unknown"], StyleDefault);

                var value = m.Groups["value"];
                Collect(value, StyleValue);

                if (exploreValues && value.Success && value.Value.IndexOf('{') == -1) // not is locomoto/guid value
                {
                    Regex.Replace(value.Value, @"\b(?:(?'bool'true|false|yes|no)|(?'number'\d+\.\d+|\.\d+|\d+))\b", vm =>
                    {
                        Collect(vm.Groups["bool"], StyleBoolean, value.Index);
                        Collect(vm.Groups["number"], StyleNumber, value.Index);
                        return(null);
                    });
                }
            }
        }
Ejemplo n.º 3
0
 private void CollectPtgs(TokenCollector temp)
 {
     if (IsIf(this.Token))
     {
         CollectIfPtgs(temp);
         return;
     }
     for (int i = 0; i < Children.Length; i++)
     {
         Children[i].CollectPtgs(temp);
     }
     temp.Add(this.Token);
 }
Ejemplo n.º 4
0
 private void CollectPtgs(TokenCollector temp)
 {
     if (IsIf(this.Token))
     {
         CollectIfPtgs(temp);
         return;
     }
     for (int i = 0; i < Children.Length; i++)
     {
         Children[i].CollectPtgs(temp);
     }
     temp.Add(this.Token);
 }
Ejemplo n.º 5
0
 private void CollectPtgs(TokenCollector temp)
 {
     if (IsIf(GetToken()))
     {
         CollectIfPtgs(temp);
         return;
     }
     for (int i = 0; i < GetChildren().Length; i++)
     {
         GetChildren()[i].CollectPtgs(temp);
     }
     temp.Add(GetToken());
 }
Ejemplo n.º 6
0
        /**
         * The IF() function Gets marked up with two or three tAttr Tokens.
         * Similar logic will be required for CHOOSE() when it is supported
         *
         * See excelfileformat.pdf sec 3.10.5 "tAttr (19H)
         */
        private void CollectIfPtgs(TokenCollector temp)
        {
            // condition goes first
            GetChildren()[0].CollectPtgs(temp);

            // placeholder for tAttrIf
            int ifAttrIndex = temp.CreatePlaceholder();

            // true parameter
            GetChildren()[1].CollectPtgs(temp);

            // placeholder for first skip attr
            int skipAfterTrueParamIndex = temp.CreatePlaceholder();
            int trueParamSize           = temp.sumTokenSizes(ifAttrIndex + 1, skipAfterTrueParamIndex);

            AttrPtg attrIf = AttrPtg.CreateIf(trueParamSize + 4);// distance to start of false parameter/tFuncVar. +4 for tAttrSkip after true

            if (GetChildren().Length > 2)
            {
                // false param present

                // false parameter
                GetChildren()[2].CollectPtgs(temp);

                int skipAfterFalseParamIndex = temp.CreatePlaceholder();
                int falseParamSize           = temp.sumTokenSizes(skipAfterTrueParamIndex + 1, skipAfterFalseParamIndex);

                AttrPtg attrSkipAfterTrue  = AttrPtg.CreateSkip(falseParamSize + 4 + 4 - 1); // 1 less than distance to end of if FuncVar(size=4). +4 for attr skip before
                AttrPtg attrSkipAfterFalse = AttrPtg.CreateSkip(4 - 1);                      // 1 less than distance to end of if FuncVar(size=4).

                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
                temp.SetPlaceholder(skipAfterFalseParamIndex, attrSkipAfterFalse);
            }
            else
            {
                // false parameter not present
                AttrPtg attrSkipAfterTrue = AttrPtg.CreateSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).

                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
            }

            temp.Add(GetToken());
        }
Ejemplo n.º 7
0
        private void CollectPtgs(TokenCollector temp)
        {
            if (IsIf(_token))
            {
                CollectIfPtgs(temp);
                return;
            }
            bool isPreFixOperator = _token is MemFuncPtg || _token is MemAreaPtg;

            if (isPreFixOperator)
            {
                temp.Add(_token);
            }
            for (int i = 0; i < GetChildren().Length; i++)
            {
                GetChildren()[i].CollectPtgs(temp);
            }
            if (!isPreFixOperator)
            {
                temp.Add(_token);
            }
        }
Ejemplo n.º 8
0
 /**
  * Collects the array of <c>Ptg</c> Tokens for the specified tree.
  */
 public static Ptg[] ToTokenArray(ParseNode rootNode)
 {
     TokenCollector temp = new TokenCollector(rootNode.TokenCount);
     rootNode.CollectPtgs(temp);
     return temp.GetResult();
 }
Ejemplo n.º 9
0
        /**
         * The IF() function Gets marked up with two or three tAttr Tokens.
         * Similar logic will be required for CHOOSE() when it is supported
         * 
         * See excelfileformat.pdf sec 3.10.5 "tAttr (19H)
         */
        private void CollectIfPtgs(TokenCollector temp)
        {

            // condition goes first
            GetChildren()[0].CollectPtgs(temp);

            // placeholder for tAttrIf
            int ifAttrIndex = temp.CreatePlaceholder();

            // true parameter
            GetChildren()[1].CollectPtgs(temp);

            // placeholder for first skip attr
            int skipAfterTrueParamIndex = temp.CreatePlaceholder();
            int trueParamSize = temp.sumTokenSizes(ifAttrIndex + 1, skipAfterTrueParamIndex);

            AttrPtg attrIf = AttrPtg.CreateIf(trueParamSize + 4);// distance to start of false parameter/tFuncVar. +4 for tAttrSkip after true

            if (GetChildren().Length > 2)
            {
                // false param present

                // false parameter
                GetChildren()[2].CollectPtgs(temp);

                int skipAfterFalseParamIndex = temp.CreatePlaceholder();
                int falseParamSize = temp.sumTokenSizes(skipAfterTrueParamIndex + 1, skipAfterFalseParamIndex);

                AttrPtg attrSkipAfterTrue = AttrPtg.CreateSkip(falseParamSize + 4 + 4 - 1); // 1 less than distance to end of if FuncVar(size=4). +4 for attr skip before
                AttrPtg attrSkipAfterFalse = AttrPtg.CreateSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).

                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
                temp.SetPlaceholder(skipAfterFalseParamIndex, attrSkipAfterFalse);
            }
            else
            {
                // false parameter not present
                AttrPtg attrSkipAfterTrue = AttrPtg.CreateSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).

                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
            }

            temp.Add(GetToken());
        }
Ejemplo n.º 10
0
        private void CollectPtgs(TokenCollector temp)
        {
            if (IsIf(_token))
            {
                CollectIfPtgs(temp);
                return;
            }
            bool isPreFixOperator = _token is MemFuncPtg || _token is MemAreaPtg;
		    if (isPreFixOperator) {
			    temp.Add(_token);
		    }
            for (int i = 0; i < GetChildren().Length; i++)
            {
                GetChildren()[i].CollectPtgs(temp);
            }
            if(!isPreFixOperator)
            {
                temp.Add(_token);
            }
        }
Ejemplo n.º 11
0
 private void CollectPtgs(TokenCollector temp)
 {
     if (IsIf(GetToken()))
     {
         CollectIfPtgs(temp);
         return;
     }
     for (int i = 0; i < GetChildren().Length; i++)
     {
         GetChildren()[i].CollectPtgs(temp);
     }
     temp.Add(GetToken());
 }
Ejemplo n.º 12
0
        /**
         * The IF() function Gets marked up with two or three tAttr Tokens.
         * Similar logic will be required for CHOOSE() when it Is supported
         * 
         * See excelfileformat.pdf sec 3.10.5 "tAttr (19H)
         */
        private void collectIfPtgs(TokenCollector temp)
        {

            // condition goes first
            GetChildren()[0].collectPtgs(temp);

            // placeholder for tAttrIf
            int ifAttrIndex = temp.CreatePlaceholder();

            // true parameter
            GetChildren()[1].collectPtgs(temp);

            // placeholder for first skip attr
            int skipAfterTrueParamIndex = temp.CreatePlaceholder();
            int trueParamSize = temp.sumTokenSizes(ifAttrIndex + 1, skipAfterTrueParamIndex);

            AttrPtg attrIf = new AttrPtg();
            attrIf.IsOptimizedIf=(true);
            AttrPtg attrSkipAfterTrue = new AttrPtg();
            attrSkipAfterTrue.IsGoto=(true);

            if (GetChildren().Length > 2)
            {
                // false param present

                // false parameter
                GetChildren()[2].collectPtgs(temp);

                int skipAfterFalseParamIndex = temp.CreatePlaceholder();

                AttrPtg attrSkipAfterFalse = new AttrPtg();
                attrSkipAfterFalse.IsGoto=(true);

                int falseParamSize = temp.sumTokenSizes(skipAfterTrueParamIndex + 1, skipAfterFalseParamIndex);

                attrIf.Data=((short)(trueParamSize + 4)); // distance To start of false parameter. +4 for skip after true
                attrSkipAfterTrue.Data=((short)(falseParamSize + 4 + 4 - 1)); // 1 less than distance To end of if FuncVar(size=4). +4 for attr skip before 
                attrSkipAfterFalse.Data=((short)(4 - 1)); // 1 less than distance To end of if FuncVar(size=4). 

                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
                temp.SetPlaceholder(skipAfterFalseParamIndex, attrSkipAfterFalse);
            }
            else
            {
                // false parameter not present
                attrIf.Data=((short)(trueParamSize + 4)); // distance To start of FuncVar. +4 for skip after true
                attrSkipAfterTrue.Data=((short)(4 - 1)); // 1 less than distance To end of if FuncVar(size=4). 

                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
            }

            temp.Add(GetToken());
        }