Ejemplo n.º 1
0
 /// <summary>
 /// Late build...
 /// </summary>
 /// <param name="engine"></param>
 /// <param name="sourceCode"></param>
 /// <param name="script"></param>
 /// <returns></returns>
 public virtual Token LateBuild(ScriptEngine engine, ref SourceCode sourceCode, ref Script script)
 {
     return null;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// This is called if condition match is true, and SimpleBuild is set to true. When called, the method should return a token that will be inserted into the call structure.
 /// </summary>
 /// <param name="lastToken"></param>
 /// <param name="engine"></param>
 /// <param name="script"></param>
 /// <param name="sourceCode"></param>
 /// <returns></returns>
 public virtual Token Build(Token lastToken, ScriptEngine engine, Script script, ref SourceCode sourceCode)
 {
     return null;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// ConditionMatch is used to see if this builder should be doing something. CurrentChar and Matches are good methods to use here.
 /// </summary>
 /// <param name="lastToken">The last token that was placed in the list of tokens. null if there was nothing before it.</param>
 /// <param name="sourceCode">The source code, has CurrentChar and Matches().</param>
 /// <returns></returns>
 public abstract bool ConditionMatch(Token lastToken, SourceCode sourceCode);
Ejemplo n.º 4
0
 /// <summary>
 /// This is called if condition match is true, and SimpleBuild is set to false, you can use this to build custom handlers to events, but shouldn't be added to the call structure.
 /// For example, a configuration handler would use AdvnacedBuild to get the details it needs from the code, and not have it listed in the call structure.
 /// 
 /// For an example of how this is used, look at ConfigBuilder.
 /// </summary>
 /// <seealso cref="ConfigBuilder"/>
 /// <param name="engine"></param>
 /// <param name="sourceCode"></param>
 /// <param name="script"></param>
 /// <returns></returns>
 public virtual bool AdvancedBuild(ScriptEngine engine, ref SourceCode sourceCode, ref Script script)
 {
     return true;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Generates tokens until the end character is reached. The only condition is that the code must not have any complex routines.<br />
        /// This method allows support for '= > < == != <= >= + - / * %' statements to be processed.
        /// </summary>
        /// <param name="sourceCode"></param>
        /// <param name="script"></param>
        /// <param name="endConditions"></param>
        /// <returns></returns>
        public List<List<Token>> BuildTokensAdvanced(ref SourceCode sourceCode, ref Script script, char[] endConditions)
        {
            var pb = new SortedDictionary<int, List<Pass>>(_postBuildOperations);
            _postBuildOperations = new SortedDictionary<int, List<Pass>>();

            int bCount = _builderQueue.Count;
            var currentLine = new List<List<Token>>();
            var ot = new List<Token>(_currentTokens);
            _currentTokens = new List<Token>();
            Token lastToken = null;
            build:
            while (!sourceCode.EOF && !endConditions.Contains(sourceCode.CurrentCode))
            {
                sourceCode++;
                while (sourceCode.SpecialChar)
                {
                    sourceCode++;
                }
                if ( /*sourceCode.CurrentCode == '=' || */sourceCode.CurrentCode == ';')
                {
                    while (_builderQueue.Count != 0)
                    {
                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode, ref script));
                    }
                    currentLine.Add(_currentTokens);
                    _currentTokens = new List<Token>();
                    if (sourceCode.CurrentCode == ';')
                    {
                        break;
                    }
                    continue;
                }
                int i;
                foreach (int weight in _customBuilders.Keys)
                {
                    for (i = 0; i < _customBuilders[weight].Count; i++)
                    {
                        if (_customBuilders[weight][i].ConditionMatch(lastToken, sourceCode))
                        {
                            if (_customBuilders[weight][i].SimpleBuild)
                            {
                                Builder builder = _customBuilders[weight][i];
                                if (builder.SplitBefore)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode,
                                                                                         ref script));
                                    }
                                    currentLine.Add(_currentTokens);
                                    _currentTokens = new List<Token>();
                                }
                                Token t = builder.Build(lastToken, this, script, ref sourceCode);
                                lastToken = t;
                                if (!(t is Token.DelayToken))
                                {
                                    t.Line = _line;
                                    _currentTokens.Add(t);
                                }
                                if (builder.SplitAfter)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode,
                                                                                         ref script));
                                    }
                                    currentLine.Add(_currentTokens);
                                    _currentTokens = new List<Token>();
                                }
                            }
                            else
                            {
                                _customBuilders[weight][i].AdvancedBuild(this, ref sourceCode, ref script);
                            }
                            sourceCode.Offset--;
                            goto build;
                        }
                    }
                }

                foreach (int weight in _internalBuilders.Keys)
                {
                    for (i = 0; i < _internalBuilders[weight].Count; i++)
                    {
                        if (_internalBuilders[weight][i].ConditionMatch(lastToken, sourceCode))
                        {
                            if (_internalBuilders[weight][i].SimpleBuild)
                            {
                                Builder builder = _internalBuilders[weight][i];
                                if (builder.SplitBefore)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode,
                                                                                         ref script));
                                    }
                                    currentLine.Add(_currentTokens);
                                    _currentTokens = new List<Token>();
                                }
                                Token t = builder.Build(lastToken, this, script, ref sourceCode);
                                lastToken = t;
                                if (!(t is Token.DelayToken))
                                {
                                    t.Line = _line;
                                    _currentTokens.Add(t);
                                }
                                if (builder.SplitAfter)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode,
                                                                                         ref script));
                                    }
                                    currentLine.Add(_currentTokens);
                                    _currentTokens = new List<Token>();
                                }
                            }
                            else
                            {
                                _internalBuilders[weight][i].AdvancedBuild(this, ref sourceCode, ref script);
                            }
                            sourceCode.Offset--;
                            goto build;
                        }
                    }
                }
            }
            if (bCount < _builderQueue.Count)
            {
                while (_builderQueue.Count > bCount)
                {
                    _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode, ref script));
                }
            }
            if (_currentTokens.Count != 0)
            {
                currentLine.Add(_currentTokens);
            }
            _currentTokens = new List<Token>(ot);

            var tCode = new List<List<List<Token>>> {currentLine};
            foreach (int weight in _postBuildOperations.Keys)
            {
                for (int i = 0; i < _postBuildOperations[weight].Count; i++)
                {
                    _postBuildOperations[weight][i].DoPass(ref tCode);
                }
            }

            _postBuildOperations = pb;
            currentLine = tCode[0];

            return currentLine;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Generates tokens until the given character is reached. The only condition is that the code must not have any complex routines.<br />
        /// This method only works for single statements, not multiple ones (So no ='s). Use BuildLongTokens to get that.
        /// </summary>
        /// <param name="sourceCode">The code we are using, should indicate the starting point.</param>
        /// <param name="endConditions">The conditions that will cause this method to return what it is working on.</param>
        /// <returns></returns>
        public List<Token> BuildTokens(ref SourceCode sourceCode, ref Script script, char[] endConditions)
        {
            List<List<Token>> code = BuildTokensAdvanced(ref sourceCode, ref script, endConditions);

            if (code.Count == 1)
            {
                return code[0];
            }

            throw new Exception("...");
        }
