Comments (7)
What happens if LexSyntax()
is called at the end of the text?
from roslyn.
Ideally you get an EOF token.
from roslyn.
What happens if
LexSyntax()
is called at the end of the text?
I expect to get an EOF token repeatedly.
from roslyn.
Feedback from API review:
- Does the underlying lexer handle double disposal? This is a struct, so could get copied and disposed twice which would dispose the underlying lexer twice.
from roslyn.
My main question regards the Result.Token
. I would expect the lexer primarily produces green nodes, but only the red nodes are public API. What does the Parent
property return here?
Separately, I would likely be in favor of moving to allow API access to green nodes, since they perform better in a number of feature scenarios.
from roslyn.
Does the underlying lexer handle double disposal?
No. I can move to a reference type for the public type if desired. A struct wrapper seemed like the nice simple approach to me, but I don't think the extra allocation will matter in grand scheme of things, and it would allow us to be safe around double disposal.
What does the Parent property return here?
null
, just like the existing ParseToken(s)
methods. I think exposing green nodes is very out of scope for this proposal so I'm not going to proceed with that.
from roslyn.
API Review (email edition)
- We investigated whether we could pull this token parser type into the common layer. This proved too complex due to needing to be able to save state for the language-specific lexer.
- Final API shape:
namespace Microsoft.CodeAnalysis.CSharp;
public static class SyntaxFactory
{
// Existing methods...
/// <summary>
/// Creates a token parser that can be used to parse tokens from a given source text.
/// </summary>
/// <param name="sourceText">The source to parse tokens from.</param>
/// <param name="options">Parse options for the source.</param>
public static SyntaxTokenParser CreateTokenParser(SourceText sourceText, CSharpParseOptions? options = null)
}
/// <summary>
/// A token parser that can be used parse tokens continuously from a source. This parser parses continuously; every call to
/// <see cref="ParseNextToken"/> will return the next token in the source text, starting from position 0. <see cref="SkipForwardTo(int)"/>
/// can be used to skip forward in the file to a specific position, and <see cref="ResetTo(Result)"/> can be used to reset the parser
/// to a previously-lexed position.
/// </summary>
/// <remarks>
/// This type is safe to double dispose, but it is not safe to use after it has been disposed. Behavior in such scenarios
/// is undefined.
/// <para />
/// This type is not thread safe.
/// </remarks>
public sealed class SyntaxTokenParser : IDisposable
{
public void Dispose();
/// <summary>
/// Parse the next token from the input at the current position. This will advance the internal position of the token parser to the
/// end of the returned token, including any trailing trivia.
/// </summary>
/// <remarks>
/// The returned token will have a parent of <see langword="null"/>.
/// </remarks>
public Result ParseNextToken();
/// <summary>
/// Skip forward in the input to the specified position. Current directive state is preserved during the skip.
/// </summary>
/// <param name="position">The absolute location in the original text to move to.</param>
/// <exception cref="ArgumentOutOfRangeException">If the given position is less than the current position of the lexer.</exception>
public void SkipForwardTo(int position);
/// <summary>
/// Resets the token parser to an earlier position in the input. The parser is reset the start of the token that was previously
/// parsed, before any leading trivia, with the directive state that existed at the start of the token.
/// </summary>
public void ResetTo(Result result);
/// <summary>
/// The result of a call to <see cref="ParseNextToken"/>. This is also a context object that can be used to reset the parser to
/// before the token it represents was parsed.
/// </summary>
/// <remarks>
/// This type is not default safe. Attempts to use <code>default(Result)</code> will result in undefined behavior.
/// </remarks>
public readonly struct Result
{
/// <summary>
/// The token that was parsed.
/// </summary>
public readonly SyntaxToken Token { get; }
/// <summary>
/// If the parsed token is potentially a contextual keyword, this will return the contextual kind of the token. Otherwise, it
/// will return <see cref="SyntaxKind.None"/>.
/// </summary>
public readonly SyntaxKind ContextualKind { get; }
}
}
from roslyn.
Related Issues (20)
- Error in 【MSBuildWorkspace.Create() .OpenSolutionAsync()】:System.Exception (cannotfound BuildHost) HOT 1
- ISymbol.ToMinimalDisplayString() ignores the SymbolDisplayTypeQualificationStyle.NameOnly formatting option. HOT 2
- [Bug] IDE0058 not work
- Task List filtering for current project doesn't work as expected
- switch expression: Warning CS8524 issued although all cases have been covered HOT 5
- Add assembly version information to method tooltips
- Automatic class initialization scaffolding
- Add string to NameOf option in intellisense
- Add IL Viewer for .NET languages HOT 1
- Make fields generated in a primary constructor able to be readonly HOT 1
- Can't see primary constructor variables in debugger
- Run a single generator at a time HOT 1
- Prepend $(NoWarn) to list analyzer rule generates a false positive. HOT 1
- Add compiler warning about CS file name not matching the class/interface contained within
- [Automated] PRs inserted in VS build feature.debugger.main-35020.110
- [Automated] PRs inserted in VS build main-35020.20
- Code Lens doesn't show references on Source Generated files
- Error/Warning messages on network share are not navigable HOT 1
- Cycle in required members
- Microsoft.DotNet.GenAPI.Tests/CSharpFileBuilderTests.cs in SDK repo test failures
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roslyn.