mercurial/HGCommandParser.st
changeset 80 8f300696b26b
parent 77 b6070a017acd
child 88 1ad71a063a20
--- a/mercurial/HGCommandParser.st	Sat Nov 17 20:20:35 2012 +0000
+++ b/mercurial/HGCommandParser.st	Mon Nov 19 21:57:45 2012 +0000
@@ -82,6 +82,18 @@
     "Modified: / 13-11-2012 / 17:28:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+parseInteger
+    "Parses integer from stream and returns it"
+
+    ^Integer readFrom: stream onError:[self error: 'integer value expected']
+
+    "
+    (HGCommandParser on: '12 34' readStream) parseInteger; skipSeparators; parseInteger
+    "
+
+    "Created: / 19-11-2012 / 20:05:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 parseLog
     "Parse output of 'hg log' command, assuming the template given
      was HGCommandParser templateLog. Return a list of HGRevision."
@@ -108,12 +120,12 @@
     | rev line message |
 
     rev := HGChangeset new.
-    rev setId: self parseNodeId. self nextLineEnd.
+    rev setId: self parseNodeId. self expectLineEnd.
     rev setBranch: self nextLine.
-    rev setParent1Id: self parseNodeId. self nextSpace.
-    rev setParent2Id: self parseNodeId. self nextSpace. self nextLineEnd.
+    rev setParent1Id: self parseNodeId. self expectSpace.
+    rev setParent2Id: self parseNodeId. self expectSpace. self expectLineEnd.
     rev setAuthor: self nextLine.
-    rev setTimestamp: self parseDate. self nextLineEnd.
+    rev setTimestamp: self parseDate. self expectLineEnd.
     message := String streamContents:[:s|
         [ line := self nextLine . line = '**EOE**' ] whileFalse:[
             s nextPutLine: line
@@ -124,7 +136,7 @@
     ^rev
 
     "Created: / 13-11-2012 / 09:45:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 13-11-2012 / 17:29:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-11-2012 / 20:12:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 parseNodeId
@@ -189,17 +201,90 @@
         | status path |
 
         status := HGStatus forCode: self next.
-        self nextSpace.
+        self expectSpace.
         path := self nextLine.
         statusesAndPaths add: { status . path }
     ].
     ^ statusesAndPaths
 
     "Created: / 23-10-2012 / 10:57:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-11-2012 / 20:06:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+parseCommandVersion
+    "Parse output of 'hg --version'"
+
+    "
+    Mercurial Distributed SCM (version 2.3.2)
+    (see http://mercurial.selenic.com for more information)
+    
+    Copyright (C) 2005-2012 Matt Mackall and others
+    This is free software; see the source for copying conditions. There is NO
+    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+    "
+
+    | major minor revision |
+
+    self 
+        expect:'Mercurial'; skipSeparators;
+        expect:'Distributed'; skipSeparators;
+        expect:'SCM'; skipSeparators;
+        expect:$(; skipSeparators;
+        expect:'version'.
+
+    major := self parseInteger.
+    self expect:$..
+    minor := self parseInteger.
+    stream peek == $. ifTrue:[
+        stream next.
+        revision := self parseInteger.
+    ].
+
+    self skipSeparators.
+    self expect:$).
+
+    ^(Array with: major with: minor with: revision)
+
+    "Created: / 19-11-2012 / 20:19:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !HGCommandParser methodsFor:'parsing-utils'!
 
+expect: aStringOrChar
+
+    | c |
+    aStringOrChar isCharacter ifTrue:[
+        (stream atEnd or:[(c := stream next) ~= aStringOrChar]) ifTrue:[
+            self error:('Expected ''%1'' got ''%2''.' bindWith: aStringOrChar with: c).
+        ].
+        ^self.
+    ].
+    aStringOrChar isString ifTrue:[
+        aStringOrChar do:[:expected|
+            (stream atEnd or:[(c := stream next) ~= expected]) ifTrue:[
+                self error:('Expected ''%1''.' bindWith: aStringOrChar).
+            ].
+        ].
+        ^self.
+    ].
+
+    self error:'Invalid expected value'.
+
+    "Created: / 19-11-2012 / 20:08:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+expectLineEnd
+    self expect: Character cr.
+
+    "Created: / 19-11-2012 / 20:06:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+expectSpace
+    self expect: Character space.
+
+    "Created: / 19-11-2012 / 20:06:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 next
     ^stream next.
 
@@ -213,25 +298,10 @@
     "Modified: / 09-11-2012 / 12:02:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-nextLineEnd
-    | c |
-    ((c := stream peek) ~= Character cr) ifTrue:[
-        self error:'New line expected. ''', c , ''' found!!'
-    ].
-    stream next.
+skipSeparators
+    stream skipSeparators
 
-    "Created: / 13-11-2012 / 10:17:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 13-11-2012 / 17:18:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-nextSpace
-    | c |
-    ((c := stream next) ~= Character space) ifTrue:[
-        self error:'Space expected. ''', c , ''' found!!'
-    ]
-
-    "Created: / 23-10-2012 / 10:58:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 09-11-2012 / 11:59:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 19-11-2012 / 20:05:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !HGCommandParser class methodsFor:'documentation'!