public IParseResult Parse([FromBody] IParseRequest parseRequest)
 {
     if (parseRequest is null)
     {
         throw new ArgumentNullException(nameof(parseRequest));
     }
     parseRequest.Config = parseHelper(parseRequest.Config);
     return(_yamlScriptController.Parse(parseRequest.Config));
 }
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        // PUBLIC PROCEDURES
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Performs a parsing operation using the parameters specified in the supplied <see cref="IParseRequest"/>
        /// and returns the resulting parse data.
        /// </summary>
        /// <param name="request">The <see cref="IParseRequest"/> that contains data about the requested parsing operation.</param>
        /// <returns>An <see cref="IParseData"/> that is the result of the parsing operation.</returns>
        /// <remarks>
        /// A <see cref="IParseRequestDispatcher"/> typically calls this method when a queued parse request is ready to be processed.
        /// </remarks>
        public override IParseData Parse(IParseRequest request)
        {
            SimpleParseData parseData = new SimpleParseData();

            //
            // NOTE: Make sure that you've set up an ambient parse request dispatcher for your application
            //   (see documentation on 'Parse Requests and Dispatchers') so that this parser is called in
            //   a worker thread as the editor is updated
            //

            // Most parsers will use the request.TextBufferReader property in some fashion to scan through
            //   text and not a snapshot directly... in this basic sample though, we're going to use the
            //   tokenization provided by the snapshot's reader so we can only proceed if there is a
            //   snapshot passed to us
            if (request.Snapshot != null)
            {
                ITextSnapshotReader reader = request.Snapshot.GetReader(0);

                bool isFunctionStart = false;
                while (!reader.IsAtSnapshotEnd)
                {
                    IToken token = reader.ReadToken();
                    if (token != null)
                    {
                        switch (token.Key)
                        {
                        case "Keyword":
                            // If a function token, mark that this is a function start... the next identifier should be the function name
                            isFunctionStart = (reader.Snapshot.GetSubstring(token.TextRange) == "function");
                            break;

                        case "Identifier":
                            // If this is the function name...
                            if (isFunctionStart)
                            {
                                parseData.Functions.Add(new TextSnapshotRange(reader.Snapshot, token.TextRange));
                                isFunctionStart = false;
                            }
                            break;

                        case "Whitespace":
                            // Ignore
                            break;

                        default:
                            // Flag as no longer in a function start
                            isFunctionStart = false;
                            break;
                        }
                    }
                }
            }

            return(parseData);
        }
 public IParseRequest GetParseRequest()
 {
     if (_parseRequest == null)
     {
         _parseRequest = new ParseRequest
         {
             Config = Sequence.Yaml
         };
     }
     return(_parseRequest);
 }
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        // PUBLIC PROCEDURES
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Performs a parsing operation using the parameters specified in the supplied <see cref="IParseRequest"/>
        /// and returns the resulting parse data.
        /// </summary>
        /// <param name="request">The <see cref="IParseRequest"/> that contains data about the requested parsing operation.</param>
        /// <returns>An <see cref="IParseData"/> that is the result of the parsing operation.</returns>
        /// <remarks>
        /// A <see cref="IParseRequestDispatcher"/> typically calls this method when a queued parse request is ready to be processed.
        /// </remarks>
        public override IParseData Parse(IParseRequest request)
        {
            if (request.Snapshot != null)
            {
                // Since the parser may be delayed a bit before this is called (due to threading),
                //   base the outlining on the most current snapshot for the document, which may be
                //   newer than the one passed in the parse request
                return(new JavascriptOutliningSource(request.Snapshot.Document.CurrentSnapshot));
            }

            return(null);
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        // PUBLIC PROCEDURES
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Creates an <see cref="IParseData"/> for the specified <see cref="IParserState"/>.
        /// </summary>
        /// <param name="request">The <see cref="IParseRequest"/> that contains data about the requested parsing operation.</param>
        /// <param name="state">The <see cref="IParserState"/> to examine.</param>
        /// <returns>The <see cref="IParseData"/> that was created.</returns>
        protected override IParseData CreateParseData(IParseRequest request, IParserState state)
        {
            var parseData = base.CreateParseData(request, state) as IDotNetParseData;

            if (parseData != null)
            {
                return(new CodeLensParseData(parseData));
            }
            else
            {
                return(null);
            }
        }
        public override IParseData Parse(IParseRequest request)
        {
            var lexer    = request.Language.GetLexer();
            var data     = new SyntaxEditorStepBroParseData();
            var snapshot = request.Snapshot;

            if (snapshot != null)
            {
                //ITextSnapshotReader reader = request.Snapshot.GetReader(0);

                //while (!reader.IsAtSnapshotEnd)
                //{
                //    var read = reader.ReadText(1000);
                //    System.Diagnostics.Debug.WriteLine($"Parse read {read.Length} chars");
                //}
            }

            return(data);
        }
        /// <summary>
        /// Performs a parsing operation using the parameters specified in the supplied <see cref="IParseRequest"/>
        /// and returns the resulting parse data.
        /// </summary>
        /// <param name="request">The <see cref="IParseRequest"/> that contains data about the requested parsing operation.</param>
        /// <returns>An <see cref="IParseData"/> that is the result of the parsing operation.</returns>
        /// <remarks>
        /// A <see cref="IParseRequestDispatcher"/> typically calls this method when a queued parse request is ready to be processed.
        /// </remarks>
        public IParseData Parse(IParseRequest request)
        {
            var wrappedParseData = wrappedParser.Parse(request) as IDotNetParseData;

            if (wrappedParseData != null)
            {
                var parseData = new CustomDotNetParseData(wrappedParseData);

                // NOTE: Normally you would place code here that inspects the parsed snapshot text or its AST and determines unused regions...
                //   However this sample is showing off the visual feature itself so the text ranges use hard-coded offset values below

                parseData.UnusedRanges = new NormalizedTextSnapshotRangeCollection(new TextSnapshotRange[] {
                    new TextSnapshotRange(wrappedParseData.Snapshot, 14, 47),
                    new TextSnapshotRange(wrappedParseData.Snapshot, 454, 473),
                    new TextSnapshotRange(wrappedParseData.Snapshot, 476, 502)
                });

                return(parseData);
            }

            return(null);
        }
Example #8
0
 public async Task <IParseResult> Parse([FromBody] IParseRequest parseRequest)
 {
     return(await _serviceController.Parse(parseRequest));
 }
Example #9
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        // PUBLIC PROCEDURES
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Performs a parsing operation using the parameters specified in the supplied <see cref="IParseRequest"/>
        /// and returns the resulting parse data.
        /// </summary>
        /// <param name="request">The <see cref="IParseRequest"/> that contains data about the requested parsing operation.</param>
        /// <returns>An <see cref="IParseData"/> that is the result of the parsing operation.</returns>
        /// <remarks>
        /// A <see cref="IParseRequestDispatcher"/> typically calls this method when a queued parse request is ready to be processed.
        /// </remarks>
        public override IParseData Parse(IParseRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            // Create parse data
            ParentParseData parseData = new ParentParseData();

            parseData.Snapshot = request.Snapshot;

            // Initialize generated text
            StringBuilder generatedText = new StringBuilder();

            generatedText.Append("using System;\n");
            generatedText.Append("using System.Collections.Generic;\n\n");
            generatedText.Append("using System.Linq;\n\n");
            generatedText.Append("[EditorBrowsable(EditorBrowsableState.Never)]\n");
            generatedText.Append("class __Generated {\n");
            generatedText.Append("\t[EditorBrowsable(EditorBrowsableState.Never)]\n");
            generatedText.Append("\tvoid __WriteOutput() {\n");

            ITextSnapshotReader sourceReader = request.Snapshot.GetReader(0);
            int  lastDelimiterOffset         = 0;
            bool lastDelimiterWasStart       = false;

            while (!sourceReader.IsAtSnapshotEnd)
            {
                IToken token = sourceReader.ReadToken();
                if (token != null)
                {
                    switch (token.Id)
                    {
                    case ParentTokenId.ChildCodeBlockStart:
                    case ParentTokenId.ChildOutputBlockStart:
                        if (token.StartOffset - lastDelimiterOffset > 0)
                        {
                            // Append generated text
                            string text = sourceReader.Snapshot.GetSubstring(new TextRange(lastDelimiterOffset, token.StartOffset), LineTerminator.Newline);
                            generatedText.Append("\t\tResponse.Write(@\"");
                            generatedText.Append(text.Replace("\"", "\"\""));
                            generatedText.Append("\");\n");
                        }

                        // Store the last delimiter offset
                        lastDelimiterOffset   = token.EndOffset;
                        lastDelimiterWasStart = true;
                        break;

                    case ParentTokenId.ChildCodeBlockEnd:
                        if ((lastDelimiterWasStart) && (token.StartOffset - lastDelimiterOffset > 0))
                        {
                            // Get the text between the delimiters
                            string text = sourceReader.Snapshot.GetSubstring(new TextRange(lastDelimiterOffset, token.StartOffset), LineTerminator.Newline);
                            generatedText.Append("\t\t");

                            // Add a mapping
                            parseData.TextRangeMappings.Add(Tuple.Create(new TextRange(lastDelimiterOffset, token.StartOffset), TextRange.FromSpan(generatedText.Length, text.Length)));

                            // Append the text directly
                            generatedText.Append(text);
                            generatedText.Append("\n");
                        }

                        // Store the last delimiter offset
                        lastDelimiterOffset   = token.EndOffset;
                        lastDelimiterWasStart = false;
                        break;

                    case ParentTokenId.ChildOutputBlockEnd:
                        if ((lastDelimiterWasStart) && (token.StartOffset - lastDelimiterOffset > 0))
                        {
                            // Get the text between the delimiters and append a Response.Write
                            string text = sourceReader.Snapshot.GetSubstring(new TextRange(lastDelimiterOffset, token.StartOffset), LineTerminator.Newline);
                            generatedText.Append("\t\tResponse.Write(");

                            // Add a mapping
                            parseData.TextRangeMappings.Add(Tuple.Create(new TextRange(lastDelimiterOffset, token.StartOffset), TextRange.FromSpan(generatedText.Length, text.Length)));

                            // Append the text directly
                            generatedText.Append(text);
                            generatedText.Append(");\n");
                        }

                        // Store the last delimiter offset
                        lastDelimiterOffset   = token.EndOffset;
                        lastDelimiterWasStart = false;
                        break;
                    }
                }
            }

            if (lastDelimiterOffset < sourceReader.Snapshot.Length)
            {
                // Append generated text
                string text = sourceReader.Snapshot.GetSubstring(new TextRange(lastDelimiterOffset, sourceReader.Snapshot.Length), LineTerminator.Newline);
                generatedText.Append("\t\tResponse.Write(@\"");
                generatedText.Append(text.Replace("\"", "\"\""));
                generatedText.Append("\");\n");
            }

            // Store the generated text
            generatedText.Append("\t}\n");
            generatedText.Append("}\n");

            // Get parse data for the translated code
            CodeDocument generatedDocument = new CodeDocument();

            generatedDocument.Language = childLanguage;
            generatedDocument.SetText(generatedText.ToString());

            // Get a reader
            ITextBufferReader generatedReader = generatedDocument.CurrentSnapshot.GetReader(0).BufferReader;

            // Create a request
            ParseRequest generatedRequest = new ParseRequest(Guid.NewGuid().ToString(), generatedReader, childParser, generatedDocument);

            generatedRequest.Snapshot = generatedDocument.CurrentSnapshot;

            // Parse
            generatedDocument.ParseData  = childParser.Parse(generatedRequest);
            parseData.GeneratedParseData = generatedDocument.ParseData as ILLParseData;

            return(parseData);
        }