Ejemplo n.º 1
0
 private PartialParseResult HandleInsertion(
     Span target,
     char previousChar,
     TextChange change
     )
 {
     // What are we inserting after?
     if (previousChar == '.')
     {
         return(HandleInsertionAfterDot(target, change));
     }
     else if (
         ParserHelpers.IsIdentifierPart(previousChar) ||
         previousChar == ')' ||
         previousChar == ']'
         )
     {
         return(HandleInsertionAfterIdPart(target, change));
     }
     else
     {
         return(PartialParseResult.Rejected);
     }
 }
Ejemplo n.º 2
0
        internal static void ParseSummary(IStringReader reader, ChangeSetDetail detail)
        {
            reader.SkipWhitespace();

            while (!reader.Done)
            {
                string line = reader.ReadLine();

                if (ParserHelpers.IsSingleNewLine(line))
                {
                    break;
                }
                else if (line.Contains('\t'))
                {
                    // n	n	path
                    string[] parts = line.Split('\t');
                    int      insertions;
                    Int32.TryParse(parts[0], out insertions);
                    int deletions;
                    Int32.TryParse(parts[1], out deletions);
                    string path = parts[2].TrimEnd();

                    detail.Files[path] = new FileInfo
                    {
                        Insertions = insertions,
                        Deletions  = deletions,
                        Binary     = parts[0] == "-" && parts[1] == "-"
                    };
                }
                else
                {
                    // n files changed, n insertions(+), n deletions(-)
                    ParserHelpers.ParseSummaryFooter(line, detail);
                }
            }
        }
Ejemplo n.º 3
0
        private StateResult QuotedLiteral(char quote, CSharpSymbolType literalType)
        {
            TakeUntil(c => c == '\\' || c == quote || ParserHelpers.IsNewLine(c));
            if (CurrentCharacter == '\\')
            {
                TakeCurrent(); // Take the '\'

                // If the next char is the same quote that started this
                if (CurrentCharacter == quote || CurrentCharacter == '\\')
                {
                    TakeCurrent(); // Take it so that we don't prematurely end the literal.
                }
                return(Stay());
            }
            else if (EndOfFile || ParserHelpers.IsNewLine(CurrentCharacter))
            {
                CurrentErrors.Add(new RazorError(RazorResources.ParseError_Unterminated_String_Literal, CurrentStart));
            }
            else
            {
                TakeCurrent(); // No-op if at EOF
            }
            return(Transition(EndSymbol(literalType), Data));
        }
Ejemplo n.º 4
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc.Count > 3 &&
                ParserHelpers.IsEqual(tc.First.Text, Field.KEY_ELSEIF))
            {
                if (tc[1].TokenKind == TokenKind.LeftParentheses &&
                    tc.Last.TokenKind == TokenKind.RightParentheses)
                {
                    ElseifTag tag = new ElseifTag();

                    TokenCollection coll = new TokenCollection();
                    coll.Add(tc, 2, tc.Count - 2);
                    tag.Test = parser.Read(coll);

                    return(tag);
                }
                else
                {
                    throw new ParseException(string.Concat("syntax error near if:", tc), parser.Context.CurrentPath, tc.First.BeginLine, tc.First.BeginColumn);
                }
            }

            return(null);
        }
        public StringTextSnapshot(string content, int versionNumber)
        {
            Content = content;
            _lines  = new List <ITextSnapshotLine>();

            var start          = 0;
            var delimiterIndex = 0;

            while (delimiterIndex != -1)
            {
                var delimiterLength = 2;
                delimiterIndex = Content.IndexOf("\r\n", start, StringComparison.Ordinal);

                if (delimiterIndex == -1)
                {
                    delimiterLength = 1;
                    for (var i = start; i < Content.Length; i++)
                    {
                        if (ParserHelpers.IsNewLine(content[i]))
                        {
                            delimiterIndex = i;
                            break;
                        }
                    }
                }

                var nextLineStartIndex = delimiterIndex != -1 ? delimiterIndex + delimiterLength : Content.Length;

                var lineText = Content.Substring(start, nextLineStartIndex - start);
                _lines.Add(new SnapshotLine(lineText, start, this));

                start = nextLineStartIndex;

                Version = new TextVersion(versionNumber);
            }
        }
        private bool ParseResourceStatement(CodeBlockInfo block)
        {
            End(MetaCodeSpan.Create);

            SourceLocation endModelLocation = CurrentLocation;

            if (_modelStatementFound)
            {
                OnError(endModelLocation, String.Format(CultureInfo.CurrentCulture, "Only one '{0}' statement is allowed in a file.", ResourceKeyword));
            }

            _modelStatementFound = true;

            // Accept Whitespace up to the new line or non-whitespace character
            Context.AcceptWhiteSpace(false);

            string typeName = null;

            if (ParserHelpers.IsIdentifierStart(CurrentCharacter))
            {
                using (Context.StartTemporaryBuffer())
                {
                    // Accept a dotted-identifier, but allow <>
                    AcceptTypeName();
                    typeName = Context.ContentBuffer.ToString();
                    Context.AcceptTemporaryBuffer();
                }
            }
            else
            {
                OnError(endModelLocation, String.Format(CultureInfo.CurrentCulture, "The '{0}' keyword must be followed by a type name on the same line.", ResourceKeyword));
            }
            CheckForInheritsAndResourceStatements();
            End(ResourceSpan.Create(Context, typeName));
            return(false);
        }
