Exemple #1
0
        public void Start()
        {
            Scanner scanner = new Scanner();
            Parser parser = new Parser(scanner);

            while (!disposing)
            {
                System.Threading.Thread.Sleep(250);
                if (!textchanged)
                    continue;

                textchanged = false;

                scanner.Init(text);
                SyntaxTree = parser.Parse(text, "", new GrammarTree());
                if (SyntaxTree.Errors.Count > 0)
                    SyntaxTree.Errors.Clear();

                try
                {
                    if (Grammar == null)
                        Grammar = (Grammar)SyntaxTree.Eval();
                    else
                    {

                        lock (Grammar)
                        {
                            Grammar = (Grammar)SyntaxTree.Eval();
                        }
                    }
                }
                catch (Exception)
                {

                }

                if (textchanged)
                    continue;

                lock (marker)
                {
                    marker.Clear();
                    foreach (ParseError err in SyntaxTree.Errors)
                    {
                        marker.AddWord(err.Position, err.Length, System.Drawing.Color.Red, err.Message);
                    }
                }

                if (UpdateSyntax != null)
                    UpdateSyntax.Invoke(this, new EventArgs());
            }
        }
Exemple #2
0
        public Grammar ParseGrammar(string input, string grammarFile)
        {
            Grammar grammar = null;
            Scanner scanner = new Scanner();
            Parser parser = new Parser(scanner);

            ParseTree tree = parser.Parse(input, grammarFile, new GrammarTree());

            if (tree.Errors.Count > 0)
            {
                this.parseErrorDelegate(tree, this.output);
            }
            else
            {
                grammar = (Grammar)tree.Eval();
                grammar.Preprocess();

                if (tree.Errors.Count == 0)
                {
                    this.output.AppendLine(grammar.PrintGrammar());
                    this.output.AppendLine(grammar.PrintFirsts());

                    this.output.AppendLine("Parse successful!\r\n");
                }
            }
            return grammar;
        }
 public Parser(Scanner scanner)
 {
     this.scanner = scanner;
 }
Exemple #4
0
        private void CompileGrammar()
        {
            DateTime starttimer = DateTime.Now;

            if (string.IsNullOrEmpty(GrammarFile))
                SaveGrammarAs();

            if (string.IsNullOrEmpty(GrammarFile))
                return;

            compiler = new TinyPG.Compiler.Compiler();
            StringBuilder output = new StringBuilder();

            // clear tree
            tvParsetree.Nodes.Clear();

            string input = textEditor.Text;
            Scanner scanner = new Scanner();
            Parser parser = new Parser(scanner);
            ParseTree tree = parser.Parse(input, GrammarFile, new GrammarTree());

            if (tree.Errors.Count > 0)
            {
                foreach (ParseError error in tree.Errors)
                    output.AppendLine(string.Format("({0},{1}): {2}", error.Line, error.Column, error.Message));
                output.AppendLine("Syntax errors in grammar found.");

                if (tree.Errors.Count > 0)
                    textEditor.Select(tree.Errors[0].Position, tree.Errors[0].Length > 0 ? tree.Errors[0].Length : 1);
            }
            else
            {

                grammar = (Grammar)tree.Eval();
                grammar.Preprocess();

                if (tree.Errors.Count == 0)
                {
                    output.AppendLine(grammar.PrintGrammar());
                    output.AppendLine(grammar.PrintFirsts());

                    output.AppendLine("Parse successful!\r\n");
                }
            }

            if (grammar != null)
            {
                SetHighlighterLanguage(grammar.Directives["TinyPG"]["Language"]);
                if (tree.Errors.Count > 0)
                {
                    foreach (ParseError error in tree.Errors)
                        output.AppendLine(string.Format("({0},{1}): {2}", error.Line, error.Column, error.Message));
                    output.AppendLine("Semantic errors in grammar found.");
                    if (tree.Errors.Count > 0)
                        textEditor.Select(tree.Errors[0].Position, tree.Errors[0].Length > 0 ? tree.Errors[0].Length : 1);

                }
                else
                {
                    output.AppendLine("Building code...");
                    compiler.Compile(grammar);
                    if (!compiler.IsCompiled)
                    {
                        foreach (string err in compiler.Errors)
                            output.AppendLine(err);
                        output.AppendLine("Compilation contains errors, could not compile.");
                    }
                    else
                    {
                        TimeSpan span = DateTime.Now.Subtract(starttimer);
                        output.AppendLine("Compilation successfull in " + span.TotalMilliseconds + "ms.");

                    }
                }
            }
            textOutput.Text = output.ToString();
            textOutput.Select(textOutput.Text.Length, 0);
            textOutput.ScrollToCaret();

            if (grammar != null && tree.Errors.Count == 0)
            {
                ICodeGenerator generator;

                string language = grammar.Directives["TinyPG"]["Language"];
                foreach (Directive d in grammar.Directives)
                {
                    generator = CodeGeneratorFactory.CreateGenerator(d.Name, language);
                    if (generator != null && d.ContainsKey("FileName"))
                        generator.FileName = d["FileName"];

                    if (generator != null && d["Generate"].ToLower() == "true")
                        File.WriteAllText(grammar.GetOutputPath() + generator.FileName, generator.Generate(grammar, false));
                }
            }
        }
Exemple #5
0
        public TextHighlighter(RichTextBox textbox, Scanner scanner, Parser parser)
        {
            Textbox = textbox;
            Scanner = scanner;
            Parser = parser;

            ClearUndo();

            //Tree = Parser.Parse(Textbox.Text);
            Textbox.TextChanged += new EventHandler(Textbox_TextChanged);
            textbox.KeyDown += new KeyEventHandler(textbox_KeyDown);
            Textbox.SelectionChanged += new EventHandler(Textbox_SelectionChanged);
            Textbox.Disposed += new EventHandler(Textbox_Disposed);

            SwitchContext = null;
            currentContext = Tree;

            threadAutoHighlight = new Thread(AutoHighlightStart);
            threadAutoHighlight.Start();
        }