Skip to content

Fold RawToken into Token and remove the old Token.#16

Merged
c42f merged 1 commit intoJuliaLang:mainfrom
KristofferC:kc/raw_into_tok
Feb 24, 2022
Merged

Fold RawToken into Token and remove the old Token.#16
c42f merged 1 commit intoJuliaLang:mainfrom
KristofferC:kc/raw_into_tok

Conversation

@KristofferC
Copy link
Copy Markdown
Member

@KristofferC KristofferC commented Feb 23, 2022

I think it is best to go "all in" on that Token only stores the byte ranges. No one will use the Token that stores the string explicitly because it is too slow. This allows quite a lot of code removal.

@KristofferC KristofferC changed the title kc/raw into tok Fold RawToken into Token and remove the old Token. Feb 23, 2022
Copy link
Copy Markdown
Member

@c42f c42f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What an unexpected-but-welcome gift of code. Thanks @KristofferC !

Comment thread Tokenize/src/token.jl
print(io, rpad(kind(t), 15, " "))
print(io, "\"", str, "\"")
function untokenize(t::Token, str::String)
String(codeunits(str)[1 .+ (t.startbyte:t.endbyte)])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice way to express this operation with codeunits. I'm usually messing with previndex for this.

Comment thread Tokenize/test/lexer.jl
end

~(tok::T.AbstractToken, t::Tuple) = tok.kind == t[1] && untokenize(tok) == t[2]
~(tok::T.Token, t::Tuple) = tok.kind == t[1] && untokenize(tok, t[3]) == t[2]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oho. I see what you did there. Very tricky.

Maybe neater to just define a utility function to take the place of collect(tokenize(ts)) instead which yields the tuples I'm testing against below. Then use == rather than having to pass str everywhere.

(Nevertheless, let's merge this as-is; that could be cleaned up separately.)

@c42f c42f merged commit f7c0dd6 into JuliaLang:main Feb 24, 2022
c42f pushed a commit to JuliaLang/julia that referenced this pull request Oct 17, 2025
topolarity pushed a commit to JuliaLang/julia that referenced this pull request Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants