Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用Parsec3解析文本输入并获取文本输出(而不是字符串)_String_Haskell_Text_Parsec - Fatal编程技术网

String 使用Parsec3解析文本输入并获取文本输出(而不是字符串)

String 使用Parsec3解析文本输入并获取文本输出(而不是字符串),string,haskell,text,parsec,String,Haskell,Text,Parsec,我看到Parsec3处理文本(而不是字符串)输入,所以我想转换一个旧的字符串解析器来获得文本输出。我正在使用的其他库也使用文本,这样可以减少所需的转换次数 现在,parsec3库似乎做到了它所说的(处理Text和String输入),这个例子来自gchi: Text.Parsec.Text Text.Parsec Data.Text> parseTest (many1 $ char 's') (pack "sss") "sss" Text.Parsec.Text Text.Parsec D

我看到Parsec3处理
文本
(而不是
字符串
)输入,所以我想转换一个旧的
字符串
解析器来获得文本输出。我正在使用的其他库也使用
文本
,这样可以减少所需的转换次数

现在,
parsec3
库似乎做到了它所说的(处理
Text
String
输入),这个例子来自gchi:

Text.Parsec.Text Text.Parsec Data.Text> parseTest (many1 $  char 's') (pack "sss")
"sss"
Text.Parsec.Text Text.Parsec Data.Text> parseTest (many1 $  char 's') "sss"
"sss"
因此,
Text
(第一种情况)和
String
(第二种情况)都有效

现在,在我的真实的、转换的解析器中(很抱歉,我必须在这里拼凑一些代码的远程部分来制作一个完整的示例)

如果我没有显式地
pack
捕获上面代码示例中的文本,那么:

UmeQueryParser.hs:288:26:
    Couldn't match expected type ‘Text’ with actual type ‘[Char]’
    In the first argument of ‘ midOfQuery’, namely ‘(plabeltype)’
    In the second argument of ‘($)’, namely
      ‘StartQuery (plabeltype) (clabeltype) (funcMT sep)’ 
因此,我似乎需要在输出中将捕获的字符串显式地转换为
Text


那么,当我要进行
Text->Text
解析时,为什么我需要经历一个从
String
Char
Text
的转换步骤呢?

你只需要做你自己的
Text
解析器,就像

midOfQuery :: Parser UmeQueryPart
midOfQuery = do
    spaces
    lexeme $ string "MidOf"
    lexeme $ char '('
    clabeltype <- lexeme alphaNums
    sep <- lexeme $ try (char ',') <|> char '~'
    plabeltype <- lexeme alphaNums
    lexeme $ char ')'
    return $ MidQuery plabeltype clabeltype (funcMT sep)
  where
    alphaNums = pack <$> many1 alphaNum
    lexeme p = p <* spaces
midOfQuery::解析器UmeQueryPart
midOfQuery=do
空间
词素$string“MidOf”
词素$char'('

clabeltype`在检查'lexeme'是否具有推断类型lexeme::forall a s u(m::*->*)时,约束中的非类型变量参数:Stream s m Char(使用FlexibleContexts允许此操作).Stream s m Char=>ParsecT s u m a->ParsecT s u m a`我无法在本地复制它;但是,您可以打开
FlexibleContexts
,或者给它一个类型签名
lexeme::Parser a->Parser a
。谢谢!它现在似乎可以工作了。谢谢您帮助我看到解析器可能有点太大了比我的例子更紧凑。
UmeQueryParser.hs:288:26:
    Couldn't match expected type ‘Text’ with actual type ‘[Char]’
    In the first argument of ‘ midOfQuery’, namely ‘(plabeltype)’
    In the second argument of ‘($)’, namely
      ‘StartQuery (plabeltype) (clabeltype) (funcMT sep)’ 
midOfQuery :: Parser UmeQueryPart
midOfQuery = do
    spaces
    lexeme $ string "MidOf"
    lexeme $ char '('
    clabeltype <- lexeme alphaNums
    sep <- lexeme $ try (char ',') <|> char '~'
    plabeltype <- lexeme alphaNums
    lexeme $ char ')'
    return $ MidQuery plabeltype clabeltype (funcMT sep)
  where
    alphaNums = pack <$> many1 alphaNum
    lexeme p = p <* spaces
midOfQuery :: Parser UmeQueryPart
midOfQuery = spaces *> lexeme (string "MidOf") *> parens (toQuery <$> lexeme alphaNums <*> lexeme matchType <*> lexeme alphaNums)
  where
    lexeme :: Parser a -> Parser a
    lexeme p = p <* spaces

    alphaNums = pack <$> many1 alphaNum

    parens = between (lexeme $ char '(') (lexeme $ char ')')

    matchType = Fuzzy <$ char '~' <|>
                Strict <$ char ','

    toQuery cLabelType sep pLabelType = MidQuery pLabelType cLabelType sep