Emma Boudreau
2 min readNov 15, 2024

--

I was kind of realizing that would be the case without the space, and with this consideration `\n` does make more sense to seek to

unfortunately, the classes _could_ be inline, so we don't always know if they're on a new line.

I guess the best solution here would be to seek to the previous } unless it is the BOF, probably just grabbing the whole space between the beginning { and the closing } or beginning of the file and simply removing all of the spaces.

At least we could set the parser to seek to the BOF on the first one, just setting the beginning/end at the end of the loop each time as we set the beginning position. Perhaps this would be the best solution. And you're absolutely right, little mistakes like this not only demonstrate why this technique can be more difficult, but also why circumstances where this technique might be necessary are typically going to require a bit more thought.

Was pretty easy to implement:

function parse(raw::String)

position = findfirst("{", raw)

classes = Vector{CSSClass}()

name_start = 0:0

while true

class_n = replace(raw[name_start[1] + 1:position[1] - 1], " " => "")

propend = findnext("}", raw, position[1])

propstring = raw[position[1] + 1:propend[1] - 1]

propsplits = split(propstring, ";")

properties = Dict{String, Any}(begin

propsplit = split(prop, ":")

string(replace(propsplit[1], " " => "", "\n" => "")) => replace(propsplit[2], " " => "", "\n" => "")

end for prop in propsplits[1:length(propsplits) - 1])

push!(classes, CSSClass(replace(class_n, " " => ""), properties))

position = findnext("{", raw, propend[1])

name_start = propend[1] + 1

if isnothing(position)

break

end

end

classes

end

This parser would be much more normal; not requiring a space after each class name in order to actually work :p

--

--

Emma Boudreau
Emma Boudreau

Written by Emma Boudreau

i am a computer nerd. I love art, programming, and hiking. https://github.com/emmaccode

Responses (1)