gpt4 book ai didi

parsing - 使用 FParsec,如何在解析器之间使用 manyCharsTill 而不会在结束字符串上失败?

转载 作者:行者123 更新时间:2023-12-05 09:15:48 25 4
gpt4 key购买 nike

我正在尝试使用 FParsec 来解析 TOML 多行字符串,但我在使用结束分隔符 (""") 时遇到了问题。我有以下解析器:

let controlChars = 
['\u0000'; '\u0001'; '\u0002'; '\u0003'; '\u0004'; '\u0005'; '\u0006'; '\u0007';
'\u0008'; '\u0009'; '\u000a'; '\u000b'; '\u000c'; '\u000d'; '\u000e'; '\u000f';
'\u0010'; '\u0011'; '\u0012'; '\u0013'; '\u0014'; '\u0015'; '\u0016'; '\u0017';
'\u0018'; '\u0019'; '\u001a'; '\u001b'; '\u001c'; '\u001d'; '\u001e'; '\u001f';
'\u007f']

let nonSpaceCtrlChars =
Set.difference (Set.ofList controlChars) (Set.ofList ['\n';'\r';'\t'])

let multiLineStringContents : Parser<char,unit> =
satisfy (isNoneOf nonSpaceCtrlChars)

let multiLineString : Parser<string,unit> =
optional newline >>. manyCharsTill multiLineStringContents (pstring "\"\"\"")
|> between (pstring "\"\"\"") (pstring "\"\"\"")

let test parser str =
match run parser str with
| Success (s1, s2, s3) -> printfn "Ok: %A %A %A" s1 s2 s3
| Failure (f1, f2, f3) -> printfn "Fail: %A %A %A" f1 f2 f3

当我针对这样的输入测试 multiLineString 时:

test multiLineString "\"\"\"x\"\"\""

解析器因以下错误而失败:

Fail: "Error in Ln: 1 Col: 8 """x""" ^ Note: The error occurred at the end of the input stream. Expecting: '"""'

我对此感到困惑。 manyCharsTill multiLineStringContents (pstring "\"\"\"") 解析器是否会在 """ 处停止,以便 between 解析器找到它?为什么解析器吃掉所有输入然后使 between 解析器失败?

这似乎是一个相关的帖子:How to parse comments with FParsec

但我真的看不出那个问题的解决方案与我在这里所做的有何不同。

最佳答案

manyCharsTill documentation 说(强调我的):

manyCharsTill cp endp parses chars with the char parser cp until the parser endp succeeds. It stops after endp and returns the parsed chars as a string.

因此,您不想将 betweenmanyCharsTill 结合使用;你想做类似 pstring "\"\"\"">>.manyCharsTill (pstring "\"\"\"") 的事情。

但碰巧,我可以为你省去很多工作。在业余时间,我一直在使用 FParsec 开发 TOML 解析器。它远未完成,但字符串部分可以正常工作并正确处理反斜杠转义(据我所知:我已经彻底测试但并非详尽无遗)。我唯一缺少的是“如果它出现在开始定界符之后,则删除第一个换行符”规则,您已经使用 optional newline 处理了它。因此,只需将该位添加到我下面的代码中,您就应该拥有一个有效的 TOML 字符串解析器。

顺便说一句,我计划在 MIT 许可下许可我的代码(如果我完成了它)。所以我特此在 MIT 许可证下发布以下代码块。如果对您有用,欢迎在您的项目中使用它。

let pShortCodepointInHex = // Anything from 0000 to FFFF, *except* the range D800-DFFF
(anyOf "dD" >>. (anyOf "01234567" <?> "a Unicode scalar value (range D800-DFFF not allowed)") .>>. exactly 2 isHex |>> fun (c,s) -> sprintf "d%c%s" c s)
<|> (exactly 4 isHex <?> "a Unicode scalar value")

let pLongCodepointInHex = // Anything from 00000000 to 0010FFFF, *except* the range D800-DFFF
(pstring "0000" >>. pShortCodepointInHex)
<|> (pstring "000" >>. exactly 5 isHex)
<|> (pstring "0010" >>. exactly 4 isHex |>> fun s -> "0010" + s)
<?> "a Unicode scalar value (i.e., in range 00000000 to 0010FFFF)"

let toCharOrSurrogatePair p =
p |> withSkippedString (fun codePoint _ -> System.Int32.Parse(codePoint, System.Globalization.NumberStyles.HexNumber) |> System.Char.ConvertFromUtf32)

let pStandardBackslashEscape =
anyOf "\\\"bfnrt"
|>> function
| 'b' -> "\b" // U+0008 BACKSPACE
| 'f' -> "\u000c" // U+000C FORM FEED
| 'n' -> "\n" // U+000A LINE FEED
| 'r' -> "\r" // U+000D CARRIAGE RETURN
| 't' -> "\t" // U+0009 CHARACTER TABULATION a.k.a. Tab or Horizonal Tab
| c -> string c

let pUnicodeEscape = (pchar 'u' >>. (pShortCodepointInHex |> toCharOrSurrogatePair))
<|> (pchar 'U' >>. ( pLongCodepointInHex |> toCharOrSurrogatePair))

let pEscapedChar = pstring "\\" >>. (pStandardBackslashEscape <|> pUnicodeEscape)

let quote = pchar '"'
let isBasicStrChar c = c <> '\\' && c <> '"' && c > '\u001f' && c <> '\u007f'
let pBasicStrChars = manySatisfy isBasicStrChar
let pBasicStr = stringsSepBy pBasicStrChars pEscapedChar |> between quote quote

let pEscapedNewline = skipChar '\\' .>> skipNewline .>> spaces
let isMultilineStrChar c = c = '\n' || isBasicStrChar c
let pMultilineStrChars = manySatisfy isMultilineStrChar


let pTripleQuote = pstring "\"\"\""

let pMultilineStr = stringsSepBy pMultilineStrChars (pEscapedChar <|> (notFollowedByString "\"\"\"" >>. pstring "\"")) |> between pTripleQuote pTripleQuote

关于parsing - 使用 FParsec,如何在解析器之间使用 manyCharsTill 而不会在结束字符串上失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51777578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com