git @ Cat's Eye Technologies Falderal / 34a6cfa
Continue to sketch. Chris Pressey 5 years ago
1 changed file(s) with 41 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
00 import codecs
11 import os
2 from os.path import basename
32 import re
43 from subprocess import Popen, PIPE
54 from tempfile import mkstemp
204203 u'> ': LiterateCode,
205204 }
206205 self.PREFIXES = self.PREFIX_MAP.keys()
206 self.VALID_PATTERNS = [
207 [u'->'],
208 [u'> '],
209 [u'| ', u'= '],
210 [u'| ', u'? '],
211 [u'| ', u'+ ', u'= '],
212 [u'| ', u'+ ', u'? '],
213 [u'+ ', u'= '],
214 [u'+ ', u'? '],
215 ]
207216
208217 def __repr__(self):
209218 filename_repr = '' if self.filename is None else ', filename=%r' % self.filename
281290 ("line %d: " % self.line_num) +
282291 "test body must be followed by expectation or test input")
283292
284 valid_patterns = [
285 [u'->'],
286 [u'> '],
287 [u'| ', u'= '],
288 [u'| ', u'? '],
289 [u'| ', u'+ ', u'= '],
290 [u'| ', u'+ ', u'? '],
291 [u'+ ', u'= '],
292 [u'+ ', u'? '],
293 ]
294 if pattern_prefixes in valid_patterns:
293 if pattern_prefixes in self.VALID_PATTERNS:
295294 return [self.PREFIX_MAP[prefix](line_num=self.line_num, filename=self.filename, lines=lines) for (prefix, lines) in pattern]
296295 raise FalderalSyntaxError(
297296 ("line %d: " % self.line_num) +
318317 ("line %d: " % self.line_num) +
319318 "test body must be followed by expectation or test input")
320319
320 if pattern_prefixes not in self.VALID_PATTERNS:
321 raise FalderalSyntaxError(
322 ("line %d: " % self.line_num) +
323 "incorrectly formatted test block")
324
321325 if pattern_prefixes == [u'->']:
322 # Pragma
323 pass
326 return Pragma(line_num=self.line_num, filename=self.filename, lines=pattern[0][1])
324327 elif pattern_prefixes[-1] in [u'= ', u'? ']:
325 # TODO: valid patterns, several other things
326
328 # TODO: several things
329
327330 if current_functionality is None:
328331 raise FalderalSyntaxError(
329332 ("line %d: " % self.line_num) +
476479 new_blocks.extend(block.split())
477480 self.blocks = new_blocks
478481
479 def parse_lines_to_tests(self):
482 def parse_lines_to_tests(self, functionalities):
480483 r"""Parse the lines of the Document into Tests.
481484
482485 """
526529 last_test_body_block = None
527530 last_used_test_body_block = None
528531 last_test_input_block = None
532 current_functionality = None
529533
530534 tests = []
531535 for block in blocks:
535539 last_desc_block = block
536540 continue
537541
538 test_or_pragma = block.classify()
542 test_or_pragma = block.classify(current_functionality=current_functionality)
539543
540544 if isinstance(test_or_pragma, Test):
541545 tests.append(test_or_pragma)
542546 elif isinstance(test_or_pragma, Pragma):
543 pass
544 # execute the pragma
547 block = test_or_pragma
548 pragma_text = block.text(seperator=' ')
549 match = re.match(r'^\s*Tests\s+for\s+functionality\s*\"(.*?)\"\s*$', pragma_text)
550 if match:
551 functionality_name = match.group(1)
552 current_functionality = functionalities.setdefault(
553 functionality_name,
554 Functionality(functionality_name)
555 )
556 match = re.match(r'^\s*Functionality\s*\"(.*?)\"\s*is\s+implemented\s+by\s+shell\s+command\s*\"(.*?)\"\s*$', pragma_text)
557 if match:
558 functionality_name = match.group(1)
559 command = match.group(2)
560 functionality = functionalities.setdefault(
561 functionality_name,
562 Functionality(functionality_name)
563 )
564 implementation = ShellImplementation(command)
565 functionality.add_implementation(implementation)
545566 else:
546567 raise NotImplementedError('need Pragma or Test')
547568