我正在写一个SVG解析器,主要作为学习如何使用Parsec的练习。目前我使用下面的数据类型来表示我的SVG文件:SVG解析和数据类型
data SVG = Element String [Attribute] [SVG]
| SelfClosingTag [Attribute]
| Body String
| Comment String
| XMLDecl String
这工作得很好,但我不知道我的数据类型的Element String [Attribute] [SVG]
一部分。 由于SVG中潜在的tags数量有限,我正在考虑使用类型来表示SVG元素,而不是使用String。类似这样的:
data SVG = Element TagName [Attribute] [SVG]
| ...
data TagName = A
| AltGlyph
| AltGlyphDef
...
| View
| Vkern
这是一个好主意吗?如果有这样做会有什么好处? 有没有更优雅的解决方案?