compiler/PPCTokenizingVisitor.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 10 May 2015 14:20:24 +0100
changeset 454 a9cd5ea7cc36
parent 452 9f4558b3be66
child 460 87a3d30ab570
permissions -rw-r--r--
Portability: fixes for Smalltalk/X * Do not use detect:ifFound: - not present in Smalltalk/X * Removed leftover debugging code (Halt if:, ...) * Do not use `aClass methods`, use `aClass methodDictionary values` * Do not use #allPairsDo; - not present in Smalltalk/X * Do not use #crShow: - not present in Smalltalk/X * On Smalltalk?X use Filename - there's no FileReference in Smalltalk/X * Do not use CharacterSet, use String

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

"{ NameSpace: Smalltalk }"

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

!PPCTokenizingVisitor methodsFor:'hooks'!

afterAccept: node retval: retval
    self isRoot ifTrue: [ 
        | tokenizerNode |
        self change.
        tokens addLast: self eofToken.
        tokens do: [ :token | token unmarkForInline  ].
        
        tokenizerNode := PPCTokenChoiceNode new
            children: tokens asArray;
            name: 'nextToken';
            yourself.
    
        ^ PPCTokenizingParserNode new
            parser: retval;
            tokenizer: tokenizerNode;
            name: #'mainParser';
            yourself
    ].
    ^ retval

    "Modified: / 10-05-2015 / 07:27:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

eofToken
    | ws  |
    ws := PPCStarNode new
        child: (PPCMessagePredicateNode new
            message: #isSeparator;
            yourself);
        yourself.
    
    ^ PPCTrimmingTokenNode new
        child: PPCEndOfFileNode new;
        whitespace: ws;
        tokenClass: PPToken;
        yourself.
! !

!PPCTokenizingVisitor methodsFor:'initialization'!

initialize
    super initialize.
    tokens := OrderedCollection new.
! !

!PPCTokenizingVisitor methodsFor:'testing'!

isRoot
    ^ openSet size = 1
! !

!PPCTokenizingVisitor methodsFor:'tokens'!

addToken: token
    (tokens contains: [:e | e == token] ) ifFalse: [ 
        tokens addLast: token
    ]
! !

!PPCTokenizingVisitor methodsFor:'visiting'!

visitActionNode: node
    (node hasProperty: #trimmingToken) ifTrue: [ 
        self change.
        self addToken: node.
        
        ^ PPCTokenConsumeNode new
            child: node;
            yourself	
    ].

    ^ super visitActionNode: node
!

visitTokenNode: node
    self change.
    self addToken: node.
    
    ^ PPCTokenConsumeNode new
        child: node;
        yourself.
!

visitTrimmingTokenNode: node
    self change.
    self addToken: node.
    
    ^ PPCTokenConsumeNode new
        child: node;
        yourself.
! !