Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 223 lines (186 sloc) 9.91 kB
dd6624f @scooby Initial commit
authored
1 from lepl import *
786b91a @scooby Working on C++ output
authored
2
dd6624f @scooby Initial commit
authored
3 def build(r):
4 # This is passed to config.lexer before we return the matcher.
5 # A possible identifier
6 ident = Token('[a-zA-Z][a-zA-Z0-9]*')
7 # All caps might be a keyword
8 kw = ident('[A-Z]+')
9 punc = Token(Any('()?!,.'))
10 inttok = Token(Integer())
24127f1 @scooby More work on responder
authored
11 strtok = Token(Literal('"') + ((Literal(':') + Any(')>o":'))
12 | Regexp('[^:"]'))[0:] + Literal('"'))
dd6624f @scooby Initial commit
authored
13 #strtok = Token('"[^"]*"')
14 nltok = Token(Newline())
15 # End of line comments are parsed as a token
16 linecomtok = Token('BTW[ \t]+.*')
17
786b91a @scooby Working on C++ output
authored
18 k = dict()
dd6624f @scooby Initial commit
authored
19 # Define keywords here so I can add misspellings.
786b91a @scooby Working on C++ output
authored
20 for w in ['A', 'ALL', 'AN', 'ANY', 'BAK', 'BIG', 'BIGGR', 'BIGGST',
21 'BOTH', 'BUKKIT', 'BY', 'DIFFRINT', 'DIFF', 'DON', 'DUZ',
b7cc307 @scooby Almost have objects for all constructs...
authored
22 'EITHER', 'EPIC', 'FAIL', 'FIND', 'FISH', 'FOUND', 'FRM', 'GIMMEH',
786b91a @scooby Working on C++ output
authored
23 'GTFO', 'HAI', ['HAS', 'HAZ'], 'HOW', 'KEEP', 'KITTEH',
24 'KTHXBYE', ['I', 'AI'], 'IF', 'IM', 'IN', ['IS', 'IZ'],
25 'LEASTEST', 'LINE', 'LISTIN', ['MA', 'MY'], 'MAEK', 'MEBBE',
1e9c5e7 @scooby Added set operations; may remove later
authored
26 'MINUS', 'MIX', 'MKAY', 'MOD', 'MOSTEST', 'N', 'NAMIN', 'NERFIN', 'NO',
786b91a @scooby Working on C++ output
authored
27 'NOOB', 'NOOBS', 'NOT', 'NOW', 'NUMBAR', 'NUMBARS', ['O', 'OH'],
28 'OIC', 'OMG', 'OMGWTF', 'OUTTA', 'PIK', 'PRODUKT', 'QUOSHUNT',
29 ['R', 'ARR'], 'RLY', 'SAEM', 'SAY', 'SMALLR', 'SMALLST',
30 'SMOOSH', 'SO', 'STEALIN', 'SUM', ['THN', 'THAN'], 'TILL', 'TO',
31 'TROOF', 'TROOFS', 'TRYN', ['U', 'YOUSE'], ['UF', 'OF', 'UV'],
32 'UPPIN', ['VERAH', 'VERY'], 'VISIBLE', 'VISIBLEZ', 'WAI', 'WILE',
1e9c5e7 @scooby Added set operations; may remove later
authored
33 'WIN', 'WIV', 'WON', ['WORD', 'WERD'], 'WTF', 'YA', ['YR', 'UR'], 'YARN',
786b91a @scooby Working on C++ output
authored
34 'YARNS']:
35 if isinstance(w, list):
36 val = Or(*[kw(x) for x in w])
37 for x in w:
38 k[x] = val
39 else:
40 k[w] = kw(w)
41 # And instance namespace...
42 k = type('keywords', (object,), k)
dd6624f @scooby Initial commit
authored
43
44 OPENPAREN = punc('(')
45 CLOSEPAREN = punc(')')
46 BANG = punc('!')
47 QMARK = punc('?')
48 DOT = punc('.')
49 COMMA = punc(',')
50
51 C = ~(linecomtok | COMMA | nltok)[1:]
52
53 with Separator(~linecomtok[0:]):
54 Variable = ident
55 FuncName = ident
56 Label = ident
57
b7cc307 @scooby Almost have objects for all constructs...
authored
58 AtomType = (~k.NOOB >> r.voidT) | (~k.TROOF >> r.boolT) | \
59 (~k.NUMBAR >> r.intT) | (~k.YARN >> r.strT)
60 AtomTypes = (~k.NOOBS >> r.voidT) | (~k.TROOFS >> r.boolT) | \
61 (~k.NUMBARS >> r.intT) | (~k.YARNS >> r.strT)
786b91a @scooby Working on C++ output
authored
62 Bukkity = ((k.VERAH | k.BIG)[0:] & ~k.BUKKIT) > r.bigness
24127f1 @scooby More work on responder
authored
63 ComplexType = ((Bukkity & ~k.UF & AtomTypes > r.setT) |
64 (Bukkity & ~k.LISTIN & AtomTypes > r.listT) |
65 (Bukkity & ~k.NAMIN & AtomTypes > r.mapT))
dd6624f @scooby Initial commit
authored
66 Type = AtomType | ComplexType
67
b7cc307 @scooby Almost have objects for all constructs...
authored
68 LiteralTroof = (~k.FAIL >> r.boolFalse) | (~k.WIN >> r.boolTrue)
69 LiteralNumbar = inttok >> r.intLit
70 LiteralYarn = strtok >> r.strLit
dd6624f @scooby Initial commit
authored
71
72 Expr = Delayed()
73
786b91a @scooby Working on C++ output
authored
74 AnPair = Expr & ~k.AN[0:1] & Expr
75 AnList = Expr & (~k.AN[0:1] & Expr)[0:] & ~k.MKAY
76 ThanPair = Expr & ~k.THN & Expr
77
78 OfPair = ~k.UF & Expr & ~k.AN[0:1] & Expr
79 OfList = ~k.UF & Expr & (~k.AN[0:1] & Expr)[0:] & ~k.MKAY
dd6624f @scooby Initial commit
authored
80
81 Parenthetical = ~OPENPAREN & Expr & ~CLOSEPAREN
786b91a @scooby Working on C++ output
authored
82
b7cc307 @scooby Almost have objects for all constructs...
authored
83 NotEqual = ~k.DIFFRINT & AnPair >> r.notEqual
786b91a @scooby Working on C++ output
authored
84 GreaterThan = ~k.IS & ~k.BIGGR & ThanPair > r.greaterThan
85 LessThan = ~k.IS & ~k.SMALLR & ThanPair > r.lessThan
86
87 BinaryEquality = ~k.BOTH & ~k.SAEM & AnPair > r.equals
88 Disjunction = ((~k.EITHER & OfPair > r.or_)
89 | (~k.ANY & OfList > r.or_))
b7cc307 @scooby Almost have objects for all constructs...
authored
90 ExclDisjunction = ~k.WON & OfPair >> r.xor
786b91a @scooby Working on C++ output
authored
91 Conjunction = ((~k.BOTH & OfPair > r.and_)
92 | (~k.ALL & OfList > r.and_))
b7cc307 @scooby Almost have objects for all constructs...
authored
93 Negation = ~k.NOT & Expr >> r.not_
786b91a @scooby Working on C++ output
authored
94
95 Concatenation = ~k.SMOOSH & AnList > r.concat
96
b7cc307 @scooby Almost have objects for all constructs...
authored
97 SliceFrom = (k.FRM | k.BAK | k.TO | k.BY) & Expr >> r.sliceClause
98 Slicing = (~k.PIK & Expr & SliceClause[0:2]) >> r.slice_
786b91a @scooby Working on C++ output
authored
99
100 Maximum = ((~k.BIGGR & OfPair) | (~k.BIGGST & OfList)) > r.max_
101 Minimum = ((~k.SMALLR & OfPair) | (~k.SMALLST & OfList)) > r.min_
102
103 Addition = ~k.SUM & OfPair > r.add
104 Difference = ~k.DIFF & OfPair > r.diff
105 Product = ~k.PRODUKT & OfPair > r.mult
106 Quotient = ~k.QUOSHUNT & OfPair > r.divide
107 Modulus = ~k.MOD & OfPair > r.modulus
b7cc307 @scooby Almost have objects for all constructs...
authored
108 Negative = ~k.MINUS & ~k.UF[0:1] & Expr >> r.negative
786b91a @scooby Working on C++ output
authored
109
b7cc307 @scooby Almost have objects for all constructs...
authored
110 SmallestKey = ~k.LEASTEST & ~k.UF & Expr >> r.first
111 GreatestKey = ~k.MOSTEST & ~k.UF & Expr >> r.last
112 GetValue = ~k.FISH & ~k.MA & Expr & ~k.OUTTA & Expr >> r.get
786b91a @scooby Working on C++ output
authored
113
b7cc307 @scooby Almost have objects for all constructs...
authored
114 InlineCast = ~k.MAEK & Expr & ~k.A & Type >> r.cast
115 FuncCall = ~k.KITTEH & FuncName & Expr[0:] & ~k.MKAY >> r.call
786b91a @scooby Working on C++ output
authored
116
dd6624f @scooby Initial commit
authored
117 Expr += (Parenthetical | NotEqual | GreaterThan | LessThan |
118 BinaryEquality | Disjunction | ExclDisjunction | Conjunction
119 | Negation | Concatenation | Slicing | Maximum | Minimum |
120 Addition | Difference | Product | Quotient | Modulus |
121 Negative | SmallestKey | GreatestKey | GetValue | InlineCast |
122 FuncCall | LiteralTroof | LiteralNumbar | LiteralYarn)
123
124 Block = Delayed()
125
b7cc307 @scooby Almost have objects for all constructs...
authored
126 ExprStatement = Expr >> r.exprSt
127 Declaration = ~k.I & ~k.HAS & ~k.A[0:1] & Variable & ~k.IS & Type >> r.decl
128 Assignment = Variable & ~k.R & Expr >> r.assign
786b91a @scooby Working on C++ output
authored
129
130 InsertStatement = ~k.MA & Variable & ~k.HAS & ~k.A[0:1] & Expr & \
b7cc307 @scooby Almost have objects for all constructs...
authored
131 (~k.BY & Expr)[0:1] >> r.insertSt
786b91a @scooby Working on C++ output
authored
132 RemoveStatement = ~k.MA & Variable & ~k.DON & ~k.HAS & ~k.A[0:1] & \
b7cc307 @scooby Almost have objects for all constructs...
authored
133 Expr >> r.removeSt
2106f49 @scooby Added more code objects and responder hooks.
authored
134 UnionStatement = ~k.MIX & ~k.MA & Variable & ~k.WIV & Variable \
b7cc307 @scooby Almost have objects for all constructs...
authored
135 >> r.unionSt
2106f49 @scooby Added more code objects and responder hooks.
authored
136 DiffStatement = ~k.MA & Variable & ~k.DON & ~k.HAS & ~k.FROM & \
b7cc307 @scooby Almost have objects for all constructs...
authored
137 Variable >> r.diffSt
2106f49 @scooby Added more code objects and responder hooks.
authored
138 InterStatement = ~k.MA & Variable & ~k.HAS & ~k.SAEM & ~k.FROM & \
b7cc307 @scooby Almost have objects for all constructs...
authored
139 Variable >> r.interSt
786b91a @scooby Working on C++ output
authored
140
b7cc307 @scooby Almost have objects for all constructs...
authored
141 CastStatement = Variable & ~k.IS & ~k.NOW & ~k.A[0:1] & Type >> r.castSt
786b91a @scooby Working on C++ output
authored
142
143 ReadType = k.WORD | k.LINE | k.NUMBAR
144 InputStatement = ~k.GIMMEH & ReadType[0:1] & Variable > r.inputSt
2106f49 @scooby Added more code objects and responder hooks.
authored
145 OutputStatement = (~k.VISIBLE & Expr[1:] > r.outputSt) \
146 | (~k.VISIBLE & Expr[1:] & ~BANG > r.outputLnSt) \
147 | (~k.VISIBLEZ & Expr[1:] > r.outputLnSt)
148 DebugStatement = (~k.INVISIBLE & Expr[1:] > r.debugSt) \
b7cc307 @scooby Almost have objects for all constructs...
authored
149 | (~k.INVISIBLE & Expr[1:] & ~BANG > r.debugLnSt) \
150 | (~k.INVISIBLEZ & Expr[1:] > r.debugLnSt)
786b91a @scooby Working on C++ output
authored
151
24127f1 @scooby More work on responder
authored
152 CondBlock = ((((~k.YA & ~k.RLY) >> r.posCon) |
153 ((~k.MEBBE & Expr) >> r.exprCon)
154 | ((~k.NO & ~k.WAI) >> r.negCon))
155 & ~C & Block) >> r.condBlock
786b91a @scooby Working on C++ output
authored
156 ConditionStatement = (Expr & ~C & ~k.O & ~k.RLY & ~QMARK[0:1] & ~C &
b7cc307 @scooby Almost have objects for all constructs...
authored
157 CondBlock[1:] & ~k.OIC) >> r.condSt
786b91a @scooby Working on C++ output
authored
158
b7cc307 @scooby Almost have objects for all constructs...
authored
159 CaseBlock = (((~k.OMG & Expr) >> r.caseExpr) | ((~k.OMGWTF & ~BANG[0:1])
160 >> r.caseDefault)) & C & Block >> r.caseBlock
786b91a @scooby Working on C++ output
authored
161 CaseStatement = (Expr & ~C & ~k.WTF & ~C & CaseBlock[1:] & ~k.OIC) \
b7cc307 @scooby Almost have objects for all constructs...
authored
162 >> r.caseSt
786b91a @scooby Working on C++ output
authored
163
b7cc307 @scooby Almost have objects for all constructs...
authored
164 TermClause = (~k.TILL & Expr >> r.until) | (~k.WILE & Expr >> r.while_)
165 LoopOpen = ~k.IM & ~k.IN & ~k.YR & Label >> r.loopLabel
166 LoopClose = ~k.IM & ~k.OUTTA & ~k.YR & Label >> r.loopLabel
2106f49 @scooby Added more code objects and responder hooks.
authored
167
b7cc307 @scooby Almost have objects for all constructs...
authored
168 CounterDir = ((~k.UPPIN >> r.ctrInc) | (~k.NERFIN >> r.ctrDec)) \
169 & ~k.YR & Variable >> r.ctrDir
170 CounterHead = CounterDir[0:1] & TermClause[0:1] >> r.ctrHead
24127f1 @scooby More work on responder
authored
171 CounterStatement = LoopOpen & CounterHead & ~C & Block & LoopClose \
172 >> r.ctrSt
2106f49 @scooby Added more code objects and responder hooks.
authored
173
174 IterVars = (~k.STEALIN & Variable & (~k.N & Variable)[0:1]) \
b7cc307 @scooby Almost have objects for all constructs...
authored
175 >> r.iterVars
176 IterHead = IterVars & k.YR & Variable & \
177 TermClause[0:1] >> r.iterHead
178 IterStatement = LoopOpen & IterHead & ~C & Block & LoopClose >> r.iterSt
2106f49 @scooby Added more code objects and responder hooks.
authored
179
b7cc307 @scooby Almost have objects for all constructs...
authored
180 BreakStatement = ~k.GTFO & Label[0:1] >> r.breakSt
181 ContinueStatement = ~k.KEEP & ~k.TRYN & Label[0:1] >> r.continueSt
182 ReturnStatement = ~k.FOUND & ~k.YR & Expr >> r.returnSt
786b91a @scooby Working on C++ output
authored
183
184 Arg = ~k.YR & Type[0:1] & Variable > r.arg
b7cc307 @scooby Almost have objects for all constructs...
authored
185 ArgList = (Arg & (~k.N & Arg)[0:])[0:1] > r.args
186 ReturnClause = ~k.AN & ~k.FIND & ~k.MA & Type >> r.returnType
24127f1 @scooby More work on responder
authored
187 FuncHead = ~k.HOW & ~k.DUZ & ~k.I & FuncName & ArgList & \
188 ReturnClause & ~QMARK[0:1]
786b91a @scooby Working on C++ output
authored
189 FuncClose = ~k.IF & ~k.U & ~k.SAY & ~k.SO
dd6624f @scooby Initial commit
authored
190 FuncDef = FuncHead & ~C & Block & FuncClose > r.funcDef
191
192 SimpleStatement = (Declaration | Assignment | CastStatement |
193 InputStatement | OutputStatement |
194 ConditionStatement | CaseStatement |
2106f49 @scooby Added more code objects and responder hooks.
authored
195 CtrStatement | IterStatement | ExprStatement |
dd6624f @scooby Initial commit
authored
196 InsertStatement | RemoveStatement)
197 ControlStatement = (BreakStatement | ContinueStatement |
198 ReturnStatement)
199
200 Block += ((SimpleStatement | ControlStatement) & ~C)[1:] > r.block
201 TopBlock = ((SimpleStatement | FuncDef) & ~C)[1:] > r.block
24127f1 @scooby More work on responder
authored
202 VersionNumber = (inttok + ~DOT)[0:] + inttok
786b91a @scooby Working on C++ output
authored
203 FileHeader = ~k.OH[0:1] & ~k.HAI & VersionNumber[0:1] & ~C > r.fileHeader
204 FileCloser = ~k.KTHXBYE[0:1]
dd6624f @scooby Initial commit
authored
205
24127f1 @scooby More work on responder
authored
206 matcher = FileHeader & TopBlock & FileCloser >> r.fileAll
dd6624f @scooby Initial commit
authored
207 matcher.config.default()
208 return matcher
209
210 if __name__ == '__main__':
786b91a @scooby Working on C++ output
authored
211 from Responder import TestResponder
212 r = TestResponder()
dd6624f @scooby Initial commit
authored
213 print "Testing parser with all .lol files in pwd"
214 m = build(r).get_parse()
215 import glob
216 import sys
217 for f in glob.glob("*.lol"):
218 print "File: %s" % f
219 with open(f) as fh:
220 s = ''.join(fh)
221 p = m(s)
222 print repr(p)
Something went wrong with that request. Please try again.