AIDL parser for Rust
Parse and validate AIDL files and return for each one an AST and diagnostics.
To use it, you need to create a [Parser
] instance, add the content and
get the validation results.
For convenience, the returned AST can be traversed via the helper functions
available in the [traverse
] module.
Usage
Add to Cargo.toml
:
[]
= "0.4.0"
Create parser, analyze results:
use aidl_parser::{traverse, Parser};
// Parse AIDL contents
let mut parser = Parser::new();
parser.add_content("id1", "package test.pkg; interface MyInterface { void hello(String); }");
parser.add_content("id2", "package test.pkg; parcelable Parcelable { int myField; }");
let results = parser.validate();
// Display results
for (id, res) in &results {
println!("{}: AST = {:#?}", id, res.ast);
println!("{}: Diagnostics = {:#?}", id, res.diagnostics);
}
// Traverse AST
let ast1 = results["id1"].ast.as_ref().expect("missing AST");
traverse::walk_symbols(ast1, traverse::SymbolFilter::All, |s| println!("- Symbol: {:#?}", s));
AIDL language support
It is currently a best effort to provide good diagnostic and navigation based on AIDL documentation.
The code base is (much) simpler than the official implementation and (arguably) more readable, easier to understand and does not support legacy options. It is planned to gradually improve language support to cover most of the functionalities of the AIDL language.
To get more insight on the current grammar and validation, please refer to:
- grammar (lalrpop): https://github.com/bwalter/rust-aidl-parser/blob/main/src/aidl.lalrpop
- unit-tests for grammar: https://github.com/bwalter/rust-aidl-parser/blob/main/src/rules.rs
- validation incl. unit-tests: https://github.com/bwalter/rust-aidl-parser/blob/main/src/validation.rs
If you need specific support, please do not hesitate to submit an issue or a pull request.
Link to AOSP AIDL implementation: https://android.googlesource.com/platform/system/tools/aidl/+/refs/heads/master
TODO
- Document how to display diagnostics (e.g. with CodeSpan)
- Annotation attached to primitive type
- union (Android 12)
- nested types (Android T)
- Parcelable declaration(= forward declaration), with optional annotations
- Allow annotations for list/map parameters?
- Android types: ParcelableHolder, IBinder, FileDescriptor, ParcelFileDescriptor,
- Ignore Java-like imports: "android.os.IInterface", "android.os.IBinder", "android.os.Parcelable", "android.os.Parcel", "android.content.Context", "java.lang.String", "java.lang.CharSequence" (but add a warning)
- Const values with arithmetic (e.g.: const int HELLO = 3 * 4)
- Allow array of parcelable/interface? and check for enums in List or Map
- validate:
- duplicated method names
- duplicated method values
- file name matching item name
- ParcelableHolder cannot (currently) be given as an argument?
- ParcelableFileDescriptor cannot be out (because it is not default-constructible)
- Add reserved keywords for C++ and Java: "break", "case", "catch", "char", "class", "continue", "default", "do", "double", "else", "enum", "false", "float", "for", "goto", "if", "int", "long", "new", "private", "protected", "public", "return", "short", "static", "switch", "this", "throw", "true", "try", "void", "volatile", "while" (but also warnings for Rust)
License
This project is licensed under the MIT license.