次のように、ファイルからの入力データをLogLineデータ型に解析するHaksellParsecParserを作成しようとしています。
--Final parser that holds the indvidual parsers.
final :: Parser [LogLine]
final = do{ logLines <- sepBy1 logLine eol
; return logLines
}
--The logline token declaration
logLine :: Parser LogLine
logLine = do
name <- plainValue -- parse the name (identifier)
many1 space -- parse and throw away a space
args1 <- bracketedValue -- parse the first arguments
many1 space -- throw away the second sapce
args2 <- bracketedValue -- parse the second list of arguments
many1 space --
constant <- plainValue -- parse the constant identifier
space
weighting <- plainValue --parse the weighting double
space
return $ LogLine name args1 args2 constant weighting
すべてが正常に解析されますが、ファイルにコメントを追加する必要があり、パーサーがそれらを無視するように変更する必要があります。「-」で始まり、「\n」で終わる単一行のコメントをサポートする必要があります。コメントトークンを次のように定義しようとしました。
comments :: Parser String
comments = do
string "--"
comment <- (manyTill anyChar newline)
return ""
そして、次のfinal
ようにパーサーに接続します。
final :: Parser [LogLine]
final = do
optional comments
logLines <- sepBy1 logLine (comments<|>newline)
optional comments
return logLines
正常にコンパイルされますが、解析されません。私はいくつかの小さな変更を試みましたが、最良の結果は最初のコメントまですべてを解析することでした。したがって、これはそれを行う方法ではないと思い始めています。PS:私はこの同様の質問を見ましたが、それは私が達成しようとしているものとは少し異なります。