public static SyntaxTrivia Scan(SlidingTextWindow TextWindow) { char xcNextChar = TextWindow.PeekCharacter(); switch (xcNextChar) { case '\r': if (TextWindow.PeekCharacter(1) == '\n') { TextWindow.SkipAhead(2); return(SyntaxFactory.EndOfLine("\r\n")); } return(SyntaxFactory.EndOfLine("\r")); case '\n': TextWindow.SkipAhead(1); return(SyntaxFactory.EndOfLine("\n")); default: // Try again if (SyntaxUtilities.IsNewLineCharacter(xcNextChar)) { TextWindow.SkipAhead(1); return(SyntaxFactory.EndOfLine("\n")); } return(null); } }
/// <summary> /// Used to analyse the current textwindow and retrieve/skip any /// trivia which includes things such as whitespace, newlines, /// comments and multi-line comments /// Returns them as a flat list /// </summary> /// <param name="bIsTrailing"></param> /// <returns></returns> private List <SyntaxTrivia> LexSyntaxTrivia(Boolean bIsTrailing = false) { // Initialise variables List <SyntaxTrivia> aoReturnList = new List <SyntaxTrivia>(); Boolean bNewLineCharFound = false; // Just keep lexing until we are told to stop // or we run out of characters to process while (TextWindow.HasCharactersLeftToProcess()) { // Check the next character and switch based on that char xcNextChar = TextWindow.PeekCharacter(); if (SyntaxUtilities.IsWhiteSpace(xcNextChar)) { // Try and scan the item SyntaxTrivia oNewItem = WhiteSpaceScanner.Scan(TextWindow); // Insert it aoReturnList.Add(oNewItem); } else if (SyntaxUtilities.IsNewLineCharacter(xcNextChar)) { bNewLineCharFound = true; // Do not steal trailing comments if a newline has been found if (bIsTrailing) { break; } // Try and scan the item SyntaxTrivia oNewItem = NewLineScanner.Scan(TextWindow); // Insert it aoReturnList.Add(oNewItem); } // Handle Comments else if (xcNextChar == '-') { // Do not touch a single -, only play with -- if (TextWindow.PeekCharacter(1) != '-') { return(aoReturnList); } // Only pick up inlines if they are directly trailing an item // Do not steal another nodes leading comments if (bNewLineCharFound && bIsTrailing) { break; } SyntaxTrivia oComment = CommentScanner.ScanSingleLineComment(TextWindow); aoReturnList.Add(oComment); } else if (xcNextChar == '/') { // Do not touch a single /, only play with /* if (TextWindow.PeekCharacter(1) != '*') { return(aoReturnList); } // Do not steal trailing comments if a newline has been found if (bNewLineCharFound && bIsTrailing) { break; } SyntaxTrivia oComment = CommentScanner.ScanMultiLineComment(TextWindow); aoReturnList.Add(oComment); } else { break; } } return(aoReturnList); }
public static SyntaxTrivia ScanSingleLineComment(SlidingTextWindow TextWindow) { // Check next char char xcNextChar = TextWindow.PeekCharacter(); // If it is two hypens together if (xcNextChar == '-' && TextWindow.PeekCharacter(1) == '-') { // Ignore Comment syntax (we just want text) TextWindow.SkipAhead(2); Boolean bIsCommentTerminated = false; String sCommentLine = String.Empty; // Iterate indefinitely until we reach the end for (int iIndex = 0; TextWindow.Position + iIndex < TextWindow.Count; iIndex++) { // NewLine char (Terminated properly) if (SyntaxUtilities.IsNewLineCharacter(TextWindow.PeekCharacter(iIndex))) { // Break the loop and the comment is terminated properly bIsCommentTerminated = true; break; } // Keep adding characters to our intermediate var sCommentLine += TextWindow.PeekCharacter(iIndex); } // Just advance the length of the text (we can't fix peoples comments 100% // of the time) TextWindow.SkipAhead(sCommentLine.Length); // If our comment was terminated properly if (bIsCommentTerminated) { return(SyntaxFactory.SingleLineComment(sCommentLine)); } // Not terminated properly.. (hardest case) // If we reach the end of the window its either an EOF comment (which is unlikely) // or it is an unterminated comment else { // Intermediate Var (Add all the text as a comment) SyntaxTrivia oCommentNode = SyntaxFactory.SingleLineComment(sCommentLine); // Add the erroraneous message oCommentNode.Comments.Add(ErrorMessageLibrary.GetErrorMessage(8002, "Single Line Comment")); // return the comment node with everything in comments return(oCommentNode); } } // This token should not be here (there is something wrong) // Intermediate Var (Add all the text as a comment) SyntaxTrivia oErraneousToken = new SyntaxTrivia( SyntaxKind.UnknownNode, // Unknown Convert.ToString(TextWindow.PeekCharacter())); // Get the character out of the stream oErraneousToken.Comments.Add(ErrorMessageLibrary.GetErrorMessage(8001, Convert.ToString(TextWindow.PopCharacter()))); // return the comment node with everything in comments return(oErraneousToken); }