mercurial/HGCommandParser.st
changeset 40 e3699c0b00f9
parent 39 10e693b3e034
child 51 61700cf82743
equal deleted inserted replaced
39:10e693b3e034 40:e3699c0b00f9
     8 !
     8 !
     9 
     9 
    10 
    10 
    11 !HGCommandParser class methodsFor:'instance creation'!
    11 !HGCommandParser class methodsFor:'instance creation'!
    12 
    12 
    13 on: aStream
    13 on: aStringOrStream
    14     ^self new stream: aStream readStream
    14     | stream |
       
    15 
       
    16     stream := aStringOrStream isStream 
       
    17                 ifTrue:[aStringOrStream]
       
    18                 ifFalse:[aStringOrStream readStream].
       
    19 
       
    20     ^self new stream: stream
    15 
    21 
    16     "Created: / 23-10-2012 / 11:07:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    22     "Created: / 23-10-2012 / 11:07:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    17     "Modified (format): / 13-11-2012 / 11:02:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    23     "Modified: / 13-11-2012 / 16:36:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    18 ! !
    24 ! !
    19 
    25 
    20 !HGCommandParser class methodsFor:'templates'!
    26 !HGCommandParser class methodsFor:'templates'!
    21 
    27 
    22 templateLog
    28 templateLog
    25 {branch}
    31 {branch}
    26 {parents}
    32 {parents}
    27 {author}
    33 {author}
    28 {date|isodate}
    34 {date|isodate}
    29 {desc}
    35 {desc}
    30 **EOE**'
    36 **EOE**
       
    37 '
    31 
    38 
    32     "Created: / 12-11-2012 / 23:06:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    39     "Created: / 12-11-2012 / 23:06:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    33     "Modified: / 13-11-2012 / 10:21:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    40     "Modified: / 13-11-2012 / 17:18:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    34 ! !
    41 ! !
    35 
    42 
    36 !HGCommandParser methodsFor:'accessing'!
    43 !HGCommandParser methodsFor:'accessing'!
    37 
    44 
    38 stream
    45 stream
    55 ! !
    62 ! !
    56 
    63 
    57 !HGCommandParser methodsFor:'parsing'!
    64 !HGCommandParser methodsFor:'parsing'!
    58 
    65 
    59 parseDate
    66 parseDate
    60     self shouldImplement
    67     | ts |
       
    68     ts := Timestamp readIso8601FormatFrom:stream.
       
    69     (stream peek == $+ or:[stream peek == $-]) ifFalse:[
       
    70         self error:'Timezone expected, ' , stream peek , ' found'
       
    71     ].
       
    72     stream next.
       
    73     4 timesRepeat:[
       
    74         ('0123456789' includes: stream peek) ifFalse:[
       
    75             self error:'Timezone expected, ' , stream peek , ' found'
       
    76         ].
       
    77         stream next.
       
    78     ].
       
    79     ^ts
    61 
    80 
    62     "Created: / 13-11-2012 / 10:22:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    81     "Created: / 13-11-2012 / 10:22:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    82     "Modified: / 13-11-2012 / 17:28:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    63 !
    83 !
    64 
    84 
    65 parseLog
    85 parseLog
    66     "Parse output of 'hg log' command, assuming the template given
    86     "Parse output of 'hg log' command, assuming the template given
    67      was HGCommandParser templateLog. Return a list of HGRevision."
    87      was HGCommandParser templateLog. Return a list of HGRevision."
    85     "Parse single revision entry, assuming the template given
   105     "Parse single revision entry, assuming the template given
    86      was HGCommandParser templateLog. Return a HGRevision."
   106      was HGCommandParser templateLog. Return a HGRevision."
    87 
   107 
    88     | rev line message |
   108     | rev line message |
    89 
   109 
    90     rev := HGRevision new.
   110     rev := HGChangeset new.
    91     rev setId: self parseNodeId. self nextLineEnd.
   111     rev setId: self parseNodeId. self nextLineEnd.
    92     rev setBranch: self nextLine.
   112     rev setBranch: self nextLine.
    93     rev setParent1Id: self parseNodeId. self nextSpace.
   113     rev setParent1Id: self parseNodeId. self nextSpace.
    94     rev setParent2Id: self parseNodeId. self nextLineEnd.
   114     rev setParent2Id: self parseNodeId. self nextSpace. self nextLineEnd.
    95     rev setAuthor: self nextLine.
   115     rev setAuthor: self nextLine.
    96     rev setDate: self parseDate. self nextLineEnd.
   116     rev setTimestamp: self parseDate. self nextLineEnd.
    97     message := String streamContents:[:s|
   117     message := String streamContents:[:s|
    98         [ line := self nextLine . line = '**EOE**' ] whileFalse:[
   118         [ line := self nextLine . line = '**EOE**' ] whileFalse:[
    99             s nextPutLine: line
   119             s nextPutLine: line
   100         ].
   120         ].
   101     ].
   121     ].
   102     rev setMessage: message
   122     rev setMessage: message.
       
   123 
       
   124     ^rev
   103 
   125 
   104     "Created: / 13-11-2012 / 09:45:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   126     "Created: / 13-11-2012 / 09:45:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   127     "Modified: / 13-11-2012 / 17:29:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   105 !
   128 !
   106 
   129 
   107 parseNodeId
   130 parseNodeId
   108     "Parses node id from stream and returns it. Support both,
   131     "Parses node id from stream and returns it. Support both,
   109      short and full node ids"
   132      short and full node ids"
   110 
   133 
   111     | c c2 sign short revno hash |
   134     ^HGNodeId readFrom: stream onError:[:msg|self error: msg]
   112 
   135 
   113     stream peek == $- ifTrue:[        
   136 
   114         stream next.
       
   115         sign := -1.
       
   116     ] ifFalse:[
       
   117         sign := 1.
       
   118     ].
       
   119 
       
   120     "/ Read revno...
       
   121     revno := 0.
       
   122     [ (c := stream peek) == $: ] whileFalse:[
       
   123         c isDigit ifFalse:[
       
   124             self error:'Digit ([0-9]) expected but ', c , 'found'.
       
   125         ].
       
   126         revno := (revno * 10) + c digitValue.
       
   127         stream next.
       
   128     ].
       
   129     stream next. "/eat :
       
   130     revno := revno * sign.
       
   131 
       
   132     "/ Read hash
       
   133     hash := ByteArray new: 20.
       
   134     short := true.
       
   135     1 to: 6 do:[:i|
       
   136         stream atEnd ifTrue:[
       
   137             self error:'Unexpected end of stream, hex digit expected'.
       
   138         ].
       
   139         c := stream peek.
       
   140         c isHexDigit ifFalse:[
       
   141             self error:'Hex digit ([0-9a-z]) expected but ', c , ' found'.
       
   142         ].
       
   143         stream next.
       
   144         stream atEnd ifTrue:[
       
   145             self error:'Unexpected end of stream, hex digit expected'.
       
   146         ].
       
   147         c2 := stream peek.
       
   148         c isHexDigit ifFalse:[
       
   149             self error:'Hex digit ([0-9a-z]) expected but ', c , ' found'.
       
   150         ].
       
   151         hash at:i put: (c digitValue << 4) + c2 digitValue.
       
   152         stream next.
       
   153     ].
       
   154     stream peek isHexDigit ifTrue:[
       
   155         "/OK, full 40-char node id
       
   156         short := false.
       
   157         7 to: 20 do:[:i|
       
   158             stream atEnd ifTrue:[
       
   159                 self error:'Unexpected end of stream, hex digit expected'.
       
   160             ].
       
   161                         c := stream peek.
       
   162             c isHexDigit ifFalse:[
       
   163                 self error:'Hex digit ([0-9a-z]) expected but ', c , ' found'.
       
   164             ].
       
   165             stream next.
       
   166             stream atEnd ifTrue:[
       
   167                 self error:'Unexpected end of stream, hex digit expected'.
       
   168             ].
       
   169             c2 := stream peek.
       
   170             c isHexDigit ifFalse:[
       
   171                 self error:'Hex digit ([0-9a-z]) expected but ', c , ' found'.
       
   172             ].
       
   173             hash at:i put: (c digitValue << 4) + c2 digitValue.
       
   174             stream next.
       
   175         ].
       
   176     ].
       
   177     ^short ifTrue:[
       
   178         self halt: 'Not yet supported'
       
   179     ] ifFalse:[
       
   180         (HGNodeId fromBytes: hash) revno: revno.
       
   181     ]
       
   182 
   137 
   183     "
   138     "
   184         (HGCommandParser on: '4:6f88e1f44d9eb86e0b56ca15e30e5d786acd83c7' readStream) parseNodeId
   139         (HGCommandParser on: '4:6f88e1f44d9eb86e0b56ca15e30e5d786acd83c7' readStream) parseNodeId
   185 
   140 
   186         Bad ones:
   141         Bad ones:
   189         (HGCommandParser on: '4:6f88Z1f44d9eb86e0b56ca15e30e5d786acd83c7' readStream) parseNodeId
   144         (HGCommandParser on: '4:6f88Z1f44d9eb86e0b56ca15e30e5d786acd83c7' readStream) parseNodeId
   190 
   145 
   191     "
   146     "
   192 
   147 
   193     "Created: / 13-11-2012 / 10:22:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   148     "Created: / 13-11-2012 / 10:22:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   149     "Modified: / 13-11-2012 / 16:52:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   194 ! !
   150 ! !
   195 
   151 
   196 !HGCommandParser methodsFor:'parsing - commands'!
   152 !HGCommandParser methodsFor:'parsing - commands'!
   197 
   153 
   198 parseCommandLog
   154 parseCommandLog
   235     "Modified: / 09-11-2012 / 12:02:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   191     "Modified: / 09-11-2012 / 12:02:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   236 !
   192 !
   237 
   193 
   238 nextLineEnd
   194 nextLineEnd
   239     | c |
   195     | c |
   240     ((c := stream next) ~= Character cr) ifTrue:[
   196     ((c := stream peek) ~= Character cr) ifTrue:[
   241         self error:'New line expected. ''', c , ''' found!!'
   197         self error:'New line expected. ''', c , ''' found!!'
   242     ]
   198     ].
       
   199     stream next.
   243 
   200 
   244     "Created: / 13-11-2012 / 10:17:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   201     "Created: / 13-11-2012 / 10:17:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   202     "Modified: / 13-11-2012 / 17:18:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   245 !
   203 !
   246 
   204 
   247 nextSpace
   205 nextSpace
   248     | c |
   206     | c |
   249     ((c := stream next) ~= Character space) ifTrue:[
   207     ((c := stream next) ~= Character space) ifTrue:[