char

package
v0.0.0-...-1dacd80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2022 License: Apache-2.0, MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlphaNum

func AlphaNum() parser.Func[rune, rune]

AlphaNum parses either an alphabet letter or digit.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.AlphaNum()

	fmt.Println(p([]rune("a")))
	fmt.Println(p([]rune("9")))
	fmt.Println(p([]rune("!")))

}
Output:

97 [] <nil>
57 [] <nil>
33 [] letter or digit, or, 2 errors occurred:
	* letter, satisfy, actual '!', unexpected
	* digit, satisfy, actual '!', unexpected

func Any

func Any() parser.Func[rune, rune]

Any parses any char.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Any()

	fmt.Println(p([]rune("apple")))
	fmt.Println(p(nil))

}
Output:

97 [112 112 108 101] <nil>
0 [] any, unexpected EOF

func Char

func Char(c rune) parser.Func[rune, rune]

Char parses a character and succeeds if the character is equal to `c`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Char('!')

	fmt.Println(p([]rune("!")))
	fmt.Println(p([]rune("A")))

}
Output:

33 [] <nil>
65 [] char, expected '!', actual 'A', unexpected

func Cmp

func Cmp(s string, cmp func(l, r rune) bool) parser.Func[rune, []rune]

Cmp parses the string `s`, using `cmp` to compare each character.

Example
package main

import (
	"fmt"
	"unicode"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Cmp("golang", func(l, r rune) bool {
		return unicode.ToLower(l) == unicode.ToLower(r)
	})

	fmt.Println(p([]rune("Golang")))
	fmt.Println(p([]rune("goto")))

}
Output:

[71 111 108 97 110 103] [] <nil>
[103 111 116] [111] cmp, expected "golang", actual "got", unexpected

func CrLf

func CrLf() parser.Func[rune, []rune]

CrLf parses carriage return and newline (`"\r\n"`), returning the newline character.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.CrLf()

	fmt.Println(p([]rune("\r\n")))
	fmt.Println(p([]rune("\r")))
	fmt.Println(p([]rune("\n")))

}
Output:

[13 10] [] <nil>
[13] [] crlf, and, unexpected EOF
[10] [] crlf, and, expected '\r', actual '\n', unexpected

func Digit

func Digit() parser.Func[rune, rune]

Digit parses a base-10 digit.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Digit()

	fmt.Println(p([]rune("9")))
	fmt.Println(p([]rune("A")))

}
Output:

57 [] <nil>
65 [] digit, satisfy, actual 'A', unexpected

func Float

func Float() parser.Func[rune, []rune]

Float parses a floating-point numbers.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/to"
)

func main() {
	p := to.Float(char.Float())

	fmt.Println(p([]rune("0")))
	fmt.Println(p([]rune("123")))
	fmt.Println(p([]rune("3.1415926")))
	fmt.Println(p([]rune("31415926e-7")))
	fmt.Println(p([]rune("nan")))
	fmt.Println(p([]rune("-inf")))
	fmt.Println(p([]rune("foobar")))

}
Output:

0 [] <nil>
123 [] <nil>
3.1415926 [] <nil>
3.1415926 [] <nil>
NaN [] <nil>
-Inf [] <nil>
0 [111 111 98 97 114] float, and then, map, recognize, pair, or, 3 errors occurred:
	* char fold, cmp, expected "nan", actual "f", unexpected
	* char fold, cmp, expected "inf", actual "f", unexpected
	* recognize, tuple3, many1, digit, satisfy, actual 'f', unexpected

func Fold

func Fold(s string) parser.Func[rune, []rune]

StringFold parses the string `s`, are equal under Unicode case-folding.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Fold("golang")

	fmt.Println(p([]rune("Golang")))
	fmt.Println(p([]rune("goto")))

}
Output:

[71 111 108 97 110 103] [] <nil>
[103 111 116] [111] char fold, cmp, expected "golang", actual "got", unexpected

func HexDigit

func HexDigit() parser.Func[rune, rune]

