diff -r 54b3bc9e3987 -r 20598d7ce9fa compiler/tests/PPCTokenizingCodeGeneratorTest.st --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/compiler/tests/PPCTokenizingCodeGeneratorTest.st Thu Apr 30 23:43:14 2015 +0200 @@ -0,0 +1,143 @@ +"{ 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 +! ! +