#text-image #text-parser #compose #fields #grammar #syntax #jetpack

bin+lib compose_parser

Rust parser created to parse the Jetpack Compose Text and Images

8 releases (5 stable)

2.1.0 Nov 14, 2023
2.0.1 Nov 12, 2023
1.0.1 Nov 7, 2023
0.1.2 Nov 7, 2023

#11 in #compose

MIT license

11KB
181 lines

Jetpack Compose Rust Parser

Link: https://crates.io/crates/compose_parser Docs: https://docs.rs/compose_parser/latest/compose_parser/

Brief Describe

  • Rust parser created to parse the Jetpack Compose Text / Image View and get all text fields and images
  • It is important that the function starts with @Composable and has any name
  • Plus use Kotlin syntax

Grammar

compose_function = { function_declaration }
function_declaration = { "@Composable\nfun " ~ function_name ~ "() " ~ block }
function_name = { identifier }
block = { "{\n" ~ statement* ~ "}" }
statement = { text_function | image_function }
text_function = { "    Text(text = " ~ string ~ ")\n" }
image_function = { "    Image(image = " ~ string_image ~ ")\n" }
string = { "\"" ~ (ASCII_ALPHANUMERIC | " ")* ~ "\"" }
string_image = { "\"" ~ ASCII_ALPHANUMERIC* ~ ".png\"" }
identifier = { ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "_")* }
  • compose_function - main function that contains all functions
  • function_declaration - function declaration with name and block
  • function_name - function name
  • block - block with statements, have or text or image
  • statement - text or image
  • text_function - text function with string
  • image_function - image function with string plus png prefix
  • string - string with text
  • string_image - string with png
  • identifier - function name

Technologies

  • Rust and pest
  • composable.pest file with grammar rules
  • Created lib.rs file with parser
  • Created tests.rs file with test grammar rules
  • Created cli.rs file with CLI

Usage by CLI

./compose_parser -h or --help # show help
./compose_parser --file <path> # parse file (example.txt)
./compose_parser --author # show author

./compose_parser without any arguments # show help

Example

./compose_parser --file example.kt

@Composable
fun Example() {
    Text(text = "World")
    Image(image = "url.png")
    Image(image = "image.png")
}

// In result you have function name and text and image fields 

Function: Example
Text: World
Image: url.png
Image: image.png

Author

Dependencies

~2.3–3MB
~62K SLoC