Ejemplo n.º 7
0
 protected Func <char, bool> CharOrWhiteSpace(char character)
 {
     return(c => c == character || ParserHelpers.IsWhitespace(c) || ParserHelpers.IsNewLine(c));
 }
Ejemplo n.º 8
0
        public async Task <IReplayHeader> ReadReplayAsync(FileStream fileStream)
        {
            if (!fileStream.CanRead)
            {
                throw new IOException($"{exceptionOriginName} - Stream does not support reading");
            }

            // Read and check Magic Numbers
            ReplayType type;

            try
            {
                type = await ParserHelpers.GetReplayTypeAsync(fileStream);
            }
            catch (Exception ex)
            {
                throw new IOException($"{exceptionOriginName} - Reading Magic Number: " + ex.Message, ex);
            }

            if (type != ReplayType.ROFL)
            {
                throw new Exception($"{exceptionOriginName} - Selected file is not in valid ROFL format");
            }

            // Read and deserialize length fields
            byte[] lengthFieldBuffer;
            try
            {
                lengthFieldBuffer = await ParserHelpers.ReadBytesAsync(fileStream, 26, 262, SeekOrigin.Begin);
            }
            catch (Exception ex)
            {
                throw new IOException($"{exceptionOriginName} - Reading Length Header: " + ex.Message, ex);
            }

            LengthFields lengthFields;

            try
            {
                lengthFields = ParseLengthFields(lengthFieldBuffer);
            }
            catch (Exception ex)
            {
                throw new Exception($"{exceptionOriginName} - Parsing Length Header: " + ex.Message, ex);
            }


            // Read and deserialize metadata
            byte[] metadataBuffer;
            try
            {
                metadataBuffer = await ParserHelpers.ReadBytesAsync(fileStream, (int)lengthFields.MetadataLength, (int)lengthFields.MetadataOffset, SeekOrigin.Begin);
            }
            catch (Exception ex)
            {
                throw new IOException($"{exceptionOriginName} - Reading JSON Metadata: " + ex.Message, ex);
            }

            MatchMetadata metadataFields;

            try
            {
                metadataFields = ParseMetadata(metadataBuffer);
            }
            catch (Exception ex)
            {
                throw new Exception($"{exceptionOriginName} - Parsing Metadata Header: " + ex.Message, ex);
            }

            // Read and deserialize payload fields
            byte[] payloadBuffer;
            try
            {
                payloadBuffer = await ParserHelpers.ReadBytesAsync(fileStream, (int)lengthFields.PayloadHeaderLength, (int)lengthFields.PayloadHeaderOffset, SeekOrigin.Begin);
            }
            catch (Exception ex)
            {
                throw new IOException($"{exceptionOriginName} - Reading Match Header: " + ex.Message, ex);
            }

            PayloadFields payloadFields;

            try
            {
                payloadFields = ParsePayloadHeader(payloadBuffer);
            }
            catch (Exception ex)
            {
                throw new Exception($"{exceptionOriginName} - Parsing Payload Header: " + ex.Message, ex);
            }


            // Combine objects to create header
            ROFLHeader result = new ROFLHeader
            {
                LengthFields  = lengthFields,
                MatchMetadata = metadataFields,
                PayloadFields = payloadFields
            };

            // Create json of entire contents
            string jsonString = JsonConvert.SerializeObject(result);

            // throw it back on the object and return
            result.RawJsonString = jsonString;
            return(result);
        }
Ejemplo n.º 9
0
        private static bool TryParseUInt16D(ReadOnlySpan <byte> source, out ushort value, out int bytesConsumed)
        {
            if (source.Length < 1)
            {
                goto FalseExit;
            }

            int index  = 0;
            int num    = source[index];
            int answer = 0;

            if (ParserHelpers.IsDigit(num))
            {
                if (num == '0')
                {
                    do
                    {
                        index++;
                        if ((uint)index >= (uint)source.Length)
                        {
                            goto Done;
                        }
                        num = source[index];
                    } while (num == '0');
                    if (!ParserHelpers.IsDigit(num))
                    {
                        goto Done;
                    }
                }

                answer = num - '0';
                index++;

                if ((uint)index >= (uint)source.Length)
                {
                    goto Done;
                }
                num = source[index];
                if (!ParserHelpers.IsDigit(num))
                {
                    goto Done;
                }
                index++;
                answer = 10 * answer + num - '0';

                if ((uint)index >= (uint)source.Length)
                {
                    goto Done;
                }
                num = source[index];
                if (!ParserHelpers.IsDigit(num))
                {
                    goto Done;
                }
                index++;
                answer = 10 * answer + num - '0';

                if ((uint)index >= (uint)source.Length)
                {
                    goto Done;
                }
                num = source[index];
                if (!ParserHelpers.IsDigit(num))
                {
                    goto Done;
                }
                index++;
                answer = 10 * answer + num - '0';

                // Potential overflow
                if ((uint)index >= (uint)source.Length)
                {
                    goto Done;
                }
                num = source[index];
                if (!ParserHelpers.IsDigit(num))
                {
                    goto Done;
                }
                index++;
                answer = answer * 10 + num - '0';
                if ((uint)answer > ushort.MaxValue)
                {
                    goto FalseExit; // Overflow
                }
                if ((uint)index >= (uint)source.Length)
                {
                    goto Done;
                }
                if (!ParserHelpers.IsDigit(source[index]))
                {
                    goto Done;
                }

                // Guaranteed overflow
                goto FalseExit;
            }

FalseExit:
            bytesConsumed = default;
            value         = default;
            return(false);

Done:
            bytesConsumed = index;
            value         = (ushort)answer;
            return(true);
        }
