SmallSense__TokenPatternParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 09 May 2014 15:22:12 +0100
changeset 202 5c8610dad14c
parent 200 SmallSense__TokenExpressionParser.st@12d6a2f82d95
child 203 c70b7351eda6
permissions -rw-r--r--
TokenExpression* renamed to TokenPattern*

"{ Package: 'jv:smallsense' }"

"{ NameSpace: SmallSense }"

Object subclass:#TokenPatternParser
	instanceVariableNames:'source stream'
	classVariableNames:''
	poolDictionaries:''
	category:'SmallSense-Utils-Matcher'
!

!TokenPatternParser class methodsFor:'documentation'!

documentation
"
    A parser to parse token patterns

    [author:]
        Jan Vrany <jan.vrany@fit.cvut.cz>

    [instance variables:]

    [class variables:]

    [see also:]

"
! !

!TokenPatternParser class methodsFor:'parsing'!

parse: anArrayOrStream
    ^ self new parse: anArrayOrStream

    "Created: / 02-05-2014 / 18:56:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TokenPatternParser methodsFor:'parsing'!

parse: anArrayOrStream
    source := anArrayOrStream readStream.
    ^ self parse.

    "Created: / 02-05-2014 / 18:56:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 02-05-2014 / 21:27:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TokenPatternParser methodsFor:'parsing-private'!

expect: token
    source peek ~~ token ifTrue:[ 
        self error:'Expeciting ', token printString, ' got ' , source peek printString
    ].

    "Created: / 02-05-2014 / 19:05:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

parse
    ^ self parseRegex

    "Created: / 02-05-2014 / 18:56:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

parseAtom
    | peek type value |

    peek := source peek.
    peek isSymbol ifTrue:[ 
        type := source next.
        source peek == #'->' ifTrue:[ 
            source next.
            source peek isString ifFalse:[ 
                self error: 'Expecting token value'.
            ] ifTrue:[ 
                value := source next.
            ].
        ].
        ^ TokenPatternNode new initializeTyoe: type value: value.
    ].
    peek isCharacter ifTrue:[ 
        type := source next.
        ^ TokenPatternNode new initializeTyoe: type value: nil.                                
    ].
    peek isArray ifTrue:[ 
        | savedSource newSource subRegex |

        newSource := source next readStream.
        savedSource := source.
        source := newSource.
        [ 
            subRegex := self parseRegex.
        ] ensure:[ 
            source := savedSource
        ].
        ^ subRegex
    ].
    self error:'Unknown atom type'.

    "Created: / 02-05-2014 / 21:17:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 09-05-2014 / 15:16:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

parseBranch

    | piece branch |

    piece := self parsePiece.
    branch := (source peek isNil or:[ source peek == #'||' ]) 
        ifTrue:[ nil ]
        ifFalse:[ self parseBranch ].
    ^ Regex::RxsBranch new 
        initializePiece: piece 
        branch: branch

    "Created: / 02-05-2014 / 19:06:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 02-05-2014 / 21:11:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

parsePiece
    | atom peek |

    atom := self parseAtom.
    peek := source peek.
    peek == $* ifTrue:[ 
        source next.
        ^ Regex::RxsPiece new initializeStarAtom: atom.  
    ].
    peek == $+ ifTrue:[ 
        source next.
        ^ Regex::RxsPiece new initializePlusAtom: atom.  
    ].
    peek == $? ifTrue:[ 
        source next.
        ^ Regex::RxsPiece new initializeOptionalAtom: atom.  
    ].
     ^Regex::RxsPiece new initializeAtom: atom

    "Created: / 02-05-2014 / 21:11:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

parseRegex
    | regex branch |

    branch := self parseBranch.
    source atEnd ifTrue:[ 
        regex := nil.
    ] ifFalse:[
        self expect: $|.
        regex := self parseRegex.
    ].
    ^ Regex::RxsRegex new initializeBranch: branch regex: regex

    "Created: / 02-05-2014 / 18:59:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !