Question

J'essaye d'analyser les commentaires de style lisp à partir d'un langage d'expression s avec FParsec.J'ai obtenu un peu d'aide pour analyser les commentaires sur une seule ligne dans ce fil précédent - Comment convertirun analyseur FParsec pour analyser les espaces

Bien que cela ait été résolu, j'ai encore besoin d'analyser les commentaires multilignes.Voici le code actuel -

/// 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

Malheureusement, l'analyse multilineComment ne réussit jamais.Puisqu'il s'agit d'un combinateur, je ne peux pas mettre de points d'arrêt ou analyser pourquoi cela ne fonctionnera pas.

Avez-vous une idée des raisons pour lesquelles cela ne fonctionnera pas?

Était-ce utile?

La solution

Essayez de changer l'argument booléen de closeMultilineCommentStr sur false

(charsTillString closeMultilineCommentStr false System.Int32.MaxValue)

Sinon, il sautera la chaîne closeMultilineCommentStr.

Pour le faire fonctionner avec des commentaires imbriqués

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top