// Let's write a parser for a really basic calculator. // The calculator understands input that looks like: // // 10 + 20 - 8+4 // // So positive numbers that can be either added or substracted, and blanks // around numbers are ignored. package examples import ( "fmt" "git.makaay.nl/mauricem/go-parsekit/parse" "git.makaay.nl/mauricem/go-parsekit/tokenize" ) func Example_basicCalculator1() { for _, c := range []struct { input string expected int64 }{ {"0", 0}, {"1", 1}, {"1+2+3", 6}, {" 10 + \t20 - 3 + 7 -10 ", 24}, {"", 0}, {" \t ", 0}, {"+", 0}, {"10.8 + 12", 0}, {"42+ ", 0}, } { output, err := ComputeSimple(c.input) if err != nil { fmt.Printf("Input: %q, got error: %s\n", c.input, err) } else { fmt.Printf("Input: %q, got outcome: %d, correct = %t\n", c.input, output, output == c.expected) } } // Output: // Input: "0", got outcome: 0, correct = true // Input: "1", got outcome: 1, correct = true // Input: "1+2+3", got outcome: 6, correct = true // Input: " 10 + \t20 - 3 + 7 -10 ", got outcome: 24, correct = true // Input: "", got error: unexpected end of file (expected integer number) at start of file // Input: " \t ", got error: unexpected input (expected integer number) at start of file // Input: "+", got error: unexpected input (expected integer number) at start of file // Input: "10.8 + 12", got error: unexpected input (expected operator, '+' or '-') at line 1, column 3 // Input: "42+ ", got error: unexpected input (expected integer number) at line 1, column 4 } // ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― // Implementation of the parser // ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― // ComputeSimple interprets a simple calculation, consisting of only integers // and add or subtract operators. It returns the result of the calculation. // An error is returned in case the calculation failed. func ComputeSimple(calculation string) (int64, error) { calculator := &simpleCalculator{op: +1} parseCalculation := parse.New(calculator.number) err := parseCalculation(calculation) return calculator.Result, err } // simpleCalculator defines the parsing state machine. We do this using methods // on a struct, so the parser can make use of state data inside that struct // during the parsing. type simpleCalculator struct { Result int64 // holds the resulting outcome of the computation op int64 // represents operation for next term (+1 = add, -1 = subtract) } // A definition of an int64, which conveniently drops surrounding blanks. var dropBlank = tokenize.M.Drop(tokenize.C.Opt(tokenize.A.Blanks)) var bareInteger = tokenize.C.Seq(dropBlank, tokenize.A.Integer, dropBlank) var int64Token = tokenize.T.Int64(nil, bareInteger) func (c *simpleCalculator) number(p *parse.API) { if p.Accept(int64Token) { c.Result += c.op * p.Result().Value(0).(int64) p.Handle(c.operatorOrEndOfFile) } else { p.Expected("integer number") } } func (c *simpleCalculator) operatorOrEndOfFile(p *parse.API) { var A = tokenize.A switch { case p.Accept(A.Add): c.op = +1 p.Handle(c.number) case p.Accept(A.Subtract): c.op = -1 p.Handle(c.number) case !p.Peek(A.EndOfFile): p.Expected("operator, '+' or '-'") default: p.ExpectEndOfFile() } }