1- //! This is a small example of the mathematics expression parser,
1+ //! This is a small example of a math expression parser,
22//! which parse only unsigned integers, brackets, [+-*/] operations,
33//! and no spaces.
44//!
55//! This example can be run with expression at first argument:
66//! ```
7- //! zig build example -- "1+1"
7+ //! zig build expression -- "1+1"
88//! ```
99const std = @import ("std" );
10- const parcom = @import ("parcom" );
11-
12- const P = parcom .Parsers ;
10+ const p = @import ("parcom" );
1311
1412const Value = i8 ;
1513
@@ -47,7 +45,7 @@ const Operation = enum {
4745/// Product ← Value (('*' / '/') Value)*
4846/// Value ← [0-9]+ / '(' Expr ')'
4947/// ```
50- fn expr (alloc : std.mem.Allocator ) ! parcom .TaggedParser (Value ) {
48+ fn expr (alloc : std.mem.Allocator ) ! p .TaggedParser (Value ) {
5149 // functions to transform parsers results
5250 const fns = struct {
5351 fn parseInt (_ : void , str : [10 :0 ]u8 ) anyerror ! Value {
@@ -57,7 +55,7 @@ fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
5755 fn valueFromParens (_ : void , result : struct { u8 , Value , u8 }) anyerror ! Value {
5856 return result [1 ];
5957 }
60- fn valueFromEither (_ : void , result : parcom .Either (Value , Value )) anyerror ! Value {
58+ fn valueFromEither (_ : void , result : p .Either (Value , Value )) anyerror ! Value {
6159 return switch (result ) {
6260 .left = > result .left ,
6361 .right = > result .right ,
@@ -79,12 +77,12 @@ fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
7977 };
8078
8179 // brackets: Int <- (<exp>)
82- const brackets = P .tuple (.{ P .char ('(' ), P .lazy (Value , alloc , expr ), P .char (')' ) })
80+ const brackets = p .tuple (.{ p .char ('(' ), p .lazy (Value , alloc , expr ), p .char (')' ) })
8381 .transform (Value , {}, fns .valueFromParens );
8482
8583 // we can't use Parsers.int to parse numbers here to avoid consumption of the '-' and '+'
8684 // number: Int <- \d+
87- const number = P .range ('0' , '9' ).repeatToSentinelArray (1 , 10 )
85+ const number = p .range ('0' , '9' ).repeatToSentinelArray (1 , 10 )
8886 .transform (Value , {}, fns .parseInt );
8987
9088 // value: Int <- <number>|<brackets>
@@ -93,14 +91,14 @@ fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
9391
9492 // product: Int <- <value>([*/]<value>)*
9593 const product = blk : {
96- const operation = P .oneCharOf ("*/" ).transform (Operation , {}, Operation .parse );
94+ const operation = p .oneCharOf ("*/" ).transform (Operation , {}, Operation .parse );
9795 break :blk value .andThen (operation .andThen (value ).repeat (alloc ))
9896 .transform (Value , alloc , fns .calculate );
9997 };
10098
10199 // sum: Int <- <product>([+-]<product>)*
102100 const sum = blk : {
103- const operation = P .oneCharOf ("+-" ).transform (Operation , {}, Operation .parse );
101+ const operation = p .oneCharOf ("+-" ).transform (Operation , {}, Operation .parse );
104102 break :blk product .andThen (operation .andThen (product ).repeat (alloc ))
105103 .transform (Value , alloc , fns .calculate );
106104 };
@@ -109,9 +107,9 @@ fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
109107}
110108
111109fn evaluate (alloc : std.mem.Allocator , expression : []const u8 ) ! ? Value {
112- const p = try expr (alloc );
113- defer p .deinit ();
114- return try p .parseString (alloc , expression );
110+ const parser = try expr (alloc );
111+ defer parser .deinit ();
112+ return try parser .parseString (alloc , expression );
115113}
116114
117115test "1+1" {
0 commit comments