Domanda

Sto implementando un parser che tratta i commenti come spazio bianco con fparsec. Sembra che richieda una banale conversione del parser, ma non so ancora come implementarlo.

Ecco il codice che sto cercando di arrivare al controllo del tipo -

let whitespaceTextChars = " \t\r\n"

/// Read whitespace characters.
let whitespaceText = many (anyOf whitespaceTextChars)

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

/// Skip any white space characters.
let skipWhitespace = skipMany (lineComment <|> whitespaceText)

/// Skip at least one white space character.
let skipWhitespace1 = skipMany1 (lineComment <|> whitespaceText)

L'errore è sul secondo argomento di entrambi i <|> Operatori (over whitespaceText). Gli errori sono -

Error   1   Type mismatch. Expecting a     Parser<string,'a>     but given a     Parser<char list,'a>     The type 'string' does not match the type 'char list'
Error   2   Type mismatch. Expecting a     Parser<string,'a>     but given a     Parser<char list,'a>     The type 'string' does not match the type 'char list'

Sembra che ho bisogno di convertire a Parser<char list, 'a> a a Parser<string, 'a>. Oppure, dal momento che li sto solo saltando, potrei convertirli entrambi a Parser<unit, 'a>. Tuttavia, non so come scrivere quel codice. È una semplice espressione di lambda?

Saluti!

È stato utile?

Soluzione

let whitespaceText = manyChars (anyOf whitespaceTextChars)

o

let whitespaceText = many (anyOf whitespaceTextChars) |>> fun cs -> System.String (Array.ofList cs)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top