Handwritten a Python parser for Sproto

Source: Internet
Author: User
Tags constant definition lexer

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.