{"id":13419558,"url":"https://github.com/vinzenz/libpypa","last_synced_at":"2025-03-15T05:31:39.837Z","repository":{"id":17684149,"uuid":"20490030","full_name":"vinzenz/libpypa","owner":"vinzenz","description":"libpypa is a Python parser implemented in pure C++","archived":true,"fork":false,"pushed_at":"2018-05-25T19:55:11.000Z","size":7233,"stargazers_count":189,"open_issues_count":5,"forks_count":48,"subscribers_count":24,"default_branch":"master","last_synced_at":"2024-07-31T22:49:51.267Z","etag":null,"topics":["ast","c-plus-plus","lexer","parse","parser-library","python"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vinzenz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-04T15:57:24.000Z","updated_at":"2024-07-17T03:17:56.000Z","dependencies_parsed_at":"2022-07-13T04:31:31.837Z","dependency_job_id":null,"html_url":"https://github.com/vinzenz/libpypa","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinzenz%2Flibpypa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinzenz%2Flibpypa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinzenz%2Flibpypa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinzenz%2Flibpypa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vinzenz","download_url":"https://codeload.github.com/vinzenz/libpypa/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243690112,"owners_count":20331726,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","c-plus-plus","lexer","parse","parser-library","python"],"created_at":"2024-07-30T22:01:17.680Z","updated_at":"2025-03-15T05:31:38.140Z","avatar_url":"https://github.com/vinzenz.png","language":"C++","funding_links":[],"categories":["TODO scan for Android support in followings"],"sub_categories":[],"readme":"[![Coverity Scan Build Status](https://scan.coverity.com/projects/3460/badge.svg)](https://scan.coverity.com/projects/3460)\n[![Build Status](https://travis-ci.org/vinzenz/libpypa.png?branch=master)](https://travis-ci.org/vinzenz/libpypa)\n\n[![Join the chat at https://gitter.im/vinzenz/libpypa](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/vinzenz/libpypa?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n# libpypa - A Python Parser Library in C++\n\n- [Introduction](#introduction)\n - [Motivation](#introduction-motivation)\n - [Goal](#introduction-goal)\n- [Example](#example)\n- [Error Reporting](#error-reporting)\n- [Requirements](#requirements)\n- [Structure](#structure)\n - [Lexer](#structure-lexer)\n - [Parser](#structure-parser)\n - [AST](#structure-ast)\n- [License](#license)\n\n\n## Introduction\n\u003ca name=\"introduction\"\u003e\n\n**libpypa** is a Python parser implemented in pure *C++*. It neither uses any\ntools like [flex][1], [yacc][2], [bison][3] etc, nor is it using any parser\nframework like [Boost.Spirit][4]. It's implementation is pure C++ code.\n\n\n### Motivation\n\u003ca name=\"introduction-motivation\"\u003e\n\nI started getting involved into the [pyston project][5] where it had an entry\nin their getting involved list for implementing a parser for Python. Never\nhaving properly tackled the problem of creating a parser library for any\nlanguage, I decided it might be worth a try, since most of the libraries I\nfound, where basically just using the builtin Python parser or where\nimplemented in Python itself.\n\n### Goal\n\u003ca name=\"introduction-goal\"\u003e\n\nThe first goal of the library is to support python 2.7 syntax, later on 3.x\nsyntax might be added.\n\n\n## Example\n\u003ca name=\"example\"\u003e\n\nAn example file:\n\n    $cat hello_world.py\n    #! /usr/bin/env python\n    # -*- coding: utf-8 -*-\n    #\n\n    \"\"\"\n        A \"Hello World\" example for the pypa parser\n    \"\"\"\n    import sys\n\n    print \u003e\u003e sys.stdout, \"Hello\", \"World!\"\n\n\nAnd here the output of the test parser:\n\n    $ ./parser-test hello_world.py\n    Parsing successfull\n\n    [Module]\n      - body:\n        [Suite]\n          - items: [\n                [DocString]\n                  - doc:\n        A \"Hello World\" example for the pypa parser\n\n\n                [Import]\n                  - names:\n                    [Alias]\n                      - as_name: \u003cNULL\u003e\n                      - name:\n                        [Name]\n                          - context: Load\n                          - dotted: False\n                          - id: sys\n\n                [Print]\n                  - destination:\n                    [Attribute]\n                      - attribute:\n                        [Name]\n                          - context: Load\n                          - dotted: False\n                          - id: stdout\n                      - context: Load\n                      - value:\n                        [Name]\n                          - context: Load\n                          - dotted: False\n                          - id: sys\n                  - newline: True\n                  - values: [\n                        [Str]\n                          - value: Hello\n\n                        [Str]\n                          - value: World!\n                        ]\n                ]\n      - kind: Module\n\nAnd here the parse tree of python: (astdump.py can be found in tools)\n\n    [Module]\n        - body: [\n\n            [Expr]\n                - value:\n                [Str]\n                    - s:\n        A \"Hello World\" example for the pypa parser\n\n\n            [Import]\n                - names: [\n\n                    [alias]\n                        - asname: None\n                        - name: sys\n                ]\n\n            [Print]\n                - dest:\n                [Attribute]\n                    - attr: stdout\n                    - ctx: Load\n                    - value:\n                    [Name]\n                        - ctx: Load\n                        - id: sys\n                - nl: True\n                - values: [\n\n                    [Str]\n                        - s: Hello\n\n                    [Str]\n                        - s: World!\n                ]\n        ]\n\n\n## Error Reporting\n\u003ca name=\"error-reporting\"\u003e\n\nThe parser supports also SyntaxError and IndentionError reporting:\n\nLet's take a look at this file `syntax_error.py` which clearly has a\nsyntax error:\n\n    #! /usr/bin/env python\n    # -*- coding: utf-8 -*-\n    \"\"\"\n        Syntax error example\n    \"\"\"\n\n    print x y z\n\nThis is the output of the test parser:\n\n    $./parser-test syntax_error.py\n      File \"syntax_error.py\", line 7\n        print x y z\n                ^\n    SyntaxError: Expected new line after statement\n    -\u003e Reported @pypa/parser/parser.cc:944 in bool pypa::simple_stmt(pypa::{anonymous}::State\u0026, pypa::AstStmt\u0026)\n\n    Parsing failed\n\nAnd this of cpython 2.7:\n\n    $ python syntax_error.py\n      File \"syntax_error.py\", line 7\n        print x y z\n                ^\n    SyntaxError: invalid syntax\n\n**libpypa** uses different error messages than python, however in the hopes that\nthat would increase the clarity.\n\n## Requirements\n\u003ca name=\"requirements\"\u003e\n\nTo be able using **libpypa**, you have to have a *C++11* compiler available.\n**libpypa** was developed on top of *g++ 4.8.2* and it heavily uses *C++11*\nfeatures where seen fit.\n\n**libpypa** currently does not depend on any other libraries than the *C++11*\nstandard library with the exception of the `class FileBuf` which currently\nuses system libraries, but might be changed to just use `fopen`/`fread`/\n`fclose`.\n\n## Structure\n\u003ca name=\"structure\"\u003e\n\n**libpypa** currently consists of 3 major parts:\n\n 1. `Lexer`\n 2. `Parser`\n 3. `AST`\n\n### Lexer\n\u003ca name=\"structure-lexer\"\u003e\n\nThe `Lexer` portion of the library tokenizes the input for the `Parser` and\ndistinguishes the different types of tokens for the `Parser`.\n\n### Parser\n\u003ca name=\"structure-parser\"\u003e\n\nThe `Parser` utilizes the `Lexer` to parse the input and generates a\npreliminary `AST` from the input.\n\n### AST\n\u003ca name=\"structure-ast\"\u003e\n\nThe AST contains the definition of all syntax elements in the code. The main\nparts of the definition are in `pypa/ast/ast.hh` which makes heavily use of\npreprocessor macros to define typedefs, mappings for compile time type lookups\nby AstType (enum class), and an implementation for a switch based visitor.\n\nThe AST types do not implement any methods, they are just structures with data.\nThe only thing which is in there for some of the bases is the constructor, to\nset the type id value and initialize the line and column values.\n\n## License\n\u003ca name=\"license\"\u003e\n\n    Copyright 2014 Vinzenz Feenstra\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n\n\n### License for src/double-conversion\n    Copyright 2006-2011, the V8 project authors. All rights reserved.\n    Redistribution and use in source and binary forms, with or without\n    modification, are permitted provided that the following conditions are\n    met:\n\n        * Redistributions of source code must retain the above copyright\n          notice, this list of conditions and the following disclaimer.\n        * Redistributions in binary form must reproduce the above\n          copyright notice, this list of conditions and the following\n          disclaimer in the documentation and/or other materials provided\n          with the distribution.\n        * Neither the name of Google Inc. nor the names of its\n          contributors may be used to endorse or promote products derived\n          from this software without specific prior written permission.\n\n    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n    \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n\n  [1]: http://flex.sourceforge.net/\n  [2]: http://invisible-island.net/byacc/byacc.html\n  [3]: http://www.gnu.org/s/bison/\n  [4]: http://boost-spirit.com\n  [5]: http://github.com/dropbox/pyston\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinzenz%2Flibpypa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvinzenz%2Flibpypa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinzenz%2Flibpypa/lists"}