コード例 #1
0
ファイル: Parser.cs プロジェクト: cg123/xenko
        /// <summary>
        /// Initializes a new instance of the <see cref="Parser"/> class.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="scanner">The scanner.</param>
        /// <param name="root">The root.</param>
        /// <exception cref="Exception">
        ///   </exception>
        public Parser(LanguageData language, Scanner scanner, NonTerminal root)
        {
            Language = language;
            Context = new ParsingContext(this);
            Scanner = scanner ?? language.CreateScanner();

            if (Scanner != null)
            {
                Scanner.Initialize(this);
            } 
            else
            {
                Language.Errors.Add(GrammarErrorLevel.Error, null, "Scanner is not initialized for this grammar");
            }
            CoreParser = new CoreParser(this);
            Root = root;
            if (Root == null)
            {
                Root = Language.Grammar.Root;
                InitialState = Language.ParserData.InitialState;
            }
            else
            {
                if (Root != Language.Grammar.Root && !Language.Grammar.SnippetRoots.Contains(Root))
                {
                    throw new Exception(string.Format(Resources.ErrRootNotRegistered, root.Name));
                }

                InitialState = Language.ParserData.InitialStates[Root];
            }
        }
コード例 #2
0
ファイル: IntegrationTests.cs プロジェクト: gregberns/Irony
 private void Init(Grammar grammar) {
   _grammar = grammar;
   _language = new LanguageData(_grammar); 
   var parser = new Parser(_language);
   _scanner = parser.Scanner;
   _context = parser.Context;
   _context.Mode = ParseMode.VsLineScan;
 }
コード例 #3
0
ファイル: EditorAdapter.cs プロジェクト: Rezura/LiveSplit
 public EditorAdapter(LanguageData language) {
   _parser = new Parser(language);
   _scanner = _parser.Scanner;
   _colorizerThread = new Thread(ColorizerLoop);
   _colorizerThread.IsBackground = true;
   _parserThread = new Thread(ParserLoop);
   _parserThread.IsBackground = true;
 }
コード例 #4
0
ファイル: Parser.cs プロジェクト: anukat2015/sones
 public Parser(LanguageData language, NonTerminal root) {
   Language = language;
   Context = new ParsingContext(this);
   Scanner = new Scanner(this);
   CoreParser = new CoreParser(this);
   Root = root; 
   if(Root == null) {
     Root = Language.Grammar.Root;
     InitialState = Language.ParserData.InitialState;
   } else {
     if(Root != Language.Grammar.Root && !Language.Grammar.SnippetRoots.Contains(Root))
       throw new Exception(string.Format(Resources.ErrRootNotRegistered, root.Name));
     InitialState = Language.ParserData.InitialStates[Root]; 
   }
 }
コード例 #5
0
ファイル: AnalysisEntry.cs プロジェクト: DarrenGZY/NPLTools
        /// <summary>
        /// Format selection block
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="textView"></param>
        internal void FormatBlock(ITextView textView)
        {
            int startLine = textView.Selection.Start.Position.GetContainingLine().LineNumber;
            int endLine   = textView.Selection.End.Position.GetContainingLine().LineNumber;

            ITextSnapshot snapshot = textView.TextSnapshot;

            int[]  indentations;
            bool[] fixedLines;
            this.Model.RetrieveIndentationsFromSyntaxTree(out indentations, out fixedLines);

            //Get long-string and block-comment spans, which are not to be added or removed indentations


            //Need a new parser and scanner here
            Irony.Parsing.Parser  parser  = new Irony.Parsing.Parser(IronyParser.LuaGrammar.Instance);
            Irony.Parsing.Scanner scanner = parser.Scanner;

            //rule 1: insert space before and after binary operator if there not any
            //rule 2: insert space after comma, semicolon if there not any
            //rule 3: indentation increase inside block
            //rule 4: multiple spaces replaced by a single space
            using (var edit = textView.TextBuffer.CreateEdit())
            {
                //IEnumerable<ITextSnapshotLine> lines = view.TextSnapshot.Lines;
                for (int lineNumber = startLine; lineNumber <= endLine; lineNumber++)
                {
                    if (fixedLines[lineNumber])
                    {
                        continue;
                    }

                    ITextSnapshotLine line = snapshot.GetLineFromLineNumber(lineNumber);
                    int    lineOffset      = line.Start.Position;
                    string lineText        = line.GetText();

                    scanner.VsSetSource(lineText, 0);

                    int state = 0;
                    Irony.Parsing.Token currentToken = scanner.VsReadToken(ref state);
                    Irony.Parsing.Token lastToken    = null;
                    // add space before the first token
                    if (currentToken != null)
                    {
                        Span   editSpan    = new Span(lineOffset, currentToken.Location.Position);
                        string indentation = "";
                        for (int i = 0; i < indentations[lineNumber]; ++i)
                        {
                            indentation += "\t";
                        }
                        edit.Replace(editSpan, indentation);
                    }

                    while (currentToken != null && currentToken.Terminal.Name != "SYNTAX_ERROR")
                    {
                        Irony.Parsing.Token nextToken = scanner.VsReadToken(ref state);
                        if (currentToken.Text == "+" ||
                            currentToken.Text == "=" ||
                            currentToken.Text == "*" ||
                            currentToken.Text == "\\" ||
                            currentToken.Text == "-")
                        {
                            if (lastToken != null)
                            {
                                int spaceStart  = lastToken.Location.Position + lastToken.Length;
                                int spaceLength = currentToken.Location.Position - spaceStart;
                                if (spaceLength != 1)
                                {
                                    Span span = new Span(lineOffset + spaceStart, spaceLength);
                                    edit.Replace(span, " ");
                                }
                            }

                            if (nextToken != null)
                            {
                                int spaceStart  = currentToken.Location.Position + currentToken.Length;
                                int spaceLength = nextToken.Location.Position - spaceStart;
                                if (spaceLength != 1)
                                {
                                    Span span = new Span(lineOffset + spaceStart, spaceLength);
                                    edit.Replace(span, " ");
                                }
                            }
                        }
                        else if (currentToken.Text == "," ||
                                 currentToken.Text == ";")
                        {
                            if (nextToken != null)
                            {
                                int spaceStart  = currentToken.Location.Position + currentToken.Length;
                                int spaceLength = nextToken.Location.Position - spaceStart;
                                if (spaceLength != 1)
                                {
                                    Span span = new Span(lineOffset + spaceStart, spaceLength);
                                    edit.Replace(span, " ");
                                }
                            }
                        }

                        lastToken    = currentToken;
                        currentToken = nextToken;
                    }
                }
                edit.Apply();
            }
        }