Ejemplo n.º 10
0
        private IField ReadFieldFromTable(DataRow dr, string elementName, SortedList <string, int> bitmapLows, SortedList <string, int> bitmapHighs)
        {
            FieldLength length;

            Enum.TryParse <FieldLength>((string)dr["FieldLength"], out length);
            FieldFormat format;

            Enum.TryParse <FieldFormat>((string)dr["FieldFormat"], out format);
            Messages.Core.Field.Empty empty;
            if (elementName != "Group" && Convert.ToBoolean(dr["Bitmap"]))
            {
                if (Convert.ToBoolean(dr["HexBitmap"]))
                {
                    empty = new BitmapHex(Convert.ToInt32(dr["BitmapFirst"]));
                }
                else
                {
                    empty = new Bitmap(Convert.ToInt32(dr["BitmapFirst"]));
                }
                bitmapLows.Add((string)dr[elementName], Convert.ToInt32(dr["BitmapStartRange"]));
                bitmapHighs.Add((string)dr[elementName], Convert.ToInt32(dr["BitmapEndRange"]));
            }
            else
            {
                empty = new Messages.Core.Field.Empty();
            }
            empty.Configure((string)dr[elementName], Convert.ToInt32(dr["Size"]), Convert.ToInt32(dr["Number"]), length, format, FormatHelpers.GetFormatter((string)dr["Formatter"]), ValidatorHelpers.GetValidator((string)dr["Validator"]), ParserHelpers.GetParser((string)dr["Parser"]));
            return(empty);
        }
Ejemplo n.º 11
0
        public async Task <ReplayFile> ReadFile(string filePath)
        {
            // Make sure file exists
            if (String.IsNullOrEmpty(filePath))
            {
                _log.Error("File reference is null");
                throw new ArgumentNullException($"File reference is null");
            }

            if (!File.Exists(filePath))
            {
                _log.Error("File path not found, does the file exist?");
                throw new FileNotFoundException($"File path not found, does the file exist?");
            }

            // Reads the first 4 bytes and tries to find out the replay type
            ReplayType type = await ParserHelpers.GetReplayTypeAsync(filePath);

            // Match parsers to file types
            ReplayFile result;

            switch (type)
            {
            case ReplayType.ROFL:       // Official Replays
                result = await ReadROFL(filePath);

                break;

            //case ReplayType.LRF:    // LOLReplay
            //    file.Type = ReplayType.LRF;
            //    file.Data = await ReadLRF(file.Location);
            //    break;
            //case ReplayType.LPR:    // BaronReplays
            //    file.Type = ReplayType.LPR;
            //    file.Data = null;
            //    break;
            default:
                _log.Error($"File {filePath} is not an accepted format: rofl");
                return(null);
            }

            // Make some educated guesses
            GameDetailsInferrer detailsInferrer = new GameDetailsInferrer();

            result.Players = result.BluePlayers.Union(result.RedPlayers).ToArray();

            try
            {
                result.MapId = detailsInferrer.InferMap(result.Players);
            }
            catch (ArgumentNullException ex)
            {
                _log.Warning("Could not infer map type\n" + ex.ToString());
                result.MapId = MapCode.Unknown;
            }

            result.MapName          = detailsInferrer.GetMapName(result.MapId);
            result.IsBlueVictorious = detailsInferrer.InferBlueVictory(result.BluePlayers, result.RedPlayers);

            foreach (var player in result.Players)
            {
                player.Id = $"{result.MatchId}_{player.PlayerID}";
            }

            // Set the alternate name to the default
            result.AlternativeName = result.Name;

            return(result);
        }