Ejemplo n.º 7
0
        public List<List<List<Token>>> BuildLongTokens(ref SourceCode sourceCode, ref Script script,
                                                       char[] endConditions)
        {
            var pb = new SortedDictionary<int, List<Pass>>(_postBuildOperations);
            _postBuildOperations = new SortedDictionary<int, List<Pass>>();

            var s = new List<List<List<Token>>>();
            var currentLine = new List<List<Token>>();

            var ot = new List<Token>(_currentTokens);
            _currentTokens = new List<Token>();

            Token lastToken = null;
            build:
            while (++sourceCode.Offset < sourceCode.Script.Length
                /* && !endConditions.Contains(sourceCode.CurrentCode)*/)
            {
                while (sourceCode.SpecialChar)
                {
                    sourceCode.Offset++;
                }

                if (endConditions.Contains(sourceCode.CurrentCode))
                {
                    break;
                }

                char code = sourceCode.CurrentCode;

                if ( /*code == '=' || */code == ';')
                {
                    while (_builderQueue.Count != 0)
                    {
                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode, ref script));
                    }
                    if (_currentTokens.Count != 0)
                    {
                        currentLine.Add(_currentTokens);
                        _currentTokens = new List<Token>();
                        if (code == ';')
                        {
                            s.Add(currentLine);
                            currentLine = new List<List<Token>>();
                        }
                    }
                    continue;
                }

                // Run though the custom builders.
                int i;

                foreach (int weight in _customBuilders.Keys)
                {
                    for (i = 0; i < _customBuilders[weight].Count; i++)
                    {
                        if (_customBuilders[weight][i].ConditionMatch(lastToken, sourceCode))
                        {
                            if (_customBuilders[weight][i].SimpleBuild)
                            {
                                Builder builder = _customBuilders[weight][i];
                                if (builder.SplitBefore)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode,
                                                                                         ref script));
                                    }
                                    if (_currentTokens.Count != 0)
                                    {
                                        currentLine.Add(_currentTokens);
                                        _currentTokens = new List<Token>();
                                    }
                                }
                                Token t = builder.Build(lastToken, this, script, ref sourceCode);
                                lastToken = t;
                                if (!(t is Token.DelayToken))
                                {
                                    t.Line = _line;
                                    _currentTokens.Add(t);
                                }
                                if (builder.SplitAfter || builder.NewLineAfter)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode,
                                                                                         ref script));
                                    }
                                    if (_currentTokens.Count != 0)
                                    {
                                        currentLine.Add(_currentTokens);
                                        _currentTokens = new List<Token>();
                                    }
                                }
                            }
                            else
                            {
                                _customBuilders[weight][i].AdvancedBuild(this, ref sourceCode, ref script);
                            }
                            sourceCode.Offset--;
                            goto build;
                        }
                    }
                }

                foreach (int weight in _internalBuilders.Keys)
                {
                    for (i = 0; i < _internalBuilders[weight].Count; i++)
                    {
                        if (_internalBuilders[weight][i].ConditionMatch(lastToken, sourceCode))
                        {
                            if (_internalBuilders[weight][i].SimpleBuild)
                            {
                                Builder builder = _internalBuilders[weight][i];
                                if (builder.SplitBefore || builder.PlaceOnNewLine)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode,
                                                                                         ref script));
                                    }
                                    if (_currentTokens.Count != 0)
                                    {
                                        currentLine.Add(_currentTokens);
                                        _currentTokens = new List<Token>();
                                    }
                                    if (builder.PlaceOnNewLine && currentLine.Count != 0)
                                    {
                                        s.Add(currentLine);
                                        currentLine = new List<List<Token>>();
                                    }
                                }
                                Token t = builder.Build(lastToken, this, script, ref sourceCode);
                                lastToken = t;
                                if (!(t is Token.DelayToken))
                                {
                                    t.Line = _line;
                                    _currentTokens.Add(t);
                                }
                                if (builder.SplitAfter || builder.NewLineAfter)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref sourceCode,
                                                                                         ref script));
                                    }
                                    if (_currentTokens.Count != 0)
                                    {
                                        currentLine.Add(_currentTokens);
                                        _currentTokens = new List<Token>();
                                    }
                                    if (builder.NewLineAfter && currentLine.Count != 0)
                                    {
                                        s.Add(currentLine);
                                        currentLine = new List<List<Token>>();
                                    }
                                }
                            }
                            else
                            {
                                _internalBuilders[weight][i].AdvancedBuild(this, ref sourceCode, ref script);
                            }
                            sourceCode.Offset--;
                            goto build;
                        }
                    }
                }
            }

            foreach (int weight in _postBuildOperations.Keys)
            {
                for (int i = 0; i < _postBuildOperations[weight].Count; i++)
                {
                    _postBuildOperations[weight][i].DoPass(ref s);
                }
            }

            _currentTokens = ot;
            _postBuildOperations = pb;

            return s;
        }
