|
| 1 | +// Copyright 2024 International Digital Economy Academy |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +priv struct ParseContext { |
| 16 | + mut offset : Int |
| 17 | + input : String |
| 18 | + end_offset : Int |
| 19 | +} |
| 20 | + |
| 21 | +fn ParseContext::make(input : String) -> ParseContext { |
| 22 | + { offset: 0, input, end_offset: input.length() } |
| 23 | +} |
| 24 | + |
| 25 | +priv type CharClass Array[(Char, Char)] |
| 26 | + |
| 27 | +fn CharClass::from_array(array : Array[(Char, Char)]) -> CharClass { |
| 28 | + CharClass(array) |
| 29 | +} |
| 30 | + |
| 31 | +fn contains(self : CharClass, c : Char) -> Bool { |
| 32 | + for left = 0, right = self.0.length(); left < right; { |
| 33 | + let middle = (left + right) / 2 |
| 34 | + let (min, max) = self.0[middle] |
| 35 | + if c < min { |
| 36 | + continue left, middle |
| 37 | + } else if c > max { |
| 38 | + continue middle + 1, right |
| 39 | + } else { |
| 40 | + break true |
| 41 | + } |
| 42 | + } else { |
| 43 | + false |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +priv enum Token { |
| 48 | + Null |
| 49 | + True |
| 50 | + False |
| 51 | + Number(Double) |
| 52 | + String(String) |
| 53 | + LBrace |
| 54 | + RBrace |
| 55 | + LBracket |
| 56 | + RBracket |
| 57 | + Comma |
| 58 | + Colon |
| 59 | +} derive(Eq, Debug, Show) |
| 60 | + |
| 61 | +priv struct StringBuilder { |
| 62 | + mut buffer : String |
| 63 | +} |
| 64 | + |
| 65 | +fn StringBuilder::make() -> StringBuilder { |
| 66 | + { buffer: "" } |
| 67 | +} |
| 68 | + |
| 69 | +fn add_string(self : StringBuilder, s : String) -> Unit { |
| 70 | + self.buffer = self.buffer + s |
| 71 | +} |
| 72 | + |
| 73 | +fn add_substring( |
| 74 | + self : StringBuilder, |
| 75 | + s : String, |
| 76 | + start : Int, |
| 77 | + end : Int |
| 78 | +) -> Unit { |
| 79 | + self.buffer = self.buffer + s.substring(~start, ~end) |
| 80 | +} |
| 81 | + |
| 82 | +fn add_char(self : StringBuilder, c : Char) -> Unit { |
| 83 | + self.buffer = self.buffer + c.to_string() |
| 84 | +} |
| 85 | + |
| 86 | +fn to_string(self : StringBuilder) -> String { |
| 87 | + self.buffer |
| 88 | +} |
0 commit comments