compiler/PPCChoiceNode.st
changeset 438 20598d7ce9fa
parent 422 116d2b2af905
child 452 9f4558b3be66
equal deleted inserted replaced
437:54b3bc9e3987 438:20598d7ce9fa
     7 	classVariableNames:''
     7 	classVariableNames:''
     8 	poolDictionaries:''
     8 	poolDictionaries:''
     9 	category:'PetitCompiler-Nodes'
     9 	category:'PetitCompiler-Nodes'
    10 !
    10 !
    11 
    11 
    12 !PPCChoiceNode methodsFor:'as yet unclassified'!
    12 !PPCChoiceNode methodsFor:'accessing'!
       
    13 
       
    14 prefix
       
    15 	^ #ch
       
    16 ! !
       
    17 
       
    18 !PPCChoiceNode methodsFor:'analysis'!
    13 
    19 
    14 acceptsEpsilon
    20 acceptsEpsilon
    15 	^ self acceptsEpsilonOpenSet: IdentitySet new.
    21 	^ self acceptsEpsilonOpenSet: IdentitySet new.
    16 !
    22 !
    17 
    23 
    18 acceptsEpsilonOpenSet: set
    24 acceptsEpsilonOpenSet: set
    19 	set add: self.
    25 	set add: self.
    20 	^ self children anySatisfy: [:e | e acceptsEpsilonOpenSet: set ].
    26 	^ self children anySatisfy: [:e | e acceptsEpsilonOpenSet: set ].
    21 !
       
    22 
       
    23 compileWith: compiler effect: effect id: id
       
    24 	| firsts guard whitespaceConsumed |
       
    25 
       
    26 	whitespaceConsumed := false.
       
    27 	firsts := (self firstSetSuchThat: [ :e | (e isKindOf: PPCTrimmingTokenNode) or: [ e isTerminal ] ]).
       
    28 	
       
    29 	compiler startMethod: id.
       
    30 	compiler addVariable: 'element'.
       
    31 
       
    32 	"If we start with trimming token, we should invoke the whitespace parser"
       
    33 	(firsts allSatisfy: [ :e | e isKindOf: PPCTrimmingTokenNode ]) ifTrue: [  
       
    34 		firsts anyOne compileWhitespace: compiler.
       
    35 		whitespaceConsumed := true.
       
    36 	].
       
    37 	
       
    38 	(1 to: children size) do: [ :idx  | |child allowGuard |
       
    39 		child := children at: idx.
       
    40 "		allowGuard := ((child isKindOf: PPCTrimmingTokenNode) and: [ whitespaceConsumed not ]) not.
       
    41 "	
       
    42 		allowGuard := whitespaceConsumed.
       
    43 				
       
    44  		(allowGuard and: [compiler guards and: [ (guard := PPCGuard on: child) makesSense ]]) ifTrue: [ 	
       
    45 			guard id: (compiler idFor: guard prefixed: #guard).
       
    46 			guard compileGuard: compiler.
       
    47 			compiler add: ' ifTrue: [ '.
       
    48 			compiler indent.
       
    49 				compiler add: 'self clearError.'.
       
    50 				compiler add: 'element := '.
       
    51 				compiler callOnLine: (child compileWith: compiler).
       
    52 				compiler add: 'error ifFalse: [ ^ element ].'.
       
    53 			compiler dedent.
       
    54 			compiler add: ' ].'.
       
    55 		] ifFalse: [
       
    56 			compiler add: 'self clearError.'.
       
    57 			compiler add: 'element := '.
       
    58 			compiler callOnLine: (child compileWith: compiler).
       
    59 			compiler add: 'error ifFalse: [ ^ element ].'.
       
    60 		]
       
    61 	].
       
    62 	compiler add: '^ self error: ''no choice suitable'''.
       
    63  ^ compiler stopMethod.
       
    64 !
       
    65 
       
    66 prefix
       
    67 	^ #ch
       
    68 ! !
    27 ! !
    69 
    28 
       
    29 !PPCChoiceNode methodsFor:'visiting'!
       
    30 
       
    31 accept: visitor
       
    32 	^ visitor visitChoiceNode: self
       
    33 ! !
       
    34