compiler/tests/PPCTokenizingCodeGeneratorTest.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 30 Apr 2015 23:43:14 +0200
changeset 438 20598d7ce9fa
child 452 9f4558b3be66
permissions -rw-r--r--
Updated to PetitCompiler-JanKurs.100, PetitCompiler-Tests-JanKurs.44 and PetitCompiler-Benchmarks-JanKurs.4 Name: PetitCompiler-JanKurs.100 Author: JanKurs Time: 30-04-2015, 10:48:52.165 AM UUID: 80196870-5921-46d9-ac20-a43bf5c2f3c2 Name: PetitCompiler-Tests-JanKurs.44 Author: JanKurs Time: 30-04-2015, 10:49:22.489 AM UUID: 348c02e8-18ce-48f6-885d-fcff4516a298 Name: PetitCompiler-Benchmarks-JanKurs.4 Author: JanKurs Time: 30-04-2015, 10:58:44.890 AM UUID: 18cadb42-f9ef-45fb-82e9-8469ade56c8b

"{ Package: 'stx:goodies/petitparser/compiler/tests' }"

"{ NameSpace: Smalltalk }"

PPAbstractParserTest subclass:#PPCTokenizingCodeGeneratorTest
	instanceVariableNames:'visitor node result compiler parser context choiceNode tokenizer
		arguments'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Tests-Visitors'
!

!PPCTokenizingCodeGeneratorTest methodsFor:'as yet unclassified'!

assert: whatever parse: input
	result := super assert: whatever parse: input.
!

assert: whatever recognizesToken: input
	whatever startSymbol: #nextToken.
	
	self assert: whatever parse: input.
	self assert: (result isKindOf: PPToken).
	
	whatever startSymbol: #start
!

compileTokenizer: aNode
	
	tokenizer := visitor visit: aNode	
!

compileTree: root
		
	| configuration |

	configuration := PPCPluggableConfiguration on: [ :_self | 
		result := (visitor visit: _self ir).
		compiler compileParser startSymbol: result methodName.
		parser := compiler compileParser new.
		_self ir: parser
	].
	parser := configuration compile: root arguments: arguments.
	
!

context	
	^ context := PPCProfilingContext new
!

literalNode: literal
	^ PPCLiteralNode new
		literal: literal;
		yourself
!

setUp
	arguments := PPCArguments default
		profile: true;
		yourself.	
			
	compiler := PPCCompiler new.
	compiler arguments: arguments.
	
	visitor := PPCTokenizingCodeGenerator new.
	visitor compiler: compiler.
!

tearDown
	| class |

	class := (Smalltalk at: #PPGeneratedParser ifAbsent: [nil]).
	class notNil ifTrue:[ 
		class removeFromSystem
	].
!

testSimpleChoice1
	| token1 token2 token1Consume token2Consume  tokenizerNode eof |
	
	token1 := (self tokenNodeForLiteral: 'foo') markForInline; yourself.
	token2 := (self tokenNodeForLiteral: 'bar') markForInline; yourself.
	eof := (self tokenNodeForEOF) markForInline; yourself.
	
	token1Consume := PPCTokenConsumeNode new
							child: token1;
							yourself.
	token2Consume := PPCTokenConsumeNode new
							child: token2;
							yourself.

	choiceNode := PPCLLChoiceNode new
		children: { token1Consume . token2Consume };
		yourself.
		
	tokenizerNode := PPCChoiceNode new
		children: { token1 . token2 . eof };
		name: 'nextToken';
		yourself.
	
	self compileTokenizer: tokenizerNode.
	self compileTree: choiceNode.
	
	self assert: parser recognizesToken: 'foo'.
	self assert: parser recognizesToken: 'bar'.
	self assert: parser recognizesToken: ''.
	
	parser := compiler compiledParser new.
	self assert: parser parse: 'foo'.
	self assert: result inputValue = 'foo'.

	parser := compiler compiledParser new.
	self assert: parser parse: 'bar'.
	self assert: result inputValue = 'bar'.

	parser := compiler compiledParser new.
	self assert: parser fail: 'baz'.	
!

tokenNode: child
	^ PPCTokenNode new
		child: child;
		tokenClass: PPToken;
		yourself
!

tokenNodeForEOF
	| eof |
	eof := PPCEndOfFileNode new
		yourself.
		
	^ PPCTokenNode new
		child: eof;
		tokenClass: PPToken;
		yourself.
!

tokenNodeForLiteral: literal
	| literalNode |
	literalNode := self literalNode: literal.
	^ self tokenNode: literalNode
! !