diff --git a/examples/example_basiccalculator1_test.go b/examples/example_basiccalculator1_test.go index 634ca34..d27bb4a 100644 --- a/examples/example_basiccalculator1_test.go +++ b/examples/example_basiccalculator1_test.go @@ -70,7 +70,7 @@ type simpleCalculator struct { } // A definition of an int64, which conveniently drops surrounding blanks. -var dropBlank = parsekit.M.Drop(parsekit.C.Opt(parsekit.A.Blank)) +var dropBlank = parsekit.M.Drop(parsekit.C.Opt(parsekit.A.Blanks)) var bareInteger = parsekit.C.Seq(dropBlank, parsekit.A.Integer, dropBlank) var int64Token = parsekit.T.Int64(nil, bareInteger) diff --git a/examples/example_basiccalculator2_test.go b/examples/example_basiccalculator2_test.go index 47ce0ce..e5973b5 100644 --- a/examples/example_basiccalculator2_test.go +++ b/examples/example_basiccalculator2_test.go @@ -53,9 +53,9 @@ func Example_basicCalculator2() { // Input: "(3.05+2)*(4.3+5.12)", got outcome: 47.571000, correct = true // Input: "8.10 + 999/233", got outcome: 12.387554, correct = true // Input: " -10 + (10.8+ (3 *-20-3*(8 +-4.12)) + 10)/5 ", got outcome: -20.168000, correct = true - // Input: "", got error: unexpected end of file at start of file - // Input: "(", got error: unexpected end of file at line 1, column 2 - // Input: "10+20-", got error: unexpected end of file at line 1, column 7 + // Input: "", got error: unexpected end of file (expected factor or parenthesized expression) at start of file + // Input: "(", got error: unexpected end of file (expected factor or parenthesized expression) at line 1, column 2 + // Input: "10+20-", got error: unexpected end of file (expected factor or parenthesized expression) at line 1, column 7 // Input: "10+20-(4*10))", got error: unexpected input (expected end of file) at line 1, column 13 // Input: "10+20-((4*10) + 17", got error: unexpected end of file (expected ')') at line 1, column 19 } @@ -76,67 +76,67 @@ type calculator struct { // value for the calculation. An error can be returned as well, in case the // calculation fails for some reason. func Compute(input string) (float64, *parsekit.Error) { - c := &calculator{} - parser := parsekit.NewParser(c.calculation) + calc := &calculator{} + parser := parsekit.NewParser(calc.calculation) err := parser.Execute(input) - return c.result, err + return calc.result, err } // = -func (c *calculator) calculation(p *parsekit.ParseAPI) { - if p.Handle(c.expr) { +func (calc *calculator) calculation(p *parsekit.ParseAPI) { + if p.Handle(calc.expr) { p.ExpectEndOfFile() - c.result = c.interpreter.result + calc.result = calc.interpreter.result } } // = ( | (ADD|SUB) ) -func (c *calculator) expr(p *parsekit.ParseAPI) { - c.interpreter.push() +func (calc *calculator) expr(p *parsekit.ParseAPI) { + calc.interpreter.push() var C, A = parsekit.C, parsekit.A - if p.Handle(c.term) { + if p.Handle(calc.term) { for p.On(C.Any(A.Add, A.Subtract)).Accept() { op := p.Result().Rune(0) - if !p.Handle(c.term) { + if !p.Handle(calc.term) { return } - c.interpreter.eval(op) + calc.interpreter.eval(op) } } - c.interpreter.pop() + calc.interpreter.pop() } // = ( | (MUL|DIV) ) -func (c *calculator) term(p *parsekit.ParseAPI) { - c.interpreter.push() +func (calc *calculator) term(p *parsekit.ParseAPI) { + calc.interpreter.push() var C, A = parsekit.C, parsekit.A - if p.Handle(c.factor) { + if p.Handle(calc.factor) { for p.On(C.Any(A.Multiply, A.Divide)).Accept() { op := p.Result().Rune(0) - if !p.Handle(c.factor) { + if !p.Handle(calc.factor) { return } - c.interpreter.eval(op) + calc.interpreter.eval(op) } } - c.interpreter.pop() + calc.interpreter.pop() } // = ( (SPACE|TAB) | "") // = (FLOAT | LPAREN RPAREN) -func (c *calculator) factor(p *parsekit.ParseAPI) { +func (calc *calculator) factor(p *parsekit.ParseAPI) { var A, T = parsekit.A, parsekit.T - p.On(A.Blank).Skip() + p.On(A.Blanks).Skip() switch { case p.On(T.Float64(nil, A.Signed(A.Float))).Accept(): value := p.Result().Value(0).(float64) - c.interpreter.pushValue(value) + calc.interpreter.pushValue(value) case p.On(A.LeftParen).Skip(): - if !p.Handle(c.expr) { + if !p.Handle(calc.expr) { return } if !p.On(A.RightParen).Skip() { @@ -144,10 +144,10 @@ func (c *calculator) factor(p *parsekit.ParseAPI) { return } default: - p.UnexpectedInput("factor or (expression)") + p.UnexpectedInput("factor or parenthesized expression") return } - p.On(A.Blank).Skip() + p.On(A.Blanks).Skip() } // --------------------------------------------------------------------------- diff --git a/examples/example_dutchpostcode_test.go b/examples/example_dutchpostcode_test.go index 7d81efc..be088a6 100644 --- a/examples/example_dutchpostcode_test.go +++ b/examples/example_dutchpostcode_test.go @@ -40,11 +40,11 @@ func Example_dutchPostcodeUsingTokenizer() { // [1] Input: "2233Ab" Output: 2233 AB Tokens: PCD(2233) PCL(AB) // [2] Input: "1001\t\tab" Output: 1001 AB Tokens: PCD(1001) PCL(AB) // [3] Input: "1818ab" Output: 1818 AB Tokens: PCD(1818) PCL(AB) - // [4] Input: "1212abc" Error: unexpected input (expected a Dutch postcode) at start of file - // [5] Input: "1234" Error: unexpected input (expected a Dutch postcode) at start of file - // [6] Input: "huh" Error: unexpected input (expected a Dutch postcode) at start of file - // [7] Input: "" Error: unexpected end of file (expected a Dutch postcode) at start of file - // [8] Input: "\xcd2222AB" Error: unexpected input (expected a Dutch postcode) at start of file + // [4] Input: "1212abc" Error: unexpected input at start of file + // [5] Input: "1234" Error: unexpected input at start of file + // [6] Input: "huh" Error: unexpected input at start of file + // [7] Input: "" Error: unexpected end of file at start of file + // [8] Input: "\xcd2222AB" Error: unexpected input at start of file } // --------------------------------------------------------------------------- @@ -65,7 +65,7 @@ func createPostcodeTokenizer() *parsekit.Tokenizer { pcDigits := C.Seq(digitNotZero, C.Rep(3, A.Digit)) pcLetter := C.Any(A.ASCIILower, A.ASCIIUpper) pcLetters := M.ToUpper(C.Seq(pcLetter, pcLetter)) - space := M.Replace(C.Opt(A.Blank), " ") + space := M.Replace(C.Opt(A.Blanks), " ") postcode := C.Seq(T.Str("PCD", pcDigits), space, T.Str("PCL", pcLetters), A.EndOfFile) // Create a Tokenizer that wraps the 'postcode' TokenHandler and allows diff --git a/examples/example_helloManyStateParser_test.go b/examples/example_helloManyStateParser_test.go index f2bb6b6..df57fed 100644 --- a/examples/example_helloManyStateParser_test.go +++ b/examples/example_helloManyStateParser_test.go @@ -39,6 +39,7 @@ func Example_helloWorldUsingParser1() { "hello , \t \t Droopy \t !", "Oh no!", "hello,!", + "hello, \t!", } { name, err := (&helloparser1{}).Parse(input) if err != nil { @@ -47,21 +48,22 @@ func Example_helloWorldUsingParser1() { fmt.Printf("[%d] Input: %q Output: %s\n", i, input, name) } } - // Output: - // [0] Input: "Hello, world!" Output: world - // [1] Input: "HELLO ,Johnny!" Output: Johnny - // [2] Input: "hello , Bob123!" Output: Bob123 - // [3] Input: "hello Pizza!" Error: unexpected input (expected comma) - // [4] Input: "" Error: unexpected end of file (expected hello) - // [5] Input: " " Error: unexpected input (expected hello) - // [6] Input: "hello" Error: unexpected end of file (expected comma) - // [7] Input: "hello," Error: unexpected end of file (expected name) - // [8] Input: "hello , " Error: unexpected end of file (expected name) - // [9] Input: "hello , Droopy" Error: unexpected end of file (expected exclamation) - // [10] Input: "hello , Droopy!" Output: Droopy - // [11] Input: "hello , \t \t Droopy \t !" Output: Droopy - // [12] Input: "Oh no!" Error: unexpected input (expected hello) - // [13] Input: "hello,!" Error: unexpected input (expected name) + // [0] Input: "Oh!" Error: unexpected input (expected hello) + // [1] Input: "Hello, world!" Output: world + // [2] Input: "HELLO ,Johnny!" Output: Johnny + // [3] Input: "hello , Bob123!" Output: Bob123 + // [4] Input: "hello Pizza!" Error: unexpected input (expected comma) + // [5] Input: "" Error: unexpected end of file (expected hello) + // [6] Input: " " Error: unexpected input (expected hello) + // [7] Input: "hello" Error: unexpected end of file (expected comma) + // [8] Input: "hello," Error: unexpected end of file (expected name) + // [9] Input: "hello , " Error: unexpected end of file (expected name) + // [10] Input: "hello , Droopy" Error: unexpected end of file (expected exclamation mark) + // [11] Input: "hello , Droopy!" Output: Droopy + // [12] Input: "hello , \t \t Droopy \t !" Output: Droopy + // [13] Input: "Oh no!" Error: unexpected input (expected hello) + // [14] Input: "hello,!" Error: The name cannot be empty + // [15] Input: "hello, \t!" Error: The name cannot be empty } // --------------------------------------------------------------------------- @@ -90,7 +92,7 @@ func (h *helloparser1) start(p *parsekit.ParseAPI) { func (h *helloparser1) comma(p *parsekit.ParseAPI) { a := parsekit.A switch { - case p.On(a.Blank).Skip(): + case p.On(a.Blanks).Skip(): p.Handle(h.comma) case p.On(a.Comma).Skip(): p.Handle(h.startName) @@ -100,13 +102,11 @@ func (h *helloparser1) comma(p *parsekit.ParseAPI) { } func (h *helloparser1) startName(p *parsekit.ParseAPI) { - c, a := parsekit.C, parsekit.A - switch { - case p.On(a.Blank).Skip(): - p.Handle(h.startName) - case p.On(c.Not(a.Excl)).Stay(): + a := parsekit.A + p.On(a.Blanks).Skip() + if p.On(a.AnyRune).Stay() { p.Handle(h.name) - default: + } else { p.UnexpectedInput("name") } } @@ -114,13 +114,13 @@ func (h *helloparser1) startName(p *parsekit.ParseAPI) { func (h *helloparser1) name(p *parsekit.ParseAPI) { a := parsekit.A switch { - case p.On(a.Excl).Skip(): + case p.On(a.Excl).Stay(): p.Handle(h.exclamation) case p.On(a.AnyRune).Accept(): h.greetee += p.Result().String() p.Handle(h.name) default: - p.UnexpectedInput("name") + p.UnexpectedInput("exclamation mark") } } diff --git a/examples/example_helloParserCombinator_test.go b/examples/example_helloParserCombinator_test.go index 2137317..51a29fc 100644 --- a/examples/example_helloParserCombinator_test.go +++ b/examples/example_helloParserCombinator_test.go @@ -37,9 +37,9 @@ func Example_helloWorldUsingTokenizer() { // [1] Input: "HELLO ,Johnny!" Output: Johnny // [2] Input: "hello , Bob123!" Output: Bob123 // [3] Input: "hello Pizza!" Output: Pizza - // [4] Input: "Oh no!" Error: unexpected input (expected a friendly greeting) at start of file - // [5] Input: "Hello, world" Error: unexpected input (expected a friendly greeting) at start of file - // [6] Input: "Hello,!" Error: unexpected input (expected a friendly greeting) at start of file + // [4] Input: "Oh no!" Error: unexpected input at start of file + // [5] Input: "Hello, world" Error: unexpected input at start of file + // [6] Input: "Hello,!" Error: unexpected input at start of file } // ---------------------------------------------------------------------------