Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Input Cursor #335

Closed
Tracked by #337
victor-pogor opened this issue Jun 18, 2024 · 0 comments · Fixed by #338
Closed
Tracked by #337

Implement Input Cursor #335

victor-pogor opened this issue Jun 18, 2024 · 0 comments · Fixed by #338
Assignees
Labels
🎯 user story Short requirements or requests written from the perspective of an end user
Milestone

Comments

@victor-pogor
Copy link
Member

victor-pogor commented Jun 18, 2024

Background and Motivation

A cursor in the context of a lexer helps manage the current position within the input text being processed, it can look ahead (peek) for the next characters, advance the cursor, etc.

Proposed API

internal sealed class TextCursor
{
    // Constructors
    public TextCursor(ReadOnlySpan<byte> text);
    public TextCursor(ReadOnlySpan<char> text);
    public TextCursor(ReadOnlySpan<string> text);

    // Properties
    public Option<byte> Current { get; }
    public int Length { get; }
    public bool IsAtEnd { get; }

    // Methods
    public Option<byte> Peek(int delta = 0);
    public void Advance(int delta = 1);
    public void Advance(Func<byte, bool> predicate);
    public bool TryAdvance(byte b);
    public bool TryAdvance(ReadOnlySpan<byte> subtext);
}

Usage Examples

ITextCursor cursor = new Cursor("test string"u8);
Option<byte> firstChar = cursor.Peek(); // 't'

Acceptance criteria

  • Do not use null, use an Option type
  • The cursor must work with UTF-8 string literal as an input
  • Unit Tests
  • Mutation Testing

Open questions

  • Naming: Cursor, Scanner?
  • How do different languages handle the scanning phase?
@victor-pogor victor-pogor self-assigned this Jun 18, 2024
@victor-pogor victor-pogor converted this from a draft issue Jun 18, 2024
@victor-pogor victor-pogor added area:compiler 🎯 user story Short requirements or requests written from the perspective of an end user labels Jun 18, 2024
@victor-pogor victor-pogor changed the title [API Proposal] Input Scanner [Compiler] Input Scanner Jun 18, 2024
@victor-pogor victor-pogor changed the title [Compiler] Input Scanner [Compiler] Implement Input Cursor Jun 19, 2024
@victor-pogor victor-pogor moved this from 📋 Backlog to 🏗 In progress in Off.NET Board Jun 19, 2024
@victor-pogor victor-pogor added this to the v0.1 milestone Jun 19, 2024
@victor-pogor victor-pogor linked a pull request Jun 21, 2024 that will close this issue
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Off.NET Board Jun 21, 2024
@victor-pogor victor-pogor changed the title [Compiler] Implement Input Cursor Implement Input Cursor Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎯 user story Short requirements or requests written from the perspective of an end user
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant