compiler/PPCScanner.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 17 Aug 2015 12:13:16 +0100
changeset 515 b5316ef15274
parent 502 1e45d3c96ec5
child 516 3b81c9e53352
child 524 f6f68d32de73
permissions -rw-r--r--
Updated to PetitCompiler-JanKurs.160, PetitCompiler-Tests-JanKurs.112, PetitCompiler-Extras-Tests-JanKurs.25, PetitCompiler-Benchmarks-JanKurs.17 Name: PetitCompiler-JanKurs.160 Author: JanKurs Time: 17-08-2015, 09:52:26.291 AM UUID: 3b4bfc98-8098-4951-af83-a59e2585b121 Name: PetitCompiler-Tests-JanKurs.112 Author: JanKurs Time: 16-08-2015, 05:00:32.936 PM UUID: 85613d47-08f3-406f-9823-9cdab451e805 Name: PetitCompiler-Extras-Tests-JanKurs.25 Author: JanKurs Time: 16-08-2015, 05:00:10.328 PM UUID: 09731810-51a1-4151-8d3a-56b636fbd1f7 Name: PetitCompiler-Benchmarks-JanKurs.17 Author: JanKurs Time: 05-08-2015, 05:29:32.407 PM UUID: e544b5f1-bcf8-470b-93a6-d2363e4dfc8a

"{ Package: 'stx:goodies/petitparser/compiler' }"

"{ NameSpace: Smalltalk }"

Object subclass:#PPCScanner
	instanceVariableNames:'match matchPosition matches tokens stream currentChar
		maxSymbolNumber position'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Scanner'
!

!PPCScanner class methodsFor:'as yet unclassified'!

acceptsLoggingOfCompilation
"	^ self == PPCScanner"
    ^ true
! !

!PPCScanner methodsFor:'accessing'!

maxSymbolNumber
    ^ maxSymbolNumber
!

maxSymbolNumber: value
    maxSymbolNumber := value
!

position
    "returns the start position before the scan method..."
    ^ position
!

stream
    ^ stream
!

stream: anObject
    stream := anObject
! !

!PPCScanner methodsFor:'initialization'!

initialize
    super initialize.
    
    maxSymbolNumber := self class classVarNamed: #MaxSymbolNumber.
    tokens := self class classVarNamed: #Tokens.
    
    matches := Array new: maxSymbolNumber withAll: -2.
    position := 0.
!

reset
    matchPosition := nil. "This flag says that multimode run the last time"

    position := stream position.
"	matches := Array new: maxSymbolNumber."
!

reset: tokenList
    "Method should not be used, it is here for debugging and testing purposes"
    self error: 'deprecated'.
    
    matchPosition := nil. "This flag says that multimode run the last time"
    
    tokens := tokenList.
    matches := Array new: tokens size.
    
!

resetDistinct
"	matches := IdentityDictionary new.	"
    match := nil.
    matchPosition := -1.						"this is a flag that the distnict mode was running"
"	matches := nil."

    position := stream position.
    
! !

!PPCScanner methodsFor:'results'!

backtrack
    matchPosition := nil.
    match := nil.
    matches := Array new: maxSymbolNumber withAll: -2.
    position := 0.
!

backtrackDistinct
    matchPosition := nil.
    match := nil.
    position := 0.
!

backtracked
    ^ position == 0
!

indexOf: symbol
    (1 to: tokens size) do: [ :index | (tokens at: index) == symbol ifTrue: [^ index ] ]. 
!

match
"	^ match isNil not."
    ^ match isNotNil
"	^ matchPosition isNil not"
!

match: symbolNumber
"	matches isNil ifTrue: [ ^ false ]."
    
    "
        The general idea here is optimization. I cannot initialize 
        the matches before each token, it would be too expensive.
    "
    ^ (matches at: symbolNumber) > position
!

matchSymbol: symbol
    matches isNil ifTrue: [ ^ false ].
    (1 to: tokens size) do: [ :index | (tokens at: index) == symbol ifTrue: [
        ^ (matches at: index) > position
    ] ]. 
!

polyResult
    | dictionary |
    "TODO JK: refactor"	
    self isSingleMatch ifFalse: [ 
        dictionary := IdentityDictionary new.
        (1 to: matches size) do: [ :index | 
            (self match: index) ifTrue: [ 
                dictionary 
                    at: (tokens at: index)
                    put: (matches at: index)
            ]
        ].
        ^ dictionary
    ].

    dictionary := IdentityDictionary new.
    match isNil ifFalse: [ 
        dictionary at: match put: matchPosition.
    ].

    ^ dictionary
!

result
    ^ match
!

resultPosition
    ^ matchPosition
!

resultPosition: symbolNumber
    ^ matches at: symbolNumber
!

resultPositionForSymbol: symbol
    tokens isNil ifTrue: [ ^ false ].
    (1 to: tokens size) do: [ :index | (tokens at: index) == symbol ifTrue: [
        ^ matches at: index
    ] ]. 
! !

!PPCScanner methodsFor:'results - distinct'!

recordDistinctMatch: matchValue
    match := matchValue.
    matchPosition := stream position.
!

recordDistinctMatch: matchValue offset: offset
    match := matchValue.
    currentChar isNil ifFalse: [ 
        matchPosition := stream position - offset.
    ] ifTrue: [ 
        matchPosition := stream position.
    ]
!

returnDistinct
    ^ match isNotNil
! !

!PPCScanner methodsFor:'results - universal'!

recordFailure: index
    matches at: index put: -1.
!

recordFailure: index offset: offset
    matches at: index put: -1.
!

recordMatch: index
 	matches at: index put: stream position.
!

recordMatch: index offset: offset
    currentChar isNil ifFalse: [ 
        matches at: index put: stream position - offset.
    ] ifTrue: [ 
        matches at: index put: stream position.
    ].
 
!

return
    ^ matches
! !

!PPCScanner methodsFor:'scanning'!

next
    self error: 'deprecated?'.
    stream next
!

peek
    ^ currentChar
!

peekBetween: start and: stop
    (currentChar == nil) ifTrue: [ ^ false ].
    ^ (start <= currentChar codePoint) and: [ currentChar codePoint <= stop ]
!

step
    currentChar := stream next
! !

!PPCScanner methodsFor:'testing'!

isSingleMatch
    ^ (matchPosition == nil) not
! !