4 releases

new 0.3.4 Nov 10, 2024
0.3.3 Nov 8, 2024
0.2.2 Nov 5, 2024
0.2.1 Nov 5, 2024

#2208 in Parser implementations

Download history 117/week @ 2024-10-31 160/week @ 2024-11-07

277 downloads per month

MIT license

175KB
341 lines

String Multiplication

The library provides a parser for simple commands that allow multiplying strings. The library supports commands for multiplying numbers in strings, duplicating strings, and multiplying all numbers in a string. The results of the parsing process are used to evaluate the commands and return their results.

The library uses the pest parser generator to define the grammar of the commands.

Parsing Process

  1. Grammar Definition: The grammar is defined in the gramm.pest file. It includes rules for:
    • num: Recognizes signed floating point or integer numbers.
    • int: Recognizes signed integer numbers.
    • mult: Recognizes multiplication operators with optional indices.
    • multAll: Recognizes multiplication operators for multiplying all numbers in a string.
    • duplicate: Recognizes duplication operators.
    • inner_str_text: Recognizes parts of the string parameters that are not numbers.
    • str_param: Recognizes string parameters surrounded by quote marks.
    • command: Recognizes complete commands for multiplying strings.
    • commands_list: Recognizes a list of commands.
    • wrong_command: Recognizes incorrect commands in commands list.

parsing scheme illustraition

  1. Parsing: The library includes parse_command and parse_list functions that parse string and return StringMultCommand and Vec<StringMultCommand> respectively.

  2. Evaluation: The library includes evaluate and evaluate_list functions that parse string and string from file respectively and return the result of the command execution.

Commands examples

  1. Multiply first number in string by provided number

    • "15 packs, 10mg/l" * 10 -> 150 packs, 10mg/l
       
  2. Multiply nth number in string by provided number

    • "15 packs, 10mg/l" *[0] 10 -> 150 packs, 10mg/l
    • "15 packs A, 10 packs B" *[1] 10 -> 15 packs A, 100 packs B
    Multiply nth from end number in string by provided number
    • "15 packs A, 10 packs B, 9..." *[-1] 10 -> 15 packs A, 10 packs B, 90...
    • "15 packs A, 10 packs B, 9..." *[-2] 10 -> 15 packs A, 100 packs B, 9...
       
  3. Multiply all numbers in string by provided number

    • "15 packs A, 10 packs B, 9..." ** 10 -> 150 packs A, 100 packs B, 90...
       
  4. Duplicate a string n times

    • "123" *** 3 -> 123123123
    • "123" *** 0 ->
    Duplicate reversed string n times
    • "123" *** -1 -> 321
    • "123" *** -2 -> 321321

       
  5. Evaluate commands list

      • "12 packs " *** 3 *2
      • "4packs" *[2]2
      • "9 bottles." **3
      • 24 packs 12 packs 12 packs
      • Error: index '2' out of range '0..1'
      • 27 bottles

Dependencies

~2.2–3MB
~60K SLoC