stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.33k stars 221 forks source link

Convert Token from enum to struct with type property #255

Closed ilyapuchka closed 5 years ago

ilyapuchka commented 5 years ago

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 5 years ago

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.

ilyapuchka commented 5 years ago

I mean enum Token in Tokenizer.swift:74

djbe commented 5 years ago

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.

ilyapuchka commented 5 years ago

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

djbe commented 5 years ago

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