I'd like to write a CSS StyleSheet Parser but...

124 views
Skip to first unread message

atd...@gmail.com

unread,
Jan 3, 2021, 2:14:18 AM1/3/21
to golang-nuts
Hello,

I am currently in need of a css stylesheet parser to retrieve the CSSOM as a go datastructure.
(probably in a map[*selector* string]map[*cssproperty* string]interface{})

The existing Go libraries that can be found online are a bit lacking in that respect, imho.

But I also have never written any lexer/parser before. Watched Rob's youtube video from a while ago which helped in understanding a little bit but it's only the lexing part.

So I was wondering, what does the parsing step consist in because the examples of codes I've been looking at online so far were a bit confusing to me.

I also would like to know if there are ways to create a parser from simply inputing the ebnf form (found it for CSS3) somewhere? Becauser I am lazy ;D

Notably, I have never understood what should my output look like after parsing... If this is an AST, should I define the Node type structure somewhere? What to do?

Bref, I am a total noob on that subject.

Please, help :D
(I also may need to be able to parse the css from an html file afterwards but I think there are better libraries that already exist for this case)

Stephan Lukits

unread,
Jan 4, 2021, 6:48:48 AM1/4/21
to atd...@gmail.com, golang-nuts


On 3 Jan 2021, at 9:15, atd...@gmail.com <atd...@gmail.com> wrote:

Hello,

I am currently in need of a css stylesheet parser to retrieve the CSSOM as a go datastructure.
(probably in a map[*selector* string]map[*cssproperty* string]interface{})

The existing Go libraries that can be found online are a bit lacking in that respect, imho.

you know about https://github.com/tdewolff/parse

you can investigate this project's sources.

Howard C. Shaw III

unread,
Jan 4, 2021, 3:11:43 PM1/4/21
to golang-nuts
An AST (Abstract Syntax Tree) is exactly what the common output of a parser is, and you can see an example of it in the Go source code. But not always - the output of a parser can also be function calls, as in a streaming XML (SAX) style parser. 

The important elements of a parser is that it knows or implements a grammar, and can report grammatical and syntactical errors, and in the absence of them, provides information to some subsequent activity that is known to be grammatically and syntactically correct. 

When the input can be described by a formal grammar such as EBNF, then yes, there exist tools that can take an EBNF grammar and write a parser for it, which returns an AST that can then be operated over. 

The project that stephan linked you to actually already contains a CSS3 parser; however, to answer your other question: https://github.com/goccmack/gocc is a package that allows you to generate a lexer/parser pair from a BNF definition. https://github.com/goccmack/gogll works on a subset of valid grammars (context-free grammars) to produce lexers and parsers.
Reply all
Reply to author
Forward
0 new messages