Example #1
0
        public LexerPosition ConsumeComment(Token <GenericToken> comment, ReadOnlyMemory <char> source, LexerPosition lexerPosition)
        {
            ReadOnlyMemory <char> commentValue;

            if (comment.IsSingleLineComment)
            {
                var position = lexerPosition.Index;
                commentValue      = EOLManager.GetToEndOfLine(source, position);
                position          = position + commentValue.Length;
                comment.SpanValue = commentValue;
                return(new LexerPosition(position, lexerPosition.Line + 1, 0));
                //LexerFsm.MovePosition(position, LexerFsm.CurrentLine + 1, 0);
            }
            else if (comment.IsMultiLineComment)
            {
                var position = lexerPosition.Index;

                var end = source.Span.Slice(position).IndexOf(MultiLineCommentEnd.AsSpan());
                if (end < 0)
                {
                    position = source.Length;
                }
                else
                {
                    position = end + position;
                }
                commentValue      = source.Slice(lexerPosition.Index, position - lexerPosition.Index);
                comment.SpanValue = commentValue;

                var newPosition = lexerPosition.Index + commentValue.Length + MultiLineCommentEnd.Length;
                var lines       = EOLManager.GetLinesLength(commentValue);
                var newLine     = lexerPosition.Line + lines.Count - 1;
                int newColumn;
                if (lines.Count > 1)
                {
                    newColumn = lines.Last() + MultiLineCommentEnd.Length;
                }
                else
                {
                    newColumn = lexerPosition.Column + lines[0] + MultiLineCommentEnd.Length;
                }

                return(new LexerPosition(newPosition, newLine, newColumn));
                // LexerFsm.MovePosition(newPosition, newLine, newColumn);
            }

            return(lexerPosition);
        }
Example #2
0
        public void ConsumeComment(Token <GenericToken> comment, string source)
        {
            string commentValue = "";

            if (comment.IsSingleLineComment)
            {
                int position = LexerFsm.CurrentPosition;
                commentValue  = EOLManager.GetToEndOfLine(source, position);
                position      = position + commentValue.Length;
                comment.Value = commentValue.Replace("\n", "").Replace("\r", "");
                LexerFsm.Move(position, LexerFsm.CurrentLine + 1, 0);
            }
            else if (comment.IsMultiLineComment)
            {
                int position = LexerFsm.CurrentPosition;
                int end      = source.IndexOf(MultiLineCommentEnd, position);
                if (end < 0)
                {
                    position = source.Length;
                }
                else
                {
                    position = end;
                }
                commentValue  = source.Substring(LexerFsm.CurrentPosition, position - LexerFsm.CurrentPosition);
                comment.Value = commentValue;

                int newPosition = LexerFsm.CurrentPosition + commentValue.Length + MultiLineCommentEnd.Length;

                var lines     = EOLManager.GetLines(commentValue);
                int newLine   = LexerFsm.CurrentLine + lines.Count - 1;
                int newColumn = 0;
                if (lines.Count > 1)
                {
                    newColumn = lines[lines.Count - 1].Length + MultiLineCommentEnd.Length;
                }
                else
                {
                    newColumn = LexerFsm.CurrentColumn + lines[0].Length + MultiLineCommentEnd.Length;
                }


                LexerFsm.Move(newPosition, newLine, newColumn);
            }
        }