Skip to main content

a thrift parser (antlr4)

Project description

thrift-parser

pip install thrift-parser

Thrift.g4 is from https://github.com/antlr/grammars-v4

parse

from thrift_parser import parse_file

lexer, tokens, parser, document = parse_file('tutorial.thrift')

or smiple

from thrift_parser import ThriftData

thrift_data = ThriftData.from_file('tutorial.thrift')

print(thrift_data.tokens[0].text)
print(thrift_data.document.children)

or you can try antlr's way

from antlr4 import FileStream
from antlr4 import CommonTokenStream
from antlr4 import ParserRuleContext

from thrift_parser.ThriftLexer import ThriftLexer
from thrift_parser.ThriftParser import ThriftParser


def main(file):
    input_stream = FileStream(file, encoding='utf8')
    lexer = ThriftLexer(input_stream)
    stream = CommonTokenStream(lexer)
    parser = ThriftParser(stream)
    ctx = ParserRuleContext()

    parser.enterRule(ctx, 0, 0)
    document = parser.document()
    return document

Why ?

python thrift parser --> thrift-fmt --> auto format my thrift files in one style

TODO

  1. more ast operate, fake_token, fake_context
  2. other language?

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thrift-parser-1.0.7.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

thrift_parser-1.0.7-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

Details for the file thrift-parser-1.0.7.tar.gz.

File metadata

  • Download URL: thrift-parser-1.0.7.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.4.8 CPython/3.10.6

File hashes

Hashes for thrift-parser-1.0.7.tar.gz
Algorithm Hash digest
SHA256 c5c444ea5d8f07349d275756fc469d1b891ebc58b2b3f8adddfe8ff44b7c94c7
MD5 c96f9a7475a7ab8c8713cc8e17393508
BLAKE2b-256 bbcc5ab9c47474732133a7afa4cdd785dde54098f10e31e72886e8042574eddb

See more details on using hashes here.

File details

Details for the file thrift_parser-1.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for thrift_parser-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 8c02fd7df41dad79d1ced1ea364d3a9164179215e78339198388970338961853
MD5 95d3b29447c0ff1a6cd37f79f7adc3e7
BLAKE2b-256 650691063c22e0534aa215cb443441791d2c0f0828146e55d6ad9a82b488f6fd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page