Exemplo n.º 1
0
        public async Task <CompletionList> Handle(CompletionParams request, CancellationToken cancellationToken)
        {
            var text   = _workspaceService.GetMarineFileBuffer(request.TextDocument.Uri.Path);
            var tokens = new LexicalAnalyzer().GetTokens(text);
            var result = new SyntaxAnalyzer().Parse(tokens);

            var tasks = new List <Task <IEnumerable <CompletionItem> > >();
            FuncDefinitionAst currentFuncDefAst = null;

            if (result.programAst != null)
            {
                currentFuncDefAst = await Task.Run(() => GetCurrentFunctionAst(result.programAst, request.Position),
                                                   cancellationToken);

                if (currentFuncDefAst != null)
                {
                    tasks.Add(Task.Run(() => CreateFunctions(result.programAst), cancellationToken));

                    tasks.Add(Task.Run(() => CreateFunctionParamaters(currentFuncDefAst), cancellationToken));
                    tasks.Add(Task.Run(() => CreateVariables(currentFuncDefAst, request.Position), cancellationToken));

                    tasks.Add(Task.Run(CreateKeywords, cancellationToken));
                }
            }

            tasks.Add(Task.Run(() => CreateSnippets(currentFuncDefAst), cancellationToken));

            return(new CompletionList((await Task.WhenAll(tasks)).SelectMany(e => e)));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //String text = "2*(2+4)";
            String text = "/*my program*/" + "\n" +
                          "var mystring = \"some text\";" +
                          "var i = 1234;" +
                          "i  = 0;" +
                          "while(i < 5) { print(i); i = i + 1; }";
            //String text = "(2*(4+5)-(2*(2+1)))*2";
            LexycalAnalyzer la = new LexycalAnalyzer();
            SyntaxAnalyzer sa = new SyntaxAnalyzer();

            List<Token> result = la.Parse(text);
            //Console.WriteLine(sa.ParseExpressionDev(result).GetValue());
            Console.WriteLine();
            //Console.WriteLine(new SyntaxAnalyzer().FindClosingBracket(result, 24));
            la.WriteTokens(result);
            foreach (Token t in result)
            {
                Console.Write(t.data);
            }

            Command commands = new SyntaxAnalyzer().Analyze(result);
            while (commands.nextCommand != null)
            {
                commands.execute();
                commands = commands.nextCommand;
            }

            Console.Read();
        }
Exemplo n.º 3
0
        public void GivenANewSyntaxComponent()
        {
            var lexer  = ScenarioContext.Current.Get <ILexer>("newLexer");
            var parser = new SyntaxAnalyzer(lexer);

            ScenarioContext.Current.Add("parser", parser);
        }
Exemplo n.º 4
0
    //REGEX; @"^(\'|\~)([a-zA-Z]+) ([a-zA-Z]+) (\d+)$"

    void Start()
    {
        parser = FindObjectOfType <SyntaxAnalyzer>();

        input      = "";
        dictionary = new Dictionary <int, string>();
    }