HexDigit parses a hexdecimal digit with uppercase and lowercase.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.HexDigit()

	fmt.Println(p([]rune("7")))
	fmt.Println(p([]rune("F")))
	fmt.Println(p([]rune("h")))

}
Output:

55 [] <nil>
70 [] <nil>
104 [] hex digit, satisfy, actual 'h', unexpected

func Letter

func Letter() parser.Func[rune, rune]

Letter parses an alphabet letter.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Letter()

	fmt.Println(p([]rune("a")))
	fmt.Println(p([]rune("A")))
	fmt.Println(p([]rune("9")))

}
Output:

97 [] <nil>
65 [] <nil>
57 [] letter, satisfy, actual '9', unexpected

func Lower

func Lower() parser.Func[rune, rune]

Lower parses a lowercase letter.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Lower()

	fmt.Println(p([]rune("a")))
	fmt.Println(p([]rune("A")))

}
Output:

97 [] <nil>
65 [] lowercase letter, satisfy, actual 'A', unexpected

func NewLine

func NewLine() parser.Func[rune, rune]

NewLine parses a newline character (`'\n'`).

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.NewLine()

	fmt.Println(p([]rune("\r")))
	fmt.Println(p([]rune("\n")))

}
Output:

13 [] newline, expected '\n', actual '\r', unexpected
10 [] <nil>

func NoneOf

func NoneOf(tokens string) parser.Func[rune, rune]

NoneOf extract one token and succeeds if it is not part of `tokens`.

func OctDigit

func OctDigit() parser.Func[rune, rune]

OctDigit parses an octal digit.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.OctDigit()

	fmt.Println(p([]rune("7")))
	fmt.Println(p([]rune("8")))

}
Output:

55 [] <nil>
56 [] octal digit, satisfy, actual '8', unexpected

func OneOf

func OneOf(tokens string) parser.Func[rune, rune]

OneOf extract one token and succeeds if it is part of `tokens`.

func Space

func Space() parser.Func[rune, rune]

Space parse a single whitespace.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Space()

	fmt.Println(p([]rune(" ")))
	fmt.Println(p([]rune("  ")))
	fmt.Println(p([]rune("!")))
	fmt.Println(p([]rune("")))

}
Output:

32 [] <nil>
32 [32] <nil>
33 [] whitespace, satisfy, actual '!', unexpected
0 [] whitespace, satisfy, unexpected EOF

func Spaces

func Spaces() parser.Func[rune, []rune]

Spaces parse zero or more spaces.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Spaces()

	fmt.Println(p([]rune(" ")))
	fmt.Println(p([]rune("  ")))
	fmt.Println(p([]rune("!")))
	fmt.Println(p([]rune("")))

}
Output:

[32] [] <nil>
[32 32] [] <nil>
[] [33] <nil>
[] [] <nil>

func String

func String(s string) parser.Func[rune, []rune]

String parses the string `s`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.String("apple")

	fmt.Println(p([]rune("apple")))
	fmt.Println(p(nil))

}
Output:

[97 112 112 108 101] [] <nil>
[] [] string, cmp, unexpected EOF

func Tab

func Tab() parser.Func[rune, rune]

Tab parses a tab character (`'\t'`).

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Tab()

	fmt.Println(p([]rune("\t")))
	fmt.Println(p([]rune(" ")))

}
Output:

9 [] <nil>
32 [] tab, expected '\t', actual ' ', unexpected

func Take

func Take(n int) parser.Func[rune, []rune]

Take reads a chars of length `n`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Take(2)

	fmt.Println(p([]rune("let")))
	fmt.Println(p([]rune("1")))

}
Output:

[108 101] [116] <nil>
[] [49] take, unexpected EOF

func Upper

func Upper() parser.Func[rune, rune]

Upper parses an uppercase letter.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
)

func main() {
	p := char.Upper()

	fmt.Println(p([]rune("a")))
	fmt.Println(p([]rune("A")))

}
Output:

97 [] uppercase letter, satisfy, actual 'a', unexpected
65 [] <nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL