compiler/PPTokenizingCompiledParser.st
changeset 452 9f4558b3be66
child 459 4751c407bb40
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compiler/PPTokenizingCompiledParser.st	Sun May 10 06:28:36 2015 +0100
@@ -0,0 +1,56 @@
+"{ Package: 'stx:goodies/petitparser/compiler' }"
+
+"{ NameSpace: Smalltalk }"
+
+PPCompiledParser subclass:#PPTokenizingCompiledParser
+	instanceVariableNames:'currentTokenValue currentTokenType'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'PetitCompiler-Parsers'
+!
+
+!PPTokenizingCompiledParser methodsFor:'tokenizing'!
+
+consume: tokenType
+    (self currentTokenTypeIs: tokenType) ifTrue: [ 
+        | retval |
+        retval := currentTokenValue.
+        currentTokenType := nil.
+        ^ retval
+    ] ifFalse: [ 
+        "self error: 'expected: ', tokenType storeString, ' got ', currentTokenType storeString."
+        self error.
+    ]
+!
+
+currentTokenType
+    currentTokenType isNil ifTrue: [ self nextToken ].
+    ^ currentTokenType
+!
+
+currentTokenTypeIs: tokenType
+    "if the type is read"
+    currentTokenType isNil ifFalse: [ ^ currentTokenType = tokenType ].
+    
+    "if not, try to read the token"
+    self perform: tokenType.
+    error ifTrue: [  
+        ^ error := false.
+    ].
+    ^ true
+!
+
+currentTokenValue
+    currentTokenType isNil ifTrue: [ self nextToken ].
+    ^ currentTokenType
+!
+
+nextToken
+    self shouldBeImplemented 
+!
+
+parseOn: input
+    currentTokenType := nil.
+    ^ super parseOn: input.
+! !
+