4 releases
0.3.4 | Nov 10, 2024 |
---|---|
0.3.3 | Nov 8, 2024 |
0.2.2 | Nov 5, 2024 |
0.2.1 | Nov 5, 2024 |
#2221 in Parser implementations
306 downloads per month
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
- 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: The library includes
parse_command
andparse_list
functions that parse string and returnStringMultCommand
andVec<StringMultCommand>
respectively. -
Evaluation: The library includes
evaluate
andevaluate_list
functions that parse string and string from file respectively and return the result of the command execution.
Commands examples
-
Multiply first number in string by provided number
"15 packs, 10mg/l" * 10
->150 packs, 10mg/l
-
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...
-
Multiply all numbers in string by provided number
"15 packs A, 10 packs B, 9..." ** 10
->150 packs A, 100 packs B, 90...
-
Duplicate a string n times
"123" *** 3
->123123123
"123" *** 0
->
Duplicate reversed string n times
"123" *** -1
->321
"123" *** -2
->321321
-
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