Ejemplo n.º 12
0
 private StateResult HexLiteral()
 {
     TakeUntil(c => !ParserHelpers.IsHexDigit(c));
     TakeIntegerSuffix();
     return(Stay(EndSymbol(CSharpSymbolType.IntegerLiteral)));
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Continues the content of the tag.
        /// </summary>
        /// <param name="raw">True if we are expected a raw tag.</param>
        /// <returns>The state result.</returns>
        private StateResult ContinueTagContent(bool raw)
        {
            if (CurrentCharacter == '@')
            {
                TakeCurrent();

                return(Stay(EndSymbol(HandlebarsSymbolType.At)));
            }

            if (HandlebarsHelpers.IsIdentifierStart(CurrentCharacter))
            {
                return(Identifier());
            }

            if (Char.IsDigit(CurrentCharacter))
            {
                return(NumericLiteral());
            }

            switch (CurrentCharacter)
            {
            case '.':
            {
                TakeCurrent();
                if (CurrentCharacter == '/')
                {
                    // We've matched a link to the current context.
                    TakeCurrent();
                    return(Stay(EndSymbol(HandlebarsSymbolType.CurrentContext)));
                }

                if (CurrentCharacter == '.' && Peek() == '/')
                {
                    // We've matched a link to the parent context.
                    TakeCurrent();
                    TakeCurrent();
                    return(Stay(EndSymbol(HandlebarsSymbolType.ParentContext)));
                }

                // We've matched a dot, which could be part of an expression.
                return(Stay(EndSymbol(HandlebarsSymbolType.Dot)));
            }

            case '/':
            {
                TakeCurrent();
                // We've matched a forward-slash, which could be part of an expression.
                return(Stay(EndSymbol(HandlebarsSymbolType.Slash)));
            }

            case ' ':
            {
                // Take all the available whitespace.
                TakeUntil(c => !ParserHelpers.IsWhiteSpace(c));
                return(Stay(EndSymbol(HandlebarsSymbolType.WhiteSpace)));
            }

            case '~':
            {
                TakeCurrent();
                // We've reached a '~' character, so jump to the end of the tag.
                return(Transition(EndSymbol(HandlebarsSymbolType.Tilde), () => EndTag(raw)));
            }

            case '"':
            case '\'':
            {
                var quote = CurrentCharacter;
                TakeCurrent();
                // We've reached a quoted literal.
                return(QuotedLiteral(quote));
            }

            case '=':
            {
                // We're reached a map assignment.
                TakeCurrent();
                return(Stay(EndSymbol(HandlebarsSymbolType.Assign)));
            }

            case '}':
            {
                // We've reached a closing tag, so transition away.
                return(Transition(() => EndTag(raw)));
            }

            default:
            {
                CurrentErrors.Add(new Error("Unexpected character: " + CurrentCharacter, CurrentLocation));
                return(Transition(Stop));
            }
            }
        }
Ejemplo n.º 14
0
        /// <inheritdoc />
        public UnaryNode ParseStatement(string statement, string type = null)
        {
            var first = statement.First();
            var last  = statement.Last();

            bool lowerInclusive;

            switch (first)
            {
            case '[':
                lowerInclusive = false;
                break;

            case '{':
                lowerInclusive = true;
                break;

            default:
                throw new InvalidOperationException($"Character '{first}' is not a valid part of a range query");
            }

            bool upperInclusive;

            switch (last)
            {
            case ']':
                upperInclusive = false;
                break;

            case '}':
                upperInclusive = true;
                break;

            default:
                throw new InvalidOperationException($"Character '{last}' is not a valid part of a range query");
            }

            var parts = statement
                        .Substring(1, statement.Length - 2)
                        .Split(',')
                        .Select(s => s.Trim())
                        .ToList();

            // If the first part is a wildcard, determine the last part only
            if (parts[0] == "*")
            {
                var lowerVal = ParserHelpers.Parse(parts[1], type);

                return(new LessThanOperator
                {
                    Value = lowerVal,
                    Inclusive = upperInclusive,
                    Name = null,
                    Statement = statement,
                });
            }

            // If the last part is a wildcard, determine the first part only
            if (parts[1] == "*")
            {
                var upperVal = ParserHelpers.Parse(parts[0], type);

                return(new GreaterThanOperator
                {
                    Value = upperVal,
                    Inclusive = lowerInclusive,
                    Name = null,
                    Statement = statement,
                });
            }

            var lower = ParserHelpers.Parse(parts[0], type);
            var upper = ParserHelpers.Parse(parts[1], type);

            return(new RangeOperator
            {
                LowerInclusive = lowerInclusive,
                UpperInclusive = upperInclusive,
                Lower = lower,
                Upper = upper,
                Name = null,
                Statement = statement,
            });
        }
        public static FormattingContext Create(
            Uri uri,
            DocumentSnapshot originalSnapshot,
            RazorCodeDocument codedocument,
            FormattingOptions options,
            Range range         = null,
            bool isFormatOnType = false)
        {
            if (uri is null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            if (originalSnapshot is null)
            {
                throw new ArgumentNullException(nameof(originalSnapshot));
            }

            if (codedocument is null)
            {
                throw new ArgumentNullException(nameof(codedocument));
            }

            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var text = codedocument.GetSourceText();

            range ??= TextSpan.FromBounds(0, text.Length).AsRange(text);

            var result = new FormattingContext()
            {
                Uri = uri,
                OriginalSnapshot = originalSnapshot,
                CodeDocument     = codedocument,
                Range            = range,
                Options          = options,
                IsFormatOnType   = isFormatOnType
            };

            var source          = codedocument.Source;
            var syntaxTree      = codedocument.GetSyntaxTree();
            var formattingSpans = syntaxTree.GetFormattingSpans();

            var total = 0;
            var previousIndentationLevel = 0;

            for (var i = 0; i < source.Lines.Count; i++)
            {
                // Get first non-whitespace character position
                var lineLength = source.Lines.GetLineLength(i);
                var nonWsChar  = 0;
                for (var j = 0; j < lineLength; j++)
                {
                    var ch = source[total + j];
                    if (!char.IsWhiteSpace(ch) && !ParserHelpers.IsNewLine(ch))
                    {
                        nonWsChar = j;
                        break;
                    }
                }

                // position now contains the first non-whitespace character or 0. Get the corresponding FormattingSpan.
                if (TryGetFormattingSpan(total + nonWsChar, formattingSpans, out var span))
                {
                    result.Indentations[i] = new IndentationContext
                    {
                        Line                     = i,
                        IndentationLevel         = span.IndentationLevel,
                        RelativeIndentationLevel = span.IndentationLevel - previousIndentationLevel,
                        ExistingIndentation      = nonWsChar,
                        FirstSpan                = span,
                    };
                    previousIndentationLevel = span.IndentationLevel;
                }
                else
                {
                    // Couldn't find a corresponding FormattingSpan.
                    result.Indentations[i] = new IndentationContext
                    {
                        Line                     = i,
                        IndentationLevel         = -1,
                        RelativeIndentationLevel = previousIndentationLevel,
                        ExistingIndentation      = nonWsChar,
                    };
                }

                total += lineLength;
            }

            return(result);
        }
Ejemplo n.º 16
0
 protected virtual string GetClassName()
 {
     return(ParserHelpers.SanitizeClassName(_baseRelativePath));
 }
Ejemplo n.º 17
0
 // CSharp Spec §2.3.2
 private StateResult SingleLineComment()
 {
     TakeUntil(c => ParserHelpers.IsNewLine(c));
     return(Stay(EndSymbol(CSharpSymbolType.Comment)));
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Tokenizes a hex literal.
        /// </summary>
        /// <returns>The state result.</returns>
        private StateResult HexLiteral()
        {
            TakeUntil(c => !ParserHelpers.IsHexDigit(c));

            return(Stay(EndSymbol(HandlebarsSymbolType.IntegerLiteral)));
        }
Ejemplo n.º 19
0
        private StateResult Data()
        {
            if (ParserHelpers.IsNewLine(CurrentCharacter))
            {
                // CSharp Spec §2.3.1
                var checkTwoCharNewline = CurrentCharacter == '\r';
                TakeCurrent();
                if (checkTwoCharNewline && CurrentCharacter == '\n')
                {
                    TakeCurrent();
                }
                return(Stay(EndSymbol(CSharpSymbolType.NewLine)));
            }
            else if (ParserHelpers.IsWhitespace(CurrentCharacter))
            {
                // CSharp Spec §2.3.3
                TakeUntil(c => !ParserHelpers.IsWhitespace(c));
                return(Stay(EndSymbol(CSharpSymbolType.WhiteSpace)));
            }
            else if (CSharpHelpers.IsIdentifierStart(CurrentCharacter))
            {
                return(Identifier());
            }
            else if (Char.IsDigit(CurrentCharacter))
            {
                return(NumericLiteral());
            }
            switch (CurrentCharacter)
            {
            case '@':
                return(AtSymbol());

            case '\'':
                TakeCurrent();
                return(Transition(() => QuotedLiteral('\'', CSharpSymbolType.CharacterLiteral)));

            case '"':
                TakeCurrent();
                return(Transition(() => QuotedLiteral('"', CSharpSymbolType.StringLiteral)));

            case '.':
                if (Char.IsDigit(Peek()))
                {
                    return(RealLiteral());
                }
                return(Stay(Single(CSharpSymbolType.Dot)));

            case '/':
                TakeCurrent();
                if (CurrentCharacter == '/')
                {
                    TakeCurrent();
                    return(SingleLineComment());
                }
                else if (CurrentCharacter == '*')
                {
                    TakeCurrent();
                    return(Transition(BlockComment));
                }
                else if (CurrentCharacter == '=')
                {
                    TakeCurrent();
                    return(Stay(EndSymbol(CSharpSymbolType.DivideAssign)));
                }
                else
                {
                    return(Stay(EndSymbol(CSharpSymbolType.Slash)));
                }

            default:
                return(Stay(EndSymbol(Operator())));
            }
        }
Ejemplo n.º 20
0
        // Internal for testing
        internal static FormattingContext CreateFormattingContext(Uri uri, RazorCodeDocument codedocument, Range range, FormattingOptions options)
        {
            var result = new FormattingContext()
            {
                Uri          = uri,
                CodeDocument = codedocument,
                Range        = range,
                Options      = options
            };

            var source          = codedocument.Source;
            var syntaxTree      = codedocument.GetSyntaxTree();
            var formattingSpans = syntaxTree.GetFormattingSpans();

            var total = 0;
            var previousIndentationLevel = 0;

            for (var i = 0; i < source.Lines.Count; i++)
            {
                // Get first non-whitespace character position
                var lineLength = source.Lines.GetLineLength(i);
                var nonWsChar  = 0;
                for (var j = 0; j < lineLength; j++)
                {
                    var ch = source[total + j];
                    if (!char.IsWhiteSpace(ch) && !ParserHelpers.IsNewLine(ch))
                    {
                        nonWsChar = j;
                        break;
                    }
                }

                // position now contains the first non-whitespace character or 0. Get the corresponding FormattingSpan.
                if (TryGetFormattingSpan(total + nonWsChar, formattingSpans, out var span))
                {
                    result.Indentations[i] = new IndentationContext
                    {
                        Line                     = i,
                        IndentationLevel         = span.IndentationLevel,
                        RelativeIndentationLevel = span.IndentationLevel - previousIndentationLevel,
                        ExistingIndentation      = nonWsChar,
                        FirstSpan                = span,
                    };
                    previousIndentationLevel = span.IndentationLevel;
                }
                else
                {
                    // Couldn't find a corresponding FormattingSpan.
                    result.Indentations[i] = new IndentationContext
                    {
                        Line                     = i,
                        IndentationLevel         = -1,
                        RelativeIndentationLevel = previousIndentationLevel,
                        ExistingIndentation      = nonWsChar,
                    };
                }

                total += lineLength;
            }

            return(result);
        }
Ejemplo n.º 21
0
        public void Append(string content)
        {
            for (int i = 0; i < content.Length; i++)
            {
                AppendCore(content[i]);

                // \r on it's own: Start a new line, otherwise wait for \n
                // Other Newline: Start a new line
                if ((content[i] == '\r' && (i + 1 == content.Length || content[i + 1] != '\n')) || (content[i] != '\r' && ParserHelpers.IsNewLine(content[i])))
                {
                    PushNewLine();
                }
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// DO NOT USE
        /// </summary>
        /// <param name="fileStream"></param>
        /// <returns></returns>
        public async Task <ReplayHeader> ReadReplayAsync(FileStream fileStream)
        {
            if (!fileStream.CanRead)
            {
                throw new IOException($"{_exceptionOriginName} - Stream does not support reading");
            }

            // Create ne LprHeader
            LprHeader header = new LprHeader();

            // These buffers will be used and reused to read data
            byte[] int32ByteBuffer = new byte[4];
            byte[] int64ByteBuffer = new byte[8];

            //// Try reading the "Magic number" it's actually the file version
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Magic Number", fileStream, int32ByteBuffer, 4);

            header.LprFileVersion = BitConverter.ToInt32(int32ByteBuffer, 0);

            // BaronReplay implements this check, not sure why
            if (!(header.LprFileVersion >= 0))
            {
                throw new IOException($"{_exceptionOriginName} - Lpr File Version Unsupported");
            }


            //// Try reading the length of the next section, so we can skip it
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Length of Spectator client version", fileStream, int32ByteBuffer, 4);

            //// Use the result of previous operation to make new array and pull string
            byte[] spectatorClientVersionBytes = new byte[BitConverter.ToInt32(int32ByteBuffer, 0)];
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Spectator client version", fileStream, spectatorClientVersionBytes, BitConverter.ToInt32(int32ByteBuffer, 0));

            header.SpectatorClientVersion = Encoding.UTF8.GetString(spectatorClientVersionBytes);


            //// Read the game ID
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Game ID", fileStream, int64ByteBuffer, 8);

            header.GameID = BitConverter.ToInt64(int64ByteBuffer, 0);


            //// Read Chunk data
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.GameEndStartupChunk = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.StartChunk = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.EndChunk = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.EndKeyframe = BitConverter.ToInt32(int32ByteBuffer, 0);


            //// Read game length
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Game length", fileStream, int32ByteBuffer, 4);

            header.GameLength = BitConverter.ToInt32(int32ByteBuffer, 0);


            //// Read more chunk/replay information
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.GameDelayTime = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.ClientAddLag = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.ChunkTimeInterval = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.KeyframeTimeInterval = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.ELOLevel = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.LastChunkTime = BitConverter.ToInt32(int32ByteBuffer, 0);

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Chunks", fileStream, int32ByteBuffer, 4);

            header.LastChunkDuration = BitConverter.ToInt32(int32ByteBuffer, 0);


            //// Read game region length
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Game region length", fileStream, int32ByteBuffer, 4);

            //// Use the length to make a new byte array to read the region string
            byte[] gameRegionBytes = new byte[BitConverter.ToInt32(int32ByteBuffer, 0)];

            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Game region", fileStream, gameRegionBytes, BitConverter.ToInt32(int32ByteBuffer, 0));

            header.GamePlatform = Encoding.UTF8.GetString(gameRegionBytes);


            //// Get length of spectator encryption key
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Spectator encryption key length", fileStream, int32ByteBuffer, 4);

            //// Use the length of spectator encryption key to make a new array to put the encryption key
            byte[] spectatorEncryptionKeyBytes = new byte[BitConverter.ToInt32(int32ByteBuffer, 0)];
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading Spectator encryption key", fileStream, spectatorEncryptionKeyBytes, BitConverter.ToInt32(int32ByteBuffer, 0));

            header.SpectatorEncryptionKey = Encoding.UTF8.GetString(spectatorEncryptionKeyBytes);


            //// Get length of creation time
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading creation time length", fileStream, int32ByteBuffer, 4);

            //// Use the length of creation time to make a new array to put the creation time
            byte[] creationTimeBytes = new byte[BitConverter.ToInt32(int32ByteBuffer, 0)];
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading creation time", fileStream, creationTimeBytes, BitConverter.ToInt32(int32ByteBuffer, 0));

            header.CreateTime = Encoding.UTF8.GetString(creationTimeBytes);


            //// Get length of start time
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading start time length", fileStream, int32ByteBuffer, 4);

            //// Use the length of start time to make a new array to put the start time
            byte[] startTimeBytes = new byte[BitConverter.ToInt32(int32ByteBuffer, 0)];
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading creation time", fileStream, startTimeBytes, BitConverter.ToInt32(int32ByteBuffer, 0));

            header.StartTime = Encoding.UTF8.GetString(startTimeBytes);


            //// Get length of end time
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading end time length", fileStream, int32ByteBuffer, 4);

            //// Use the length of start time to make a new array to put the start time
            byte[] endTimeBytes = new byte[BitConverter.ToInt32(int32ByteBuffer, 0)];
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading end time", fileStream, endTimeBytes, BitConverter.ToInt32(int32ByteBuffer, 0));

            header.EndTime = Encoding.UTF8.GetString(endTimeBytes);


            //// Get length of league of legends version
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading league of legends version length", fileStream, int32ByteBuffer, 4);

            //// Use the length of start time to make a new array to put the start time
            byte[] leagueVersionBytes = new byte[BitConverter.ToInt32(int32ByteBuffer, 0)];
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading end time", fileStream, leagueVersionBytes, BitConverter.ToInt32(int32ByteBuffer, 0));

            // TODO
            header.LeagueVersion = Encoding.UTF8.GetString(leagueVersionBytes);


            //// Do end game result exists?
            byte[] resultsExistByte = new byte[1];
            await ParserHelpers.ReadBytes($"{_exceptionOriginName} - Reading result boolean", fileStream, resultsExistByte, 1);

            // TODO
            bool resultsExist = BitConverter.ToBoolean(resultsExistByte, 0);

            if (resultsExist)
            {
                //await ReadEndGameResults(fileStream);
            }
            else
            {
                header.OldResults = await ReadOldEndGameResults(fileStream);
            }


            // TODO BROKEN
            return(null);
        }
Ejemplo n.º 23
0
        internal static ChangeSet ParseCommit(IStringReader reader)
        {
            // commit hash
            reader.ReadUntilWhitespace();
            reader.SkipWhitespace();
            string id = reader.ReadUntilWhitespace();

            // Merges will have (from hash) so we're skipping that
            reader.ReadLine();

            string author = null;
            string email  = null;
            string date   = null;

            while (!reader.Done)
            {
                string line = reader.ReadLine();

                if (ParserHelpers.IsSingleNewLine(line))
                {
                    break;
                }

                var    subReader = line.AsReader();
                string key       = subReader.ReadUntil(':');
                // Skip :
                subReader.Skip();
                subReader.SkipWhitespace();
                string value = subReader.ReadToEnd().Trim();

                if (key.Equals("Author", StringComparison.OrdinalIgnoreCase))
                {
                    // Author <email>
                    var authorReader = value.AsReader();
                    author = authorReader.ReadUntil('<').Trim();
                    authorReader.Skip();
                    email = authorReader.ReadUntil('>');
                }
                else if (key.Equals("Date", StringComparison.OrdinalIgnoreCase))
                {
                    date = value;
                }
            }

            var messageBuilder = new StringBuilder();

            while (!reader.Done)
            {
                string line = reader.ReadLine();

                if (ParserHelpers.IsSingleNewLine(line))
                {
                    break;
                }
                messageBuilder.Append(line);
            }

            string message = messageBuilder.ToString().Trim();

            return(new ChangeSet(id, author, email, message, DateTimeOffset.ParseExact(date, "ddd MMM d HH:mm:ss yyyy zzz", CultureInfo.InvariantCulture)));
        }
Ejemplo n.º 24
0
 // CSharp Spec §2.3.2
 private StateResult SingleLineComment()
 {
     TakeUntil(c => ParserHelpers.IsNewLine(c));
     return(Stay(EndToken(SyntaxKind.CSharpComment)));
 }
Ejemplo n.º 25
0
        private static bool TryParseSByteN(ReadOnlySpan <byte> source, out sbyte value, out int bytesConsumed)
        {
            if (source.Length < 1)
            {
                goto FalseExit;
            }

            int sign  = 1;
            int index = 0;
            int c     = source[index];

            if (c == '-')
            {
                sign = -1;
                index++;
                if ((uint)index >= (uint)source.Length)
                {
                    goto FalseExit;
                }
                c = source[index];
            }
            else if (c == '+')
            {
                index++;
                if ((uint)index >= (uint)source.Length)
                {
                    goto FalseExit;
                }
                c = source[index];
            }

            int answer;

            // Handle the first digit (or period) as a special case. This ensures some compatible edge-case behavior with the classic parse routines
            // (at least one digit must precede any commas, and a string without any digits prior to the decimal point must have at least
            // one digit after the decimal point.)
            if (c == Utf8Constants.Period)
            {
                goto FractionalPartWithoutLeadingDigits;
            }
            if (!ParserHelpers.IsDigit(c))
            {
                goto FalseExit;
            }
            answer = c - '0';

            for (; ;)
            {
                index++;
                if ((uint)index >= (uint)source.Length)
                {
                    goto Done;
                }

                c = source[index];
                if (c == Utf8Constants.Comma)
                {
                    continue;
                }

                if (c == Utf8Constants.Period)
                {
                    goto FractionalDigits;
                }

                if (!ParserHelpers.IsDigit(c))
                {
                    goto Done;
                }

                answer = answer * 10 + c - '0';

                // if sign < 0, (-1 * sign + 1) / 2 = 1
                // else, (-1 * sign + 1) / 2 = 0
                if (answer > sbyte.MaxValue + (-1 * sign + 1) / 2)
                {
                    goto FalseExit; // Overflow
                }
            }

FractionalPartWithoutLeadingDigits: // If we got here, we found a decimal point before we found any digits. This is legal as long as there's at least one zero after the decimal point.
            answer = 0;
            index++;
            if ((uint)index >= (uint)source.Length)
            {
                goto FalseExit;
            }
            if (source[index] != '0')
            {
                goto FalseExit;
            }

FractionalDigits: // "N" format allows a fractional portion despite being an integer format but only if the post-fraction digits are all 0.
            do
            {
                index++;
                if ((uint)index >= (uint)source.Length)
                {
                    goto Done;
                }
                c = source[index];
            }while (c == '0');

            if (ParserHelpers.IsDigit(c))
            {
                goto FalseExit; // The fractional portion contained a non-zero digit. Treat this as an error, not an early termination.
            }
            goto Done;

FalseExit:
            bytesConsumed = default;
            value         = default;
            return(false);

Done:
            bytesConsumed = index;
            value         = (sbyte)(answer * sign);
            return(true);
        }
 protected override string GetClassName(string virtualPath)
 {
     return(ParserHelpers.SanitizeClassName(Path.GetFileNameWithoutExtension(virtualPath)));
 }
 private StateResult Data()
 {
     // We are accepting more characters and whitespace/newlines then the VB Spec defines, to simplify things
     // Since the code must still be compiled by a VB compiler, this will not cause adverse effects.
     if (ParserHelpers.IsNewLine(CurrentCharacter))
     {
         // VB Spec §2.1.1
         bool checkTwoCharNewline = CurrentCharacter == '\r';
         TakeCurrent();
         if (checkTwoCharNewline && CurrentCharacter == '\n')
         {
             TakeCurrent();
         }
         return(Stay(EndSymbol(VBSymbolType.NewLine)));
     }
     else if (ParserHelpers.IsWhitespace(CurrentCharacter))
     {
         // CSharp Spec §2.1.3
         TakeUntil(c => !ParserHelpers.IsWhitespace(c));
         return(Stay(EndSymbol(VBSymbolType.WhiteSpace)));
     }
     else if (VBHelpers.IsSingleQuote(CurrentCharacter))
     {
         TakeCurrent();
         return(CommentBody());
     }
     else if (IsIdentifierStart())
     {
         return(Identifier());
     }
     else if (Char.IsDigit(CurrentCharacter))
     {
         return(DecimalLiteral());
     }
     else if (CurrentCharacter == '&')
     {
         char next = Char.ToLower(Peek(), CultureInfo.InvariantCulture);
         if (next == 'h')
         {
             return(HexLiteral());
         }
         else if (next == 'o')
         {
             return(OctLiteral());
         }
     }
     else if (CurrentCharacter == '.' && Char.IsDigit(Peek()))
     {
         return(FloatingPointLiteralEnd());
     }
     else if (VBHelpers.IsDoubleQuote(CurrentCharacter))
     {
         TakeCurrent();
         return(Transition(QuotedLiteral));
     }
     else if (AtDateLiteral())
     {
         return(DateLiteral());
     }
     else if (CurrentCharacter == '@')
     {
         TakeCurrent();
         if (CurrentCharacter == '*')
         {
             return(Transition(EndSymbol(VBSymbolType.RazorCommentTransition), AfterRazorCommentTransition));
         }
         else if (CurrentCharacter == '@')
         {
             // Could be escaped comment transition
             return(Transition(EndSymbol(VBSymbolType.Transition), () =>
             {
                 TakeCurrent();
                 return Transition(EndSymbol(VBSymbolType.Transition), Data);
             }));
         }
         else
         {
             return(Stay(EndSymbol(VBSymbolType.Transition)));
         }
     }
     return(Stay(EndSymbol(Operator())));
 }
Ejemplo n.º 28
0
        public void RootContextTest()
        {
            var code = File.ReadAllText(@"DataBiTemporal\Parser\simple.sql");

            Assert.IsNotNull(ParserHelpers.GetRootContext(code) as BiTempDefParser.CompileUnitContext);
        }
Ejemplo n.º 29
0
        protected virtual string GetClassName()
        {
            string filename = Path.GetFileNameWithoutExtension(this.File.VirtualPath);

            return("__" + ParserHelpers.SanitizeClassName(filename));
        }
Ejemplo n.º 30
0
        public void WalkTest()
        {
            var code = File.ReadAllText(@"DataBiTemporal\Parser\simple.sql");

            Assert.AreEqual(33, ParserHelpers.WalkTree(code));
        }