简体   繁体   中英

Haskell: Why isn't my parser backtracking properly?

I decided to teach myself how to use Parsec , and I've hit a bit of a road block with the toy project I assigned myself.

I'm trying to parse HTML, specifically:

<html>
  <head>
    <title>Insert Clever Title</title>
  </head>
  <body>
    What don't you like?
    <select id="some stuff">
      <option name="first" font="green">boilerplate</option>
      <option selected name="second" font="blue">parsing HTML with regexes</option>
      <option name="third" font="red">closing tags for option elements
    </select>
    That was short.
  </body>
</html>

My code is:

{-# LANGUAGE FlexibleContexts, RankNTypes #-}
module Main where

import System.Environment (getArgs)
import Data.Map hiding (null)
import Text.Parsec hiding ((<|>), label, many, optional)
import Text.Parsec.Token
import Control.Applicative

data HTML = Element { tag :: String, attributes :: Map String (Maybe String), children :: [HTML] }
          | Text { contents :: String }
  deriving (Show, Eq)

type HTMLParser a = forall s u m. Stream s m Char => ParsecT s u m a

htmlDoc :: HTMLParser HTML
htmlDoc = do
  spaces
  doc <- html
  spaces >> eof
  return doc

html :: HTMLParser HTML
html = text <|> element

text  :: HTMLParser HTML
text = Text <$> (many1 $ noneOf "<")

label :: HTMLParser String
label = many1 . oneOf $ ['a' .. 'z']  ++ ['A' .. 'Z']

value :: HTMLParser String
value = between (char '"') (char '"') (many anyChar) <|> label

attribute :: HTMLParser (String, Maybe String)
attribute = (,) <$> label <*> (optionMaybe $ spaces >> char '=' >> spaces >> value)

element :: HTMLParser HTML
element = do
  char '<' >> spaces
  tag <- label
  -- at least one space between each attribute and what was before
  attributes <- fromList <$> many (space >> spaces >> attribute)
  spaces >> char '>' 
  -- nested html
  children <- many html
  optional $ string "</" >> spaces >> string tag >> spaces >> char '>'
  return $ Element tag attributes children

main = do
  source : _ <- getArgs
  result <- parse htmlDoc source <$> readFile source
  print result

The problem seems to be that my parser doesn't like closing tags - it seems to be greedily assuming < always means an opening tag (as far as I can tell):

% HTMLParser temp.html
Left "temp.html" (line 3, column 32):
unexpected "/"
expecting white space

I've been playing around with it for a bit, and I'm not sure why it's not backtracking past the char '<' match.

Like ehird said, I needed to use try:

attribute = (,) <$> label <*> (optionMaybe . try $ spaces >> char '=' >> spaces >> value) 
--...
attributes <- fromList <$> many (try $ space >> spaces >> attribute)
--...
children <- many $ try html
optional . try $ string "</" >> spaces >> string tag >> spaces >> char '>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM