StataScriptLogParser.st
changeset 0 da028ec9cc07
equal deleted inserted replaced
-1:000000000000 0:da028ec9cc07
       
     1 "{ Package: 'jv:statascripteditor' }"
       
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
       
     5 Object subclass:#StataScriptLogParser
       
     6 	instanceVariableNames:'input handler'
       
     7 	classVariableNames:''
       
     8 	poolDictionaries:''
       
     9 	category:'Stata Script Editor'
       
    10 !
       
    11 
       
    12 !StataScriptLogParser methodsFor:'accessing'!
       
    13 
       
    14 handler: anObject
       
    15     handler := anObject
       
    16 
       
    17     "Created: / 10-02-2015 / 22:16:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    18 ! !
       
    19 
       
    20 !StataScriptLogParser methodsFor:'parsing'!
       
    21 
       
    22 parse: aStream
       
    23     input := aStream.
       
    24     self parse.
       
    25 
       
    26     "Created: / 10-02-2015 / 21:54:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    27 ! !
       
    28 
       
    29 !StataScriptLogParser methodsFor:'parsing - private'!
       
    30 
       
    31 parse
       
    32     [ input atEnd ] whileFalse:[ 
       
    33         input peek == ${ ifTrue:[ 
       
    34             self parseTag
       
    35         ] ifFalse:[ 
       
    36             self parseCharacterData
       
    37         ].
       
    38     ].
       
    39 
       
    40     "Created: / 10-02-2015 / 21:54:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    41 !
       
    42 
       
    43 parseCharacterData
       
    44     | c b |
       
    45 
       
    46     b := String new writeStream.
       
    47     [ input atEnd not and:[ (c := input peek) ~~ ${ and:[c ~~ Character cr ] ] ] whileTrue:[ 
       
    48         b nextPut: c.
       
    49         input next.     
       
    50     ].
       
    51     c == Character cr ifTrue:[ 
       
    52         input next.
       
    53     ].
       
    54     handler characterData: b contents.
       
    55 
       
    56     "Created: / 10-02-2015 / 22:04:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    57     "Modified: / 11-02-2015 / 00:40:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    58 !
       
    59 
       
    60 parseTag
       
    61     | c b |
       
    62 
       
    63     self expect: ${.
       
    64     b := String new writeStream.
       
    65     [ (c := input peek) ~~ $} ] whileTrue:[ 
       
    66         b nextPut: c.
       
    67         input next.     
       
    68     ].
       
    69     input next. "/ eat closing $}
       
    70     handler tag: b contents.
       
    71 
       
    72     "Created: / 10-02-2015 / 22:04:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    73     "Modified: / 11-02-2015 / 00:37:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    74 ! !
       
    75 
       
    76 !StataScriptLogParser methodsFor:'parsing - utils'!
       
    77 
       
    78 expect: aStringOrChar
       
    79 
       
    80     | c |
       
    81     aStringOrChar isCharacter ifTrue:[
       
    82         (input atEnd or:[(c := input next) ~= aStringOrChar]) ifTrue:[
       
    83             self parseError:('Expected ''%1'' got ''%2''.' bindWith: aStringOrChar with: c).
       
    84         ].
       
    85         ^self.
       
    86     ].
       
    87     aStringOrChar isString ifTrue:[
       
    88         aStringOrChar do:[:expected|
       
    89             (input atEnd or:[(c := input next) ~= expected]) ifTrue:[
       
    90                 self parseError:('Expected ''%1''.' bindWith: aStringOrChar).
       
    91             ].
       
    92         ].
       
    93         ^self.
       
    94     ].
       
    95 
       
    96     self parseError:'Invalid expected value'.
       
    97 
       
    98     "Created: / 19-11-2012 / 20:08:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    99     "Modified: / 10-02-2015 / 22:08:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   100 !
       
   101 
       
   102 parseError: message
       
   103     ^ self error: message
       
   104 
       
   105     "Created: / 10-02-2015 / 22:08:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   106 ! !
       
   107