compiler/PPCompiledParser.st
changeset 452 9f4558b3be66
parent 438 20598d7ce9fa
child 515 b5316ef15274
equal deleted inserted replaced
438:20598d7ce9fa 452:9f4558b3be66
     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 currentTokenValue
     6 	instanceVariableNames:'startSymbol context failure error'
     7 		currentTokenType'
       
     8 	classVariableNames:''
     7 	classVariableNames:''
     9 	poolDictionaries:''
     8 	poolDictionaries:''
    10 	category:'PetitCompiler-Core'
     9 	category:'PetitCompiler-Parsers'
    11 !
    10 !
    12 
    11 
    13 PPCompiledParser class instanceVariableNames:'parsers constants referringParser startSymbol'
    12 PPCompiledParser class instanceVariableNames:'parsers constants referringParser startSymbol'
    14 
    13 
    15 "
    14 "
    18 !
    17 !
    19 
    18 
    20 !PPCompiledParser class methodsFor:'as yet unclassified'!
    19 !PPCompiledParser class methodsFor:'as yet unclassified'!
    21 
    20 
    22 addConstant: value as: id
    21 addConstant: value as: id
    23 	self constants at: id ifPresent: [ 
    22     self constants at: id ifPresent: [ 
    24 		((self constants at: id) = value) ifFalse: [self error: 'ooups']].	
    23         ((self constants at: id) = value) ifFalse: [self error: 'ooups']].	
    25 	
    24     
    26 	self constants at: id put: value.
    25     self constants at: id put: value.
    27 !
    26 !
    28 
    27 
    29 constants
    28 constants
    30 	constants ifNil: [ constants := IdentityDictionary new ].
    29     constants ifNil: [ constants := IdentityDictionary new ].
    31 	^ constants
    30     ^ constants
    32 !
    31 !
    33 
    32 
    34 parse: input
    33 parse: input
    35 	^ self new parse: input
    34     ^ self new parse: input
    36 !
    35 !
    37 
    36 
    38 referringParser
    37 referringParser
    39 	^ referringParser ifNil: [ ^ PPSentinel new ]
    38     ^ referringParser ifNil: [ ^ PPSentinel new ]
    40 !
    39 !
    41 
    40 
    42 referringParser: aPPParser
    41 referringParser: aPPParser
    43 	referringParser := aPPParser
    42     referringParser := aPPParser
    44 !
    43 !
    45 
    44 
    46 startSymbol
    45 startSymbol
    47 	^ startSymbol ifNil: [ ^ #start ]
    46     ^ startSymbol ifNil: [ ^ #start ]
    48 !
    47 !
    49 
    48 
    50 startSymbol: symbol
    49 startSymbol: symbol
    51 	startSymbol := symbol
    50     startSymbol := symbol
    52 ! !
    51 ! !
    53 
    52 
    54 !PPCompiledParser methodsFor:'as yet unclassified'!
    53 !PPCompiledParser methodsFor:'as yet unclassified'!
    55 
    54 
    56 callParser: id
    55 callParser: id
    57 	| retval |
    56     | retval |
    58 	retval := (self class parsers at: id) parseOn: context.
    57     retval := (self class parsers at: id) parseOn: context.
    59 	retval isPetitFailure 	ifTrue: [ self error: retval message at: retval position ]
    58     retval isPetitFailure 	ifTrue: [ self error: retval message at: retval position ]
    60 									ifFalse: [ self clearError ].
    59                                     ifFalse: [ self clearError ].
    61 	^ retval
    60     ^ retval
    62 !
    61 !
    63 
    62 
    64 clearError
    63 clearError
    65 	error := false.
    64     error := false.
    66 !
    65 !
    67 
    66 
    68 error
    67 error
    69 	^ self error: '' at: context position
    68     ^ self error: '' at: context position
    70 !
    69 !
    71 
    70 
    72 error: message
    71 error: message
    73 	^ self error: message at: context position
    72     ^ self error: message at: context position
    74 !
    73 !
    75 
    74 
    76 error: aMessage at: position
    75 error: aMessage at: position
    77 	failure position < position ifTrue: [
    76     failure position < position ifTrue: [
    78 		failure message: aMessage.
    77         failure message: aMessage.
    79 		failure position: position
    78         failure position: position
    80 	].
    79     ].
    81 	error := true.
    80     error := true.
    82 	^ failure
    81     ^ failure
    83 !
    82 !
    84 
    83 
    85 initialize
    84 initialize
    86 	super initialize.
    85     super initialize.
    87 	
    86     
    88 	self class constants keysAndValuesDo: [ :key :value |
    87     self class constants keysAndValuesDo: [ :key :value |
    89 		self instVarNamed: key put: value.
    88         self instVarNamed: key put: value.
    90 	].
    89     ].
    91 
    90 
    92 	startSymbol := self class startSymbol.
    91     startSymbol := self class startSymbol.
    93 
    92 
    94 	
    93     
    95 !
    94 !
    96 
    95 
    97 isCompiled
    96 isCompiled
    98 	^ true
    97     ^ true
    99 !
    98 !
   100 
    99 
   101 isError
   100 isError
   102 	^ error
   101     ^ error
   103 !
   102 !
   104 
   103 
   105 parse: input rule: symbol
   104 parse: input rule: symbol
   106 	startSymbol := symbol.
   105     startSymbol := symbol.
   107 	^ self parse: input.
   106     ^ self parse: input.
   108 !
   107 !
   109 
   108 
   110 start
   109 start
   111 	^ self subclassResponsibility
   110     ^ self subclassResponsibility
   112 !
   111 !
   113 
   112 
   114 startSymbol: aSymbol
   113 startSymbol: aSymbol
   115 	startSymbol := aSymbol
   114     startSymbol := aSymbol
   116 ! !
   115 ! !
   117 
   116 
   118 !PPCompiledParser methodsFor:'parsing'!
   117 !PPCompiledParser methodsFor:'parsing'!
   119 
   118 
   120 parseOn: aPPContext
   119 parseOn: aPPContext
   121 	| retval |
   120     | retval |
   122 "	context := aPPContext asCompiledParserContext."
   121 "	context := aPPContext asCompiledParserContext."
   123 	context := aPPContext.
   122     context := aPPContext.
   124 	context compiledParser: self.
   123     context compiledParser: self.
   125 	failure := PPFailure new message: nil; context: context; position: -1.
   124     failure := PPFailure new message: nil; context: context; position: -1.
   126 	context noteFailure: failure.
   125     context noteFailure: failure.
   127 	error := false.
   126     error := false.
   128 
   127 
   129 	retval := self perform: startSymbol.
   128     retval := self perform: startSymbol.
   130 	(retval isPetitFailure) ifTrue: [ aPPContext noteFailure: failure ].
   129     (retval isPetitFailure) ifTrue: [ aPPContext noteFailure: failure ].
   131 	error ifTrue: [ aPPContext noteFailure: failure. retval := failure ].
   130     error ifTrue: [ aPPContext noteFailure: failure. retval := failure ].
   132 	
   131     
   133 "	aPPContext position: context position."
   132 "	aPPContext position: context position."
   134 	^ retval
   133     ^ retval
   135 ! !
   134 ! !
   136 
   135 
   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