-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammar.hs
83 lines (72 loc) · 2.34 KB
/
Grammar.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module Grammar (html) where
import qualified Data.Map as M
import Combinators
import Control.Applicative (Alternative(..))
import qualified Data.Map as Map
import DOM (DOMTree(..))
import Data.Char (isAsciiLower, isSpace, isAsciiUpper, isDigit)
html :: Parser DOMTree
html = do
(HTMLElement openTagName attributes children) <- tagOpen
children <- many' (textParser <|> html)
(HTMLElement closeTagName _ _) <- tagClose
if openTagName /= tail closeTagName then Parser $ \input -> Left [TagsNotMatched]
else return (HTMLElement openTagName attributes children)
tags:: [String]
tags =
[ "html", "head", "title", "body", "header", "footer", "nav", "main"
, "section", "article", "aside", "h1", "h2", "h3", "h4", "h5", "h6"
, "p", "ul", "ol", "li", "a", "img", "div", "span", "form", "input"
, "button", "label", "textarea", "select", "option", "table", "tr"
, "td", "th", "thead", "tbody", "footer", "script", "style", "link"
, "meta", "canvas", "figure", "figcaption", "audio", "video"
]
tagOpen :: Parser DOMTree
tagOpen = do
_ <- char '<'
tagName <- choice (map string tags)
whitespaceParser
attrs <- attributes
whitespaceParser
_ <- char '>'
return $ HTMLElement tagName attrs []
tagClose :: Parser DOMTree
tagClose = do
_ <- string "</"
tagName <- choice (map string tags)
_ <- char '>'
return $ HTMLElement ("/" ++ tagName) M.empty []
attributes :: Parser (M.Map String String)
attributes = M.fromList <$> many' attribute
attribute :: Parser (String, String)
attribute = attributeSingleQuoted <|> attributeDoubleQuoted
attributeSingleQuoted :: Parser (String, String)
attributeSingleQuoted = do
whitespaceParser
key <- many' (satisfy isAsciiLower)
whitespaceParser
char '='
whitespaceParser
char '\''
value <- many' (satisfy (/= '\''))
char '\''
return (key, value)
attributeDoubleQuoted :: Parser (String, String)
attributeDoubleQuoted = do
whitespaceParser
key <- many' (satisfy isAsciiLower)
whitespaceParser
char '='
whitespaceParser
char '\"'
value <- many' (satisfy (/= '\"'))
char '\"'
return (key, value)
textParser :: Parser DOMTree
textParser = do
txt <- many1 (satisfy (\c -> isAsciiLower c || isAsciiUpper c || isDigit c || isSpace c))
return $ TextNode txt
whitespaceParser :: Parser Token
whitespaceParser = do
ws <- many' (satisfy isSpace)
return $ Whitespace ws