-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web-css: cssom builder and parser improvements 🔨
- Loading branch information
Showing
7 changed files
with
285 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <karm-io/funcs.h> | ||
|
||
#include "lexer.h" | ||
|
||
namespace Web::Css { | ||
|
||
struct Ast; | ||
|
||
using Content = Vec<Ast>; | ||
|
||
#define FOREACH_AST(AST) \ | ||
AST(QUALIFIED_RULE) \ | ||
AST(FUNC) \ | ||
AST(DECL) \ | ||
AST(LIST) \ | ||
AST(TOKEN) \ | ||
AST(BLOCK) | ||
|
||
struct Ast { | ||
enum struct _Type { | ||
#define ITER(NAME) NAME, | ||
FOREACH_AST(ITER) | ||
#undef ITER | ||
}; | ||
|
||
using enum _Type; | ||
|
||
_Type type; | ||
Opt<Token> token{}; | ||
Opt<Box<Ast>> prefix{}; | ||
Content content{}; | ||
|
||
Ast(_Type type) : type(type) {} | ||
}; | ||
} // namespace Web::Css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#pragma once | ||
|
||
#include <web-cssom/stylesheet.h> | ||
|
||
#include "ast.h" | ||
|
||
namespace Web::Css { | ||
|
||
static CSSOM::CSSStyleRule parseQualifiedRule(Ast rule) { | ||
CSSOM::CSSStyleRule parsed; | ||
|
||
auto &prefix = rule.prefix.unwrap()->content; | ||
for (usize i = 0; i < prefix.len(); i++) { | ||
switch (prefix[i].type) { | ||
case Ast::QUALIFIED_RULE: | ||
case Ast::FUNC: | ||
case Ast::DECL: | ||
case Ast::LIST: | ||
case Ast::BLOCK: | ||
break; | ||
case Ast::TOKEN: | ||
parsed.selector.add(prefix[i].token.unwrap()); | ||
break; | ||
} | ||
} | ||
|
||
auto block = rule.content[0].content; | ||
bool parsingContent = false; | ||
for (usize i = 0; i < block.len(); i++) { | ||
switch (block[i].type) { | ||
case Ast::QUALIFIED_RULE: | ||
case Ast::FUNC: | ||
case Ast::DECL: | ||
case Ast::LIST: | ||
case Ast::BLOCK: | ||
break; | ||
case Ast::TOKEN: | ||
if (!parsingContent) { | ||
if (block[i].token->type != Token::WHITESPACE) { | ||
if (block[i].token->type != Token::COLON) { | ||
parsed.declarations.add(CSSOM::CSSStyleDeclaration(block[i].token.unwrap())); | ||
} else { | ||
parsingContent = true; | ||
} | ||
} | ||
} else { | ||
if (block[i].token->type != Token::SEMICOLON) { | ||
last(parsed.declarations).value.add(block[i].token.unwrap()); | ||
} else { | ||
parsingContent = false; | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
return parsed; | ||
} | ||
|
||
// No spec, we take the AST we built and convert it to a usable list of rules | ||
Vec<CSSOM::CSSStyleRule> parseAST(Ast ast) { | ||
Vec<CSSOM::CSSStyleRule> rules; | ||
for (usize i = 0; i < ast.content.len(); i++) { | ||
switch (ast.content[i].type) { | ||
|
||
case Ast::_Type::QUALIFIED_RULE: | ||
rules.add(parseQualifiedRule(ast.content[i])); | ||
break; | ||
case Ast::_Type::FUNC: | ||
case Ast::_Type::DECL: | ||
case Ast::_Type::LIST: | ||
case Ast::_Type::TOKEN: | ||
case Ast::_Type::BLOCK: | ||
break; | ||
} | ||
} | ||
return rules; | ||
} | ||
|
||
} // namespace Web::Css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.