我在输入 `->' 时遇到解析错误

Posted

技术标签:

【中文标题】我在输入 `->\' 时遇到解析错误【英文标题】:I'm getting parse error on input `->'我在输入 `->' 时遇到解析错误 【发布时间】:2014-01-22 13:19:14 【问题描述】:

我正在尝试运行以下代码,它在 -> 处给我解析错误

- Make sure you have the hxt, url and http packages:
        cabal install hxt
        cabal install url
        cabal install http
        cabal install hxt-curl -

import Text.XML.HXT.Core
import Network.HTTP
import Network.URI

weatherDataURL = "http://www.weather.gov/xml/current_obs/KAGC.xml"

retrieveWeatherData = do
  case parseURI weatherDataURL of
    Nothing  -> ioError . userError $ "Invalid URL"
    Just uri -> get uri
 - | Download a URL.  (Left errorMessage) if an error, (Right doc) if success. -
get uri = do
  eresp <- simpleHTTP (Request uri GET [] "")
  case eresp of
    Left _    -> ioError . userError $ "Failed to get " ++ show uri
    Right res -> return $ rspBody res

    -readString :: Attributes -> String -> iostateArrow s b XmlTreeSource read a document
    that is stored in a normal Haskell String the same function as readDocument, but the
    parameter forms the input. All options available for readDocument are applicable for
    readString. -


parseXML doc = readString [ withValidate no --turn off the validation step.
                          , withRemoveWS yes ---- throw away formatting WS
                          ] doc

data Weather = Weather
   location, observationTime,
    summary, windDirection :: String,

    temperature, humidity,
    dewpoint,
    pressure, windSpeed,
    visibility             :: Float 
  deriving (Eq, Show)

atTag tag = deep (isElem >>> hasName tag) -- Selecting all "top level" tag in XML document
text = getChildren >>> getText
textAtTag tag = atTag tag >>> text

getWeather = atTag "current_observation" >>>
        proc x -> do -- proc (arrow abstraction) except that it constructs an arrow instead of a function.
        loc     <- textAtTag "location"          -< x --  reserved symbol used for building commands from an expression of arrow type and an expression to be fed as input to that arrow
        obsTime <- textAtTag "observation_time"  -< x
        summ    <- textAtTag "weather"           -< x
        windDir <- textAtTag "wind_dir"          -< x
        temp    <- textAtTag "temp_c"            -< x
        humi    <- textAtTag "relative_humidity" -< x
        wind    <- textAtTag "wind_mph"          -< x
        pres    <- textAtTag "pressure_mb"       -< x
        dew     <- textAtTag "dewpoint_c"        -< x
        vis     <- textAtTag "visibility_mi"     -< x
    returnA -< Weather
       location        = loc,
        observationTime = obsTime,
        summary         = summ,
        windDirection   = windDir,
        temperature     = read temp,
        humidity        = read humi,
        windSpeed       = read wind * 1.61,
        pressure        = read pres,
        dewpoint        = read dew,
        visibility      = read vis * 1.61 

-- GHCi test:
-- Main> retrieveWeatherData >>= \ doc -> runX (parseXML doc >>> getWeather)

main = do
  doc    <- retrieveWeatherData
  xml    <- return $ parseXML doc
  result <- runX (xml >>> getWeather)
  case result of
    []  -> putStrLn "Unable to parse weather data."
    w:_ -> print w

【问题讨论】:

如果您只显示有语法错误的部分而不是整个文件,对我们来说会容易得多。 【参考方案1】:

我猜1你的解析错误在这一行:

proc x -> do

如果是这样,这意味着您还没有告诉友好的邻居 Haskell 编译器您希望它支持箭头语法。您可以通过将其放在文件顶部来做到这一点:

-# LANGUAGE Arrows #-

1但是以后请告诉我们,这样我们就不用去猜测了。

【讨论】:

以上是关于我在输入 `->' 时遇到解析错误的主要内容,如果未能解决你的问题,请参考以下文章

Hive 创建表:解析 json 文件时解析错误缺少“>”

我在尝试恢复节点模块时收到错误“错误意外结束 JSON 输入时解析'...“依赖项”:“tsli'”[重复]

通过 npm 安装 firebase 时遇到错误 |在 [关闭] 附近解析时 JSON 输入意外结束

Icarus Verilog:多位数组解析错误

我在解析 php sdk 时遇到问题

为啥我在 ReSharper 中收到错误“无法解析符号 <symbolname>”?