compiler/PPCTokenDetector.st
changeset 438 20598d7ce9fa
child 452 9f4558b3be66
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compiler/PPCTokenDetector.st	Thu Apr 30 23:43:14 2015 +0200
@@ -0,0 +1,67 @@
+"{ Package: 'stx:goodies/petitparser/compiler' }"
+
+"{ NameSpace: Smalltalk }"
+
+PPCRewritingVisitor subclass:#PPCTokenDetector
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'PetitCompiler-Visitors'
+!
+
+!PPCTokenDetector methodsFor:'visiting'!
+
+visitActionNode: node
+	(node hasProperty: #trimmingToken) ifTrue: [ 
+		| newWs newChild  |
+		self change.
+
+		newChild := self visitWithTokenVisitor: node child children second child. "Oups, what a chain"
+		newWs := self visitWithTokenVisitor: node child children first.
+
+		^ PPCTrimmingTokenNode new
+			name: node name;
+			child: newChild;
+			tokenClass: node child children second tokenClass;
+			whitespace: newWs;
+			yourself.
+	].
+
+	^ super visitActionNode: node
+!
+
+visitNotNode: node
+	"We don't need result of the not,..."
+	| child newChild |
+	self change.
+	child := node child.
+	newChild := self visitWithTokenVisitor: child.
+	node replace: child with: newChild.
+	^ node
+!
+
+visitTokenNode: node
+	| child newChild |
+	
+	self change.
+	child := node child.
+	newChild := self visitWithTokenVisitor: child.
+	node replace: child with: newChild.
+	
+	^ node
+!
+
+visitWithTokenVisitor: node
+	| retval forbiddenNodes copyVisitor tokenVisitor |
+	
+	copyVisitor := PPCCopyVisitor new.
+	tokenVisitor := PPCTokenVisitor new.
+	
+	forbiddenNodes := openSet copy.
+	tokenVisitor forbiddenNodes: forbiddenNodes.
+
+	retval := copyVisitor visit: node.
+	retval := tokenVisitor visit: retval.
+	^ retval
+! !
+