#shopping #list #grammar #parser #items #pest-parser #containing

bin+lib shopping_list_parser

Shopping-list parser for edu purposes

5 releases

0.1.4 Nov 16, 2024
0.1.3 Nov 15, 2024
0.1.2 Nov 11, 2024
0.1.1 Nov 6, 2024
0.1.0 Nov 6, 2024

#1481 in Parser implementations

Download history 184/week @ 2024-11-01 146/week @ 2024-11-08 218/week @ 2024-11-15 12/week @ 2024-11-22 15/week @ 2024-11-29 9/week @ 2024-12-06

258 downloads per month

MIT license

11KB
151 lines

Shopping List Parser

https://crates.io/crates/shopping_list_parser

https://docs.rs/shopping_list_parser/latest/shopping_list_parser/

Description

This project is a shopping_list_parser designed for educational purposes. It allows you to parse a structured list of shopping items using a grammar defined in pest.

Grammar rules

Shopping List Grammar The following grammar defines the structure of each item in the shopping list, with rules for attributes like item names, quantities, units, and optional descriptions. This structure is used to parse and validate items, ensuring consistent formatting.

Grammar Rules
index: Numeric identifier for each item
. index = { ASCII_DIGIT+ }
Example: 1, 25

quantity: The amount or count of the item.
quantity = { ASCII_DIGIT+ }
Example: 2, 5

name: Name of the item, allowing letters, spaces, and hyphens.
name = { (ASCII_ALPHA | " " | "-")+ }
Example: Apples, Brown Rice

brand: Optional brand information, placed in parentheses.
brand = { "(" ~ (ASCII_ALPHA | " ")+ ~ ")" }
Example: (Green Organic), (Local Brand)

description: Optional additional details, placed in curly braces.
description = { "{" ~ (ASCII_ALPHA | ASCII_DIGIT | " ")+ ~ "}" }
Example: {Sweet and crunchy}, {High in fiber}

unit: Measurement unit for the quantity.
unit = { "kg" | "g" | "ltr" | "ml" | "pcs" | "oz" }
Example: kg, pcs, oz

category: A label for organizing items, placed in square brackets.
category = { "[" ~ ASCII_ALPHA+ ~ ASCII_DIGIT* ~ "]" }
Example: [Fruits], [Snacks1]

item: Full definition of an item entry, including mandatory and optional elements.
item = { index ~ "." ~ WHITE_SPACE? ~ name ~ WHITE_SPACE? ~ quantity ~ WHITE_SPACE? ~ unit ~ (WHITE_SPACE? ~ brand)? ~ (WHITE_SPACE? ~ description)? }
Example: 1. Apples 2 kg (Green Organic) {Sweet and crunchy}

shopping_list: A collection of items and categories, with support for whitespace and line breaks.
shopping_list = { SOI ~ ((WHITE_SPACE* ~ (category | item) ~ WHITE_SPACE* ~ NEWLINE?)* ~ EOI) }

   [SMTH]
   1. Apples 2 kg (Green Organic) {Sweet and crunchy}
   2. Milk 1 ltr (Dairy Best)
   3. Bread 1 pcs {Whole grain, freshly baked}
   [Fruits]
   4. Oranges 3 kg
   5. Bananas 1 kg

## Features
  • Parses a structured shopping list with items, quantities, and units
  • Uses the pest parser generator library to define the grammar
  • Provides a simple API to parse the shopping list

Getting Started

Prerequisites

  • Rust programming language
  • Cargo package manager

Usage

  1. Import the shopping_list_parser crate in your Rust project:

    use shopping_list_parser::parse_shopping_list;
    
  2. Call the parse_shopping_list function with a string containing the shopping list:

    let input = "1. Apples 5 pcs\n2. Bananas 3 kg\n3. Mango 1 pcs";
    parse_shopping_list(&input)?;
    

Dependencies

~4MB
~75K SLoC