"청크"목록으로 구문 분석 할 문자열이 있습니다. 내 문자열이haskell에서 문자열 구문 분석하기
"some text [[anchor]] some more text, [[another anchor]]. An isolated ["
처럼 그리고 나는 다시 내가 함수와 내가 무엇을해야 유형을 작성하는 관리했습니다이
[
TextChunk "some text ",
Anchor "anchor",
TextChunk " some more text, "
Anchor "another anchor",
TextChunk ". An isolated ["
]
뭔가를 얻을 것으로 예상하지만, 지나치게 추한 것 같다. 더 좋은 방법이 있나요?
data Token = TextChunk String | Anchor String deriving (Show)
data TokenizerMode = EatString | EatAnchor deriving (Show)
tokenize::[String] -> [Token]
tokenize xs =
let (_,_,tokens) = tokenize' (EatString, unlines xs, [TextChunk ""])
in reverse tokens
tokenize' :: (TokenizerMode, String, [Token]) -> (TokenizerMode, String,[Token])
-- If we're starting an anchor, add a new anchor and switch modes
tokenize' (EatString, '[':'[':xs, tokens) = tokenize' (EatIdentifier, xs, (Identifier ""):tokens)
-- If we're ending an anchor ass a new text chunk and switch modes
tokenize' (EatAnchor, ']':']':xs, tokens) = tokenize' (EatString, xs, (TextChunk ""):tokens)
-- Otherwise if we've got stuff to consume append it
tokenize' (EatString, x:xs, (TextChunk t):tokens) = tokenize'(EatString, xs, (TextChunk (t++[x])):tokens)
tokenize' (EatAnchor, x:xs, (Identifier t):tokens) = tokenize'(EatAnchor, xs, (Identifier (t++[x])):tokens)
--If we've got nothing more to consume we're done.
tokenize' (EatString, [], tokens) = (EatString, [], tokens)
--We'll only get here if we're given an invalid string
tokenize' xx = error ("Error parsing .. so far " ++ (show xx))
정말 토큰 화가 아니며 구문 분석 중입니다. 파싱 요구 사항에 따라, 파섹. –
@CatPlusPlus는 구문 분석에 일치하는 텍스트와 제목을 업데이트하는 것에 동의했습니다. –
@CatPlusPlus parsec을 사용하여 이것이 어떻게 보이는지 보여 줄 수 있습니까? 나는 내가 좋아하는 것에 대해 약간은 모호한 문서/tutes를 찾는다. –