Exemplo n.º 1
0
        public bool BuildCodeCommands()
        {
            var scopes = new Stack <CodeScope>();

            contentRootScope = new CodeScope();
            var ctx = new CodeContext()
            {
                latestComments = null,
                activeScope    = contentRootScope,
                latestCommand  = null
            };

#if true
            while (CurrentSymbol != null)
            {
                var currentSymbol = CurrentSymbol;
                if (currentSymbol is ISeparator)
                {
                    if (ctx.latestCommand == null)
                    {
                        return(false);
                    }
                }

                if (ctx.latestCommand != null &&
                    ctx.latestCommand.CanAbsorb(currentSymbol))
                {
                    ctx.latestCommand.Add(currentSymbol);
                    cursorSymbol++;
                    continue;
                }

                if (ctx.activeScope != null &&
                    ctx.activeScope.CanAbsorb(currentSymbol))
                {
                    cursorSymbol++;
                    continue;
                }

                switch (currentSymbol)
                {
                case ISeparator separator:
                {
                    //If the separator has not been absorbed, it's considered a failure
                    return(false);
                }

                case IScopeOpen opener:
                {
                    if (ctx.latestCommand != null && !ctx.latestCommand.AllowInternalScope)
                    {
                        return(false);
                    }

                    var newScope = registry.SymbolToCommand[currentSymbol.GetType()]() as CodeScope;
                    if (newScope == null)
                    {
                        return(false);
                    }

                    newScope.opener = opener;
                    newScope.Add(ctx.latestComments);
                    if (ctx.latestCommand != null)
                    {
                        newScope.owner = ctx.latestCommand;
                        ctx.latestCommand.Add(newScope);
                    }
                    else
                    {
                        newScope.owner = ctx.activeScope;
                        ctx.activeScope.Add(newScope);
                    }

                    scopes.Push(ctx.activeScope);
                    ctx.activeScope   = newScope;
                    ctx.latestCommand = null;

                    break;
                }

                case IScopeClose closer:
                {
                    if (scopes.Count == 0)
                    {
                        return(false);
                    }

                    if (!ctx.activeScope.Match(closer))
                    {
                        return(false);
                    }

                    ctx.activeScope.Close();
                    ctx.activeScope    = scopes.Pop();
                    ctx.latestComments = new CodeComment();
                    ctx.latestCommand  = null;

                    break;
                }

                case IComment keywordComment:
                case LiteralValue literalValue:
                {
                    var newCommand = registry.SymbolToCommand[currentSymbol.GetType()]() as CodeCommand;
                    if (newCommand == null)
                    {
                        return(false);
                    }

                    if (currentSymbol is IComment)
                    {
                        if (ctx.latestComments == null)
                        {
                            ctx.latestComments = newCommand as CodeComment;
                        }
                    }
                    else
                    {
                        ctx.latestCommand = newCommand;
                    }

                    newCommand.Add(currentSymbol);
                    break;
                }

                case IKeyword keywordSymbol:
                {
                    var newKeyword = registry.SymbolToCommand[currentSymbol.GetType()]() as CodeKeyword;
                    if (newKeyword == null)
                    {
                        return(false);
                    }

                    newKeyword.keyword = keywordSymbol;
                    newKeyword.Add(ctx.latestComments);
                    ctx.activeScope.Add(newKeyword);

                    ctx.latestCommand = newKeyword;

                    break;
                }

                case IClearCommand clearCommand:
                {
                    ctx.latestCommand = null;

                    break;
                }
                }

                cursorSymbol++;
            }
#else
            while (CurrentSymbol != null)
            {
                var currentSymbol = CurrentSymbol;
                if (currentSymbol is IComment commentSymbol)
                {
                    ctx.latestComments.Set(commentSymbol);

                    cursorSymbol++;
                    continue;
                }

                if (currentSymbol is ScopeCodeBegin)
                {
                    if (ctx.latestCommand != null && !ctx.latestCommand.AllowInternalScope)
                    {
                        return(false);
                    }

                    var newScope = new CodeScope();
                    newScope.Add(ctx.latestComments);
                    if (ctx.latestCommand != null)
                    {
                        ctx.latestCommand.Add(newScope);
                    }
                    else
                    {
                        ctx.activeScope.Add(newScope);
                    }

                    scopes.Push(ctx.activeScope);
                    ctx.activeScope   = newScope;
                    ctx.latestCommand = null;
                }
                else if (currentSymbol is ScopeCodeEnd)
                {
                    if (scopes.Count == 0)
                    {
                        return(false);
                    }

                    ctx.activeScope    = scopes.Pop();
                    ctx.latestComments = new CodeComment();
                    ctx.latestCommand  = null;
                }

                if (currentSymbol is Keyword keywordSymbol)
                {
                    var newKeyword = new CodeKeyword {
                        keyword = keywordSymbol
                    };
                    newKeyword.Add(ctx.latestComments);
                    ctx.activeScope.Add(newKeyword);

                    ctx.latestCommand = newKeyword;

                    if (!(NextSymbol is ScopeInvokeBegin))
                    {
                        if (NextSymbol == null)
                        {
                            return(false);
                        }

                        cursorSymbol++;
                        continue;
                    }

                    cursorSymbol++;

                    var latestSymbol   = (Symbol)null;
                    var foundInvokeEnd = false;
                    cursorSymbol++;
                    while (CurrentSymbol != null)
                    {
                        currentSymbol = CurrentSymbol;
                        if (currentSymbol is VariableSeparator)
                        {
                            if (!(latestSymbol is Keyword))
                            {
                                return(false);
                            }
                        }
                        else if (currentSymbol is Keyword argSymbol)
                        {
                            if (latestSymbol is Keyword)
                            {
                                return(false);
                            }

                            newKeyword.Add(argSymbol);
                        }
                        else if (currentSymbol is ScopeInvokeEnd)
                        {
                            foundInvokeEnd = true;
                            break;
                        }
                        else
                        {
                            return(false);
                        }

                        latestSymbol = currentSymbol;
                        cursorSymbol++;
                    }

                    if (!foundInvokeEnd)
                    {
                        return(false);
                    }
                }

                if (currentSymbol is LiteralValue literalSymbol)
                {
                    var newLiteral = new CodeLiteral();
                    newLiteral.Add(literalSymbol);

                    ctx.activeScope.Add(newLiteral);

                    if (!(NextSymbol is LiteralValue))
                    {
                        if (NextSymbol == null)
                        {
                            return(false);
                        }

                        cursorSymbol++;
                        continue;
                    }

                    cursorSymbol++;
                    while (CurrentSymbol != null)
                    {
                        currentSymbol = CurrentSymbol;
                        if (currentSymbol is LiteralValue nextLiteral)
                        {
                            newLiteral.Add(nextLiteral);
                        }

                        if (!(NextSymbol is LiteralValue))
                        {
                            break;
                        }

                        cursorSymbol++;
                    }
                }

                cursorSymbol++;
            }
#endif

            return(true);
        }