def setUp(self): self.p = SequenceParser([ LiteralParser(42), LiteralParser(43), LiteralParser(44), LiteralParser(45) ])
def setUp(self): ParserTestCase.setUp(self) integer = LiteralParser("1") class factor: @staticmethod def apply(c): return AlternativeParser([ integer, SequenceParser([ LiteralParser("("), expr, LiteralParser(")") ]) ]).apply(c) term = SequenceParser([ factor, RepeatedParser(SequenceParser([ LiteralParser("*"), factor ])) ]) expr = SequenceParser([ term, RepeatedParser(SequenceParser([ LiteralParser("+"), term ])) ]) self.p = expr
def apply(c): return AlternativeParser([ integer, SequenceParser([ LiteralParser("("), expr, LiteralParser(")") ]) ]).apply(c)
def setUp(self): self.p = AlternativeParser([ SequenceParser([LiteralParser(42), LiteralParser(43)]), SequenceParser([LiteralParser(44), LiteralParser(45)]), SequenceParser([LiteralParser(46), LiteralParser(47)]) ])
def testRepetition(self): self.p = RepeatedParser(LiteralParser(42), match=self.match.object) self.match.expect([42, 42]).andReturn(43) self.expectSuccess([42, 42], 43)
def testOptionalParser(self): self.p = OptionalParser(LiteralParser(42), match=self.match.object, noMatch=43) self.match.expect(42).andReturn(44) self.expectSuccess([42], 44) self.expectSuccess([], 43)
def testSequenceParser(self): self.p = SequenceParser([LiteralParser(42), LiteralParser(43)], match=self.match.object) self.match.expect(42, 43).andReturn(45) self.expectSuccess([42, 43], 45)
def testAlternativeParser(self): self.p = AlternativeParser([LiteralParser(42), LiteralParser(43)], match=self.match.object) self.match.expect(42).andReturn(43) self.expectSuccess([42], 43)
def testLiteralParser(self): self.p = LiteralParser(42, match=43) self.expectSuccess([42], 43)
def setUp(self): self.p = OptionalParser(LiteralParser(42))
def setUp(self): self.p = RepeatedParser(LiteralParser(42))
def setUp(self): self.p = LiteralParser(42)
def apply(cursor): return SequenceParser([ LiteralParser(Tok.StartOption), Internal.definitionsList, LiteralParser(Tok.EndOption) ], lambda s, d, e: builder.makeOptional(d)).apply(cursor)
def apply(cursor): return SequenceParser([ LiteralParser(Tok.StartRepeat), Internal.definitionsList, LiteralParser(Tok.EndRepeat) ], lambda s, d, e: builder.makeRepeated(d)).apply(cursor)
def apply(cursor): return SequenceParser([ LiteralParser(Tok.StartGroup), Internal.definitionsList, LiteralParser(Tok.EndGroup) ], lambda s, d, e: d).apply(cursor)
class Internal: # 4.21 emptySequence = SequenceParser([], lambda: builder.makeSequence([])) # 4.16 terminal = ClassParser(Tok.Terminal, lambda t: builder.makeTerminal(t.value)) # 4.14 metaIdentifier = ClassParser(Tok.MetaIdentifier, lambda name: name.value) nonTerminal = ClassParser( Tok.MetaIdentifier, lambda name: builder.makeNonTerminal(name.value)) # 4.13 class groupedSequence: @staticmethod def apply(cursor): return SequenceParser([ LiteralParser(Tok.StartGroup), Internal.definitionsList, LiteralParser(Tok.EndGroup) ], lambda s, d, e: d).apply(cursor) # 4.12 class repeatedSequence: @staticmethod def apply(cursor): return SequenceParser([ LiteralParser(Tok.StartRepeat), Internal.definitionsList, LiteralParser(Tok.EndRepeat) ], lambda s, d, e: builder.makeRepeated(d)).apply(cursor) # 4.11 class optionalSequence: @staticmethod def apply(cursor): return SequenceParser([ LiteralParser(Tok.StartOption), Internal.definitionsList, LiteralParser(Tok.EndOption) ], lambda s, d, e: builder.makeOptional(d)).apply(cursor) # 4.10 syntacticPrimary = AlternativeParser([ optionalSequence, repeatedSequence, groupedSequence, nonTerminal, terminal, # specialSequence, # @todo Implement emptySequence ]) # 4.9 integer = ClassParser(Tok.Integer, lambda i: i.value) # 4.8 syntacticFactor = AlternativeParser([ SequenceParser( [integer, LiteralParser(Tok.Repetition), syntacticPrimary], lambda i, rep, p: builder.makeRepetition(i, p)), syntacticPrimary ]) # 4.7 syntacticException = syntacticFactor # 4.6 and 4.7 syntacticTerm = AlternativeParser([ SequenceParser([ syntacticFactor, LiteralParser(Tok.Except), syntacticException ], lambda a, e, b: builder.makeRestriction(a, b)), syntacticFactor ]) # 4.5 singleDefinition = SequenceParser( [ syntacticTerm, RepeatedParser( SequenceParser( [LiteralParser(Tok.Concatenate), syntacticTerm], lambda s, d: d)) ], lambda t1, ts: t1 if len(ts) == 0 else builder.makeSequence([t1] + ts)) # 4.4 definitionsList = SequenceParser( [ singleDefinition, RepeatedParser( SequenceParser([ LiteralParser(Tok.DefinitionSeparator), singleDefinition ], lambda s, d: d)) ], lambda d1, ds: d1 if len(ds) == 0 else builder.makeAlternative([d1] + ds)) # 4.3 syntaxRule = SequenceParser([ metaIdentifier, LiteralParser(Tok.Defining), definitionsList, LiteralParser(Tok.Terminator) ], lambda name, defining, value, terminator: builder.makeRule( name, value)) # 4.2 syntax = RepeatedParser(syntaxRule, builder.makeSyntax)