Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

My new text markup langage

Name: Anonymous 2009-10-01 12:11

It's better than BBCode:
`b`i`o`u{I AM AN EXPERT PROGRAMMER}
`sup{NO `sup{MORE `sup{NESTING `sub{PROBLEMS`b`i{!!}}}}}


Also, the parser in Haskell is 30 lines.

Name: Anonymous 2009-10-03 10:39

>>26

module Parser where

import Text.ParserCombinators.Parsec
import Data.Maybe (fromJust)

delimiters = [('{','}'), ('(',')'), ('<','>'), ('[',']'), ('^','$')]

data Entity = Text String
            | Tag String Entity
            | Collection [Entity]
             deriving (Show, Eq)

Collection xs `addChild` e = Collection $ xs ++ [e]

character resvd = ((char '\\' >> anyChar) <?> "escaped char") <|> noneOf ('`' : resvd)
tagChar = ((char '\\' >> anyChar) <?> "escaped char") <|> noneOf ('`' : map fst delimiters)

nested = do char '`'
            tag <- many1 tagChar
            value <- nested <|> block
            return $ Tag tag value
block = do opening <- oneOf $ map fst delimiters
           let closing = fromJust $ lookup opening delimiters
           source (Collection []) (char closing) [closing]
text resvd = return . Text =<< many1 (character resvd)

source :: Entity -> Parser a -> [Char] -> Parser Entity
source coll fin resvd = (<|>)
    (fin >> return coll)
    (do e <- (nested <?> "nested tag") <|> (text resvd <?> "text")
        source (coll `addChild` e) fin resvd)

full = source (Collection []) eof []

toBBCode (Collection xs) = concatMap toBBCode xs
toBBCode (Tag t v) = "[" ++ t ++ "]" ++ toBBCode v ++ "[/" ++ t ++ "]"
toBBCode (Text t) = t

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List