This is the third article in the Sproto series, you can refer to the previous "Add Python bindings for Sproto", "Add map support for Python-sproto".
Sproto is a cloud-inspired serialization protocol designed to efficiently package and unpack game protocol data. A bit like Google's protobuf, but faster than PROTOBUF. The structure is somewhat similar to the CAP ' n Proto, but is not intended to be used directly as a memory organization, so there is less data-aligned parts. The current usage scenario is mainly on the RPC protocol of the game client and server side.
The interesting point of Sproto is that it can be self-describing, describing itself with the Sproto protocol itself: (Excerpt from the Cloud blog)
. type {. field {name0:stringtype1:stringID2: integer array3: boolean} name0:string Fields1: *Field}.protocol {Name0:stringID1: Integer Request2:stringResponse3:string}.group {type0: *Type Protocol1: *protocol}
Such a simple structure, just to get to practice handwriting parser. Lua's Lpeg library was so powerful that he tried pypeg2 to solve the problem first. The process of the attempt see: Https://github.com/spin6lock/sproto_python_parser, finally failed. In retrospect, the peg grammar is quite similar to the following unrelated grammar, except that there is no ambiguity, and the next parse tree can be fully determined by a token. This is a bit similar to the recursive descent method, and then it is very handy: https://github.com/spin6lock/yapsp
The code is mainly divided into two parts, lexer and parser. The token identified by Lexer can be found in the constant definition, which is basically done with regular expressions and takes only a little time. Parser spent a good period, mainly forget to encapsulate some convenient functions to use, such as expecting and optional. Parser implementation is a recursive descent method based on the parser, because the syntax of Sproto is particularly simple, so you can peek into the next token, then know what the next to parse the structure, and then call the corresponding method to parse.
Handwritten a Python parser for Sproto