compiler/benchmarks/PPCSmalltalkNoopParser.st
changeset 462 89464ab03518
child 477 b18b6cc7aabc
equal deleted inserted replaced
461:5986bf6d7d60 462:89464ab03518
       
     1 "{ Package: 'stx:goodies/petitparser/compiler/benchmarks' }"
       
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
       
     5 PPSmalltalkGrammar subclass:#PPCSmalltalkNoopParser
       
     6 	instanceVariableNames:''
       
     7 	classVariableNames:''
       
     8 	poolDictionaries:''
       
     9 	category:'PetitCompiler-Benchmarks-Parsers'
       
    10 !
       
    11 
       
    12 !PPCSmalltalkNoopParser methodsFor:'accessing'!
       
    13 
       
    14 startExpression
       
    15 	"Make the sequence node has a method node as its parent and that the source is set."
       
    16 
       
    17 	^ ([ :stream | stream collection ] asParser and , super startExpression) map: [ :source :node | 
       
    18 		(RBMethodNode selector: #doIt body: node)
       
    19 			source: source.
       
    20 		(node statements size = 1 and: [ node temporaries isEmpty ])
       
    21 			ifTrue: [ node statements first ]
       
    22 			ifFalse: [ node ] ]
       
    23 !
       
    24 
       
    25 startMethod
       
    26         "Make sure the method node has the source code properly set."
       
    27         
       
    28         ^ ([ :stream | stream collection ] asParser and , super startMethod)
       
    29                 map: [ :source :node | ]
       
    30 
       
    31     "Modified: / 16-05-2015 / 09:47:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    32 ! !
       
    33 
       
    34 !PPCSmalltalkNoopParser methodsFor:'grammar'!
       
    35 
       
    36 array
       
    37         ^ super array map: [ :openNode :statementNodes :closeNode | ]
       
    38 
       
    39     "Modified: / 15-05-2015 / 08:54:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    40 !
       
    41 
       
    42 expression
       
    43         ^ super expression map: [ :variableNodes :expressionNodes |  ]
       
    44 
       
    45     "Modified: / 15-05-2015 / 08:55:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    46 !
       
    47 
       
    48 method
       
    49         ^ super method map: [ :methodNode :bodyNode | ]
       
    50 
       
    51     "Modified (format): / 15-05-2015 / 08:55:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    52 !
       
    53 
       
    54 methodDeclaration
       
    55         ^ super methodDeclaration ==> [ :nodes | nodes ]
       
    56 
       
    57     "Modified: / 15-05-2015 / 08:55:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    58 !
       
    59 
       
    60 methodSequence
       
    61         ^ super methodSequence map: [ :periodNodes1 :pragmaNodes1 :periodNodes2 :tempNodes :periodNodes3 :pragmaNodes2 :periodNodes4 :statementNodes | ]
       
    62 
       
    63     "Modified: / 15-05-2015 / 08:55:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    64 !
       
    65 
       
    66 parens
       
    67         ^ super parens map: [ :openToken :expressionNode :closeToken |  ]
       
    68 
       
    69     "Modified: / 15-05-2015 / 08:55:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    70 !
       
    71 
       
    72 pragma
       
    73         ^ super pragma ==> [ :nodes | nodes ]
       
    74 
       
    75     "Modified: / 15-05-2015 / 08:55:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    76 !
       
    77 
       
    78 return
       
    79         ^ super return map: [ :token :expressionNode |  ]
       
    80 
       
    81     "Modified: / 15-05-2015 / 08:55:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    82 !
       
    83 
       
    84 sequence
       
    85         ^ super sequence map: [ :tempNodes :periodNodes :statementNodes |  ]
       
    86 
       
    87     "Modified: / 15-05-2015 / 08:56:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    88 !
       
    89 
       
    90 variable
       
    91         ^ super variable ==> [ :token |  ]
       
    92 
       
    93     "Modified: / 15-05-2015 / 08:56:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    94 ! !
       
    95 
       
    96 !PPCSmalltalkNoopParser methodsFor:'grammar-blocks'!
       
    97 
       
    98 block
       
    99         ^ super block map: [ :leftToken :blockNode :rightToken | ]
       
   100 
       
   101     "Modified: / 15-05-2015 / 08:56:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   102 !
       
   103 
       
   104 blockArgument
       
   105 	^ super blockArgument ==> #second
       
   106 !
       
   107 
       
   108 blockBody
       
   109         ^ super blockBody
       
   110                 ==> [ :nodes |  ]
       
   111 
       
   112     "Modified: / 15-05-2015 / 08:56:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   113 ! !
       
   114 
       
   115 !PPCSmalltalkNoopParser methodsFor:'grammar-literals'!
       
   116 
       
   117 arrayLiteral
       
   118         ^ super arrayLiteral ==> [ :nodes | nodes ]
       
   119 
       
   120     "Modified (format): / 15-05-2015 / 08:56:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   121 !
       
   122 
       
   123 arrayLiteralArray
       
   124         ^ super arrayLiteralArray ==> [ :nodes | nodes ]
       
   125 
       
   126     "Modified: / 15-05-2015 / 08:56:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   127 !
       
   128 
       
   129 byteLiteral
       
   130         ^ super byteLiteral ==> [ :nodes | nodes ]
       
   131 
       
   132     "Modified: / 15-05-2015 / 08:56:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   133 !
       
   134 
       
   135 byteLiteralArray
       
   136         ^ super byteLiteralArray ==> [ :nodes | nodes ]
       
   137 
       
   138     "Modified: / 15-05-2015 / 08:56:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   139 !
       
   140 
       
   141 charLiteral
       
   142         ^ super charLiteral ==> [ :nodes | nodes ]
       
   143 
       
   144     "Modified: / 15-05-2015 / 08:57:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   145 !
       
   146 
       
   147 falseLiteral
       
   148         ^ super falseLiteral ==> [ :nodes | nodes ]
       
   149 
       
   150     "Modified: / 15-05-2015 / 08:57:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   151 !
       
   152 
       
   153 nilLiteral
       
   154         ^ super nilLiteral ==> [ :nodes | nodes ]
       
   155 
       
   156     "Modified: / 15-05-2015 / 08:57:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   157 !
       
   158 
       
   159 numberLiteral
       
   160     ^ super numberLiteral ==> [ :nodes | nodes ]
       
   161 
       
   162     "Modified: / 15-05-2015 / 08:57:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   163 !
       
   164 
       
   165 stringLiteral
       
   166         ^ super stringLiteral ==> [ :nodes | nodes ]
       
   167 
       
   168     "Modified: / 15-05-2015 / 08:57:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   169 !
       
   170 
       
   171 symbolLiteral
       
   172         ^ super symbolLiteral ==> [ :nodes | nodes ]
       
   173 
       
   174     "Modified: / 15-05-2015 / 08:57:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   175 !
       
   176 
       
   177 symbolLiteralArray
       
   178         ^ super symbolLiteralArray ==> [ :nodes | nodes ]
       
   179 
       
   180     "Modified: / 15-05-2015 / 08:57:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   181 !
       
   182 
       
   183 trueLiteral
       
   184         ^ super trueLiteral ==> [ :nodes | nodes ]
       
   185 
       
   186     "Modified: / 15-05-2015 / 08:57:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   187 ! !
       
   188 
       
   189 !PPCSmalltalkNoopParser methodsFor:'grammar-messages'!
       
   190 
       
   191 binaryExpression
       
   192         ^ super binaryExpression map: [ :receiverNode :messageNodes |  ]
       
   193 
       
   194     "Modified: / 15-05-2015 / 08:57:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   195 !
       
   196 
       
   197 cascadeExpression
       
   198         ^ super cascadeExpression map: [ :receiverNode :messageNodes | ]
       
   199 
       
   200     "Modified: / 15-05-2015 / 08:57:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   201 !
       
   202 
       
   203 keywordExpression
       
   204         ^ super keywordExpression map: [ :receiveNode :messageNode | ]
       
   205 
       
   206     "Modified: / 15-05-2015 / 08:58:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   207 !
       
   208 
       
   209 unaryExpression
       
   210         ^ super unaryExpression map: [ :receiverNode :messageNodes | ]
       
   211 
       
   212     "Modified: / 15-05-2015 / 08:58:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   213 ! !
       
   214 
       
   215 !PPCSmalltalkNoopParser methodsFor:'private'!
       
   216 
       
   217 addStatements: aCollection into: aNode
       
   218 	aCollection isNil 
       
   219 		ifTrue: [ ^ aNode ].
       
   220 	aCollection do: [ :each |
       
   221 		each class == PPSmalltalkToken
       
   222 			ifFalse: [ aNode addNode:  each ]
       
   223 			ifTrue: [
       
   224 				aNode statements isEmpty
       
   225 					ifTrue: [ aNode addComments: each comments ]
       
   226 					ifFalse: [ aNode statements last addComments: each comments ].
       
   227 				aNode periods: (aNode periods asOrderedCollection
       
   228 					addLast: each start;
       
   229 					yourself) ] ].
       
   230 	^ aNode
       
   231 !
       
   232 
       
   233 build: aNode assignment: anArray
       
   234 	^ anArray isEmpty
       
   235 		ifTrue: [ aNode ]
       
   236 		ifFalse: [
       
   237 			anArray reverse 
       
   238 				inject: aNode
       
   239 				into: [ :result :each |
       
   240 					RBAssignmentNode 
       
   241 						variable: each first
       
   242 						value: result
       
   243 						position: each second start ] ]
       
   244 !
       
   245 
       
   246 build: aNode cascade: anArray 
       
   247 	| messages semicolons |
       
   248 	^ (anArray isNil or: [ anArray isEmpty ]) 
       
   249 		ifTrue: [ aNode ]
       
   250 		ifFalse: [
       
   251 			messages := OrderedCollection new: anArray size + 1.
       
   252 			messages addLast: aNode.
       
   253 			semicolons := OrderedCollection new.
       
   254 			anArray do: [ :each | 
       
   255 				messages addLast: (self 
       
   256 					build: aNode receiver
       
   257 					messages: (Array with: each second)).
       
   258 				semicolons addLast: each first start ].
       
   259 			RBCascadeNode messages: messages semicolons: semicolons ]
       
   260 !
       
   261 
       
   262 build: aNode messages: anArray 
       
   263 	^ (anArray isNil or: [ anArray isEmpty ]) 
       
   264 		ifTrue: [ aNode ]
       
   265 		ifFalse: [
       
   266 			anArray 
       
   267 				inject: aNode
       
   268 				into: [ :rec :msg | 
       
   269 					msg isNil 
       
   270 						ifTrue: [ rec ]
       
   271 						ifFalse: [
       
   272 							RBMessageNode 
       
   273 								receiver: rec
       
   274 								selectorParts: msg first
       
   275 								arguments: msg second ] ] ]
       
   276 !
       
   277 
       
   278 build: aTempCollection sequence: aStatementCollection
       
   279 	| result |
       
   280 	result := self
       
   281 		addStatements: aStatementCollection
       
   282 		into: RBSequenceNode new.
       
   283 	aTempCollection isEmpty ifFalse: [
       
   284 		result
       
   285 			leftBar: aTempCollection first start
       
   286 			temporaries: aTempCollection second
       
   287 			rightBar: aTempCollection last start ].
       
   288 	^ result
       
   289 !
       
   290 
       
   291 buildArray: aStatementCollection
       
   292 	^ self addStatements: aStatementCollection into: RBArrayNode new
       
   293 !
       
   294 
       
   295 buildMethod: aMethodNode
       
   296 	aMethodNode selectorParts 
       
   297 		do: [ :each | aMethodNode addComments: each comments ].
       
   298 	aMethodNode arguments
       
   299 		do: [ :each | aMethodNode addComments: each token comments ].
       
   300 	aMethodNode pragmas do: [ :pragma |
       
   301 		aMethodNode addComments: pragma comments.
       
   302 		pragma selectorParts 
       
   303 			do: [ :each | aMethodNode addComments: each comments ].
       
   304 		pragma arguments do: [ :each | 
       
   305 			each isLiteralArray
       
   306 				ifFalse: [ aMethodNode addComments: each token comments ] ].
       
   307 		pragma comments: nil ].
       
   308 	^ aMethodNode
       
   309 !
       
   310 
       
   311 buildString: aString 
       
   312 	(aString isEmpty or: [ aString first ~= $' or: [ aString last ~= $' ] ])
       
   313 		ifTrue: [ ^ aString ].
       
   314 	^ (aString 
       
   315 		copyFrom: 2
       
   316 		to: aString size - 1) 
       
   317 		copyReplaceAll: ''''''
       
   318 		with: ''''
       
   319 ! !
       
   320 
       
   321 !PPCSmalltalkNoopParser methodsFor:'token'!
       
   322 
       
   323 binaryToken
       
   324         ^ super binaryToken ==> [ :token | token ]
       
   325 
       
   326     "Modified: / 15-05-2015 / 08:54:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   327 !
       
   328 
       
   329 identifierToken
       
   330         ^ super identifierToken ==> [ :token | token ]
       
   331 
       
   332     "Modified: / 15-05-2015 / 08:54:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   333 !
       
   334 
       
   335 keywordToken
       
   336         ^ super keywordToken ==> [ :token | token ]
       
   337 
       
   338     "Modified: / 15-05-2015 / 08:54:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   339 !
       
   340 
       
   341 unaryToken
       
   342         ^ super unaryToken ==> [ :token | token ]
       
   343 
       
   344     "Modified: / 15-05-2015 / 08:54:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   345 ! !
       
   346