Using PEG.js to Convert ROS Message Definitions to Cap'n Proto Format

[This is an ongoing project, expect updates.]

In my make-my-own-ROS project, I want to ensure compatibility with the original ROS ecosystem, so I need to provide the same set of functionality like roscpp. This is can be achieved in a number of steps, of which the first is to convert the message definitions. Initially I want to use Rust for that, but upon discovering an online PEG debugger, I decided to use that first.

Procedure

Really simple. Just write grammar->test->change PEG->test->...

The final version of the PEG definition is posted below:

// ROS Message Definition Parser
// ==========================

file = (result)*
result = sp (comment / definition)* linebreak

_ "whitespace"
  = [ \t\n\r]*

sp "space"
  = $[ \t]*

linebreak = [\n\r]

types "types" = ("bool" / "uint8" / "float32" / "string" / "Header")

array "array" = types ("[]")

type "type" = array / types

identifier "identifier" = $[a-zA-Z_]+

constant "constant" = type sp identifier sp [=] sp [0-9]+

variable "variable" = (type / array) sp identifier sp ![=]
definition "definition" = (variable / constant)

string "string" = sp $[^\n\r]+

comment "comment" = (['#']) string

Result

Thanks to the amazing web-based PEG.js debugger by phrogz, I was able to visualize the result easily in my browser :) As you can see, all tokens are differentiated with different colors. The next step would be translating from grammar tree to Cap'n Proto code!

Final Result