func (parser *Parser) tokenComment() bool {
	return parser.nextToken().Type == scanner.TokenComment
}
Returns true if next token is a comment
package parser
import (
	"errors"
	"fmt"
	"regexp"
	"strings"
	"github.com/gorilla/css/scanner"
	"github.com/aymerick/douceur/css"
)
const (
	importantSuffixRegexp = `(?i)\s*!important\s*$`
)
var (
	importantRegexp *regexp.Regexp
)
type Parser struct {
	scan *scanner.Scanner  
	tokens []*scanner.Token
	embedLevel int
}
func init() {
	importantRegexp = regexp.MustCompile(importantSuffixRegexp)
}
func NewParser(txt string) *Parser {
	return &Parser{
		scan: scanner.New(txt),
	}
}
func Parse(text string) (*css.Stylesheet, error) {
	result, err := NewParser(text).ParseStylesheet()
	if err != nil {
		return nil, err
	}
	return result, nil
}
func ParseDeclarations(text string) ([]*css.Declaration, error) {
	result, err := NewParser(text).ParseDeclarations()
	if err != nil {
		return nil, err
	}
	return result, nil
}
func (parser *Parser) ParseStylesheet() (*css.Stylesheet, error) {
	result := css.NewStylesheet()
	if _, err := parser.parseBOM(); err != nil {
		return result, err
	}
	rules, err := parser.ParseRules()
	if err != nil {
		return result, err
	}
	result.Rules = rules
	return result, nil
}
func (parser *Parser) ParseRules() ([]*css.Rule, error) {
	result := []*css.Rule{}
	inBlock := false
	if parser.tokenChar("{") {
		inBlock = true
		parser.embedLevel++
		parser.shiftToken()
	}
	for parser.tokenParsable() {
		if parser.tokenIgnorable() {
			parser.shiftToken()
		} else if parser.tokenChar("}") {
			if !inBlock {
				errMsg := fmt.Sprintf("Unexpected } character: %s", parser.nextToken().String())
				return result, errors.New(errMsg)
			}
			parser.shiftToken()
			parser.embedLevel--
			break
		} else {
			rule, err := parser.ParseRule()
			if err != nil {
				return result, err
			}
			rule.EmbedLevel = parser.embedLevel
			result = append(result, rule)
		}
	}
	return result, parser.err()
}
func (parser *Parser) ParseRule() (*css.Rule, error) {
	if parser.tokenAtKeyword() {
		return parser.parseAtRule()
	}
	return parser.parseQualifiedRule()
}
func (parser *Parser) ParseDeclarations() ([]*css.Declaration, error) {
	result := []*css.Declaration{}
	if parser.tokenChar("{") {
		parser.shiftToken()
	}
	for parser.tokenParsable() {
		if parser.tokenIgnorable() {
			parser.shiftToken()
		} else if parser.tokenChar("}") {
			parser.shiftToken()
			break
		} else {
			declaration, err := parser.ParseDeclaration()
			if err != nil {
				return result, err
			}
			result = append(result, declaration)
		}
	}
	return result, parser.err()
}
func (parser *Parser) ParseDeclaration() (*css.Declaration, error) {
	result := css.NewDeclaration()
	curValue := ""
	for parser.tokenParsable() {
		if parser.tokenChar(":") {
			result.Property = strings.TrimSpace(curValue)
			curValue = ""
			parser.shiftToken()
		} else if parser.tokenChar(";") || parser.tokenChar("}") {
			if result.Property == "" {
				errMsg := fmt.Sprintf("Unexpected ; character: %s", parser.nextToken().String())
				return result, errors.New(errMsg)
			}
			if importantRegexp.MatchString(curValue) {
				result.Important = true
				curValue = importantRegexp.ReplaceAllString(curValue, "")
			}
			result.Value = strings.TrimSpace(curValue)
			if parser.tokenChar(";") {
				parser.shiftToken()
			}
			break
		} else {
			token := parser.shiftToken()
			curValue += token.Value
		}
	}
	return result, parser.err()
}
func (parser *Parser) parseAtRule() (*css.Rule, error) {
	token := parser.shiftToken()
	result := css.NewRule(css.AtRule)
	result.Name = token.Value
	for parser.tokenParsable() {
		if parser.tokenChar(";") {
			parser.shiftToken()
			break
		} else if parser.tokenChar("{") {
			if result.EmbedsRules() {
				rules, err := parser.ParseRules()
				if err != nil {
					return result, err
				}
				result.Rules = rules
			} else {
				declarations, err := parser.ParseDeclarations()
				if err != nil {
					return result, err
				}
				result.Declarations = declarations
			}
			break
		} else {
			prelude, err := parser.parsePrelude()
			if err != nil {
				return result, err
			}
			result.Prelude = prelude
		}
	}
	return result, parser.err()
}
func (parser *Parser) parseQualifiedRule() (*css.Rule, error) {
	result := css.NewRule(css.QualifiedRule)
	for parser.tokenParsable() {
		if parser.tokenChar("{") {
			if result.Prelude == "" {
				errMsg := fmt.Sprintf("Unexpected { character: %s", parser.nextToken().String())
				return result, errors.New(errMsg)
			}
			declarations, err := parser.ParseDeclarations()
			if err != nil {
				return result, err
			}
			result.Declarations = declarations
			break
		} else {
			prelude, err := parser.parsePrelude()
			if err != nil {
				return result, err
			}
			result.Prelude = prelude
		}
	}
	result.Selectors = strings.Split(result.Prelude, ",")
	for i, sel := range result.Selectors {
		result.Selectors[i] = strings.TrimSpace(sel)
	}
	return result, parser.err()
}
func (parser *Parser) parsePrelude() (string, error) {
	result := ""
	for parser.tokenParsable() && !parser.tokenEndOfPrelude() {
		token := parser.shiftToken()
		result += token.Value
	}
	result = strings.TrimSpace(result)
	return result, parser.err()
}
func (parser *Parser) parseBOM() (bool, error) {
	if parser.nextToken().Type == scanner.TokenBOM {
		parser.shiftToken()
		return true, nil
	}
	return false, parser.err()
}
func (parser *Parser) nextToken() *scanner.Token {
	if len(parser.tokens) == 0 {
		nextToken := parser.scan.Next()
		parser.tokens = append(parser.tokens, nextToken)
	}
	return parser.tokens[0]
}
func (parser *Parser) shiftToken() *scanner.Token {
	var result *scanner.Token
	result, parser.tokens = parser.tokens[0], parser.tokens[1:]
	return result
}
func (parser *Parser) err() error {
	if parser.tokenError() {
		token := parser.nextToken()
		return fmt.Errorf("Tokenizer error: %s", token.String())
	}
	return nil
}
func (parser *Parser) tokenError() bool {
	return parser.nextToken().Type == scanner.TokenError
}
func (parser *Parser) tokenEOF() bool {
	return parser.nextToken().Type == scanner.TokenEOF
}
func (parser *Parser) tokenWS() bool {
	return parser.nextToken().Type == scanner.TokenS
}