parsing a css file

Discussion in 'OT Technology' started by Seeders, Mar 11, 2010.

  1. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
    I need to write a CSS parser that outputs to XML. Do you have any tips?


    Would I just read in the css file line by line looking for these characters { : ; } /* */ and use everything in between as the terms?

    shouldn't be too difficult right?
     
  2. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
    this is harder than i thought.

    i started doing a loop going line by line:

    Code:
              Do Until textstream.AtEndOfStream
                    line = textstream.ReadAll
                    
                    if(status = "looking_for_rule") then
                        i = instr(line,"{")
                        if not i = 0 then
                            status = "looking_for_property"
                            rule = Trim(Mid(line,1,i-1)                
                            response.write(rule &  VbCrLf )
                        end if                                            
                    end if
              Loop
    
    but this isn't going to get me very far, because what if the person writes their CSS like:

    Code:
        div
        {
             color: #000;
         }
    
    instead of

    Code:
        div {
             color: #000;
        }
    
    so i have to go character by character i guess.
     
  3. antiyou

    antiyou OT Supporter

    Joined:
    Jul 13, 2005
    Messages:
    25,295
    Likes Received:
    0
    Location:
    in ur base
    dude, you're parsing a regular language, go top to bottom left to right, generate tokens, use a stack if you have to but you need to maintain the state of where you are in the program while you're parsing.

    example when you come across an open stache { push tokens onto the stack and close stache } pop from the stack. If the last symbol of a token is ';' do something if the beginning of a token is '.' do something else...

    I hope that helps as you don't seem to understand what you're attempting to accomplish in it's entirety.
     
  4. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    :rofl: chill out dude hes never written a compiler im guessing he doesnt have any idea what the best way to go about it is. to build a good css parser you're going to have to actually write a parser that does what antiyou was mentioning above. Take a look at the wikipedia page for Parser and see if thats going to be over your head or not and or worth the work. There's also already open source solutions available to it. Depends on what you're doing this for.
     
  5. antiyou

    antiyou OT Supporter

    Joined:
    Jul 13, 2005
    Messages:
    25,295
    Likes Received:
    0
    Location:
    in ur base
    oh, I was trying to help him understand, I saw that he posted earlier about the same thing in the main forum and it was phrased as though he were completely at a loss as to understanding the requirements of this project. For instance the other post said he needs to output to XML...and I'm like output what to xml, the css tokens?
     

Share This Page