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

Emit Alternate Token From Callback #391

Open
UnexDev opened this issue May 8, 2024 · 3 comments
Open

Emit Alternate Token From Callback #391

UnexDev opened this issue May 8, 2024 · 3 comments
Labels
question Further information is requested

Comments

@UnexDev
Copy link

UnexDev commented May 8, 2024

Is it possible to emit an alternate token from a callback (i.e not the lexed token)?

For example, a stack-based white-space-sensetive parser:
`fn indent(lex: &mut logos::Lexer) -> /* ??? */ {
let extras = &mut lex.extras;
let mut indent = 0;

// maybe use a take_while then a replace?
for char in lex.remainder().chars() {
    if char == '\t' {
        for i in 1..8 {
            if (indent + i) % 8 == 0 {
                indent += i;
                break;
            }
        }
    } else if char == ' ' {
        indent += 1;
    } else {
        break;
    }

    lex.bump(1);
    let last = extras.indent.last();

    if indent < last {
        extras.indent.pop();
       /* emit Dedent token instead of Indent */
    } else if indent > last {
        extras.indent.push(indent);
        /* emit Indent */
    }
}

}`

@kotx
Copy link

kotx commented May 11, 2024

You can return a Token from your callback. There's also utilities like Filter, Skip, FilterResult, etc. See https://logos.maciej.codes/callbacks.html

@UnexDev
Copy link
Author

UnexDev commented May 14, 2024

You can return a Token from your callback. There's also utilities like Filter, Skip, FilterResult, etc. See https://logos.maciej.codes/callbacks.html

Thank you, that worked wonderfully!

Just one more thing - can I emit multiple tokens of the same type? I need to emit multiple dedent tokens. I'm assuming this is not possible?

@kotx
Copy link

kotx commented May 14, 2024

Just one more thing - can I emit multiple tokens of the same type? I need to emit multiple dedent tokens. I'm assuming this is not possible?

I don't think it's possible, but I decided to use a Dedent(usize) token as a workaround.

@jeertmans jeertmans added the question Further information is requested label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants