compiler/tests/PPCTokenizingCodeGeneratorTest.st
changeset 438 20598d7ce9fa
child 452 9f4558b3be66
equal deleted inserted replaced
437:54b3bc9e3987 438:20598d7ce9fa
       
     1 "{ Package: 'stx:goodies/petitparser/compiler/tests' }"
       
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
       
     5 PPAbstractParserTest subclass:#PPCTokenizingCodeGeneratorTest
       
     6 	instanceVariableNames:'visitor node result compiler parser context choiceNode tokenizer
       
     7 		arguments'
       
     8 	classVariableNames:''
       
     9 	poolDictionaries:''
       
    10 	category:'PetitCompiler-Tests-Visitors'
       
    11 !
       
    12 
       
    13 !PPCTokenizingCodeGeneratorTest methodsFor:'as yet unclassified'!
       
    14 
       
    15 assert: whatever parse: input
       
    16 	result := super assert: whatever parse: input.
       
    17 !
       
    18 
       
    19 assert: whatever recognizesToken: input
       
    20 	whatever startSymbol: #nextToken.
       
    21 	
       
    22 	self assert: whatever parse: input.
       
    23 	self assert: (result isKindOf: PPToken).
       
    24 	
       
    25 	whatever startSymbol: #start
       
    26 !
       
    27 
       
    28 compileTokenizer: aNode
       
    29 	
       
    30 	tokenizer := visitor visit: aNode	
       
    31 !
       
    32 
       
    33 compileTree: root
       
    34 		
       
    35 	| configuration |
       
    36 
       
    37 	configuration := PPCPluggableConfiguration on: [ :_self | 
       
    38 		result := (visitor visit: _self ir).
       
    39 		compiler compileParser startSymbol: result methodName.
       
    40 		parser := compiler compileParser new.
       
    41 		_self ir: parser
       
    42 	].
       
    43 	parser := configuration compile: root arguments: arguments.
       
    44 	
       
    45 !
       
    46 
       
    47 context	
       
    48 	^ context := PPCProfilingContext new
       
    49 !
       
    50 
       
    51 literalNode: literal
       
    52 	^ PPCLiteralNode new
       
    53 		literal: literal;
       
    54 		yourself
       
    55 !
       
    56 
       
    57 setUp
       
    58 	arguments := PPCArguments default
       
    59 		profile: true;
       
    60 		yourself.	
       
    61 			
       
    62 	compiler := PPCCompiler new.
       
    63 	compiler arguments: arguments.
       
    64 	
       
    65 	visitor := PPCTokenizingCodeGenerator new.
       
    66 	visitor compiler: compiler.
       
    67 !
       
    68 
       
    69 tearDown
       
    70 	| class |
       
    71 
       
    72 	class := (Smalltalk at: #PPGeneratedParser ifAbsent: [nil]).
       
    73 	class notNil ifTrue:[ 
       
    74 		class removeFromSystem
       
    75 	].
       
    76 !
       
    77 
       
    78 testSimpleChoice1
       
    79 	| token1 token2 token1Consume token2Consume  tokenizerNode eof |
       
    80 	
       
    81 	token1 := (self tokenNodeForLiteral: 'foo') markForInline; yourself.
       
    82 	token2 := (self tokenNodeForLiteral: 'bar') markForInline; yourself.
       
    83 	eof := (self tokenNodeForEOF) markForInline; yourself.
       
    84 	
       
    85 	token1Consume := PPCTokenConsumeNode new
       
    86 							child: token1;
       
    87 							yourself.
       
    88 	token2Consume := PPCTokenConsumeNode new
       
    89 							child: token2;
       
    90 							yourself.
       
    91 
       
    92 	choiceNode := PPCLLChoiceNode new
       
    93 		children: { token1Consume . token2Consume };
       
    94 		yourself.
       
    95 		
       
    96 	tokenizerNode := PPCChoiceNode new
       
    97 		children: { token1 . token2 . eof };
       
    98 		name: 'nextToken';
       
    99 		yourself.
       
   100 	
       
   101 	self compileTokenizer: tokenizerNode.
       
   102 	self compileTree: choiceNode.
       
   103 	
       
   104 	self assert: parser recognizesToken: 'foo'.
       
   105 	self assert: parser recognizesToken: 'bar'.
       
   106 	self assert: parser recognizesToken: ''.
       
   107 	
       
   108 	parser := compiler compiledParser new.
       
   109 	self assert: parser parse: 'foo'.
       
   110 	self assert: result inputValue = 'foo'.
       
   111 
       
   112 	parser := compiler compiledParser new.
       
   113 	self assert: parser parse: 'bar'.
       
   114 	self assert: result inputValue = 'bar'.
       
   115 
       
   116 	parser := compiler compiledParser new.
       
   117 	self assert: parser fail: 'baz'.	
       
   118 !
       
   119 
       
   120 tokenNode: child
       
   121 	^ PPCTokenNode new
       
   122 		child: child;
       
   123 		tokenClass: PPToken;
       
   124 		yourself
       
   125 !
       
   126 
       
   127 tokenNodeForEOF
       
   128 	| eof |
       
   129 	eof := PPCEndOfFileNode new
       
   130 		yourself.
       
   131 		
       
   132 	^ PPCTokenNode new
       
   133 		child: eof;
       
   134 		tokenClass: PPToken;
       
   135 		yourself.
       
   136 !
       
   137 
       
   138 tokenNodeForLiteral: literal
       
   139 	| literalNode |
       
   140 	literalNode := self literalNode: literal.
       
   141 	^ self tokenNode: literalNode
       
   142 ! !
       
   143