StataScriptLogParser.st
changeset 0 da028ec9cc07
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StataScriptLogParser.st	Fri Mar 13 22:51:48 2015 +0000
@@ -0,0 +1,107 @@
+"{ Package: 'jv:statascripteditor' }"
+
+"{ NameSpace: Smalltalk }"
+
+Object subclass:#StataScriptLogParser
+	instanceVariableNames:'input handler'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Stata Script Editor'
+!
+
+!StataScriptLogParser methodsFor:'accessing'!
+
+handler: anObject
+    handler := anObject
+
+    "Created: / 10-02-2015 / 22:16:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!StataScriptLogParser methodsFor:'parsing'!
+
+parse: aStream
+    input := aStream.
+    self parse.
+
+    "Created: / 10-02-2015 / 21:54:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!StataScriptLogParser methodsFor:'parsing - private'!
+
+parse
+    [ input atEnd ] whileFalse:[ 
+        input peek == ${ ifTrue:[ 
+            self parseTag
+        ] ifFalse:[ 
+            self parseCharacterData
+        ].
+    ].
+
+    "Created: / 10-02-2015 / 21:54:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+parseCharacterData
+    | c b |
+
+    b := String new writeStream.
+    [ input atEnd not and:[ (c := input peek) ~~ ${ and:[c ~~ Character cr ] ] ] whileTrue:[ 
+        b nextPut: c.
+        input next.     
+    ].
+    c == Character cr ifTrue:[ 
+        input next.
+    ].
+    handler characterData: b contents.
+
+    "Created: / 10-02-2015 / 22:04:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 11-02-2015 / 00:40:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+parseTag
+    | c b |
+
+    self expect: ${.
+    b := String new writeStream.
+    [ (c := input peek) ~~ $} ] whileTrue:[ 
+        b nextPut: c.
+        input next.     
+    ].
+    input next. "/ eat closing $}
+    handler tag: b contents.
+
+    "Created: / 10-02-2015 / 22:04:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 11-02-2015 / 00:37:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!StataScriptLogParser methodsFor:'parsing - utils'!
+
+expect: aStringOrChar
+
+    | c |
+    aStringOrChar isCharacter ifTrue:[
+        (input atEnd or:[(c := input next) ~= aStringOrChar]) ifTrue:[
+            self parseError:('Expected ''%1'' got ''%2''.' bindWith: aStringOrChar with: c).
+        ].
+        ^self.
+    ].
+    aStringOrChar isString ifTrue:[
+        aStringOrChar do:[:expected|
+            (input atEnd or:[(c := input next) ~= expected]) ifTrue:[
+                self parseError:('Expected ''%1''.' bindWith: aStringOrChar).
+            ].
+        ].
+        ^self.
+    ].
+
+    self parseError:'Invalid expected value'.
+
+    "Created: / 19-11-2012 / 20:08:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 10-02-2015 / 22:08:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+parseError: message
+    ^ self error: message
+
+    "Created: / 10-02-2015 / 22:08:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+