Skip to content

Commit

Permalink
feat: Add pbkit as a Protobuf parser (#592)
Browse files Browse the repository at this point in the history
* Add protobuf parser

* feat: add protocol buffers parser

* doc: add pbkit to README

* remove an unused babel plugin

* remove verdored parser

* chore: up pbkit (#1)

it resolves some parse error cases

Co-authored-by: JongChan Choi <disjukr@naver.com>
  • Loading branch information
cometkim and disjukr authored Aug 16, 2021
1 parent 2ee98fa commit a8c7d5f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ The AST explorer provides following code parsers:
- [regexp-tree][]
- [regexpp][]
- [regjsparser][]
- Protocol Buffers:
- [pbkit][]
- Scala
- [Scalameta][]
- Solidity:
Expand Down Expand Up @@ -150,6 +152,7 @@ are included so you can prototype your own plugins:
[luaparse]: https://oxyc.github.io/luaparse/
[meriyah]: https://github.com/meriyah/meriyah/
[parse5]: https://github.com/inikulin/parse5
[pbkit]: https://github.com/riiid/pbkit
[postcss-safe-parser]: https://github.com/postcss/postcss-safe-parser
[postcss-scss]: https://github.com/postcss/postcss-scss
[postcss]: https://github.com/postcss/postcss
Expand Down
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"meriyah": "^1.9.12",
"parse5": "^6.0.0",
"parse5-htmlparser2-tree-adapter": "^6.0.0",
"pbkit": "^0.0.12",
"php-parser": "^3.0.0",
"postcss": "^7.0.27",
"postcss-less": "^3.1.2",
Expand Down
32 changes: 32 additions & 0 deletions website/src/parsers/protobuf/codeExample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
syntax = "proto3";
package tutorial;

import "google/protobuf/timestamp.proto";

option go_package = "github.com/protocolbuffers/protobuf/examples/go/tutorialpb";

message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;

enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}

message PhoneNumber {
string number = 1;
PhoneType type = 2;
}

repeated PhoneNumber phones = 4;

google.protobuf.Timestamp last_updated = 5;
}

// Our address book file is just one of these.
message AddressBook {
repeated Person people = 1;
}
6 changes: 6 additions & 0 deletions website/src/parsers/protobuf/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'codemirror/mode/protobuf/protobuf';

export const id = 'protobuf';
export const displayName = 'Protocol Buffers';
export const mimeTypes = ['text/x-protobuf'];
export const fileExtension = 'proto';
33 changes: 33 additions & 0 deletions website/src/parsers/protobuf/pbkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import defaultParserInterface from '../utils/defaultParserInterface';
import pkg from 'pbkit/package.json';

const ID = 'pbkit';

export default {
...defaultParserInterface,
id: ID,
displayName: ID,
version: pkg.version,
homepage: 'https://github.com/riiid/pbkit',
locationProps: new Set(['start', 'end']),
typeProps: new Set(['type']),

loadParser(callback) {
require(['pbkit/core/parser/proto'], callback);
},

parse(parser, code) {
return parser.parse(code).ast;
},

nodeToRange(node) {
const { start, end } = node;
return [start, end];
},

opensByDefault(node, key) {
if (key === 'statements') {
return true;
}
},
};
12 changes: 6 additions & 6 deletions website/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ const plugins = [
// Shim ESLint stuff that's only relevant for Node.js
new webpack.NormalModuleReplacementPlugin(
/cli-engine/,
'node-libs-browser/mock/empty'
'node-libs-browser/mock/empty',
),
new webpack.NormalModuleReplacementPlugin(
/load-rules/,
__dirname + '/src/parsers/js/transformers/eslint1/loadRulesShim.js'
__dirname + '/src/parsers/js/transformers/eslint1/loadRulesShim.js',
),

// There seems to be a problem with webpack loading an index.js file that
Expand All @@ -60,15 +60,15 @@ const plugins = [
if (/css-tree/.test(module.context)) {
module.request += '/index.js';
}
}
},
),

// More shims

// Doesn't look like jest-validate is useful in our case (prettier uses it)
new webpack.NormalModuleReplacementPlugin(
/jest-validate/,
__dirname + '/src/shims/jest-validate.js'
__dirname + '/src/shims/jest-validate.js',
),

// Hack to disable Webpack dynamic requires in ESLint, so we don't end up
Expand All @@ -94,7 +94,7 @@ const plugins = [
new webpack.ProgressPlugin({
modules: false,
activeModules: false,
profile: false
profile: false,
}),
];

Expand Down Expand Up @@ -270,5 +270,5 @@ module.exports = Object.assign({
{
devtool: 'eval',
} :
{}
{},
);
5 changes: 5 additions & 0 deletions website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8445,6 +8445,11 @@ pbkdf2@^3.0.3:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

pbkit@^0.0.12:
version "0.0.12"
resolved "https://registry.yarnpkg.com/pbkit/-/pbkit-0.0.12.tgz#f67a307867763da732f38198a53034342373d62d"
integrity sha512-NtHgxsdLpxwXyKWeeObkFbVLPQWoigyQfHJVZZJVI2h2pLC7wM39mbMzT0l89MPjB7Z7QTKzzVhtzMG9zWC2ug==

php-parser@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.0.0.tgz#7e0abef4941cabbf10f25f1445bca0f758f5102f"
Expand Down

0 comments on commit a8c7d5f

Please sign in to comment.