compiler/PPCTokenDetector.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 17 Aug 2015 23:11:56 +0100
changeset 518 a6d8b93441b0
parent 515 b5316ef15274
child 524 f6f68d32de73
permissions -rw-r--r--
Portability fixes * do not use Object>>asString. Not all Smalltalks implement it. * do not use Object>>name. Not all Smalltalks implement it. * do not use Dictionary keysAndValuesRemove:. Not all Smalltalks implement it. * do not use Class>>methods The semantics is different among Smalltalks. Use `Class methodDictionary values` instead. * do not modify dictionary in #at:ifAbsentPut: block!

"{ Package: 'stx:goodies/petitparser/compiler' }"

"{ NameSpace: Smalltalk }"

PPCRewritingVisitor subclass:#PPCTokenDetector
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Visitors'
!

!PPCTokenDetector methodsFor:'as yet unclassified'!

visitActionNode: node	

    (node hasProperty: #trimmingToken) ifTrue: [ 
        | child whitespace |
        self change.
        child := self visitWithTokenVisitor: node child secondChild.
        whitespace := self visitWithTokenVisitor: node child firstChild.
        
        ^ PPCTrimmingTokenNode new
            name: node name;
            child: child;
            whitespace: whitespace;
            tokenClass: node child secondChild tokenClass;
            properties: node properties copy;
            yourself.
    ].

    ^ super visitActionNode: node
!

visitTokenNode: node	
    | child newChild |
    self change.
    child := node child.
    newChild := self visitWithTokenVisitor: node child.
    node replace: child with: newChild.
    
    ^ node
!

visitTrimNode: node
    self visitChildren: node.

    (node child isKindOf: PPCTokenNode) ifTrue: [  
        self change.
        ^ PPCTrimmingTokenNode new
            name: node name;
            child: node child child;
            tokenClass: node child tokenClass;
            whitespace: node trimmer;
            parser: node parser;
            yourself
    ]. 

    (node child isKindOf: PPCTokenConsumeNode) ifTrue: [  
        self change.
        self halt: 'JK: this can happen???'.
        ^ PPCTrimmingTokenNode new
            name: node name;
            child: node child;
            tokenClass: node child child tokenClass;
            whitespace: node trimmer;
            yourself
    ]. 


    ^ 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
! !