我試圖使用解析器組合器來解決問題。我試過如下:函數式編程:瞭解解析器組合器
注意:下面的代碼使用combinator library
styleParserItalic : Bool -> Parser (List (List Char , Style))
styleParserItalic bolded =
let
style = if bolded then Italic else Unstyled
in
(end `andThen` always ( succeed ([])))
<|> (string "(!ITALIC!)" `andThen` \_ -> styleParserItalic (not bolded) )
<|> (anyChar `andThen` \c -> styleParserItalic bolded `andThen` \cs -> succeed ((c :: [],style) :: cs))
我在努力理解它是如何運行的解析器,因爲解析器成功之前styleParserItalic
解析器被調用。
有人能解釋解析器如何給它一個字符串時的工作方式嗎?
如果有人對分析器的目的和完整代碼感興趣,here是我以前的問題。
這是我就此瞭解遠遠
解析器會首先檢查它是否是一個行的末尾,如果沒有它會嘗試解析器字符串(!ITALIC!)
如果這樣,那麼它會調用解析器的參數爲True或False(如果爲false,則會使其爲真)..
如果解析器未找到字符串(!ITALIC!),它將嘗試解析任何字符,然後它會調用解析器再次。
讓我感到困惑的是解析器只要解析任何字符,就會繼續調用自己!
編輯:*注意以下不是問題的一部分只是爲了共享代碼,如果有人有興趣
感謝所有響應,我已經更新瞭解析器解析粗體斜體下劃線...,按下面的屏幕截圖
type Style = Bold| Unstyled | Italic | Coded | Lined | Titled | Marked | Underline
styleParser : Bool ->Bool ->Bool ->Bool-> Bool-> Bool->Bool
-> Parser (List (List Char , (Style,Style,Style,Style,Style,Style,Style)))
--(bold,italic ,code,line ,Titled,mark)
styleParser bolded italiced coded lined titled marked underlined=
let
style = (
if bolded then Bold else Unstyled
,if italiced then Italic else Unstyled
,if coded then Coded else Unstyled
,if lined then Lined else Unstyled
,if titled then Titled else Unstyled
,if marked then Marked else Unstyled
,if underlined then Underline else Unstyled
)
in
(end `andThen` always (succeed ([])))
<|> (string "//" `andThen` \_ -> styleParser bolded italiced coded lined titled marked (not underlined))
<|> (string "**" `andThen` \_ -> styleParser (not bolded) italiced coded lined titled marked underlined)
<|> (string "*" `andThen` \_ -> styleParser bolded (not italiced) coded lined titled marked underlined)
<|> (string "`" `andThen` \_ -> styleParser bolded italiced (not coded) lined titled marked underlined)
<|> (string "/br" `andThen` \_ -> styleParser bolded italiced coded (not lined) titled marked underlined)
<|> (string "/*" `andThen` \_ -> styleParser bolded italiced coded lined (not titled) marked underlined)
<|> (string "{-" `andThen` \_ -> styleParser bolded italiced coded lined titled (not marked) underlined)
<|> (anyChar `andThen` \c -> styleParser bolded italiced coded lined titled marked underlined `andThen` \cs -> succeed ((c :: [],style) :: cs))
foldStyleHtml : List (List Char , ( Style,Style,Style,Style,Style,Style,Style)) -> List (Html Msg)
foldStyleHtml lst =
List.map styleToHtml lst
styleToHtml : (List Char, (Style ,Style,Style,Style,Style,Style,Style)) -> Html Msg
styleToHtml (a,b) =
case b of
(Bold,Italic,_,_,_,_,Unstyled) -> strong [] [em [][ text (String.fromList a)]]
(Bold,Italic,_,_,_,_,Underline) -> u[][ strong [] [em [][ text (String.fromList a)]]]
(Bold,Unstyled,_,_,_,_,Underline) -> u[][ strong [] [text (String.fromList a)]]
(Unstyled,Italic,_,_,_,_,Underline) -> u[][ em [] [text (String.fromList a)]]
(Unstyled,Italic,_,_,_,_,_) -> em[] [text (String.fromList a)]
(Bold,Unstyled,_,_,_,_,_) -> strong [][ text (String.fromList a)]
(_,_,Coded,_,_,_,_) -> code [codeStyle ][text (String.fromList a)]
(_,_,_,Lined,_,_,_) -> br [][text " "]
-- (_,_,_,_,Titled,_,_) -> div [][text (String.fromList a)]
(_,_,_,_,_,Marked,_) -> mark [][text (String.fromList a)]
(_,_,_,_,_,_,Underline) -> u [][text (String.fromList a)]
(_,_,_,_,_,_,_) -> text (String.fromList a)
htmlParser : Parser (List (Html Msg))
htmlParser =
styleParser False False False False False False False `andThen` (succeed << foldStyleHtml)
runParser : Parser (List (Html Msg)) -> String -> Html Msg
runParser parser str =
case parse parser str of
(Ok htmls,_)-> div [] htmls
(Err err, _) -> div [ style [("color", "red")] ] [ text <| toString <| err]
我會虛心提[印跡後我幾年前寫的(https://oldfashionedsoftware.com/2008/08/16/easy-parsing-in-scala /)作爲分析器組合器的介紹。 –