compiler/PPCompiledParser.st
changeset 438 20598d7ce9fa
parent 431 dd353f15b0ef
child 452 9f4558b3be66
equal deleted inserted replaced
437:54b3bc9e3987 438:20598d7ce9fa
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     2 
     2 
     3 "{ NameSpace: Smalltalk }"
     3 "{ NameSpace: Smalltalk }"
     4 
     4 
     5 PPParser subclass:#PPCompiledParser
     5 PPParser subclass:#PPCompiledParser
     6 	instanceVariableNames:'startSymbol context failure error'
     6 	instanceVariableNames:'startSymbol context failure error currentTokenValue
       
     7 		currentTokenType'
     7 	classVariableNames:''
     8 	classVariableNames:''
     8 	poolDictionaries:''
     9 	poolDictionaries:''
     9 	category:'PetitCompiler-Core'
    10 	category:'PetitCompiler-Core'
    10 !
    11 !
    11 
    12 
    12 PPCompiledParser class instanceVariableNames:'parsers constants referringParser'
    13 PPCompiledParser class instanceVariableNames:'parsers constants referringParser startSymbol'
    13 
    14 
    14 "
    15 "
    15  No other class instance variables are inherited by this class.
    16  No other class instance variables are inherited by this class.
    16 "
    17 "
    17 !
    18 !
    18 
    19 
    19 !PPCompiledParser class methodsFor:'as yet unclassified'!
    20 !PPCompiledParser class methodsFor:'as yet unclassified'!
    20 
    21 
    21 addConstant: value as: id
    22 addConstant: value as: id
    22 	self constants at: id ifPresent: [:ignored | 
    23 	self constants at: id ifPresent: [ 
    23 		((self constants at: id) = value) ifFalse: [self error: 'ooups']].	
    24 		((self constants at: id) = value) ifFalse: [self error: 'ooups']].	
    24 	
    25 	
    25 	self constants at: id put: value.
    26 	self constants at: id put: value.
    26 !
       
    27 
       
    28 addParser: aPPParser as: id
       
    29 	
       
    30 	"(self parsers includesKey: id) ifTrue: [self error: 'Ooups' ]."
       
    31 	self parsers at: id put: aPPParser.
       
    32 !
    27 !
    33 
    28 
    34 constants
    29 constants
    35 	constants ifNil: [ constants := IdentityDictionary new ].
    30 	constants ifNil: [ constants := IdentityDictionary new ].
    36 	^ constants
    31 	^ constants
    38 
    33 
    39 parse: input
    34 parse: input
    40 	^ self new parse: input
    35 	^ self new parse: input
    41 !
    36 !
    42 
    37 
    43 parsers
       
    44 	parsers ifNil: [ parsers := IdentityDictionary new ].
       
    45 	^ parsers
       
    46 !
       
    47 
       
    48 referringParser
    38 referringParser
    49 	^ referringParser 
    39 	^ referringParser ifNil: [ ^ PPSentinel new ]
    50 !
    40 !
    51 
    41 
    52 referringParser: aPPParser
    42 referringParser: aPPParser
    53 	referringParser := aPPParser
    43 	referringParser := aPPParser
       
    44 !
       
    45 
       
    46 startSymbol
       
    47 	^ startSymbol ifNil: [ ^ #start ]
       
    48 !
       
    49 
       
    50 startSymbol: symbol
       
    51 	startSymbol := symbol
    54 ! !
    52 ! !
    55 
    53 
    56 !PPCompiledParser methodsFor:'as yet unclassified'!
    54 !PPCompiledParser methodsFor:'as yet unclassified'!
    57 
    55 
    58 callParser: id
    56 callParser: id
    89 	
    87 	
    90 	self class constants keysAndValuesDo: [ :key :value |
    88 	self class constants keysAndValuesDo: [ :key :value |
    91 		self instVarNamed: key put: value.
    89 		self instVarNamed: key put: value.
    92 	].
    90 	].
    93 
    91 
    94 	startSymbol := #start.
    92 	startSymbol := self class startSymbol.
    95 
    93 
    96 	
    94 	
    97 
       
    98 !
    95 !
    99 
    96 
   100 isCompiled
    97 isCompiled
   101 	^ true
    98 	^ true
   102 !
    99 !
   114 	^ self subclassResponsibility
   111 	^ self subclassResponsibility
   115 !
   112 !
   116 
   113 
   117 startSymbol: aSymbol
   114 startSymbol: aSymbol
   118 	startSymbol := aSymbol
   115 	startSymbol := aSymbol
   119 !
       
   120 
       
   121 updateContext: aPPContext
       
   122 	self class referringParser allParsersDo: [ :p | p updateContext: aPPContext ].
       
   123 ! !
   116 ! !
   124 
   117 
   125 !PPCompiledParser methodsFor:'parsing'!
   118 !PPCompiledParser methodsFor:'parsing'!
   126 
   119 
   127 parseOn: aPPContext
   120 parseOn: aPPContext
   139 	
   132 	
   140 "	aPPContext position: context position."
   133 "	aPPContext position: context position."
   141 	^ retval
   134 	^ retval
   142 ! !
   135 ! !
   143 
   136 
       
   137 !PPCompiledParser methodsFor:'tokenizing'!
       
   138 
       
   139 consume: tokenType
       
   140 	(currentTokenType = tokenType) ifTrue: [ 
       
   141 		| retval |
       
   142 		retval := currentTokenValue.
       
   143 		self nextToken.
       
   144 		^ retval
       
   145 	] ifFalse: [ 
       
   146 		self error: 'expected: ', tokenType storeString, ' got ', currentTokenType storeString.
       
   147 	]
       
   148 !
       
   149 
       
   150 currentTokenType
       
   151 	currentTokenType isNil ifTrue: [ self nextToken ].
       
   152 	^ currentTokenType
       
   153 !
       
   154 
       
   155 currentTokenValue
       
   156 	currentTokenType isNil ifTrue: [ self nextToken ].
       
   157 	^ currentTokenType
       
   158 !
       
   159 
       
   160 nextToken
       
   161 	self shouldBeImplemented
       
   162 ! !
       
   163