diff -r 20598d7ce9fa -r 9f4558b3be66 compiler/tests/PPCLL1Test.st --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/compiler/tests/PPCLL1Test.st Sun May 10 06:28:36 2015 +0100 @@ -0,0 +1,251 @@ +"{ Package: 'stx:goodies/petitparser/compiler/tests' }" + +"{ NameSpace: Smalltalk }" + +PPAbstractParserTest subclass:#PPCLL1Test + instanceVariableNames:'parser result context node compiler id node2 id2 id1 node1 node3 + arguments configuration' + classVariableNames:'' + poolDictionaries:'' + category:'PetitCompiler-Tests-Core' +! + +!PPCLL1Test methodsFor:'as yet unclassified'! + +assert: p parse: whatever + ^ result := super assert: p parse: whatever. +! + +cleanClass + | parserClass | + parserClass := (Smalltalk at: arguments name ifAbsent: [nil]). + parserClass notNil ifTrue:[ + self flag: 'uncomment'. +" parserClass removeFromSystem" + ]. +! + +context + ^ context := PPCProfilingContext new +! + +parse: whatever + ^ result := super parse: whatever. +! + +setUp + arguments := PPCArguments default + profile: true; + guards: false; + yourself. + + configuration := PPCLL1Configuration new + arguments: arguments; + yourself. + + self cleanClass. +! + +tearDown + self cleanClass +! + +testChoiceOrder + parser := ( + 'a' asParser token, 'b' asParser token / + 'a' asParser token) + compileWithConfiguration: configuration. + + self assert: parser parse: 'ab'. + self assert: result first inputValue = 'a'. + self assert: result second inputValue = 'b'. + + self assert: parser parse: 'a'. + self assert: result inputValue = 'a'. + + self assert: parser fail: '_'. + +! + +testChoiceOrder2 + | p1 p2 | + p1 := 'a' asParser token, 'b' asParser token. + p2 := 'b' asParser token / 'a' asParser token. + + parser := p1 / p2 compileWithConfiguration: configuration. + + self assert: parser parse: 'ab'. + self assert: result first inputValue = 'a'. + self assert: result second inputValue = 'b'. + + self assert: parser parse: 'a'. + self assert: result inputValue = 'a'. + + self assert: parser parse: 'b'. + self assert: result inputValue = 'b'. + + self assert: parser fail: 'c'. + +! + +testChoiceOrder3 + | p1 p2 a1 a2 | + a1 := 'a' asParser token name: 't1'; yourself. + a2 := 'a' asParser token name: 't2'; yourself. + + p1 := a1, 'b' asParser token. + p2 := a2. + + parser := p1 / p2 compileWithConfiguration: configuration. + + self assert: parser parse: 'ab'. + self assert: result first inputValue = 'a'. + self assert: result second inputValue = 'b'. + + self assert: parser parse: 'a'. + self assert: result inputValue = 'a'. + + self assert: parser fail: 'b'. + +! + +testChoiceOrder4 + | p1 p2 a1 a2 | + a1 := 'a' asParser token name: 't1'; yourself. + a2 := 'a' asParser token name: 't2'; yourself. + + p1 := a1, 'b' asParser token. + p2 := 'b' asParser token / a2. + + parser := p1 / p2 compileWithConfiguration: configuration. + + self assert: parser parse: 'ab'. + self assert: result first inputValue = 'a'. + self assert: result second inputValue = 'b'. + + self assert: parser parse: 'a'. + self assert: result inputValue = 'a'. + + self assert: parser parse: 'b'. + self assert: result inputValue = 'b'. + + self assert: parser fail: 'c'. + +! + +testCompileChoice + parser := ('foo' asParser / 'bar' asParser) compileWithConfiguration: configuration. + + self assert: parser parse: 'foo' to: 'foo'. + self assert: parser parse: 'bar' to: 'bar'. + self assert: parser fail: '_'. + +! + +testCompileChoice2 + parser := ('foo' asParser token trim / 'bar' asParser token trim) compileWithConfiguration: configuration. + + self assert: parser parse: 'foo'. + self assert: result inputValue = 'foo'. + self assert: parser parse: 'bar'. + self assert: result inputValue = 'bar'. + self assert: parser fail: '_'. + +! + +testCompileLiteral + parser := 'foo' asParser token compileWithConfiguration: configuration. + + self assert: parser parse: 'foo'. + self assert: result inputValue = 'foo'. + self assert: parser fail: 'boo'. +! + +testCompileSequence + parser := ('foo' asParser token), ('bar' asParser token) + compileWithConfiguration: configuration. + + self assert: parser parse: 'foobar'. + self assert: result first inputValue = 'foo'. + self assert: result second inputValue = 'bar'. +! + +testCompileTokenComplex2 + | a b argumentsWith | + "based on the PPSmalltlakGrammar>>blockArgumentsWith" + a := $| asParser smalltalkToken + yourself. + b := $] asParser smalltalkToken + yourself. + argumentsWith := (a / b and ==> [:t | ]) wrapped + name: 'argumentsWith'; + yourself. + + parser := argumentsWith compileWithConfiguration: configuration. + self assert: parser parse: '|'. + + parser := argumentsWith compileWithConfiguration: configuration. + self assert: parser parse: ']'. +! + +testCompileTokenComplex3 + | choice1 choice2 a1 b1 a2 b2 tricky | + a1 := $| asParser token + yourself. + b1 := $] asParser token + yourself. + choice1 := (a1 / b1) wrapped + name: 'choice1'; + yourself. + + a2 := $| asParser token + yourself. + b2 := $] asParser token + yourself. + choice2 := (a2 / b2) wrapped + name: 'choice1'; + yourself. + + tricky := (a1 asParser, choice1) / (b2 asParser, choice2). + + parser := tricky compileWithConfiguration: configuration. + self assert: parser parse: '||'. + + parser := tricky compileWithConfiguration: configuration. + self assert: parser parse: '|]'. + + parser := tricky compileWithConfiguration: configuration. + self assert: parser parse: ']|'. + + parser := tricky compileWithConfiguration: configuration. + self assert: parser parse: ']]'. +! + +testCompileTrim + parser := 'foo' asParser token trim end compileWithConfiguration: configuration. + + self assert: parser parse: 'foo'. + self assert: result inputValue = 'foo'. + + self assert: parser parse: 'foo '. + self assert: result inputValue = 'foo'. + + + self assert: parser parse: ' foo'. + self assert: result inputValue = 'foo'. + + self assert: parser fail: 'boo'. +! + +testTokenName + | token | + token := 'foo' asParser token name: 'fooToken'; yourself. + parser := token plus + compileWithConfiguration: configuration. + + self assert: parser parse: 'foofoo'. + self assert: result first inputValue = 'foo'. + self assert: result second inputValue = 'foo'. + self assert: (parser class methodDictionary includesKey: #fooToken). +! ! +