compiler/PPCScanner.st
changeset 524 f6f68d32de73
parent 515 b5316ef15274
child 525 751532c8f3db
equal deleted inserted replaced
515:b5316ef15274 524:f6f68d32de73
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     2 
     2 
     3 "{ NameSpace: Smalltalk }"
     3 "{ NameSpace: Smalltalk }"
     4 
     4 
     5 Object subclass:#PPCScanner
     5 Object subclass:#PPCScanner
     6 	instanceVariableNames:'match matchPosition matches tokens stream currentChar
     6 	instanceVariableNames:'match matchPosition matches tokens currentChar maxSymbolNumber
     7 		maxSymbolNumber position'
     7 		position context'
     8 	classVariableNames:''
     8 	classVariableNames:''
     9 	poolDictionaries:''
     9 	poolDictionaries:''
    10 	category:'PetitCompiler-Scanner'
    10 	category:'PetitCompiler-Scanner'
    11 !
    11 !
    12 
    12 
    31     "returns the start position before the scan method..."
    31     "returns the start position before the scan method..."
    32     ^ position
    32     ^ position
    33 !
    33 !
    34 
    34 
    35 stream
    35 stream
    36     ^ stream
    36     ^ context
    37 !
    37 !
    38 
    38 
    39 stream: anObject
    39 stream: anObject
    40     stream := anObject
    40     context := anObject
    41 ! !
    41 ! !
    42 
    42 
    43 !PPCScanner methodsFor:'initialization'!
    43 !PPCScanner methodsFor:'initialization'!
    44 
    44 
    45 initialize
    45 initialize
    53 !
    53 !
    54 
    54 
    55 reset
    55 reset
    56     matchPosition := nil. "This flag says that multimode run the last time"
    56     matchPosition := nil. "This flag says that multimode run the last time"
    57 
    57 
    58     position := stream position.
    58     position := context position.
    59 "	matches := Array new: maxSymbolNumber."
    59 "	matches := Array new: maxSymbolNumber."
    60 !
    60 !
    61 
    61 
    62 reset: tokenList
    62 reset: tokenList
    63     "Method should not be used, it is here for debugging and testing purposes"
    63     "Method should not be used, it is here for debugging and testing purposes"
    74 "	matches := IdentityDictionary new.	"
    74 "	matches := IdentityDictionary new.	"
    75     match := nil.
    75     match := nil.
    76     matchPosition := -1.						"this is a flag that the distnict mode was running"
    76     matchPosition := -1.						"this is a flag that the distnict mode was running"
    77 "	matches := nil."
    77 "	matches := nil."
    78 
    78 
    79     position := stream position.
    79     position := context position.
    80     
    80     
    81 ! !
    81 ! !
    82 
    82 
    83 !PPCScanner methodsFor:'results'!
    83 !PPCScanner methodsFor:'results'!
    84 
    84 
   168     ] ]. 
   168     ] ]. 
   169 ! !
   169 ! !
   170 
   170 
   171 !PPCScanner methodsFor:'results - distinct'!
   171 !PPCScanner methodsFor:'results - distinct'!
   172 
   172 
       
   173 recordDistinctFailure
       
   174     match := nil.
       
   175     matchPosition := position.
       
   176     ^ false
       
   177 !
       
   178 
       
   179 recordDistinctFailure: matchValue
       
   180     match := matchValue.
       
   181     matchPosition := context position.
       
   182     ^ false
       
   183 !
       
   184 
   173 recordDistinctMatch: matchValue
   185 recordDistinctMatch: matchValue
   174     match := matchValue.
   186     match := matchValue.
   175     matchPosition := stream position.
   187     matchPosition := context position.
       
   188     ^ true
   176 !
   189 !
   177 
   190 
   178 recordDistinctMatch: matchValue offset: offset
   191 recordDistinctMatch: matchValue offset: offset
   179     match := matchValue.
   192     match := matchValue.
   180     currentChar isNil ifFalse: [ 
   193     currentChar isNil ifFalse: [ 
   181         matchPosition := stream position - offset.
   194         matchPosition := context position - offset.
   182     ] ifTrue: [ 
   195     ] ifTrue: [ 
   183         matchPosition := stream position.
   196         matchPosition := context position.
   184     ]
   197     ].
       
   198     ^ true
   185 !
   199 !
   186 
   200 
   187 returnDistinct
   201 returnDistinct
   188     ^ match isNotNil
   202     ^ match isNotNil
   189 ! !
   203 ! !
   197 recordFailure: index offset: offset
   211 recordFailure: index offset: offset
   198     matches at: index put: -1.
   212     matches at: index put: -1.
   199 !
   213 !
   200 
   214 
   201 recordMatch: index
   215 recordMatch: index
   202  	matches at: index put: stream position.
   216  	matches at: index put: context position.
   203 !
   217 !
   204 
   218 
   205 recordMatch: index offset: offset
   219 recordMatch: index offset: offset
   206     currentChar isNil ifFalse: [ 
   220     currentChar isNil ifFalse: [ 
   207         matches at: index put: stream position - offset.
   221         matches at: index put: context position - offset.
   208     ] ifTrue: [ 
   222     ] ifTrue: [ 
   209         matches at: index put: stream position.
   223         matches at: index put: context position.
   210     ].
   224     ].
   211  
   225  
   212 !
   226 !
   213 
   227 
   214 return
   228 return
   215     ^ matches
   229     ^ matches
   216 ! !
   230 ! !
   217 
   231 
   218 !PPCScanner methodsFor:'scanning'!
   232 !PPCScanner methodsFor:'scanning'!
   219 
   233 
   220 next
   234 back
   221     self error: 'deprecated?'.
   235     currentChar isNil ifFalse: [ 
   222     stream next
   236         context skip: -1
       
   237     ]
   223 !
   238 !
   224 
   239 
   225 peek
   240 peek
       
   241     self flag: 'inline this, do not call peek!!'.
   226     ^ currentChar
   242     ^ currentChar
   227 !
   243 !
   228 
   244 
   229 peekBetween: start and: stop
   245 peekBetween: start and: stop
   230     (currentChar == nil) ifTrue: [ ^ false ].
   246     (currentChar == nil) ifTrue: [ ^ false ].
   231     ^ (start <= currentChar codePoint) and: [ currentChar codePoint <= stop ]
   247     ^ (start <= currentChar codePoint) and: [ currentChar codePoint <= stop ]
   232 !
   248 !
   233 
   249 
   234 step
   250 step
   235     currentChar := stream next
   251     currentChar := context next
   236 ! !
   252 ! !
   237 
   253 
   238 !PPCScanner methodsFor:'testing'!
   254 !PPCScanner methodsFor:'testing'!
   239 
   255 
   240 isSingleMatch
   256 isSingleMatch