Merged in PetitCompiler-JanVrany.170, PetitCompiler-Tests-JanKurs.116, PetitCompiler-Extras-Tests-JanKurs.29, PetitCompiler-Benchmarks-JanKurs.19
Name: PetitCompiler-JanVrany.170
Author: JanVrany
Time: 24-08-2015, 03:19:51.340 PM
UUID: c20a744f-3b41-4aaa-bb8a-71ce74a2a952
Name: PetitCompiler-Tests-JanKurs.116
Author: JanKurs
Time: 24-08-2015, 11:37:54.332 AM
UUID: 549e0927-358a-4a1b-8270-050ccfcb4217
Name: PetitCompiler-Extras-Tests-JanKurs.29
Author: JanKurs
Time: 24-08-2015, 11:36:52.503 AM
UUID: ea1dbb67-f884-4237-8f34-adb0677c0954
Name: PetitCompiler-Benchmarks-JanKurs.19
Author: JanKurs
Time: 24-08-2015, 11:48:47.045 AM
UUID: 1c342fdb-8ddd-4104-9c47-a8f589c51694
"{ Package: 'stx:goodies/petitparser/compiler' }"
"{ NameSpace: Smalltalk }"
PPCCodeGenerator subclass:#PPCTokenizingCodeGenerator
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category:'PetitCompiler-Visitors-CodeGenerators'
!
!PPCTokenizingCodeGenerator methodsFor:'accessing'!
guards
self flag: 'Hack alert, the codeGenerator needs refactoring so that we can remove this method'.
self halt: 'deprecated'.
"When tokenizing, do not use guards"
^ false
! !
!PPCTokenizingCodeGenerator methodsFor:'initialization'!
initialize
super initialize.
codeGen := PPCTokenizingCodeGen new
! !
!PPCTokenizingCodeGenerator methodsFor:'visiting'!
visitAndNode: node
| mementoVar |
mementoVar := codeGen allocateTemporaryVariableNamed: 'memento'.
codeGen remember: node child to: mementoVar.
codeGen
codeEvaluateAndAssign:[ self visit:node child ]
to:self retvalVar.
codeGen restore: node child from: mementoVar.
codeGen codeReturn.
!
visitChoiceNode: node
" true ifTrue: [ ^ super visitChoiceNode: node ]."
| possibleError memento |
possibleError := true.
memento := codeGen allocateTemporaryVariableNamed: 'memento'.
codeGen codeScannerRememberTo: memento.
node children do: [ :child |
| tokenMethodName |
child acceptsEpsilon "false" ifTrue: [
possibleError := false.
codeGen codeEvaluateAndAssign:[ self visit:child ] to:self retvalVar.
codeGen codeReturn.
"makes no sense to compile more..."
^ self
] ifFalse: [
child firstSetWithTokens do: [ :first |
"For each child, for each first compile this:"
tokenMethodName := codeGen idFor: first.
codeGen codeIf: '(scanner ', tokenMethodName asString, ')' then: [
codeGen codeEvaluateAndAssignParsedValueOf:[ self visit:child ] to:self retvalVar.
codeGen codeIf: 'error' then: [
codeGen codeClearError.
codeGen codeScannerRestoreFrom: memento.
] else: [
codeGen codeReturn: self retvalVar.
]
]
]
]
].
possibleError ifTrue: [
codeGen codeError: 'no choice found'.
]
"Modified: / 31-07-2015 / 08:07:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
visitDeterministicChoiceNode: node
| dictionary isInlined |
dictionary := IdentityDictionary new.
" codeGen currentMethod methodName asSymbol = #'ch' ifTrue: [ self halt ]."
isInlined := node isMarkedForInline.
node children do: [ :child |
| firstSet |
firstSet := child firstSetWithTokens.
self assert: firstSet size = 1.
dictionary at: child put: firstSet anyOne.
].
"Tokens are unique"
self assert: dictionary values asSet size = node children size.
node children do: [ :child |
| tokenMethodName |
tokenMethodName := codeGen idFor: (dictionary at: child).
codeGen code: '(scanner ', tokenMethodName asString, ')'.
codeGen codeOnLine: ' ifTrue: ['.
codeGen indent.
codeGen codeReturnParsedValueOf:[ self visit:child ].
codeGen dedent.
isInlined ifTrue:[
codeGen code: '] ifFalse: ['
] ifFalse:[
codeGen code: '].'.
]
].
codeGen codeError: 'no choice found'.
isInlined ifTrue:[
node children size timesRepeat: [ codeGen addOnLine: ']' ].
codeGen codeOnLine: '.'.
]
"Modified: / 21-05-2015 / 15:31:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
visitTokenConsumeNode: node
| id |
id := codeGen idFor: node.
codeGen codeReturn: 'scanner ', id, ' ifNil: [ error := true ].'.
!
visitTokenNode: node
self error: 'should not happen!!'
!
visitTokenizingParserNode: node
codeGen codeHaltIfShiftPressed.
codeGen
codeEvaluateAndAssign:[ self visit:node parser ]
to:self retvalVar.
codeGen codeReturn.
!
visitTrimmingTokenCharacterNode: node
self error: 'should not happen!!'
!
visitTrimmingTokenNode: node
self error: 'should not happen!!'
! !