Ejemplo n.º 8
0
        public Script Build(string source)
        {
            var script = new SourceCode {Offset = -1, Script = source};
            _line = 1;
            script.NewLine += new SourceCode.NLine(ScriptNewLine);

            var s = new Script(this);

            var currentLine = new List<List<Token>>();
            _currentTokens = new List<Token>();
            _postBuildOperations = new SortedDictionary<int, List<Pass>>();

            Token lastToken = null;
            build:
            while ((++script).Offset < script.Script.Length)
            {
                while (!script.EOF && char.IsWhiteSpace(script.Script[script.Offset]))
                {
                    script.Offset++;
                }
                if (script.EOF)
                {
                    break;
                }

                char code = script.Script[script.Offset];

                if ( /*code == '=' || */code == ';')
                {
                    while (_builderQueue.Count != 0)
                    {
                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref script, ref s));
                    }
                    if (_currentTokens.Count != 0)
                    {
                        currentLine.Add(_currentTokens);
                        _currentTokens = new List<Token>();
                        if (code == ';')
                        {
                            s.AddTokens(currentLine);
                            currentLine = new List<List<Token>>();
                            lastToken = null;
                        }
                    }
                    continue;
                }

                // Run though the custom builders.
                int i;
                foreach (int weight in _customBuilders.Keys)
                {
                    for (i = 0; i < _customBuilders[weight].Count; i++)
                    {
                        if (_customBuilders[weight][i].ConditionMatch(lastToken, script))
                        {
                            if (_customBuilders[weight][i].SimpleBuild)
                            {
                                Builder builder = _customBuilders[weight][i];
                                if (builder.SplitBefore)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref script, ref s));
                                    }
                                    if (_currentTokens.Count != 0)
                                    {
                                        currentLine.Add(_currentTokens);
                                        _currentTokens = new List<Token>();
                                    }
                                }
                                Token t = builder.Build(lastToken, this, s, ref script);
                                lastToken = t;
                                if (!(t is Token.DelayToken))
                                {
                                    t.Line = _line;
                                    _currentTokens.Add(t);
                                }
                                if (builder.SplitAfter || builder.NewLineAfter)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref script, ref s));
                                    }
                                    if (_currentTokens.Count != 0)
                                    {
                                        currentLine.Add(_currentTokens);
                                        _currentTokens = new List<Token>();
                                    }
                                }
                            }
                            else
                            {
                                _customBuilders[weight][i].AdvancedBuild(this, ref script, ref s);
                            }
                            script.Offset--;
                            goto build;
                        }
                    }
                }

                foreach (int weight in _internalBuilders.Keys)
                {
                    for (i = 0; i < _internalBuilders[weight].Count; i++)
                    {
                        if (_internalBuilders[weight][i].ConditionMatch(lastToken, script))
                        {
                            if (_internalBuilders[weight][i].SimpleBuild)
                            {
                                Builder builder = _internalBuilders[weight][i];
                                if (builder.SplitBefore || builder.PlaceOnNewLine)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref script, ref s));
                                    }
                                    if (_currentTokens.Count != 0)
                                    {
                                        currentLine.Add(_currentTokens);
                                        _currentTokens = new List<Token>();
                                    }
                                    if (builder.PlaceOnNewLine && currentLine.Count != 0)
                                    {
                                        s.AddTokens(currentLine);
                                        currentLine = new List<List<Token>>();
                                    }
                                }
                                Token t = builder.Build(lastToken, this, s, ref script);
                                lastToken = t;
                                if (!(t is Token.DelayToken))
                                {
                                    t.Line = _line;
                                    _currentTokens.Add(t);
                                }
                                if (builder.SplitAfter || builder.NewLineAfter)
                                {
                                    while (_builderQueue.Count != 0)
                                    {
                                        _currentTokens.Add(_builderQueue.Pop().LateBuild(this, ref script, ref s));
                                    }
                                    if (_currentTokens.Count != 0)
                                    {
                                        currentLine.Add(_currentTokens);
                                        _currentTokens = new List<Token>();
                                    }
                                    if (builder.NewLineAfter && currentLine.Count != 0)
                                    {
                                        s.AddTokens(currentLine);
                                        currentLine = new List<List<Token>>();
                                    }
                                }
                            }
                            else
                            {
                                _internalBuilders[weight][i].AdvancedBuild(this, ref script, ref s);
                            }
                            script.Offset--;
                            goto build;
                        }
                    }
                }
            }

            if (_currentTokens.Count != 0)
            {
                currentLine.Add(_currentTokens);
            }

            if (currentLine.Count != 0)
            {
                s.AddTokens(currentLine);
            }

            // Once we get here, do some final checks, and return the script.

            foreach (int weight in _postBuildOperations.Keys)
            {
                for (int i = 0; i < _postBuildOperations[weight].Count; i++)
                {
                    _postBuildOperations[weight][i].DoPass(ref s._tokens);
                }
            }

            // Run the final pass. Remove Token.AssignmentOperator and Token.StatementEnd.

            return s;
        }