tests/PPScriptingTest.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 05 Oct 2014 00:05:20 +0100
changeset 380 8fe3cb4e607f
parent 379 451b5ae38b72
child 382 1825151d6455
permissions -rw-r--r--
Remove Pharoisms to make code more portable and running on Smalltalk/X * Use ANSI `(Character codePoint: 13)` (`10`) instead of `Character cr` (`lf`), This is more portable and does not depend on dialects interpretation of `#cr` - Smalltalk/X convert it according to platform line end convention (UNIX/Windows/Mac) * Do not assume exact value of a printstring in tests, i.e., instead of `msg includesSubstring: '$a' code `msg includesSubstring: $a printString. This way, the test is independent on the printString value, which may differ among dialects. Q: Is printString value of String and/or Character defined in ANSI? * In assestions, instead of `#equals:` use plain old `#=`, which is more portable. * Removed Character>>- used to create range parser. Use portable `(Interval from: $a to: $z) asParser` instead of just `$a - $z`. Do not use ($a to: $z) asParser as in Pharo, Character>>to: does not create an Interval but an Array (sigh).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
376
a2656b27cace Added monticelloName to package definition to ease export to .mcz
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 375
diff changeset
     1
"{ Package: 'stx:goodies/petitparser/tests' }"
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
193
Claus Gittinger <cg@exept.de>
parents: 20
diff changeset
     3
PPAbstractParserTest subclass:#PPScriptingTest
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:''
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
	classVariableNames:''
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'PetitTests-Tests'
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
20
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    10
PPScriptingTest comment:'These are some simple demo-scripts of parser combinators for the compiler construction course.
http://www.iam.unibe.ch/~scg/Teaching/CC/index.html'
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
!PPScriptingTest methodsFor:'examples'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
expressionInterpreter
379
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    17
        "Same as #expressionInterpreter but with semantic actions."
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    18
        
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    19
        | mul prim add dec |
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    20
        add := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    21
        mul := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    22
        prim := PPUnresolvedParser new.
380
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    23
        dec := (Interval from: $0 to: $9) asParser ==> [ :token | token codePoint - $0 codePoint ].
379
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    24
        add def: ((mul , $+ asParser , add) ==> [ :nodes | (nodes at: 1) + (nodes at: 3) ])
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    25
                / mul.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    26
        mul def: ((prim , $* asParser , mul) ==> [ :nodes | (nodes at: 1) * (nodes at: 3) ])
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    27
                / prim.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    28
        prim def: (($( asParser , add , $) asParser) ==> [ :nodes | nodes at: 2 ])
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    29
                / dec.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    30
        ^ add end
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    31
380
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    32
    "Modified: / 05-10-2014 / 00:02:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
expressionParser
379
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    36
        "Simple demo of scripting an expression parser."
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    37
        
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    38
        | mul prim add dec |
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    39
        add := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    40
        mul := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    41
        prim := PPUnresolvedParser new.
380
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    42
        dec := (Interval from: $0 to: $9) asParser.
379
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    43
        add def: (mul , $+ asParser , add)
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    44
                / mul.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    45
        mul def: (prim , $* asParser , mul)
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    46
                / prim.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    47
        prim def: ($( asParser , add , $) asParser)
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    48
                / dec.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    49
        ^ add end
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    50
380
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    51
    "Modified: / 05-10-2014 / 00:03:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
straightLineParser
379
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    55
        | goal stm stmList id char dec exp expList mulExp primExp nonzero num lower upper |
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    56
        goal := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    57
        stmList := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    58
        stm := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    59
        exp := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    60
        expList := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    61
        mulExp := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    62
        primExp := PPUnresolvedParser new.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    63
        
380
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    64
        lower := (Interval from: $a to: $z) asParser.
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    65
        upper := (Interval from: $A to: $Z) asParser.
379
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    66
        char := lower / upper.
380
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    67
        nonzero := (Interval from: $1 to: $9) asParser.
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    68
        dec := (Interval from: $0 to: $9) asParser.
379
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    69
        id := char, ( char / dec ) star.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    70
        num := $0 asParser / ( nonzero, dec star).
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
379
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    72
        goal def: stmList end.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    73
        stmList def: stm , ( $; asParser, stm ) star.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    74
        stm def: ( id, ':=' asParser, exp )
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    75
                / ( 'print' asParser, $( asParser, expList, $) asParser ). 
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    76
        exp def: mulExp, ( ( $+ asParser / $- asParser ), mulExp ) star.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    77
        expList def: exp, ( $, asParser, exp ) star.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    78
        mulExp def: primExp, ( ( $* asParser / $/ asParser ), primExp ) star.
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    79
        primExp def: id
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    80
                / num
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    81
                / ( $( asParser, stmList, $, asParser, exp, $) asParser ).
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    82
        ^ goal
451b5ae38b72 Some tests fixed.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    83
380
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 379
diff changeset
    84
    "Modified: / 05-10-2014 / 00:03:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
!PPScriptingTest methodsFor:'tests'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
testExpressionInterpreter
20
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    90
	self 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
		assert: self expressionInterpreter
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
		parse: '2*(3+4)'
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
		to: 14
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
testExpressionParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
	self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
		assert: self expressionParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
		parse: '2*(3+4)'
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
		to: #($2 $* ($( ($3 $+ $4) $)))
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
testSLassign
20
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   104
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
	self assert: self straightLineParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
		parse: 'abc:=1'
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
		to: #(#($a #($b $c) ':=' #(#(#($1 #()) #()) #())) #())
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
testSLprint
20
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   111
	self 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
		assert: self straightLineParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
		parse: 'print(3,4)'
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
		to: #(('print' $( ((($3 ()) ()) () (($, ((($4 ()) ()) ())))) $)) ())
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
!PPScriptingTest class methodsFor:'documentation'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
20
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   119
version
193
Claus Gittinger <cg@exept.de>
parents: 20
diff changeset
   120
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPScriptingTest.st,v 1.4 2014-03-04 14:34:23 cg Exp $'
20
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   121
!
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   122
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   123
version_CVS
193
Claus Gittinger <cg@exept.de>
parents: 20
diff changeset
   124
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPScriptingTest.st,v 1.4 2014-03-04 14:34:23 cg Exp $'
20
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   125
!
46d4542c5f5e Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   126
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
version_SVN
193
Claus Gittinger <cg@exept.de>
parents: 20
diff changeset
   128
    ^ '$Id: PPScriptingTest.st,v 1.4 2014-03-04 14:34:23 cg Exp $'
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
! !
193
Claus Gittinger <cg@exept.de>
parents: 20
diff changeset
   130