8 releases (stable)

1.5.0 Jul 15, 2023
1.4.0 Apr 26, 2023
1.3.0 Feb 22, 2023
1.1.0 Jan 28, 2023
0.0.2 Jan 23, 2023

#6 in #capnp

36 downloads per month

MIT license

400KB
15K SLoC

C 14K SLoC JavaScript 486 SLoC // 0.1% comments Scheme 197 SLoC // 0.1% comments Rust 31 SLoC

tree-sitter-capnp

This crate provides a Cap'n Proto grammar for the tree-sitter parsing library. To use this crate, add it to the [dependencies] section of your Cargo.toml file. (Note that you will probably also need to depend on the tree-sitter crate to use the parsed result in any useful way.)

[dependencies]
tree-sitter = "~0.20.3"
tree-sitter-capnp = "1.5.0"

Typically, you will use the language function to add this grammar to a tree-sitter Parser, and then use the parser to parse some code:

let code = r#"
@0xa73956d2621fc3ee;

using Cxx = import "/capnp/c++.capnp";

$Cxx.namespace("capnp::compiler");

struct Token {
  union {
    identifier @0 :Text;
    stringLiteral @1 :Text;
    binaryLiteral @9 :Data;
    integerLiteral @2 :UInt64;
    floatLiteral @3 :Float64;
    operator @4 :Text;
    parenthesizedList @5 :List(List(Token));
    bracketedList @6 :List(List(Token));
  }

  startByte @7 :UInt32;
  endByte @8 :UInt32;
}

struct Statement {
  tokens @0 :List(Token);
  union {
    line @1 :Void;
    block @2 :List(Statement);
  }

  docComment @3 :Text;

  startByte @4 :UInt32;
  endByte @5 :UInt32;
}

struct LexedTokens {
  # Lexer output when asked to parse tokens that don't form statements.

  tokens @0 :List(Token);
}

struct LexedStatements {
  # Lexer output when asked to parse statements.

  statements @0 :List(Statement);
}
"#;
let mut parser = Parser::new();
parser.set_language(tree_sitter_capnp::language()).expect("Error loading Cap'n Proto grammar");
let parsed = parser.parse(code, None);

If you have any questions, please reach out to us in the tree-sitter discussions page.

Dependencies

~2.9–4MB
~77K SLoC