--- factorcss/Parser.y 2004/12/19 22:10:41 61 +++ factorcss/Parser.y 2005/01/07 21:54:20 69 @@ -1,3 +1,10 @@ +-- +-- This file is part of FactorCSS +-- Licensed under the MIT License, +-- http://www.opensource.org/licenses/mit-license +-- Copyright 2004 James Bursa +-- + -- Parser for CSS 2.1 -- See CSS 2.1 G.2 @@ -8,66 +15,69 @@ } %name parser -%tokentype { Token } +%tokentype { TokenPosn } %token - INCLUDES { INCLUDES } - DASHMATCH { DASHMATCH } - LBRACE { LBRACE } - PLUS { PLUS } - GREATER { GREATER } - COMMA { COMMA } - STRING { STRING $$ } - IDENT { IDENT $$ } - HASH { HASH $$ } - IMPORT_SYM { IMPORT_SYM } - PAGE_SYM { PAGE_SYM } - MEDIA_SYM { MEDIA_SYM } - CHARSET_SYM { CHARSET_SYM } - IMPORTANT_SYM { IMPORTANT_SYM } - EMS { EMS $$ } - EXS { EXS $$ } - LENGTH { LENGTH $$ } - ANGLE { ANGLE $$ } - TIME { TIME $$ } - FREQ { FREQ $$ } - DIMEN { DIMEN $$ } - PERCENTAGE { PERCENTAGE $$ } - NUMBER { NUMBER $$ } - URI { URI $$ } - FUNCTION { FUNCTION $$ } - DELIM { DELIM $$ } - SEMI { SEMI } - RBRACE { RBRACE } - COLON { COLON } - SLASH { SLASH } - MINUS { MINUS } - DOT { DOT } - LBRAC { LBRAC } - RBRAC { RBRAC } - ASTERISK { ASTERISK } - EQUALS { EQUALS } - RPAREN { RPAREN } + S { (S , _) } + INCLUDES { (INCLUDES , _) } + DASHMATCH { (DASHMATCH , _) } + LBRACE { (LBRACE , _) } + PLUS { (PLUS , _) } + GREATER { (GREATER , _) } + COMMA { (COMMA , _) } + STRING { (STRING $$ , _) } + IDENT { (IDENT $$ , _) } + HASH { (HASH $$ , _) } + IMPORT_SYM { (IMPORT_SYM , _) } + PAGE_SYM { (PAGE_SYM , _) } + MEDIA_SYM { (MEDIA_SYM , _) } + CHARSET_SYM { (CHARSET_SYM , _) } + IMPORTANT_SYM { (IMPORTANT_SYM, _) } + EMS { (EMS $$ , _) } + EXS { (EXS $$ , _) } + LENGTH { (LENGTH $$ , _) } + ANGLE { (ANGLE $$ , _) } + TIME { (TIME $$ , _) } + FREQ { (FREQ $$ , _) } + DIMEN { (DIMEN $$ , _) } + PERCENTAGE { (PERCENTAGE $$, _) } + NUMBER { (NUMBER $$ , _) } + URI { (URI $$ , _) } + FUNCTION { (FUNCTION $$ , _) } + DELIM { (DELIM $$ , _) } + SEMI { (SEMI , _) } + RBRACE { (RBRACE , _) } + COLON { (COLON , _) } + SLASH { (SLASH , _) } + MINUS { (MINUS , _) } + DOT { (DOT , _) } + LBRAC { (LBRAC , _) } + RBRAC { (RBRAC , _) } + ASTERISK { (ASTERISK , _) } + EQUALS { (EQUALS , _) } + RPAREN { (RPAREN , _) } %% stylesheet :: { Stylesheet } - : charset import_list statement_list { Stylesheet $1 $2 $3 } + : charset s import_list s statement_list s + { Stylesheet $1 $3 $5 } charset :: { Maybe String } - : { Nothing } - | CHARSET_SYM STRING SEMI { Just $2 } + : { Nothing } + | CHARSET_SYM s STRING s SEMI { Just $3 } import_list :: { [(String, [String])] } : { [] } - | import_list import { $2 : $1 } + | import_list s import { $3 : $1 } import :: { (String, [String]) } - : IMPORT_SYM STRING medium_list SEMI { ($2, $3) } - | IMPORT_SYM URI medium_list SEMI { ($2, $3) } + : IMPORT_SYM s STRING s medium_list SEMI s + { ($3, $5) } + | IMPORT_SYM s URI s medium_list SEMI s { ($3, $5) } statement_list :: { [Statement] } : { [] } - | statement_list statement { $2 : $1 } + | statement_list s statement { $3 : $1 } statement :: { Statement } : ruleset { $1 } @@ -80,50 +90,52 @@ medium_list_1 :: { [String] } : medium { [$1] } - | medium_list_1 COMMA medium { $3 : $1 } + | medium_list_1 s COMMA s medium { $5 : $1 } media :: { Statement } - : MEDIA_SYM medium_list_1 LBRACE ruleset_list RBRACE - { Media $2 $4 } + : MEDIA_SYM s medium_list_1 s LBRACE s ruleset_list s RBRACE + { Media $3 $7 } medium :: { String } : IDENT { $1 } ruleset_list :: { [Statement] } : { [] } - | ruleset_list ruleset { $2 : $1 } + | ruleset_list s ruleset { $3 : $1 } page :: { Statement } - : PAGE_SYM LBRACE declaration_list_1 RBRACE - { Page Nothing $3 } - | PAGE_SYM pseudo_page LBRACE declaration_list_1 RBRACE - { Page (Just $2) $4 } + : PAGE_SYM s LBRACE s declaration_list_1 s RBRACE + { Page Nothing $5 } + | PAGE_SYM s pseudo_page s LBRACE s declaration_list_1 RBRACE + { Page (Just $3) $7 } declaration_list_1 :: { [Declaration] } : { [] } - | declaration { [$1] } - | declaration_list_1 SEMI { $1 } - | declaration_list_1 SEMI declaration { $3 : $1 } + | declaration s { [$1] } + | declaration_list_1 s SEMI { $1 } + | declaration_list_1 s SEMI s declaration + { $5 : $1 } pseudo_page :: { String } : COLON IDENT { $2 } ruleset :: { Statement } - : selector_list_1 LBRACE declaration_list_1 RBRACE - { Ruleset $1 $3 } + : selector_list_1 s LBRACE s declaration_list_1 s RBRACE + { Ruleset $1 $5 } selector_list_1 :: { [Selector] } : selector { [$1] } - | selector_list_1 COMMA selector { $3 : $1 } + | selector_list_1 COMMA s selector { $4 : $1 } selector :: { Selector } : simple_selector { [($1, NoMore)] } - | simple_selector combinator selector { ($1, $2) : $3 } + | simple_selector combinator selector + { ($1, $2) : $3 } combinator :: { Combinator } - : { Ancestor } - | PLUS { Preceded } - | GREATER { Parent } + : PLUS s { Preceded } + | GREATER s { Parent } + | S { Ancestor } simple_selector :: { SimpleSelector } : element_name detail_list_1 { ($1, $2) } @@ -148,22 +160,28 @@ : DOT IDENT { Class $2 } attrib :: { Detail } - : LBRAC IDENT RBRAC { Attrib $2 } - | LBRAC IDENT EQUALS IDENT RBRAC { AttribEq $2 $4 } - | LBRAC IDENT EQUALS STRING RBRAC { AttribEq $2 $4 } - | LBRAC IDENT INCLUDES IDENT RBRAC { AttribInc $2 $4 } - | LBRAC IDENT INCLUDES STRING RBRAC { AttribInc $2 $4 } - | LBRAC IDENT DASHMATCH IDENT RBRAC { AttribDM $2 $4 } - | LBRAC IDENT DASHMATCH STRING RBRAC { AttribDM $2 $4 } + : LBRAC s IDENT s RBRAC { Attrib $3 } + | LBRAC s IDENT s EQUALS s IDENT s RBRAC + { AttribEq $3 $7 } + | LBRAC s IDENT s EQUALS s STRING s RBRAC + { AttribEq $3 $7 } + | LBRAC s IDENT s INCLUDES s IDENT s RBRAC + { AttribInc $3 $7 } + | LBRAC s IDENT s INCLUDES s STRING s RBRAC + { AttribInc $3 $7 } + | LBRAC s IDENT s DASHMATCH s IDENT s RBRAC + { AttribDM $3 $7 } + | LBRAC s IDENT s DASHMATCH s STRING s RBRAC + { AttribDM $3 $7 } pseudo :: { Detail } : COLON IDENT { Pseudo $2 } - | COLON FUNCTION RPAREN { PseudoFunc $2 "" } - | COLON FUNCTION IDENT RPAREN { PseudoFunc $2 $3 } + | COLON FUNCTION s RPAREN { PseudoFunc $2 "" } + | COLON FUNCTION s IDENT s RPAREN { PseudoFunc $2 $4 } declaration :: { Declaration } - : property COLON value_list_1 { ($1, $3, False) } - | property COLON value_list_1 prio { ($1, $3, True) } + : property COLON s value_list_1 { ($1, $4, False) } + | property COLON s value_list_1 s prio { ($1, $4, True) } property :: { String } : IDENT { $1 } @@ -173,11 +191,11 @@ value_list :: { [Value] } : { [] } - | value value_list { $1 : $2 } + | value s value_list { $1 : $3 } value_list_1 :: { [Value] } : value { [$1] } - | value value_list_1 { $1 : $2 } + | value s value_list_1 { $1 : $3 } value :: { Value } : NUMBER { Number $1 } @@ -194,9 +212,19 @@ | HASH { HexColour $1 } | SLASH { Slash } | COMMA { Comma } - | FUNCTION value_list RPAREN { Function $1 $2 } + | FUNCTION s value_list s RPAREN { Function $1 $3 } + +s :: {} + : { } + | S { } { -happyError :: [Token] -> a -happyError _ = error "Parse error" +happyError :: [TokenPosn] -> a +happyError tks = error ("Parse error at " ++ lcn) + where + lcn = case tks of + [] -> "end of file" + tk:_ -> "line " ++ show l ++ ", column " ++ show c + where + AlexPn _ l c = snd tk }