stencilproject/Stencil

Convert Token from enum to struct with type property

ilyapuchka opened this issue · 5 comments

Currently, Token is represented as enum which does not allow to store any properties in it, so we components property is computed, which affects performance as they can be parsed several times. We should instead convert it to struct and use a property of enum type like type.

djbe commented

Sounds good, I think.

Which token are we talking about though? Because if this is related to #253, it could be Token (in lexer), it could be IfToken in if-tag, and I'm sure there are some tokes somewhere in the code.

I mean enum Token in Tokenizer.swift:74

djbe commented

Yeah, that's a clear candidate for a struct. Every case has the same associated value 🤦‍♂️

Is components accessed for every Token instance? Or only specific cases? I'm pretty sure we don't need the components of a .text token. Because if so, we may want to make components a lazy property.

Yes, we also don't need to parse it right away, so lazy makes total sense

djbe commented

Then we may want a class instead of a struct I think, if we want to use private(set) lazy var, not sure though. Yeah it needs to be a class, otherwise we'll get:

Cannot use mutating getter on immutable value: 'token' is a 'let' constant