Skip to content

Commit

Permalink
add an initial css lexer+parser+printer (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Sep 12, 2020
1 parent f9858fc commit 99bafd0
Show file tree
Hide file tree
Showing 6 changed files with 1,964 additions and 0 deletions.
111 changes: 111 additions & 0 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package css_ast

import (
"github.com/evanw/esbuild/internal/css_lexer"
"github.com/evanw/esbuild/internal/logger"
)

type AST struct {
Rules []R
}

// This interface is never called. Its purpose is to encode a variant type in
// Go's type system.
type R interface {
isRule()
}

type RAtImport struct {
PathText string
PathRange logger.Range
}

type RKnownAt struct {
Name css_lexer.Token
Prelude []css_lexer.Token
Rules []R
}

type RUnknownAt struct {
Name css_lexer.Token
Prelude []css_lexer.Token
Block []css_lexer.Token
}

type RSelector struct {
Selectors []ComplexSelector
Rules []R
}

type RQualified struct {
Prelude []css_lexer.Token
Rules []R
}

type RDeclaration struct {
Key css_lexer.Token
Value []css_lexer.Token
Important bool
}

type RBadDeclaration struct {
Tokens []css_lexer.Token
}

func (*RAtImport) isRule() {}
func (*RKnownAt) isRule() {}
func (*RUnknownAt) isRule() {}
func (*RSelector) isRule() {}
func (*RQualified) isRule() {}
func (*RDeclaration) isRule() {}
func (*RBadDeclaration) isRule() {}

type ComplexSelector struct {
Selectors []CompoundSelector
}

type CompoundSelector struct {
Combinator string // Optional, may be ""
TypeSelector *NamespacedName
SubclassSelectors []SS
PseudoClassSelectors []SSPseudoClass // If present, these follow a ":" character
}

type NamespacedName struct {
// If present, this is an identifier or "*" or "" and is followed by a "|" character
NamespacePrefix *string

// This is an identifier or "*" or "&"
Name string
}

// This interface is never called. Its purpose is to encode a variant type in
// Go's type system.
type SS interface {
isSubclassSelector()
}

type SSHash struct {
Name string
}

type SSClass struct {
Name string
}

type SSAttribute struct {
NamespacedName NamespacedName
MatcherOp string
MatcherValue string
MatcherModifier byte
}

type SSPseudoClass struct {
Name string
Args []css_lexer.Token
}

func (*SSHash) isSubclassSelector() {}
func (*SSClass) isSubclassSelector() {}
func (*SSAttribute) isSubclassSelector() {}
func (*SSPseudoClass) isSubclassSelector() {}

0 comments on commit 99bafd0

Please sign in to comment.