Exemple #1
0
        public bool ValidateRule(CodeKeyword codeKeyword, string scope)
        {
            if (keyword != codeKeyword.keyword.Content || this.scope != scope)
            {
                return(false);
            }

            if (!arguments.Check(codeKeyword.arguments.Count))
            {
                return(false);
            }

            if (needOpenScope)
            {
                if (codeKeyword.scopeContent == null)
                {
                    return(false);
                }
            }

            if (needScopeData)
            {
                if (codeKeyword.scopeContent == null)
                {
                    return(false);
                }

                if (codeKeyword.scopeContent.innerCommands.Count == 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #2
0
        ///-----------------------------------------------------------------
        public bool ExistInVocabulary(CodeKeyword rootKeyword)
        {
            foreach (var keywordRule in keywordUsages)
            {
                if (keywordRule.keyword == rootKeyword.keyword.Content)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
        ///-----------------------------------------------------------------
        public KeywordUsage GetKeywordUsage(CodeKeyword keyword, string activeScope)
        {
            foreach (var keywordRule in keywordUsages)
            {
                if (!keywordRule.ValidateRule(keyword, activeScope))
                {
                    continue;
                }

                return(keywordRule);
            }

            return(default);
Exemple #4
0
        ///---------------------------------------------------------------------
        private CodeFile RetrieveCodeFile(CodeKeyword codeKeyword, List <CodeFile> codeFiles)
        {
            var fileInfoRule = KeywordCreator.GetFileInfos();

            if (!fileInfoRule.ValidateRule(codeKeyword, string.Empty))
            {
                return(null);
            }

            var arg0     = codeKeyword.arguments[0].Content;
            var arg1     = codeKeyword.arguments[1].Content;
            var codeFile = codeFiles.Find(x => { return(x.fileName == arg0 && x.fileExtension == arg1); });

            if (codeFile == null)
            {
                codeFile = new CodeFile {
                    fileName = arg0, fileExtension = arg1
                };
                codeFiles.Add(codeFile);
            }

            return(codeFile);
        }
Exemple #5
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);
        }
Exemple #6
0
        private BuildResult FeedCodeFile(ref FileData fileData, CodeFile codeFile, CodeKeyword rootKeyword, string rootScope)
        {
            var  scriptActions = ScriptActionRegistry.Actions;
            bool foundOne      = false;

            for (var s = 0; s < scriptActions.Count; s++)
            {
                var action = scriptActions[s];
                if (action.ExistInVocabulary(rootKeyword))
                {
                    foundOne = true;
                    break;
                }
            }

            if (!foundOne)
            {
                return(Error((BuildResult)BuildResult.ValueType.PrateekScriptInvalidKeyword + rootKeyword.keyword.Content, ref fileData));
            }

            var scopeRule = (KeywordUsage) default;

            for (var s = 0; s < scriptActions.Count; s++)
            {
                var action = scriptActions[s];
                if (!codeFile.AllowRule(action))
                {
                    continue;
                }

                var keywordRule = action.GetKeywordUsage(rootKeyword, rootScope);
                if (keywordRule.keywordUsageType == KeywordUsageType.None ||
                    keywordRule.keywordUsageType == KeywordUsageType.Ignore)
                {
                    continue;
                }

                if (!action.FeedCodeFile(fileData, codeFile, keywordRule, rootKeyword))
                {
                    return(Error((BuildResult)BuildResult.ValueType.PrateekScriptDataNotTreated + rootKeyword.keyword.Content, ref fileData));
                }

                scopeRule = keywordRule;
                break;
            }

            var scopes = rootKeyword.scopeContent != null ? new List <CodeScope> {
                rootKeyword.scopeContent
            } : null;

            for (var s = 0; scopes != null && s < scopes.Count; s++)
            {
                var scope = scopes[s];
                foreach (var codeCommand in scope.innerCommands)
                {
                    if (!(codeCommand is CodeKeyword innerKeyword))
                    {
                        continue;
                    }

                    var buildResult = FeedCodeFile(ref fileData, codeFile, innerKeyword, rootKeyword.keyword.Content);
                    if (buildResult.Value != BuildResult.ValueType.Success)
                    {
                        return(buildResult);
                    }
                }

                if (scope.innerScopes.Count == 0)
                {
                    continue;
                }

                scopes.InsertRange(s + 1, scope.innerScopes);
            }

            if (scopeRule.keywordUsageType != KeywordUsageType.None && scopeRule.onCloseScope != null)
            {
                scopeRule.onCloseScope(codeFile, rootKeyword.keyword.Content);
            }

            return(BuildResult.ValueType.Success);
        }
Exemple #7
0
        public bool FeedCodeFile(FileData fileData, CodeFile codeFile, KeywordUsage keywordUsage, CodeKeyword rootKeyword)
        {
            var scriptContent = codeFile.ScriptContent;

            if (keywordUsage.createNewScriptContent)
            {
                if (scriptContent == null)
                {
                    scriptContent = codeFile.NewData(this);
                }

                if (scriptContent.scriptAction == null || scriptContent.scriptAction != this)
                {
                    return(false);
                }
            }

            var data = string.Empty;

            if (keywordUsage.needScopeData)
            {
                foreach (var codeCommand in rootKeyword.scopeContent.innerCommands)
                {
                    if (!(codeCommand is CodeLiteral codeLiteral))
                    {
                        return(false);
                    }

                    foreach (var literalValue in codeLiteral.literals)
                    {
                        data += literalValue.Content;
                    }
                }
            }

            return(keywordUsage.onFeedCodeFile.Invoke(fileData, codeFile, scriptContent, rootKeyword.arguments, data));
        }
Exemple #8
0
    public void read()
    {
        List <CodeKeyword> codekeyStack = new List <CodeKeyword>();
        List <CodeSymbol>  symbolStack  = new List <CodeSymbol>();
        List <PBElement>   elementStack = new List <PBElement>();

        elementStack.Add(this);

        if (charArr == null)
        {
            charArr = code.ToCharArray();
        }

        PBElement curPBElement = null;

        int startIndex = 0;

        for (int i = 0; i < code.Length; i++)
        {
            startIndex = i;
            CodeSymbol curSymbol = FindNextSymbol(code, startIndex);
            int        Index     = curSymbol.Index;

            if (Index > 0)
            {
                //跳过注释
                if (curSymbol.Symbol == '/')
                {
                    if (charArr [curSymbol.Index + 1] == '*')
                    {
                        while (Index < charArr.Length)
                        {
                            curSymbol = FindNextSymbol(code, Index + 1, '/');
                            Index     = curSymbol.Index;

                            if (Index == -1)
                            {
                                Debug.LogErrorFormat("{0} not find '/' end", name);
                                return;
                            }
                            if (charArr [curSymbol.Index - 1] == '*')
                            {
                                break;
                            }
                        }

                        i = Index;

                        Debug.LogWarningFormat("{0} jump /**/ index {1} ", name, Index);

                        continue;
                    }
                }

                string key = code.Substring(startIndex, Index - startIndex);

                if (curPBElement != null && key.StartsWith("//"))
                {
                    //	curPBElement.Notes = key;
                }

                if (key == "")
                {
                }
                else
                {
                    //关键字压入
                    codekeyStack.Add(new CodeKeyword(startIndex, key));
                }

                if (key.Equals("import"))
                {
                    //PBFile pbFile = new PBFile ();
                    PBImportFile pushPBImportFile = new PBImportFile();
                    pushPBImportFile.startIndex = startIndex;

                    elementStack.Add(pushPBImportFile);
                    curPBElement = pushPBImportFile;
                }
                else
                if (key.Equals("package"))
                {
                    PBPackage pushPBPackage = new PBPackage();
                    pushPBPackage.startIndex = startIndex;

                    elementStack.Add(pushPBPackage);
                    curPBElement = pushPBPackage;
                }
                else
                if (key.Equals("required") || key.Equals("optional") || key.Equals("repeated"))
                {
                    PBAttribute pushPBAttribute = new PBAttribute();
                    pushPBAttribute.startIndex = startIndex;

                    elementStack.Add(pushPBAttribute);
                    curPBElement = pushPBAttribute;
                }
                else
                //enum压入
                if (key == "enum")
                {
                    PBEnum pushPBElement = new PBEnum();
                    pushPBElement.pbFile     = this;
                    pushPBElement.name       = key;
                    pushPBElement.startIndex = Index;

                    elementStack.Add(pushPBElement);

                    curPBElement = pushPBElement;
                }
                else
                //message压入
                if (key == "message")
                {
                    PBMessage pushPBMessage = new PBMessage();
                    pushPBMessage.pbFile     = this;
                    pushPBMessage.name       = key;
                    pushPBMessage.startIndex = Index;

                    elementStack.Add(pushPBMessage);

                    curPBElement = pushPBMessage;

                    if (codekeyStack.Count >= 2 && codekeyStack[codekeyStack.Count - 2].value.StartsWith("//"))
                    {
                        pushPBMessage.Notes = codekeyStack[codekeyStack.Count - 2].value;
                    }
                }

                /*
                 * if(curSymbol.Symbol == '='){
                 *      string pname  =  (string)codekeyStack[codekeyStack.Count-1];
                 *      Debug.LogWarning(" >> pname :)))))   -------->  " + pname);
                 * }else
                 */

                if (curSymbol.Symbol == '{')
                {
                    CodeKeyword ptype = codekeyStack[codekeyStack.Count - 2];
                    CodeKeyword pname = codekeyStack[codekeyStack.Count - 1];
                    //Debug.LogWarning(" >> Index :)))))   -------->  " + pname.Index + ", >> pname :)))))   -------->  " + pname.value );
                    //压入符号栈
                    symbolStack.Add(curSymbol);
                }
                else
                if (curSymbol.Symbol == '}')
                {
                    //块结束
                    CodeSymbol pushSymbol = symbolStack[symbolStack.Count - 1];
                    if (pushSymbol.Symbol == '{')
                    {
                        symbolStack.RemoveAt(symbolStack.Count - 1);
                        //弹出符号栈
                        PBElement popUpPBElement = elementStack[elementStack.Count - 1];
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        elementStack.RemoveAt(elementStack.Count - 1);

                        if (elementStack.Count > 0)
                        {
                            elementStack[elementStack.Count - 1].addChild(popUpPBElement);
                        }
                    }
                    else
                    {
                        Debug.LogError("Symbol Error { ");
                    }
                }
                else
                if (curSymbol.Symbol == '[')
                {
                    symbolStack.Add(curSymbol);

                    PBKeyValue pushPBKeyValue = new PBKeyValue();
                    pushPBKeyValue.startIndex = Index;
                    elementStack.Add(pushPBKeyValue);
                }
                else
                if (curSymbol.Symbol == ']')
                {
                    CodeSymbol pushSymbol = symbolStack[symbolStack.Count - 1];
                    if (pushSymbol.Symbol == '[')
                    {
                        symbolStack.RemoveAt(symbolStack.Count - 1);
                        //弹出
                        PBElement popUpPBElement = elementStack[elementStack.Count - 1];
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        elementStack.RemoveAt(elementStack.Count - 1);

                        if (elementStack.Count > 0)
                        {
                            elementStack[elementStack.Count - 1].addChild(popUpPBElement);
                        }
                    }
                    else
                    {
                        Debug.LogError("Symbol Error [ ");
                    }
                }
                else
                if (curSymbol.Symbol == ';')
                {
                    //弹出
                    PBElement popUpPBElement = elementStack[elementStack.Count - 1];

                    if (popUpPBElement.pb_type == PBElement.type_PBPackage)
                    {
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        this.package = (popUpPBElement as PBPackage).pbPackageName;

                        elementStack.RemoveAt(elementStack.Count - 1);
                    }
                    else
                    if (popUpPBElement.pb_type == PBElement.type_PBImportFile)
                    {
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        PBFile pbFile = new PBFile();
                        pbFile.name = (popUpPBElement as PBImportFile).pbFilePath;
                        pbFile.readFile(pbFile.name);
                        pbFile.read();

                        PBFileImportDic.Add(pbFile.name, pbFile);

                        elementStack.RemoveAt(elementStack.Count - 1);
                    }
                    else
                    //枚举 属性
                    if (popUpPBElement.pb_type == PBElement.type_PBEnum)
                    {
                        PBKeyValue PBKeyValue = new PBKeyValue();
                        PBKeyValue.key        = codekeyStack[codekeyStack.Count - 2].value;
                        PBKeyValue.value      = codekeyStack[codekeyStack.Count - 1].value;
                        PBKeyValue.startIndex = codekeyStack[codekeyStack.Count - 2].Index;
                        PBKeyValue.endIndex   = Index;
                        PBKeyValue.cut(code);

                        popUpPBElement.addChild(PBKeyValue);

                        curPBElement = PBKeyValue;

                        codekeyStack.RemoveAt(codekeyStack.Count - 1);
                        codekeyStack.RemoveAt(codekeyStack.Count - 1);

                        CodeSymbol nextSymbol = FindNextSymbol(code, Index + 1);
                        string     nextKey    = code.Substring(Index + 1, nextSymbol.Index - (Index + 1));

                        if (curPBElement != null && nextKey.StartsWith("//"))
                        {
                            curPBElement.Notes = nextKey;
                        }
                    }
                    else
                    //message 属性
                    if (popUpPBElement.pb_type == PBElement.type_PBAttribute)
                    {
                        popUpPBElement.endIndex = Index;
                        popUpPBElement.cut(code);

                        elementStack.RemoveAt(elementStack.Count - 1);

                        curPBElement = popUpPBElement;

                        if (elementStack.Count > 0)
                        {
                            elementStack[elementStack.Count - 1].addChild(popUpPBElement);
                        }

                        CodeSymbol nextSymbol = PBFile.FindNextSymbol(code, Index + 1);
                        string     nextKey    = code.Substring(Index + 1, nextSymbol.Index - (Index + 1));

                        if (curPBElement != null && nextKey.StartsWith("//"))
                        {
                            curPBElement.Notes = nextKey;
                        }
                    }
                }
            }
            i = Index;
        }
        //getAllChild(this);
    }