1///|
2pub fn type Parser[A] (Input) -> (A, Input) raise ParseError
Parser::(f : (Char) -> Bool, label~ : String) -> Parser[Char]
satisfy((Char) -> Bool
f : (Char
Char) -> Bool
Bool, String
label~ : String
String) -> type Parser[A] (Input) -> (A, Input) raise ParseError
Parser[Char
Char] {
3 fn(Input
input) {
4 let { @string.StringView
chars, Int
row, Int
col } = Input
input
5 match @string.StringView
chars {
6 @string.StringView
[Char
x@string.StringView
, .. chars] if (Char) -> Bool
f(Char
x) =>
7 if Char
x (self : Char, other : Char) -> Bool
Compares two characters for equality.
Parameters:
self : The first character to compare.
other : The second character to compare.
Returns true if both characters represent the same Unicode code point,
false otherwise.
Example:
let a = 'A'
let b = 'A'
let c = 'B'
inspect(a == b, content="true")
inspect(a == c, content="false")
== '\n' {
8 (Char
x, struct Input {
chars: @string.StringView
row: Int
col: Int
}
Input::{ @string.StringView
chars, Int
row: Int
row (self : Int, other : Int) -> Int
Adds two 32-bit signed integers. Performs two's complement arithmetic, which
means the operation will wrap around if the result exceeds the range of a
32-bit integer.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns a new integer that is the sum of the two operands. If the
mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to
2,147,483,647), the result wraps around according to two's complement rules.
Example:
inspect(42 + 1, content="43")
inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+ 1, Int
col: 0 })
9 } else {
10 (Char
x, struct Input {
chars: @string.StringView
row: Int
col: Int
}
Input::{ ..Input
input, @string.StringView
chars, Int
col: Int
col (self : Int, other : Int) -> Int
Adds two 32-bit signed integers. Performs two's complement arithmetic, which
means the operation will wrap around if the result exceeds the range of a
32-bit integer.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns a new integer that is the sum of the two operands. If the
mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to
2,147,483,647), the result wraps around according to two's complement rules.
Example:
inspect(42 + 1, content="43")
inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+ 1 })
11 }
12 _ => raise (row~ : Int, col~ : Int, msg : String) -> ParseError
expect(Int
row~, Int
col~, String
label)
13 }
14 }
15}
16
17///|
18pub fn type Parser[A] (Input) -> (A, Input) raise ParseError
Parser::(ch : Char) -> Parser[Char]
char(Char
ch : Char
Char) -> type Parser[A] (Input) -> (A, Input) raise ParseError
Parser[Char
Char] {
19 (f : (Char) -> Bool, label~ : String) -> Parser[Char]
satisfy(Char
c => Char
c (self : Char, other : Char) -> Bool
Compares two characters for equality.
Parameters:
self : The first character to compare.
other : The second character to compare.
Returns true if both characters represent the same Unicode code point,
false otherwise.
Example:
let a = 'A'
let b = 'A'
let c = 'B'
inspect(a == b, content="true")
inspect(a == c, content="false")
== Char
ch, String
label=Char
ch.(self : Char) -> String
Convert Char to String
to_string())
20}
21
22///|
23pub fn type Parser[A] (Input) -> (A, Input) raise ParseError
Parser::(xs : String) -> Parser[Char]
one_of(String
xs : String
String) -> type Parser[A] (Input) -> (A, Input) raise ParseError
Parser[Char
Char] {
24 (f : (Char) -> Bool, label~ : String) -> Parser[Char]
satisfy((String, Char) -> Bool
String::(self : String, c : Char) -> Bool
Returns true if this string contains the given character.
contains_char(String
xs, _), String
label="one of \{String
xs.(self : String) -> Iter[Char]
Returns an iterator over the Unicode characters in the string.
Note: This iterator yields Unicode characters, not Utf16 code units.
As a result, the count of characters returned by iter().count() may not be equal to the length of the string returned by length().
let s = "Hello, World!🤣";
assert_eq(s.iter().count(), 14); // Unicode characters
assert_eq(s.length(), 15); // Utf16 code units
iter()}}")
25}
26
27///|
28pub fn type Parser[A] (Input) -> (A, Input) raise ParseError
Parser::(xs : String) -> Parser[Char]
none_of(String
xs : String
String) -> type Parser[A] (Input) -> (A, Input) raise ParseError
Parser[Char
Char] {
29 (f : (Char) -> Bool, label~ : String) -> Parser[Char]
satisfy(
30 Char
ch => (x : Bool) -> Bool
Performs logical negation on a boolean value.
Parameters:
value : The boolean value to negate.
Returns the logical NOT of the input value: true if the input is false,
and false if the input is true.
Example:
inspect(not(true), content="false")
inspect(not(false), content="true")
not((String, Char) -> Bool
String::(self : String, c : Char) -> Bool
Returns true if this string contains the given character.
contains_char(String
xs, Char
ch)),
31 String
label="none of \{String
xs.(self : String) -> Iter[Char]
Returns an iterator over the Unicode characters in the string.
Note: This iterator yields Unicode characters, not Utf16 code units.
As a result, the count of characters returned by iter().count() may not be equal to the length of the string returned by length().
let s = "Hello, World!🤣";
assert_eq(s.iter().count(), 14); // Unicode characters
assert_eq(s.length(), 15); // Utf16 code units
iter()}}",
32 )
33}
34
35///|
36pub fn type Parser[A] (Input) -> (A, Input) raise ParseError
Parser::(str : String) -> Parser[String]
string(String
str : String
String) -> type Parser[A] (Input) -> (A, Input) raise ParseError
Parser[String
String] {
37 fn(Input
input) {
38 guard (x : Bool) -> Bool
Performs logical negation on a boolean value.
Parameters:
value : The boolean value to negate.
Returns the logical NOT of the input value: true if the input is false,
and false if the input is true.
Example:
inspect(not(true), content="false")
inspect(not(false), content="true")
not(String
str.(self : String, c : Char) -> Bool
Returns true if this string contains the given character.
contains_char('\n')) else { () -> (String, Input)
panic() }
39 let { @string.StringView
chars, Int
row, Int
col } = Input
input
40 let Int
len = String
str.(self : String) -> Int
Returns the number of UTF-16 code units in the string. Note that this is not
necessarily equal to the number of Unicode characters (code points) in the
string, as some characters may be represented by multiple UTF-16 code units.
Parameters:
string : The string whose length is to be determined.
Returns the number of UTF-16 code units in the string.
Example:
inspect("hello".length(), content="5")
inspect("🤣".length(), content="2") // Emoji uses two UTF-16 code units
inspect("".length(), content="0") // Empty string
length()
41 guard @string.StringView
chars.(self : @string.StringView) -> Int
Returns the length of the view.
This method counts the charcodes(code unit) in the view and has O(1) complexity.
length() (self_ : Int, other : Int) -> Bool
>= Int
len (Bool, Bool) -> Bool
&& @string.StringView
chars.(self : @string.StringView, end_offset~ : Int, start_offset~ : Int = ..) -> @string.StringView
Returns a new view of the view with the given start and end offsets.
view(Int
end_offset=Int
len) (self : @string.StringView, other : @string.StringView) -> Bool
Compares two views for equality. Returns true only if both views
have the same length and contain identical characters in the same order.
== String
str else {
42 raise (row~ : Int, col~ : Int, msg : String) -> ParseError
expect(Int
row~, Int
col~, String
str)
43 }
44 (
45 String
str,
46 struct Input {
chars: @string.StringView
row: Int
col: Int
}
Input::{ ..Input
input, @string.StringView
chars: @string.StringView
chars.(self : @string.StringView, start_offset~ : Int, end_offset? : Int) -> @string.StringView
Returns a new view of the view with the given start and end offsets.
view(Int
start_offset=Int
len), Int
col: Int
col (self : Int, other : Int) -> Int
Adds two 32-bit signed integers. Performs two's complement arithmetic, which
means the operation will wrap around if the result exceeds the range of a
32-bit integer.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns a new integer that is the sum of the two operands. If the
mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to
2,147,483,647), the result wraps around according to two's complement rules.
Example:
inspect(42 + 1, content="43")
inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+ Int
len },
47 )
48 }
49}
50
51///|
52pub fn[A] type Parser[A] (Input) -> (A, Input) raise ParseError
Parser::(x : A) -> Parser[A]
eof(A
x : type parameter A
A) -> type Parser[A] (Input) -> (A, Input) raise ParseError
Parser[type parameter A
A] {
53 fn(Input
input) {
54 let { @string.StringView
chars, Int
row, Int
col } = Input
input
55 match @string.StringView
chars {
56 [] => (A
x, Input
input)
57 _ => raise (row~ : Int, col~ : Int, msg : String) -> ParseError
expect(Int
row~, Int
col~, "eof")
58 }
59 }
60}
61