Ejemplo n.º 1
0
        private List <Scope> GetCapturedStyles(Match regexMatch,
                                               int currentIndex,
                                               CompiledLanguage compiledLanguage)
        {
            var capturedStyles = new List <Scope>();

            for (int i = 0; i < regexMatch.Groups.Count; i++)
            {
                Group  regexGroup = regexMatch.Groups[i];
                string styleName  = compiledLanguage.Captures[i];

                if (regexGroup.Length == 0 || String.IsNullOrEmpty(styleName))
                {
                    continue;
                }
                else
                {
                    foreach (Capture regexCapture in regexGroup.Captures)
                    {
                        AppendCapturedStylesForRegexCapture(regexCapture, currentIndex, styleName, capturedStyles);
                    }
                }
            }

            return(capturedStyles);
        }
Ejemplo n.º 2
0
        public RunResultDTO RunTask(TaskSolutionDTO taskSolution, task task)
        {
            if (taskSolution == null)
            {
                ExceptionHandler.ThrowException(HttpStatusCode.BadRequest, "No body provided");
            }

            if (taskSolution.Code == null || taskSolution.Code.Trim() == "")
            {
                ExceptionHandler.ThrowException(HttpStatusCode.BadRequest, "No code supplied");
            }

            ILanguage languageType = null;
            string    filePath     = codeLocation;

            if (taskSolution.ProgrammingLanguage == ProgrammingLanguage.JAVA)
            {
                filePath += "\\Main.java";

                languageType = new CompiledLanguage(
                    filePath,
                    "javac -d \"" + binLocation + "\" \"" + filePath + "\"",
                    "java -cp \"" + binLocation + "\"; Main");
            }
            else if (taskSolution.ProgrammingLanguage == ProgrammingLanguage.C_PLUS_PLUS)
            {
                filePath += "\\main.cpp";

                /*
                 * languageType = new CompiledLanguage(
                 *  filePath,
                 *  "gcc -cpp \"" + filePath + "\" -o \"" + binLocation + "\\mainCpp\"",
                 *  "\"" + binLocation + "\\mainCpp\"");
                 * */

                languageType = new CompiledLanguage(
                    filePath,
                    "g++ -std=c++1z -c \"" + filePath + "\" -o \"" + binLocation + "\\main.o\" " +        //create o file in bin folder
                    "&& g++ -std=c++1z \"" + binLocation + "\\main.o\" -o \"" + binLocation + "\\main\"", //link o file to exe
                    "\"" + binLocation + "\\main\"");
            }
            else if (taskSolution.ProgrammingLanguage == ProgrammingLanguage.C_SHARP)
            {
                filePath += "\\Main.cs";

                languageType = new CompiledLanguage(
                    filePath,
                    "csc -out:\"" + binLocation + "\\main.exe\" \"" + filePath + "\"",
                    "\"" + binLocation + "\\main\"");
            }
            else
            {
                ExceptionHandler.ThrowException(HttpStatusCode.BadRequest, "Specified language not supported");
            }

            return(languageType.RunSolution(taskSolution.Code, task));
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public void Parse(string sourceCode, ILanguage language, Action <string, IList <Scope> > parseHandler)
        {
            if (string.IsNullOrEmpty(sourceCode))
            {
                return;
            }

            CompiledLanguage compiledLanguage = languageCompiler.Compile(language);

            Parse(sourceCode, compiledLanguage, parseHandler);
        }
Ejemplo n.º 4
0
        private void Parse(string sourceCode,
                           CompiledLanguage compiledLanguage,
                           Action <string, IList <Scope> > parseHandler)
        {
            Match regexMatch = compiledLanguage.Regex.Match(sourceCode);

            if (!regexMatch.Success)
            {
                parseHandler(sourceCode, new List <Scope>());
            }
            else
            {
                int currentIndex = 0;

                while (regexMatch.Success)
                {
                    string sourceCodeBeforeMatch = sourceCode.Substring(currentIndex, regexMatch.Index - currentIndex);
                    if (!string.IsNullOrEmpty(sourceCodeBeforeMatch))
                    {
                        parseHandler(sourceCodeBeforeMatch, new List <Scope>());
                    }

                    string matchedSourceCode = sourceCode.Substring(regexMatch.Index, regexMatch.Length);
                    if (!string.IsNullOrEmpty(matchedSourceCode))
                    {
                        List <Scope> capturedStylesForMatchedFragment = GetCapturedStyles(regexMatch,
                                                                                          regexMatch.Index,
                                                                                          compiledLanguage);
                        List <Scope> capturedStyleTree = CreateCapturedStyleTree(capturedStylesForMatchedFragment);
                        parseHandler(matchedSourceCode, capturedStyleTree);
                    }

                    currentIndex = regexMatch.Index + regexMatch.Length;
                    regexMatch   = regexMatch.NextMatch();
                }

                string sourceCodeAfterAllMatches = sourceCode.Substring(currentIndex);
                if (!string.IsNullOrEmpty(sourceCodeAfterAllMatches))
                {
                    parseHandler(sourceCodeAfterAllMatches, new List <Scope>());
                }
            }
        }
Ejemplo n.º 5
0
        private void AppendCapturedStylesForNestedLanguage(Capture regexCapture,
                                                           int offset,
                                                           string nestedLanguageId,
                                                           ICollection <Scope> capturedStyles)
        {
            ILanguage nestedLanguage = languageRepository.FindById(nestedLanguageId);

            if (nestedLanguage == null)
            {
                throw new InvalidOperationException("The nested language was not found in the language repository.");
            }
            else
            {
                CompiledLanguage nestedCompiledLanguage = languageCompiler.Compile(nestedLanguage);

                Match regexMatch = nestedCompiledLanguage.Regex.Match(regexCapture.Value, 0, regexCapture.Value.Length);

                if (!regexMatch.Success)
                {
                    return;
                }
                else
                {
                    while (regexMatch.Success)
                    {
                        List <Scope> capturedStylesForMatchedFragment = GetCapturedStyles(regexMatch,
                                                                                          0,
                                                                                          nestedCompiledLanguage);
                        List <Scope> capturedStyleTree = CreateCapturedStyleTree(capturedStylesForMatchedFragment);

                        foreach (var nestedCapturedStyle in capturedStyleTree)
                        {
                            IncreaseCapturedStyleIndicies(capturedStyleTree, offset);
                            capturedStyles.Add(nestedCapturedStyle);
                        }

                        regexMatch = regexMatch.NextMatch();
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private List <Scope> GetCapturedStyles(Match regexMatch, int currentIndex, CompiledLanguage compiledLanguage)
        {
            var capturedStyles = new List <Scope>();

            for (int i = 0; i < regexMatch.Groups.Count; i++)
            {
                Group regexGroup = regexMatch.Groups[i];
                if (regexGroup.Length > 0 && i < compiledLanguage.Captures.Count)
                {
                    // note: i can be >= Captures.Count due to named groups; these do capture a group but always get added after all non-named groups (which is why we do not count them in numberOfCaptures)
                    string styleName = compiledLanguage.Captures[i];
                    if (!string.IsNullOrEmpty(styleName))
                    {
                        foreach (Capture regexCapture in regexGroup.Captures)
                        {
                            AppendCapturedStylesForRegexCapture(regexCapture, currentIndex, styleName, capturedStyles);
                        }
                    }
                }
            }

            return(capturedStyles);
        }