Exemplo n.º 5
0
        public static void Analyze(
            SyntaxNodeAnalysisContext context,
            InvocationExpressionSyntax invocation,
            MemberAccessExpressionSyntax memberAccess,
            string methodName)
        {
            if (memberAccess.Expression?.IsKind(SyntaxKind.InvocationExpression) == true)
            {
                var invocation2 = (InvocationExpressionSyntax)memberAccess.Expression;

                if (invocation2.ArgumentList?.Arguments.Count == 1 &&
                    invocation2.Expression?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true)
                {
                    var memberAccess2 = (MemberAccessExpressionSyntax)invocation2.Expression;

                    if (memberAccess2.Name?.Identifier.ValueText == "Where" &&
                        SyntaxAnalyzer.IsEnumerableExtensionMethod(invocation, methodName, 1, context.SemanticModel, context.CancellationToken) &&
                        (SyntaxAnalyzer.IsEnumerableWhereOrImmutableArrayWhereMethod(invocation2, context.SemanticModel, context.CancellationToken)))
                    {
                        TextSpan span = TextSpan.FromBounds(memberAccess2.Name.Span.Start, invocation.Span.End);

                        if (!invocation.ContainsDirectives(span))
                        {
                            context.ReportDiagnostic(
                                DiagnosticDescriptors.SimplifyLinqMethodChain,
                                Location.Create(invocation.SyntaxTree, span));
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        internal static bool EvalExpression([MarshalAs(UnmanagedType.LPWStr)] string expr, IntPtr opaque, out IntPtr errorText, out int typeId, out int size, out IntPtr result)
        {
            SyntaxAnalyzer sa = new SyntaxAnalyzer(expr);

            StringBuilder scriptText = new StringBuilder("#line hidden\n");

            // Generate prefix with variables assignment to __context members
            foreach (string us in sa.unresolvedSymbols)
            {
                scriptText.AppendFormat("var {0} = __context.{0};\n", us);
            }

            scriptText.Append("#line 1\n");
            scriptText.Append(expr);

            errorText = IntPtr.Zero;
            result    = IntPtr.Zero;
            typeId    = 0;
            size      = 0;
            try
            {
                var scriptOptions = ScriptOptions.Default
                                    .WithImports("System")
                                    .WithReferences(typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo).Assembly);
                var script = CSharpScript.Create(scriptText.ToString(), scriptOptions, globalsType: typeof(Globals));
                script.Compile();
                var returnValue = script.RunAsync(new Globals {
                    __context = new ContextVariable(opaque, IntPtr.Zero)
                }).Result.ReturnValue;
                if (returnValue is ContextVariable)
                {
                    typeId = -1;
                    result = (returnValue as ContextVariable).m_corValue;
                }
                else
                {
                    if (returnValue is null)
                    {
                        typeId = 0;
                        return(true);
                    }
                    for (int i = 1; i < basicTypes.Length; i++)
                    {
                        if (returnValue.GetType() == Type.GetType(basicTypes[i]))
                        {
                            typeId = i;
                            MarshalValue(returnValue, out size, out result);
                            return(true);
                        }
                    }
                    return(false);
                }
            }
            catch (Exception e)
            {
                errorText = Marshal.StringToBSTR(e.ToString());
                return(false);
            }
            return(true);
        }
Exemplo n.º 7
0
        public TelemetryFilter()
        {
            List <TraceAggregationConfig> traceAggregationConfigs;
            List <ParseResult>            parseResults = SyntaxAnalyzer.Parse(ConfigFileName, new string[] { }, Utility.TraceSource.WriteError, false, out traceAggregationConfigs);

            foreach (var telConfig in traceAggregationConfigs)
            {
                try
                {
                    // including the aggregator to white-list
                    this.whiteListedEventsFromConfigFile.Add(new Tuple <string, string>(telConfig.TaskName, telConfig.EventName), telConfig);
                }
                catch (ArgumentNullException e)
                {
                    Utility.TraceSource.WriteError(LogSourceId, "Unexpected null value parsed from config file: {0} - Exception: {1}.", ConfigFileName, e);
                }
                catch (ArgumentException e)
                {
                    if (telConfig.DifferentiatorFieldName == this.whiteListedEventsFromConfigFile[new Tuple <string, string>(telConfig.TaskName, telConfig.EventName)].DifferentiatorFieldName)
                    {
                        Utility.TraceSource.WriteWarning(LogSourceId, "Conflicting trace white-listing (trace already white-listed) -Trace: {0} - Exception : {1}.", telConfig, e);
                    }
                }
            }

            // Tracing any errors that may have occured during parsing
            foreach (var parseRes in parseResults)
            {
                if (parseRes.ParseCode != ParseCode.Success)
                {
                    Utility.TraceSource.WriteError(LogSourceId, "{0}({1},{2}) : Error : {3}", ConfigFileName, parseRes.LineNumber, parseRes.ColumnPos + 1, parseRes.ErrorMsg);
                }
            }
        }
Exemplo n.º 8
0
        private void button1_Click(object sender, EventArgs e)
        {
            f = false;
            richTextBox6.Text   = null;
            TokenList2          = new LinkList();
            LineNo              = 1;
            BreakWordList       = new LinkList();
            BreakWordList_Clear = new LinkList();
            // richTextBox2.Text = null;
            // richTextBox3.Text = null;
            allText      = richTextBox1.Text;
            StringLenght = allText.Length;
            //BreakWordFunction(allText);
            break_words(allText);

            // richTextBox2.Text = BreakWordList_Clear.DisplayList();

            AddItemsInToken();
            // richTextBox3.Text = TokenList.DisplayTokens();
            //SyntaxAnalyzer SA = new SyntaxAnalyzer(TokenList);


            CurrentNode123 = TokenList.GetHead();

            OutNextLine();

            richTextBox5.Text = TokenList2.DisplayTokens();

            SyntaxAnalyzer SA = new SyntaxAnalyzer(TokenList2);

            Do pa = new Do();

            pa = SA.ABS();
            if (pa.cppp == "Valid Syntax")
            {
                richTextBox4.Text = pa.cppp;
            }
            else
            {
                richTextBox4.Text = pa.cppp + "\n" + "Syntax Error on Line" + pa.cpline;
            }

            int i = 1;

            while (i < 100)
            {
                richTextBox6.Text += i + "\n";
                i++;
            }


            //StreamWriter streamWriter = new StreamWriter(@"C:\Users\Mahad Khan\source\repos\BreakWord\BreakWord\TokensFile.txt");
            //streamWriter.WriteLine(richTextBox3.Text);
            //streamWriter.Close();

            BreakWordList = null;
            TokenList     = null;
            TokenList2    = null;
        }
Exemplo n.º 9
0
 public AnalyzerLEDEER(string xsentences)
 {
     //Set sentencias
     sentences = xsentences;
     //Construir análisis
     lexan = new LexicalAnalyzer();                                  //Analizador léxico
     synan = new SyntaxAnalyzer(lexan.createTableSymbol(sentences)); //Crear tabla de símbolo
 }
        public static bool CanRefactor(
            InvocationExpressionSyntax invocation,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            if (SyntaxAnalyzer.IsEnumerableExtensionOrImmutableArrayExtensionMethod(invocation, "Select", 2, semanticModel, cancellationToken))
            {
                ArgumentListSyntax argumentList = invocation.ArgumentList;

                if (argumentList?.IsMissing == false)
                {
                    SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

                    if (arguments.Count == 1)
                    {
                        ArgumentSyntax argument = arguments.First();

                        ExpressionSyntax expression = argument.Expression;

                        if (expression?.IsMissing == false)
                        {
                            SyntaxKind expressionKind = expression.Kind();

                            if (expressionKind == SyntaxKind.SimpleLambdaExpression)
                            {
                                var lambda = (SimpleLambdaExpressionSyntax)expression;

                                if (CanRefactor(lambda.Parameter, lambda.Body))
                                {
                                    return(true);
                                }
                            }
                            else if (expressionKind == SyntaxKind.ParenthesizedLambdaExpression)
                            {
                                var lambda = (ParenthesizedLambdaExpressionSyntax)expression;

                                ParameterListSyntax parameterList = lambda.ParameterList;

                                if (parameterList != null)
                                {
                                    SeparatedSyntaxList <ParameterSyntax> parameters = parameterList.Parameters;

                                    if (parameters.Count == 1 &&
                                        CanRefactor(parameters.First(), lambda.Body))
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 11
0
 public Compiler(LexicalAnalyzer lexicalAnalyzer,
                 SyntaxAnalyzer syntaxAnalyer,
                 SemanticsAnalyzer semanticsAnalyzer,
                 ObjectCodeGenerator objCodeGenerator)
 {
     this._lexicalAnalyzer  = lexicalAnalyzer;
     this._syntaxAnalyer    = syntaxAnalyer;
     this._semanticsAnalyer = semanticsAnalyzer;
     this._objCodeGenerator = objCodeGenerator;
 }
Exemplo n.º 12
0
        private void buttonAnalyze_Click(object sender, EventArgs e)
        {
            // Очищаем поле сообщений и таблицу распознанных токенов.
            richTextBoxMessages.Clear();
            dataGridViewRecognizedTokens.Rows.Clear();
            treeViewSyntaxTree.Nodes.Clear(); // Очищаем дерево.

            try
            {
                lexAn = GetMethod(richTextBoxInput.Lines);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                return;
            }

            try
            {
                if (comboBoxTypeAnalyzer.SelectedIndex == 1)
                {
                    SyntaxAnalyzer syntaxAnalyzer = new SyntaxAnalyzer(lexAn);
                    syntaxAnalyzer.ParseText(out SyntaxTreeNode treeRoot);
                    richTextBoxMessages.AppendText("Текст правильный");
                    VisualizeSyntaxTree(treeRoot); // Визуализируем синтаксическое дерево в компоненте treeViewSyntaxTree.

                    Generator generator = new Generator(treeRoot);
                    generator.GenerateStructuredText();

                    ShowOutputText(generator.OutputText);
                }
                else
                {
                    int k = 0; // Инициализируем счетчик распознанных токенов.

                    // Цикл чтения текста от начала до конца.
                    do
                    {
                        lexAn.RecognizeNextToken(); // Распознаем очередной токен в тексте.

                        k++;
                        dataGridViewRecognizedTokens.Rows.Add(k, lexAn.Token.Value, lexAn.Token.Type,
                                                              lexAn.Token.LineIndex + 1,
                                                              lexAn.Token.SymStartIndex + 1); // Добавляем распознанный токен в таблицу.
                    }while (lexAn.Token.Type != TokenKind.EndOfText);                         // Цикл работает до тех пор, пока не будет возвращен токен "Конец текста".

                    richTextBoxMessages.AppendText("Текст правильный");
                }
            }
            catch (AnalyzerException analyzerException)
            {
                richTextBoxMessages.AppendText(analyzerException.ToString());                         // Добавляем описание ошибки в поле сообщений.
                LocateCursorAtErrorPosition(analyzerException.LineIndex, analyzerException.SymIndex); // Располагаем курсор в исходном тексте на позиции ошибки.
            }
        }
Exemplo n.º 13
0
        private void AnalizeText()
        {
            lexAnalyzer.Source = tbCondition.Text;
            syntaxAnalyzer     = new SyntaxAnalyzer(lexAnalyzer, tbCondition.Dictionary.Keys.ToList());

            listLexic.Items.Clear();
            foreach (SyntaxToken token in syntaxAnalyzer.Tokens)
            {
                listLexic.Items.Add(token.ToString());
            }

            lexFormater   = new LexicalFormater(lexAnalyzer);
            tbResult.Text = lexFormater.Text;
            if (syntaxAnalyzer == null)
            {
                return;
            }
            if (listMessages.Items.Count > 0)
            {
                listMessages.Items.Clear();
            }
            errors = new List <SyntaxToken>();
            foreach (SyntaxToken token in syntaxAnalyzer.Tokens)
            {
                if (token.Qualifier != SyntaxToken.Qualifiers.Correct)
                {
                    listMessages.Items.Add(token.ToString());
                    errors.Add(token);
                    if (errors.Count == 1)
                    {
                        lbMsg.Text      = token.ToString();
                        lbMsg.ForeColor = Color.Red;
                    }
                }
            }
            if (errors.Count == 0)
            {
                if (syntaxAnalyzer.Tokens.Count == 0 || syntaxAnalyzer.Tokens.Last().Type == Token.Types.Id ||
                    (syntaxAnalyzer.Tokens.Last().Type == Token.Types.None && (syntaxAnalyzer.Tokens.Count == 1 || syntaxAnalyzer.Tokens[syntaxAnalyzer.Tokens.Count - 2].Type == Token.Types.Id)))
                {
                    lbMsg.Text      = "Ready.";
                    lbMsg.ForeColor = Color.Black;
                }
                else
                {
                    lbMsg.Text      = "Incomplete condition.";
                    lbMsg.ForeColor = Color.Red;
                }
            }
            btOk.Enabled = lbMsg.ForeColor == Color.Black;
            SyntaxTree syntaxTree = new SyntaxTree();

            syntaxTree.CreateTree(syntaxAnalyzer);
        }
Exemplo n.º 14
0
        private void CheckVariablesUsage()
        {
            LexicalAnalyzer lexAnalyzer = new LexicalAnalyzer();
            SyntaxAnalyzer  syntaxAnalyzer;

            foreach (Transition trans in TransitionsList)
            {
                if (trans is SimpleTransition strans)
                {
                    lexAnalyzer.Source = strans.Condition;
                    syntaxAnalyzer     = new SyntaxAnalyzer(lexAnalyzer, VariableCollection.GetConditionDictionary(trans.OwnerDraw.OwnerSheet).Keys.ToList());
                    foreach (SyntaxToken token in syntaxAnalyzer.Tokens)
                    {
                        if (token.Qualifier != SyntaxToken.Qualifiers.Correct)
                        {
                            MessagesList.Add(new CheckMessage(CheckMessage.MessageTypes.Error, string.Format("{0}: {1}.", trans.Name, token.ToString()), trans));
                        }
                    }
                    foreach (string outputOperation in strans.OutputsList)
                    {
                        string outputName = LexicalRules.GetOutputId(outputOperation);
                        if (!trans.OwnerDraw.OwnerSheet.Variables.InternalOutputs.Exists(output => output.Name == outputName))
                        {
                            MessagesList.Add(new CheckMessage(CheckMessage.MessageTypes.Error, "Output " + outputName + " is not defined in variables list.", trans));
                        }
                    }
                }
            }
            foreach (DrawableObject obj in ObjectsTable)
            {
                if (obj is State state)
                {
                    //state.EnterOutputsList.RemoveAll(str => !state.OwnerDraw.OwnerSheet.Variables.InternalOutputs.Exists(var => var.Name == LexicalAnalyzer.GetId(str)));
                    foreach (string outputOperation in state.EnterOutputsList)
                    {
                        string outputName = LexicalRules.GetOutputId(outputOperation);
                        if (!state.OwnerDraw.OwnerSheet.Variables.InternalOutputs.Exists(var => var.Name == LexicalAnalyzer.GetId(outputName)))
                        {
                            MessagesList.Add(new CheckMessage(CheckMessage.MessageTypes.Error, "Output " + outputName + " is not defined in variables list.", state));
                        }
                    }
                    //state.ExitOutputsList.RemoveAll(str => !state.OwnerDraw.OwnerSheet.Variables.InternalOutputs.Exists(var => var.Name == LexicalAnalyzer.GetId(str)));
                    foreach (string outputOperation in state.ExitOutputsList)
                    {
                        string outputName = LexicalRules.GetOutputId(outputOperation);
                        if (!state.OwnerDraw.OwnerSheet.Variables.InternalOutputs.Exists(var => var.Name == LexicalAnalyzer.GetId(outputName)))
                        {
                            MessagesList.Add(new CheckMessage(CheckMessage.MessageTypes.Error, "Output " + outputName + " is not defined in variables list.", state));
                        }
                    }
                }
            }
        }
Exemplo n.º 15
0
        public void TestCodeGenerated()
        {
            const string expression       = "1+2+3";
            var          lexicalAnalyser  = new LexicalAnalyzer(expression, 0);
            var          syntaxAnalyzer   = new SyntaxAnalyzer(lexicalAnalyser);
            var          node             = syntaxAnalyzer.Expression(0);
            var          fileWriter       = new FileWriter("");
            var          semanticAnalyzer = new SemanticAnalyzer(syntaxAnalyzer);
            var          codeGenerator    = new CodeGenerator(semanticAnalyzer, fileWriter);

            codeGenerator.GenerateCode(node);
            Assert.True(File.Exists(path) && new FileInfo(path).Length != 0);
        }
Exemplo n.º 16
0
    /************************
     *      INTERPRET CODE!
     *************************/
    private void Interpret()
    {
        clearConsole();
        List <Token> tokens = lexAnalyze();

        if (tokens != null)
        {
            SyntaxAnalyzer.analyze(tokens, this);
        }
        else
        {
            print("Error!");
        }
    }
        public static void Analyze(SyntaxNodeAnalysisContext context, AssignmentExpressionSyntax assignment)
        {
            ExpressionSyntax right = assignment.Right;

            if (right?.IsKind(SyntaxKind.ObjectCreationExpression) == true)
            {
                var objectCreation = (ObjectCreationExpressionSyntax)right;

                SemanticModel     semanticModel     = context.SemanticModel;
                CancellationToken cancellationToken = context.CancellationToken;

                if (SyntaxAnalyzer.IsEventHandlerOrGenericEventHandler(objectCreation, semanticModel, cancellationToken))
                {
                    ArgumentListSyntax argumentList = objectCreation.ArgumentList;

                    if (argumentList != null)
                    {
                        SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

                        if (arguments.Count == 1)
                        {
                            ArgumentSyntax argument = arguments.First();

                            ExpressionSyntax expression = argument.Expression;

                            if (expression != null)
                            {
                                IMethodSymbol methodSymbol = semanticModel.GetMethodSymbol(expression, cancellationToken);

                                if (methodSymbol != null)
                                {
                                    ExpressionSyntax left = assignment.Left;

                                    if (left?.IsMissing == false &&
                                        SyntaxAnalyzer.IsEvent(left, semanticModel, cancellationToken) &&
                                        !objectCreation.SpanContainsDirectives())
                                    {
                                        context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantDelegateCreation, right.GetLocation());

                                        context.FadeOutToken(DiagnosticDescriptors.RemoveRedundantDelegateCreationFadeOut, objectCreation.NewKeyword);
                                        context.FadeOutNode(DiagnosticDescriptors.RemoveRedundantDelegateCreationFadeOut, objectCreation.Type);
                                        context.FadeOutParentheses(DiagnosticDescriptors.RemoveRedundantDelegateCreationFadeOut, objectCreation.ArgumentList);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 18
0
        private bool Compile()
        {
            CompileStatus.Text = "Compiling...";
            if (Parser.Parse(MyTextEditor.Text.ToCharArray()))
            {
                try
                {
                    SyntaxTree syntaxtree         = SyntaxAnalyzer.GetTree(Parser.Tokens);
                    ArrayList  InstructionCode    = new ArrayList();
                    NumberAndIdentifierTable nids = new NumberAndIdentifierTable();

                    CodeGenerator.GenerateCode(InstructionCode, nids, syntaxtree);

                    if (CurrentFile == "")
                    {
                        CodeGenerator.SaveFile(InstructionCode, nids, Application.StartupPath + "\\Tempfile.imc");
                    }
                    else
                    {
                        FileInfo finfo = new FileInfo(CurrentFile);
                        CodeGenerator.SaveFile(
                            InstructionCode, nids, Path.ChangeExtension(CurrentFile, "imc"));
                    }
                    CompileStatus.Text = "Compilation process completed successfully.";
                    return(true);
                }
                catch (Exception exc)
                {
                    MyTextEditor.Select((int)SyntaxAnalyzer.CurrentToken.startpointer,
                                        (int)(SyntaxAnalyzer.CurrentToken.endpointer - SyntaxAnalyzer.CurrentToken.startpointer));
                    CompileStatus.Text = "Error occured, compilation failed.";
                }
            }
            else
            {
                foreach (Token tkn in Parser.Tokens)
                {
                    if (tkn.TType == TokenType.ERROR)
                    {
                        MyTextEditor.Select((int)tkn.startpointer,
                                            (int)(tkn.endpointer - tkn.startpointer));
                        CompileStatus.Text = "Error occured, compilation failed.";
                        break;
                    }
                }
            }


            return(false);
        }
        public static void Load(string block, Canvas canvas, Point upperLeftCorner)
        {
            LexicalAnalyzer la = new LexicalAnalyzer(block);
            SyntaxAnalyzer sa = new SyntaxAnalyzer(la);
            if (sa.IsValid)
            {
                BlockCreator bc = new BlockCreator(sa, upperLeftCorner);

                foreach (TextBlock tb in bc.Blocks)
                {
                    canvas.Children.Add(tb);
                }
            }
        }
Exemplo n.º 20
0
        static void Main(string[] args)
        {
            do
            {
                Console.Write("Введите выражение: ");
                string str = Console.ReadLine();

                bool   flag = false;
                string buf1 = str;
                int    k    = 0;
                for (int i = 0; i < buf1.Length; i++)
                {
                    if (buf1[i] == ',' || buf1[i] == ' ')
                    {
                        string buf;
                        buf = buf1.Substring(0, i);
                        if (buf.EndsWith("."))
                        {
                            Console.WriteLine("Лексема не должна заканчиваться точкой, исправьте");
                            buf1 = "";
                            flag = true;
                            break;
                        }
                        buf  = buf + str[i];
                        buf1 = buf1.Replace(buf, "");
                        i    = -1;
                    }

                    k++;
                    if (k == str.Length)
                    {
                        break;
                    }
                }


                if (flag == false)
                {
                    //Console.WriteLine("Lexer: ");

                    //foreach (var lexem in lexems) //Цикл по полученному списку
                    //{
                    //    Console.WriteLine(lexem.TokType + " " + lexem.Value);
                    //}
                    var lexems = LexicalAnalyzer.getLex(str); //Запуск лексического анализатора
                    SyntaxAnalyzer.getParse(lexems);
                }
            } while (end_enter() != "Да");
        }
Exemplo n.º 21
0
        public static void AnalyzeSyntax(Token root)
        {
            SyntaxAnalyzer analyzer = new SyntaxAnalyzer();

            Token srcs = root.Find("Sources");

            Token syntax = root.Find("Syntax");
            List<Token> synflw = new List<Token>();
            foreach (Token f in srcs.Follows)
            {
                if (f.Group == "SourcePath") { continue; }
                if (f.Group == "SourceText") { synflw.Add(analyzer.Run(f.Value, f.First.Value)); }
            }
            syntax.Follows = synflw.ToArray();
        }
Exemplo n.º 22
0
            public static void InterpretingInConsole(this SSScope scope)
            {
                var w      = new Stopwatch();
                var file   = new CodeFile();
                var syntax = new SyntaxAnalyzer();

                while (true)
                {
                    file.ReSet();
                    syntax.ReSet();
                    try
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write(">>> ");
                        String code;
                        if ((code = Console.ReadLine()).NotEmpty())
                        {
                            file.SourceCode = code;
                            file.Parse();
                            if (file.ErrorList.NotEmpty())
                            {
                                file.PrintErrors(Console.Out);
                            }
                            syntax.Take(file.TokenList);
                            if (syntax.ErrorList.NotEmpty())
                            {
                                syntax.PrintErrors(Console.Out);
                            }

                            Console.ForegroundColor = ConsoleColor.Green;

                            w.Reset();
                            w.Start();
                            Console.WriteLine(">>> " + syntax.Expressions.First().Evaluate(scope));
                            w.Stop();
                            Console.WriteLine(w.ElapsedMilliseconds + "ms");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                        // ex.StackTrace.Split("at".ToCharArray()).Take(3).ForEach(a => scope.Output.WriteLine(a));
                    }
                }
            }
Exemplo n.º 23
0
        private Container <Diagnostic> Validate(string text)
        {
            var tokens = new LexicalAnalyzer().GetTokens(text);
            var result = new SyntaxAnalyzer().Parse(tokens);

            if (result.IsError)
            {
                return
                    (new Container <Diagnostic>(result.parseErrorInfos.Select(e => new Diagnostic()
                {
                    Range = ToRange(e.ErrorRangePosition),
                    Message = e.FullErrorMessage,
                })));
            }

            return(new Container <Diagnostic>());
        }
Exemplo n.º 24
0
        /// <summary>
        /// 执行更新命令
        /// </summary>
        /// <param name="cmd">Command对象</param>
        /// <param name="oql">对象查询语句</param>
        /// <param name="parameters">参数列表</param>
        /// <returns>影响的记录行数</returns>
        internal static int ExcuteUpdateCommand(SqlCommand cmd, string oql, ParameterList parameters)
        {
            //解析过滤部份Sql语句
            string updateString = SyntaxAnalyzer.ParseSql(oql, new JCUserMap());

            cmd.CommandText = "update JC_User set " + updateString;
            cmd.Parameters.Clear();
            //添加参数
            if (parameters != null)
            {
                foreach (string key in parameters.Keys)
                {
                    cmd.Parameters.Add(new SqlParameter(key, parameters[key]));
                }
            }
            return(cmd.ExecuteNonQuery());
        }
        public BlockCreator(SyntaxAnalyzer syntaxAnalyzer, Point upperLeft)
        {
            this.syntaxAnalyzer = syntaxAnalyzer;
            this.upperLeft = upperLeft;
            x = upperLeft.X;
            y = 0;
            blocks = new List<TextBlock>();
            tokens = new Stack<Token>();

            rows = new List<List<TextBlock>>();
            rows.Add(new List<TextBlock>());

            toCenter = new List<BlockSpan>();

            Start();
            CenterBlocks();
        }
        public static void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess)
        {
            if (SyntaxAnalyzer.IsEnumerableExtensionMethod(invocation, "Count", 1, context.SemanticModel, context.CancellationToken))
            {
                string propertyName = SyntaxHelper.GetCountOrLengthPropertyName(memberAccess.Expression, context.SemanticModel, allowImmutableArray: true, cancellationToken: context.CancellationToken);

                if (propertyName != null)
                {
                    TextSpan span = TextSpan.FromBounds(memberAccess.Name.Span.Start, invocation.Span.End);
                    if (invocation
                        .DescendantTrivia(span)
                        .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        Diagnostic diagnostic = Diagnostic.Create(
                            DiagnosticDescriptors.ReplaceCountMethodWithCountOrLengthProperty,
                            Location.Create(context.Node.SyntaxTree, span),
                            ImmutableDictionary.CreateRange(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("PropertyName", propertyName) }),
                            propertyName);

                        context.ReportDiagnostic(diagnostic);
                    }
                }
                else if (invocation.Parent?.IsKind(
                             SyntaxKind.EqualsExpression,
                             SyntaxKind.GreaterThanExpression,
                             SyntaxKind.LessThanExpression) == true)
                {
                    var binaryExpression = (BinaryExpressionSyntax)invocation.Parent;

                    if (IsFixableBinaryExpression(binaryExpression))
                    {
                        TextSpan span = TextSpan.FromBounds(invocation.Span.End, binaryExpression.Span.End);

                        if (binaryExpression
                            .DescendantTrivia(span)
                            .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                        {
                            context.ReportDiagnostic(
                                DiagnosticDescriptors.ReplaceCountMethodWithAnyMethod,
                                binaryExpression.GetLocation());
                        }
                    }
                }
            }
        }
Exemplo n.º 27
0
    /// <summary>
    /// Revisa si la sintaxis del escenario es correcta, recibe como parámetro las sentencias
    /// </summary>
    /// <returns></returns>
    public bool CheckSyntaxB(string xsentences)
    {
        if (xsentences.CompareTo(String.Empty) != 0)
        {
            sentences = xsentences;
            lexan     = new LexicalAnalyzer();                                  //Analizador léxico nuevo
            synan     = new SyntaxAnalyzer(lexan.createTableSymbol(sentences)); //Crear tabla de símbolo nueva

            ResultAnalysis ra = synan.checkSyntax();
            if (ra.Error.Count == 0)
            {
                return(true);
            }

            return(false);
        }
        return(true);
    }
Exemplo n.º 28
0
        public static void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess)
        {
            if (invocation.Parent?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == false &&
                SyntaxAnalyzer.IsEnumerableExtensionMethod(invocation, "Any", 1, context.SemanticModel, context.CancellationToken))
            {
                string propertyName = SyntaxHelper.GetCountOrLengthPropertyName(memberAccess.Expression, context.SemanticModel, allowImmutableArray: false, cancellationToken: context.CancellationToken);

                if (propertyName != null)
                {
                    string messageArg = null;

                    TextSpan span = TextSpan.FromBounds(memberAccess.Name.Span.Start, invocation.Span.End);

                    if (invocation.DescendantTrivia(span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        if (invocation.Parent?.IsKind(SyntaxKind.LogicalNotExpression) == true)
                        {
                            var logicalNot = (PrefixUnaryExpressionSyntax)invocation.Parent;

                            if (logicalNot.OperatorToken.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()) &&
                                logicalNot.Operand.GetLeadingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                            {
                                messageArg = $"{propertyName} == 0";
                            }
                        }
                        else
                        {
                            messageArg = $"{propertyName} > 0";
                        }
                    }

                    if (messageArg != null)
                    {
                        Diagnostic diagnostic = Diagnostic.Create(
                            DiagnosticDescriptors.ReplaceAnyMethodWithCountOrLengthProperty,
                            Location.Create(context.Node.SyntaxTree, span),
                            ImmutableDictionary.CreateRange(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("PropertyName", propertyName) }),
                            messageArg);

                        context.ReportDiagnostic(diagnostic);
                    }
                }
            }
        }
Exemplo n.º 29
0
        private static bool IsEnumerableElementAtMethod(
            InvocationExpressionSyntax invocation,
            SemanticModel semanticModel)
        {
            var methodSymbol = semanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;

            if (methodSymbol?.ReducedFrom != null)
            {
                methodSymbol = methodSymbol.ReducedFrom;

                return(methodSymbol.MetadataName == "ElementAt" &&
                       methodSymbol.Parameters.Length == 2 &&
                       methodSymbol.ContainingType?.Equals(semanticModel.Compilation.GetTypeByMetadataName("System.Linq.Enumerable")) == true &&
                       SyntaxAnalyzer.IsGenericIEnumerable(methodSymbol.Parameters[0].Type) &&
                       methodSymbol.Parameters[1].Type.IsInt32());
            }

            return(false);
        }
        public static async Task ComputeRefactoringAsync(RefactoringContext context, ThrowStatementSyntax throwStatement)
        {
            ExpressionSyntax expression = throwStatement.Expression;

            if (expression?.IsMissing == false &&
                context.Span.IsContainedInSpanOrBetweenSpans(throwStatement))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

                if (typeSymbol?.IsErrorType() == false &&
                    SyntaxAnalyzer.IsException(typeSymbol, semanticModel))
                {
                    MemberDeclarationSyntax member = GetContainingMember(throwStatement);

                    if (member != null)
                    {
                        SyntaxTrivia trivia = member.GetSingleLineDocumentationComment();

                        if (trivia.IsSingleLineDocumentationCommentTrivia())
                        {
                            var comment = trivia.GetStructure() as DocumentationCommentTriviaSyntax;

                            if (comment?.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia) == true)
                            {
                                string exceptionName = typeSymbol
                                                       .ToMinimalDisplayString(semanticModel, trivia.FullSpan.End)
                                                       .Replace('<', '{')
                                                       .Replace('>', '}');

                                if (!ContainsException(comment, typeSymbol, semanticModel, context.CancellationToken))
                                {
                                    context.RegisterRefactoring(
                                        "Add exception to documentation comment",
                                        cancellationToken => RefactorAsync(context.Document, trivia, exceptionName, cancellationToken));
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 31
0
        private static string GetCountOrLengthPropertyName(
            ExpressionSyntax expression,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ITypeSymbol typeSymbol = semanticModel
                                     .GetTypeInfo(expression, cancellationToken)
                                     .Type;

            if (typeSymbol?.IsErrorType() == false &&
                !SyntaxAnalyzer.IsGenericIEnumerable(typeSymbol))
            {
                if (typeSymbol.BaseType?.SpecialType == SpecialType.System_Array)
                {
                    return("Length");
                }

                if (SyntaxAnalyzer.IsGenericImmutableArray(typeSymbol, semanticModel))
                {
                    return("Length");
                }

                ImmutableArray <INamedTypeSymbol> allInterfaces = typeSymbol.AllInterfaces;

                for (int i = 0; i < allInterfaces.Length; i++)
                {
                    if (allInterfaces[i].ConstructedFrom.SpecialType == SpecialType.System_Collections_Generic_ICollection_T)
                    {
                        foreach (ISymbol members in typeSymbol.GetMembers("Count"))
                        {
                            if (members.IsProperty() &&
                                members.IsPublic())
                            {
                                return("Count");
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 32
0
            public static void BulkEval(this SSScope scope, string src)
            {
                if (!src.NotEmpty())
                {
                    return;
                }

                var file   = new CodeFile();
                var syntax = new SyntaxAnalyzer();

                file.SourceCode = src;
                file.Parse();
                if (file.ErrorList.Count > 0)
                {
                    scope.Output.WriteLine("Errors:");
                    syntax.ErrorList.ForEach(a => scope.Output.WriteLine(a.ToString()));
                    return;
                }
                syntax.Take(file.TokenList);
                if (syntax.ErrorList.NotEmpty())
                {
                    scope.Output.WriteLine("Errors:");
                    syntax.ErrorList.ForEach(a => scope.Output.WriteLine(a.ToString()));
                    return;
                }
                try
                {
                    // prevent lazy eval
                    foreach (var exp in syntax.Expressions)
                    {
                        exp.Evaluate(scope); //);
                    }
                }//
                catch (Exception ex)
                {
                    scope.Output.WriteLine("Failed to evaluate");
                    scope.Output.WriteLine(ex);
                    ex.StackTrace.Split('\r').Take(3).ForEach(a => scope.Output.WriteLine(a));
                }
                //    scope.VariableTable.ForEach(a => scope.Output.WriteLine(">>> Added {0} : {1} "
                //       .Fmt(a.Key, a.Value.GetType())));
            }
Exemplo n.º 33
0
        static void Main(string[] args)
        {
            string sourcePath = "";

            if (args.Any())
            {
                sourcePath = args[0];
            }
            else
            {
                Console.Write($"Paste source file need to compile:{Environment.NewLine}" +
                              "> ");
                sourcePath = Console.ReadLine();
            }

            try
            {
                LexicalAnalyzer.Parse(sourcePath);
                SyntaxAnalyzer.Parse();
                Console.WriteLine("Синтаксический анализ окончен");
                SemanticAnalyzer.Parse();
                Console.WriteLine("Семантический анализ окончен");
                if (!(IsSyntaxCorrect && Root.IsSemanticCorrect))
                {
                    Console.WriteLine("Возникли ошибки сборки. Ассемблерный код не будет сгенерирован");
                }
                else
                {
                    Root.GenerateIntermediateCode();
                    GeneratingAssembleCode.Generate();
                    Console.WriteLine("Ассемблерный код сгенерирован");
                }
            }
            catch (CompilerException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("Press any key to finish...");
            Console.ReadLine();
        }
Exemplo n.º 34
0
        private static bool IsImmutableArrayExtensionMethod(
            InvocationExpressionSyntax invocation,
            string methodName,
            SemanticModel semanticModel)
        {
            var methodSymbol = semanticModel
                               .GetSymbolInfo(invocation)
                               .Symbol as IMethodSymbol;

            if (methodSymbol?.ReducedFrom != null)
            {
                methodSymbol = methodSymbol.ReducedFrom;

                return(methodSymbol.MetadataName == methodName &&
                       methodSymbol.Parameters.Length == 1 &&
                       methodSymbol.ContainingType?.Equals(semanticModel.Compilation.GetTypeByMetadataName("System.Linq.ImmutableArrayExtensions")) == true &&
                       SyntaxAnalyzer.IsGenericImmutableArray(methodSymbol.Parameters[0].Type, semanticModel));
            }

            return(false);
        }
Exemplo n.º 35
0
 public SyntaxAnalyzerFxt()
 {
     Analyzer = new SyntaxAnalyzer();
 }
Exemplo n.º 36
0
        public static void Tokenize(string[] args, Box<bool> quit, LineEditMode lem)
        {
            string src;
            src = Cty.ToText(lem.Lines);
            SyntaxAnalyzer p = new SyntaxAnalyzer();
            p.Init(src, "");

            ITokenEnumerator tkz = p.Tokens;
            while (tkz.EOF == false)
            {
                lem.CW.WN(tkz.Cur.ToString());
                tkz.Next();
            }
        }