Pregunta

Estoy intentando analizar los comentarios al estilo LISP de un lenguaje de expresión S con FPARSEC. Recibí un poco de ayuda para analizar los comentarios de una sola línea en este hilo anterior - Cómo convertir un analizador FPARSEC para analizar el espacio en blanco

Si bien eso se resolvió, todavía necesito analizar los comentarios multiline. Aquí está el código actual -

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

Desafortunadamente, el análisis de multilinecomment nunca tiene éxito. Como este es un combinador, no puedo poner puntos de interrupción ni analizar por qué no funcionará.

¿Alguna idea por qué no funcionará?

¿Fue útil?

Solución

Intente cambiar el argumento de bool para closeMultilineCommentStr a falso

(charsTillString closeMultilineCommentStr false System.Int32.MaxValue)

De lo contrario se saltará el closeMultilineCommentStr cuerda.

Para que funcione con comentarios anidados

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top