compiler/PPCTokenizingVisitor.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 05 Jun 2015 00:05:08 +0100
changeset 484 e829f3860745
parent 461 5986bf6d7d60
child 503 ff58cd9f1f3c
permissions -rw-r--r--
Do not create intermediate collection when parsing sequence if not necesary. The collection is not needed when the result of a choice is being used in mapped parser. In that case, store parsed objects in variables and inline action code to use these variables to access parsed objects.

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

"{ NameSpace: Smalltalk }"

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


!PPCTokenizingVisitor methodsFor:'hooks'!

afterAccept: node retval: parserNode
    self isRoot ifTrue: [ 
        | tokenizerNode whitespaceNode |
        self change.
        tokens addLast: self eofToken.
        tokens do: [ :token | token unmarkForInline  ].
        
        whitespaceNode := tokens detect: [ :e | e isTrimmingTokenNode ] ifNone:[nil]. 
        whitespaceNode notNil ifTrue:[
            whitespaceNode := whitespaceNode whitespace copy
                                unmarkForInline;
                                name: 'consumeWhitespace';
                                yourself 
        ] ifFalse:[
            whitespaceNode := (PPCNilNode new)
                                name: 'consumeWhitespace';
                                yourself
        ].        
        tokenizerNode := PPCTokenChoiceNode new
            children: tokens asArray;
            name: 'nextToken';
            yourself.
    
        ^ PPCTokenizingParserNode new
            parser: parserNode;
            tokenizer: tokenizerNode;
            whitespace: whitespaceNode;
            name: #'mainParser';
            yourself
    ].
    ^ parserNode

    "Modified: / 12-05-2015 / 01:37:57 / 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.
! !

!PPCTokenizingVisitor class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !