func ExampleMany() { var r interface{} var ok bool prt := func() { if ok { fmt.Printf("Result: %c\n", r) } else { fmt.Printf("Error: %v\n", r) } } letter := kern.Regexp(`\pL`) p := kern.Many(letter) t := utf88.Text("abc78d") u := utf88.Text("789def") r, ok = kern.ParseText(p, t) prt() r, ok = kern.ParseText(p, u) prt() // Output: // Result: [[a] [b] [c]] // Result: [] }
func ExampleParseText() { var r interface{} var ok bool prt := func() { if ok { fmt.Printf("Result: %c\n", r) } else { fmt.Printf("Error: %v\n", r) } } p := kern.Symbol(utf88.Codepoint('a')) t := utf88.Text("abc") r, ok = kern.ParseText(p, t) prt() u := utf88.Text("defg") r, ok = kern.ParseText(p, u) prt() // Output: // Result: a // Error: Unexpected d input. }
func ExampleOption() { var r interface{} var ok bool prt := func() { if ok { fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text))) } else { fmt.Printf("Error: %v\n", r) } } letter := kern.Regexp(`\pL`) p := kern.Option(utf88.Text("None."), letter) t := utf88.Text("abc") u := utf88.Text("789") r, ok = kern.ParseText(p, t) prt() r, ok = kern.ParseText(p, u) prt() // Output: // Result: a // Result: None. }
func ExampleSepBy() { var r interface{} var ok bool prt := func() { if ok { fmt.Printf("Result: %c\n", r) } else { fmt.Printf("Error: %v\n", r) } } letter := kern.Regexp(`\pL`) punc := kern.Regexp(`\pP`) p := kern.SepBy(punc, letter) t := utf88.Text("a;b:c789") u := utf88.Text(";789") r, ok = kern.ParseText(p, t) prt() r, ok = kern.ParseText(p, u) prt() // Output: // Result: [[a] [b] [c]] // Result: [] }
func ExampleAlt() { var r interface{} var ok bool prt := func() { if ok { fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text))) } else { fmt.Printf("Error: %v\n", r) } } lower := kern.Regexp(`\p{Ll}`) upper := kern.Regexp(`\p{Lu}`) digit := kern.Regexp(`\p{Nd}`) p := kern.Alt(lower, digit) q := kern.Alt(lower, digit, upper) t := utf88.Text("7ef") u := utf88.Text(";ef") r, ok = kern.ParseText(p, t) prt() r, ok = kern.ParseText(p, u) prt() r, ok = kern.ParseText(q, t) prt() // Output: // Result: 7 // Error: No alternatives selected. // Result: 7 }
func ExampleNoneOf() { p := kern.NoneOf(utf88.Text("xyz\n\t")) t := utf88.Text("abcde") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %s\n", utf88.Sur(r.(utf88.Codepoint))) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: a }
func ExampleToken() { p := kern.Token(utf88.Text("ab")) t := utf88.Text("abc") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text))) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: ab }
func ExampleFail() { p := kern.Fail(utf88.Text("Some Failure")) t := utf88.Text("anything") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %v\n", r) } else { fmt.Printf("Error: %v\n", r) } // Output: // Error: Some Failure }
func ExampleTry() { p := kern.Flatten(kern.Regexp(`\pL`), kern.Regexp(`\pL`)) q := kern.Flatten(kern.Regexp(`\pL`), kern.Regexp(`\pN`)) t := utf88.Text("a7bcde") var r interface{} var ok bool prt := func() { if ok { fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text))) } else { fmt.Printf("Error: %v\n", r) } } r, ok = kern.ParseText(kern.Alt(p, q), t) prt() r, ok = kern.ParseText(kern.Alt(kern.Try(p), q), t) prt() // Output: // Error: Unexpected 7 input. // Result: a7 }
func ExampleBind() { var r interface{} var ok bool prt := func() { if ok { fmt.Printf("Result: %s\n", r) } else { fmt.Printf("Error: %v\n", r) } } lower := kern.Regexp(`\p{Ll}`) intNum := kern.Regexp(`\p{Nd}*`) p := kern.Bind(lower, func(c interface{}) kern.Parser { return kern.Bind(intNum, func(d interface{}) kern.Parser { return kern.Bind(lower, func(e interface{}) kern.Parser { return kern.Return(utf88.Surr(c.(utf88.Text)) + "," + utf88.Surr(d.(utf88.Text)) + "," + utf88.Surr(e.(utf88.Text))) }) }) }) t := utf88.Text("e789fg") r, ok = kern.ParseText(p, t) prt() // Output: // Result: e,789,f }
func ExampleAsk() { letter := kern.Regexp(`\pL`) digit := kern.Regexp(`\p{Nd}`) p := kern.Ask(kern.Collect(digit, letter), utf88.Text("digit,letter")) t := utf88.Text(";efg") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %s\n", utf88.Sur(r.(utf88.Codepoint))) } else { fmt.Printf("Error: %v\n", r) } // Output: // Error: Unexpected ; input. Expecting digit,letter }
func ExamplePrint() { p := Alt(digit, Alt(punct, Print(u8.Text("Just before the letter:"), letter))) parseStr(p, "defg") // Output: // Just before the letter: "defg"/0, ok, empty }
func ExamplePrintState() { p := Alt(digit, Alt(punct, Bind(PrintState(u8.Text("Just before the letter:")), func(x interface{}) (p Parser) { return letter }))) parseStr(p, "defg") // Output: // Just before the letter: "defg"/0, ok, empty }
//================================================================================ func TestInputAndPosition(t *testing.T) { ts.LogAsserts("InputAndPosition", t, func(tt *ts.T) { letter := Regexp(`\pL`) p1 := Flatten(letter, letter, letter) tt.AssertEqual(parseStr(SeqRight(SetInput(u8.Desur("Hi!")), GetInput), "abcdefg"), PState{input: "Hi!", pos: 0, value: u8.Desur("Hi!"), empty: true, ok: true}) tt.AssertEqual(parseStr(SeqRight(letter, GetInput), "abcdefg"), PState{input: "bcdefg", pos: 1, value: u8.Desur("bcdefg"), ok: true}) tt.AssertEqual(parseStr(SeqRight(SetPosition(100), GetPosition), "abcdefg"), PState{input: "abcdefg", pos: 100, value: uint64(100), empty: true, ok: true}) tt.AssertEqual(parseStr(SeqRight(SetInputAndPosition(InputAndPosition{u8.Desur("Hi there!"), 5}), GetInputAndPosition), "abcdefg"), PState{input: "Hi there!", pos: 5, value: InputAndPosition{u8.Desur("Hi there!"), 5}, empty: true, ok: true}) tt.AssertEqual(parseStr(Collect(p1, SeqRight(SetInput(u8.Desur("XYZ")), p1)), "ABCDEFG"), PState{input: "", pos: 6, value: arrText("ABC", "XYZ"), ok: true}) //TODO: what should pos be? tt.AssertEqual(parseStr(Collect(p1, SeqRight(SetInput(u8.Desur("XYZ")), p1)), "ABC"), PState{input: "", pos: 6, value: arrText("ABC", "XYZ"), ok: true}) //TODO: ditto tt.AssertEqual(parseStr(Collect(p1), "ABC"), PState{input: "", pos: 3, value: arrText("ABC"), ok: true}) p2 := SeqRight(Skip(SetInput(u8.Text("XY0")), SetPosition(0)), p1) tt.AssertEqual(parseStr(Collect(p1, p2), "ABC"), PState{input: "0", pos: 2, value: nil, error: makeUnexpInp("0")}) p3 := SeqRight(Skip(SetInput(u8.Text("WXYZ")), SetPosition(0)), GetPosition) tt.AssertEqual(parseStr(SeqRight(p1, p3), "ABC"), PState{input: "WXYZ", pos: 0, value: uint64(0), ok: true}) p4 := SeqRight(SetInput(u8.Text("XYZ")), GetInput) tt.AssertEqual(parseStr(SeqRight(p1, p4), "ABC"), PState{input: "XYZ", pos: 3, value: u8.Text("XYZ"), ok: true}) tt.AssertEqual(parseStr(Collect(p1, SeqRight(SetInputAndPosition(InputAndPosition{u8.Desur("XYZ"), 2}), p1)), "ABCDEFG"), //posn changed from 3 to 2... PState{input: "", pos: 5, value: arrText("ABC", "XYZ"), ok: true}) //...so posn is 5 instead of 6 }) }
func ExampleReturn() { p := kern.Return(1234567890) t := utf88.Text("anything") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %v\n", r) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: 1234567890 }
func ExampleSymbol() { p := kern.Symbol(utf88.Codepoint('a')) t := utf88.Text("abc") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %c\n", r) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: a }
func ExampleRegexp() { p := kern.Regexp(`z|a|b`) t := utf88.Text("abc") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text))) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: a }
func ExampleFwd() { var expr func() kern.Parser paren := func() kern.Parser { return kern.SeqRight(kern.Token(utf88.Text(string('('))), kern.SeqLeft(kern.Fwd(expr), // Fwd will evaluate passed expression lazily kern.Token(utf88.Text(string(')'))))) } expr = func() kern.Parser { // will parse string enclosed in parenthesis pairs to any depth return kern.Alt(kern.Token(utf88.Text(string('a'))), paren()) } t := utf88.Text("(((a)))") r, ok := kern.ParseText(expr(), t) if ok { fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text))) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: a }
func ExampleFwdWithParams() { var expr func(...interface{}) kern.Parser paren := func(as ...interface{}) kern.Parser { return kern.SeqRight(kern.Token(utf88.Text(string('('))), kern.SeqLeft(kern.FwdWithParams(expr, as...), // FwdWithParams will evaluate passed expression lazily kern.Token(utf88.Text(string(')'))))) } expr = func(as ...interface{}) kern.Parser { // will parse string enclosed in parenthesis pairs to any depth return kern.Alt(kern.Token(utf88.Text(string('a'))), paren(as...)) } t := utf88.Text("(((a)))") r, ok := kern.ParseText(expr(101, 102), t) // call expr with extra (unused in this e.g.) args if ok { fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text))) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: a }
func ExampleSatisfy() { p := kern.Satisfy(func(c utf88.Codepoint) bool { return c == 'a' }) t := utf88.Text("abcde") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %c\n", r) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: a }
func ExampleCollect() { letter := kern.Regexp(`\pL`) digit := kern.Regexp(`\p{Nd}`) p := kern.Collect(letter, digit, letter) t := utf88.Text("a5bc") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %c\n", r) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: [[a] [5] [b]] }
func ExampleSeqRight() { digit := kern.Regexp(`\p{Nd}`) letter := kern.Regexp(`\pL`) p := kern.SeqRight(digit, letter) t := utf88.Text("7efg") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %s\n", utf88.Surr(r.(utf88.Text))) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: e }
// AlterTopStateStack assumes the user state in the parser state record // is a slice, and alters the final value. func AlterTopStateStack(k interface{}) Parser { return Bind(GetState, func(s interface{}) Parser { var vec []interface{} tx := u8.Text("AlterTopStateStack doesn't handle zero-sized stacks.") if s == nil { vec = []interface{}{} return Fail(tx) } else if len(s.([]interface{})) == 0 { return Fail(tx) } else { vec = make([]interface{}, 0) for _, n := range s.([]interface{})[:len(s.([]interface{}))-1] { vec = append(vec, n) } vec = append(vec, k) } return SeqRight(PutState(vec), Return(nil)) }) }
func ExampleApply() { letter := kern.Regexp(`\pL`) p := kern.Apply(func(c interface{}) interface{} { cs := utf88.Surr(c.(utf88.Text)) return cs + cs + cs }, letter) t := utf88.Text("abc") r, ok := kern.ParseText(p, t) if ok { fmt.Printf("Result: %s\n", r) } else { fmt.Printf("Error: %v\n", r) } // Output: // Result: aaa }
//================================================================================ func TestAsciiLexer(t *testing.T) { ts.LogAsserts("AsciiLexer", t, func(tt *ts.T) { a := NewAsciiLexer(false) at := NewAsciiLexer(true) tt.AssertEqual(parseStr(a.Letter(), "defg"), PState{input: "efg", pos: 1, value: u8.Text("d"), ok: true}) tt.AssertEqual(parseStr(a.Letter(), ";efg"), PState{input: ";efg", empty: true, error: makeUnexpInp(";")}) tt.AssertEqual(parseStr(a.Lower(), "defg"), PState{input: "efg", pos: 1, value: u8.Text("d"), ok: true}) tt.AssertEqual(parseStr(a.Lower(), "Defg"), PState{input: "Defg", empty: true, error: makeUnexpInp("D")}) tt.AssertEqual(parseStr(a.Lower(), ";efg"), PState{input: ";efg", empty: true, error: makeUnexpInp(";")}) tt.AssertEqual(parseStr(a.Upper(), "Defg"), PState{input: "efg", pos: 1, value: u8.Text("D"), ok: true}) tt.AssertEqual(parseStr(a.Upper(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) tt.AssertEqual(parseStr(a.Upper(), ";efg"), PState{input: ";efg", empty: true, error: makeUnexpInp(";")}) tt.AssertEqual(parseStr(a.Digit(), "6efg"), PState{input: "efg", pos: 1, value: u8.Text("6"), ok: true}) tt.AssertEqual(parseStr(a.Digit(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) tt.AssertEqual(parseStr(a.Space(), " efg"), PState{input: "efg", pos: 1, value: u8.Text(" "), ok: true}) tt.AssertEqual(parseStr(a.Space(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) tt.AssertEqual(parseStr(a.Tab(), "\tefg"), PState{input: "efg", pos: 1, value: u8.Text("\t"), ok: true}) tt.AssertEqual(parseStr(a.Tab(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) tt.AssertEqual(parseStr(a.Newline(), "\nefg"), PState{input: "efg", pos: 1, value: u8.Text("\n"), ok: true}) tt.AssertEqual(parseStr(a.Newline(), "\r\nefg"), PState{input: "\r\nefg", empty: true, error: makeUnexpInp("\r")}) tt.AssertEqual(parseStr(at.Newline(), "\nefg"), PState{input: "efg", pos: 1, value: u8.Text("\n"), ok: true}) tt.AssertEqual(parseStr(at.Newline(), "\r\nefg"), PState{input: "efg", pos: 2, value: u8.Text("\r\n"), ok: true}) tt.AssertEqual(parseStr(a.Whitespace(), " efg"), PState{input: "efg", pos: 1, value: u8.Text(" "), ok: true}) tt.AssertEqual(parseStr(a.Whitespace(), "\tefg"), PState{input: "efg", pos: 1, value: u8.Text("\t"), ok: true}) tt.AssertEqual(parseStr(a.Whitespace(), "\refg"), PState{input: "efg", pos: 1, value: u8.Text("\r"), ok: true}) tt.AssertEqual(parseStr(a.Whitespace(), "\nefg"), PState{input: "efg", pos: 1, value: u8.Text("\n"), ok: true}) tt.AssertEqual(parseStr(a.Whitespace(), "\fefg"), PState{input: "efg", pos: 1, value: u8.Text("\f"), ok: true}) tt.AssertEqual(parseStr(a.Whitespace(), "\vefg"), PState{input: "efg", pos: 1, value: u8.Text("\v"), ok: true}) tt.AssertEqual(parseStr(a.Whitespace(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) // Punct() Parser{ return Regexp(`[!"#%&'()*,\-./:;?@[\\\]_{}]`) // StartPunct() Parser{ return Regexp(`[([{]`) // EndPunct() Parser{ return Regexp(`[)\]}]`) // ConnectorPunct() Parser{ return Regexp(`_`) // DashPunct() Parser{ return Regexp(`-`) // OtherPunct() Parser{ return Regexp(`[!"#%&'*,./:;?@\\]`) // Symbol() Parser{ return Regexp(`[$+<=>^`+"`"+`|~]`) // CurrencySymbol() Parser{ return Regexp(`$`) // MathSymbol() Parser{ return Regexp(`[+<=>|~]`) // ModifierSymbol() Parser{ return Regexp(`[^`+"`"+`]`) // Control() Parser{ return Regexp(`[\x00-\x1F\x7F]`) //[:cntrl:] // Graphical() Parser{ return Regexp(`[\x21-\x7E]`) //[:graph:] // Printable() Parser{ return Regexp(`[\x20-\x7E]`) //[:print:] }) }
//================================================================================ func TestLexer(t *testing.T) { ts.LogAsserts("Lexer", t, func(tt *ts.T) { u := NewUnicodeLexer() tt.AssertEqual(parseStr(u.Letter(), "defg"), PState{input: "efg", pos: 1, value: u8.Text("d"), ok: true}) tt.AssertEqual(parseStr(u.Letter(), ";efg"), PState{input: ";efg", empty: true, error: makeUnexpInp(";")}) tt.AssertEqual(parseStr(u.Lower(), "defg"), PState{input: "efg", pos: 1, value: u8.Text("d"), ok: true}) tt.AssertEqual(parseStr(u.Lower(), "Defg"), PState{input: "Defg", empty: true, error: makeUnexpInp("D")}) tt.AssertEqual(parseStr(u.Lower(), ";efg"), PState{input: ";efg", empty: true, error: makeUnexpInp(";")}) tt.AssertEqual(parseStr(u.Upper(), "Defg"), PState{input: "efg", pos: 1, value: u8.Text("D"), ok: true}) tt.AssertEqual(parseStr(u.Upper(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) tt.AssertEqual(parseStr(u.Upper(), ";efg"), PState{input: ";efg", empty: true, error: makeUnexpInp(";")}) tt.AssertEqual(parseStr(u.Digit(), "6efg"), PState{input: "efg", pos: 1, value: u8.Text("6"), ok: true}) tt.AssertEqual(parseStr(u.Digit(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) tt.AssertEqual(parseStr(u.Whitespace(), " efg"), PState{input: "efg", pos: 1, value: u8.Codepoint(' '), ok: true}) tt.AssertEqual(parseStr(u.Whitespace(), "\tefg"), PState{input: "efg", pos: 1, value: u8.Codepoint('\t'), ok: true}) tt.AssertEqual(parseStr(u.Whitespace(), "\refg"), PState{input: "efg", pos: 1, value: u8.Codepoint('\r'), ok: true}) tt.AssertEqual(parseStr(u.Whitespace(), "\nefg"), PState{input: "efg", pos: 1, value: u8.Codepoint('\n'), ok: true}) tt.AssertEqual(parseStr(u.Whitespace(), "\fefg"), PState{input: "efg", pos: 1, value: u8.Codepoint('\f'), ok: true}) tt.AssertEqual(parseStr(u.Whitespace(), "\vefg"), PState{input: "efg", pos: 1, value: u8.Codepoint('\v'), ok: true}) tt.AssertEqual(parseStr(u.Whitespace(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) tt.AssertEqual(parseStr(u.Space(), " efg"), PState{input: "efg", pos: 1, value: u8.Text(" "), ok: true}) tt.AssertEqual(parseStr(u.Space(), "defg"), PState{input: "defg", empty: true, error: makeUnexpInp("d")}) // TitlecaseLetter() Parser{ return Regexp(`\p{Lt}`) // ModifyingLetter() Parser{ return Regexp(`\p{Lm}`) // OtherLetter() Parser{ return Regexp(`\p{Lo}`) // Number() Parser{ return Regexp(`\pN`) // LetterNumber() Parser{ return Regexp(`\p{Nl}`) // OtherNumber() Parser{ return Regexp(`\p{No}`) // Mark() Parser{ return Regexp(`\pM`) // SpacingMark() Parser{ return Regexp(`\p{Mc}`) // EnclosingMark() Parser{ return Regexp(`\p{Me}`) // NonspacingMark() Parser{ return Regexp(`\p{Mn}`) // Punct() Parser{ return Regexp(`\pP`) // StartPunct() Parser{ return Regexp(`\p{Ps}`) // EndPunct() Parser{ return Regexp(`\p{Pe}`) // InitialPunct() Parser{ return Regexp(`\p{Pi}`) // FinalPunct() Parser{ return Regexp(`\p{Pf}`) // ConnectorPunct() Parser{ return Regexp(`\p{Pc}`) // DashPunct() Parser{ return Regexp(`\p{Pd}`) // OtherPunct() Parser{ return Regexp(`\p{Po}`) // Symbol() Parser{ return Regexp(`\pS`) // CurrencySymbol() Parser{ return Regexp(`\p{Sc}`) // MathSymbol() Parser{ return Regexp(`\p{Sm}`) // ModifierSymbol() Parser{ return Regexp(`\p{Sk}`) // OtherSymbol() Parser{ return Regexp(`\p{So}`) // Spacing() Parser{ return Regexp(`\pZ`) // LineSeparator() Parser{ return Regexp(`\p{Zl}`) // ParagraphSeparator() Parser{ return Regexp(`\p{Zp}`) // OtherCategory() Parser{ return Regexp(`\pC`) // Control() Parser{ return Regexp(`\p{Cc}`) // Format() Parser{ return Regexp(`\p{Cf}`) // PrivateUse() Parser{ return Regexp(`\p{Co}`) // Nonchar() Parser{ return Regexp(`\p{Cn}`) // Surrogate() Parser{ return Regexp(`\p{Cs}`) // Graphical() Parser{ return len(cc) == 1 && unicode.IsGraphic(cc[0]) // Printable() Parser{ return len(cc) == 1 && unicode.IsPrint(cc[0]) // SpecialControl() Parser{ return len(cc) == 1 && unicode.IsControl(cc[0]) // CommonScript() Parser{ return len(cc) == 1 && unicode.In(cc[0], unicode.Common) // InheritedScript() Parser{ return len(cc) == 1 && unicode.In(cc[0], unicode.Inherited) // UnicodeIn(ranges ...*unicode.RangeTable) Parser { return len(cc) == 1 && unicode.In(cc[0], ranges...) }) }