質問

FPARSECを使用したS-Expression言語からのLISPスタイルのコメントを解析しようとしています。この前のスレッドでシングルラインのコメントを解析することで少し助けました - fparsecパーサーをホワイトスペースを解析する方法

それが解決されましたが、私はまだマルチラインのコメントを解析する必要があります。これが現在のコードです -

/// Read whitespace character as a string.
let spaceAsStr = anyOf whitespaceChars |>> fun chr -> string chr

/// Read a line comment.
let lineComment = pchar lineCommentChar >>. restOfLine true

/// Read a multiline comment.
/// TODO: make multiline comments nest.
let multilineComment =
    between
        (pstring openMultilineCommentStr)
        (pstring closeMultilineCommentStr)
        (charsTillString closeMultilineCommentStr true System.Int32.MaxValue)

/// Read whitespace text.
let whitespace =
    lineComment <|>
    multilineComment <|>
    spaceAsStr

/// Skip any white space characters.
let skipWhitespace = skipMany whitespace

/// Skip at least one white space character.
let skipWhitespace1 = skipMany1 whitespace

残念ながら、マルチリネコムメントの分割は決して成功しません。これはコンビネーターであるため、ブレークポイントを置いたり、機能しない理由を分析することはできません。

なぜそれがうまくいかないのかという考えはありますか?

役に立ちましたか?

解決

Bool引数を変更してみてください closeMultilineCommentStr 偽り

(charsTillString closeMultilineCommentStr false System.Int32.MaxValue)

それ以外の場合、それはスキップします closeMultilineCommentStr ストリング。

ネストされたコメントで動作させるため

let rec multilineComment o=
    let ign x = charsTillString x false System.Int32.MaxValue
    between
        (pstring openMultilineCommentStr)
        (pstring closeMultilineCommentStr)
        (attempt (ign openMultilineCommentStr >>. multilineComment >>. ign closeMultilineCommentStr) <|> 
        ign closeMultilineCommentStr) <|o
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top