-
Notifications
You must be signed in to change notification settings - Fork 2
/
grammar.py
70 lines (52 loc) · 2.01 KB
/
grammar.py
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
from pyparsing import Suppress, Literal, Word, ZeroOrMore, OneOrMore, \
Optional, stringEnd, alphas, Forward, Empty, Group, \
quotedString
from regl import conf
from regl.aux import chop
indentTok, dedentTok, lineEndTok, hspaceTok, superTok, parTok = \
list(map(Suppress, [conf.indentTok, conf.dedentTok,
conf.lineEndTok, conf.hspaceTok,
conf.superTok, conf.parTok]))
escapedChar = (Literal("<") + Word(alphas + "-") + Literal(">"))\
.setParseAction(lambda t: conf.charMapI[''.join(t)])
def wordParseAction(t):
word = ''.join(t)
return escapedChar.transformString(word)
return word
def specWord(chrs):
return Word(chrs).setParseAction(wordParseAction)
def specWords(chrs):
return OneOrMore(specWord(chrs))\
.setParseAction(lambda t: ' '.join(t))
words = specWords(conf.wordChars)
line = words + lineEndTok
lines = OneOrMore(line).setParseAction(lambda t: ' '.join(t))
class Item:
def __init__(self, name, content):
self.name = name
self.content = content
def __repr__(self):
return "Item(%s,%s)" % (repr(self.name), repr(self.content))
def createItem(head, body, tail):
return (head + body + tail)\
.setParseAction(lambda t: Item(t[0],t[1]))
def createBlock(itemHead, itemBody, itemTail):
return OneOrMore(createItem(itemHead, itemBody, itemTail))\
.setParseAction(lambda t: tuple(t[:]))
indent = indentTok + Suppress(quotedString)
descriptionBlock = Forward()
body = descriptionBlock | lines
descriptionBlock << createBlock(line + indent, body, dedentTok)
itemBlock = createBlock(words + hspaceTok, body, Empty())
body = itemBlock | body
parBlock = createBlock(words + parTok + lineEndTok, body, Empty())
body = parBlock | body
superParBlock = createBlock(superTok + words + superTok + lineEndTok,
body, Empty())
body = superParBlock | body
sectBlock = createBlock(indent + line + dedentTok, body, Empty())
body = sectBlock | body
superSectBlock = createBlock(indent + superTok + words
+ superTok + lineEndTok + dedentTok, body, Empty())
body = superSectBlock | body
regl = body + stringEnd