# proto-parser

> A parser for proto files

Latest version **0.0.9** (published 2022-12-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install proto-parser
pnpm add proto-parser
yarn add proto-parser
bun add proto-parser
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2022-12-09 |
| First published | 2020-02-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 64.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | lancewuz |
| Maintainers | lancewuz |
| Keywords | proto, protoc, ast, parser, protobuf, protocol buffers |

## Links

- npm: https://www.npmjs.com/package/proto-parser
- Repository: https://github.com/lancewuz/proto-parser
- Issues: https://github.com/lancewuz/proto-parser/issues
- npm.io page: https://npm.io/package/proto-parser

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.0.9 (latest) — 2022-12-09
- 0.0.8-0 (alpha) — 2021-10-22
- 0.0.5-0 (beta) — 2020-04-10
- 0.0.8 — 2021-10-22
- 0.0.7 — 2021-08-09
- 0.0.7-0 — 2021-08-09
- 0.0.6 — 2021-07-08
- 0.0.5 — 2020-04-10
- 0.0.4 — 2020-03-04
- 0.0.3 — 2020-03-01
- 0.0.2 — 2020-02-28
- 0.0.1 — 2020-02-28

## README

# proto-parser [![CircleCI](https://circleci.com/gh/lancewuz/proto-parser.svg?style=svg)](https://circleci.com/gh/lancewuz/proto-parser) [![codecov](https://codecov.io/gh/lancewuz/proto-parser/branch/master/graph/badge.svg)](https://codecov.io/gh/lancewuz/proto-parser) [![npm version](https://img.shields.io/npm/v/proto-parser.svg?style=flat)](https://www.npmjs.com/package/proto-parser)

A parser for [proto](https://developers.google.com/protocol-buffers/docs/proto3) files.

## Table of Contents

* [Introduction](#introduction)
* [Usage](#usage)
  * [Install](#install)
  * [Coding](#coding)
* [API](#api)
  * [parse](#parse)

## Introduction

The purpose of proto-parser is to generate an AST for a proto file, regardless of injecting upper functions into the AST structure, such as encoding/decoding.

### proto-parser vs protobuf.js

Proto-parser is developed based on [protobuf.js](https://github.com/protobufjs/protobuf.js), and uses some codes of protobuf.js。

Compared with protobuf.js, proto-parser is more focused on parsing. so, proto-parser gains some advantages:

- have less code and higher performance
- output more pure AST structure
- support TypeScript

Note that, the AST structure generated by proto-parser keeps basic compatible with that by protobuf.js.

## Usage

### Install

```
npm install proto-parser
```

### Coding

A code example:

```ts
import * as t from 'proto-parser';

const content = `
syntax = 'proto3';
message Foo {
  string key = 1;
}
`;

const protoDocument = t.parse(content) as t.ProtoDocument;
console.log(JSON.stringify(protoDocument, null, 2));
```

The resulted AST:

```json
{
  "syntax": "proto3",
  "root": {
    "name": "",
    "fullName": "",
    "syntaxType": "ProtoRoot",
    "nested": {
      "Foo": {
        "name": "Foo",
        "fullName": ".Foo",
        "comment": null,
        "syntaxType": "MessageDefinition",
        "fields": {
          "key": {
            "name": "key",
            "fullName": ".Foo.key",
            "comment": null,
            "type": {
              "value": "string",
              "syntaxType": "BaseType"
            },
            "id": 1,
            "required": false,
            "optional": true,
            "repeated": false,
            "map": false
          }
        }
      }
    }
  },
  "syntaxType": "ProtoDocument"
}
```

## API

### <a name="parse"></a> `> parse(source: string, option?: ParseOption): ProtoDocument | ProtoError`

Parse the content of a proto file to an AST.

The definition of ParseOption:

```ts
interface ParseOption {
  // Set whether to keep the origin case. The default value is true.
  keepCase?: boolean

  // Set whether to enable alternateCommentMode. The default value is true.
  alternateCommentMode?: boolean 

  // Set whether to resolve types. The default value is true.
  resolve?: boolean

  // Set whether to weak resolve types(do not throw errors when resolve failed).
  // The default value is true.
  weakResolve?: boolean

  // Set whether to return pure json Object. The default value is true.
  toJson?: boolean
}
```

## License

[MIT](https://github.com/microsoft/vscode/blob/master/LICENSE.txt)

---
_Source: https://npm.io/package/proto-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
