• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moonbitlang / x / 301

10 Dec 2024 06:19AM UTC coverage: 85.204% (-2.6%) from 87.841%
301

Pull #78

github

web-flow
Merge b830031f4 into 91f0fdf48
Pull Request #78: feat: new package encoding

105 of 161 new or added lines in 3 files covered. (65.22%)

124 existing lines in 29 files now uncovered.

1169 of 1372 relevant lines covered (85.2%)

434.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.79
/json5/lex_number.mbt
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
///|
16
fn lex_decimal_integer(ctx : ParseContext, start~ : Int) -> Double!ParseError {
17
  for {
44✔
18
    match read_char(ctx) {
50✔
19
      Some('.') => return lex_decimal_point!(ctx, start~)
5✔
20
      Some('e' | 'E') => return lex_decimal_exponent!(ctx, start~)
5✔
21
      Some(c) => {
35✔
22
        if c >= '0' && c <= '9' {
23
          continue
6✔
24
        }
25
        ctx.offset -= 1
26
        return lex_number_end!(ctx, start, ctx.offset)
27
      }
29✔
28
      None => return lex_number_end!(ctx, start, ctx.offset)
3✔
29
    }
30
  }
31
}
32

33
///|
34
fn lex_decimal_point_leading(
35
  ctx : ParseContext,
36
  start~ : Int
37
) -> Double!ParseError {
38
  match read_char(ctx) {
4✔
39
    Some(c) => {
4✔
40
      if c >= '0' && c <= '9' {
41
        return lex_decimal_fraction!(ctx, start~)
3✔
42
      }
43
      ctx.offset -= 1
44
      invalid_char!(ctx)
UNCOV
45
    }
×
UNCOV
46
    None => parse_error!(InvalidEof)
×
47
  }
48
}
49

50
///|
51
fn lex_decimal_point(ctx : ParseContext, start~ : Int) -> Double!ParseError {
52
  match read_char(ctx) {
6✔
53
    Some('e' | 'E') => return lex_decimal_exponent!(ctx, start~)
1✔
54
    Some(c) => {
5✔
55
      if c >= '0' && c <= '9' {
56
        return lex_decimal_fraction!(ctx, start~)
4✔
57
      }
58
      ctx.offset -= 1
59
      return lex_number_end!(ctx, start, ctx.offset)
60
    }
1✔
UNCOV
61
    None => return lex_number_end!(ctx, start, ctx.offset)
×
62
  }
63
}
64

65
///|
66
fn lex_decimal_fraction(ctx : ParseContext, start~ : Int) -> Double!ParseError {
67
  for {
7✔
68
    match read_char(ctx) {
10✔
69
      Some('e' | 'E') => return lex_decimal_exponent!(ctx, start~)
2✔
70
      Some(c) => {
8✔
71
        if c >= '0' && c <= '9' {
72
          continue
3✔
73
        }
74
        ctx.offset -= 1
75
        return lex_number_end!(ctx, start, ctx.offset)
76
      }
5✔
UNCOV
77
      None => return lex_number_end!(ctx, start, ctx.offset)
×
78
    }
79
  }
80
}
81

82
///|
83
fn lex_decimal_exponent(ctx : ParseContext, start~ : Int) -> Double!ParseError {
84
  match read_char(ctx) {
11✔
85
    Some('+') | Some('-') => return lex_decimal_exponent_sign!(ctx, start~)
2✔
86
    Some(c) => {
8✔
87
      if c >= '0' && c <= '9' {
88
        return lex_decimal_exponent_integer!(ctx, start~)
7✔
89
      }
90
      ctx.offset -= 1
91
      invalid_char!(ctx)
UNCOV
92
    }
×
UNCOV
93
    None => parse_error!(InvalidEof)
×
94
  }
95
}
96

97
///|
98
fn lex_decimal_exponent_sign(
99
  ctx : ParseContext,
100
  start~ : Int
101
) -> Double!ParseError {
102
  match read_char(ctx) {
3✔
103
    Some(c) => {
3✔
104
      if c >= '0' && c <= '9' {
105
        return lex_decimal_exponent_integer!(ctx, start~)
2✔
106
      }
107
      ctx.offset -= 1
108
      invalid_char!(ctx)
UNCOV
109
    }
×
UNCOV
110
    None => parse_error!(InvalidEof)
×
111
  }
112
}
113

114
///|
115
fn lex_decimal_exponent_integer(
116
  ctx : ParseContext,
117
  start~ : Int
118
) -> Double!ParseError {
119
  for {
9✔
120
    match read_char(ctx) {
12✔
121
      Some(c) => {
11✔
122
        if c >= '0' && c <= '9' {
123
          continue
3✔
124
        }
125
        ctx.offset -= 1
126
        return lex_number_end!(ctx, start, ctx.offset)
127
      }
8✔
128
      None => return lex_number_end!(ctx, start, ctx.offset)
1✔
129
    }
130
  }
131
}
132

133
///|
134
fn lex_zero(ctx : ParseContext, neg~ : Bool, start~ : Int) -> Double!ParseError {
135
  match read_char(ctx) {
11✔
136
    Some('.') => return lex_decimal_point!(ctx, start~)
1✔
137
    Some('e' | 'E') => return lex_decimal_exponent!(ctx, start~)
1✔
138
    Some('x' | 'X') => return lex_hexadecimal!(ctx, neg~)
6✔
139
    Some(c) => {
2✔
140
      if c >= '0' && c <= '9' {
UNCOV
141
        ctx.offset -= 1
×
UNCOV
142
        invalid_char!(ctx)
×
143
      }
144
      ctx.offset -= 1
145
      return lex_number_end!(ctx, start, ctx.offset)
146
    }
2✔
UNCOV
147
    None => return lex_number_end!(ctx, start, ctx.offset)
×
148
  }
149
}
150

151
///|
152
fn lex_hexadecimal(ctx : ParseContext, neg~ : Bool) -> Double!ParseError {
153
  match read_char(ctx) {
7✔
154
    Some(c) => {
7✔
155
      if (c >= '0' && c <= '9') ||
156
        (c >= 'a' && c <= 'f') ||
157
        (c >= 'A' && c <= 'F') {
158
        let n = lex_hexadecimal_integer(ctx, hex_digit_to_int(c))
6✔
159
        return if neg { -n } else { n }
1✔
160
      }
161
      ctx.offset -= 1
162
      invalid_char!(ctx)
UNCOV
163
    }
×
UNCOV
164
    None => parse_error!(InvalidEof)
×
165
  }
166
}
167

168
///|
169
fn lex_hexadecimal_integer(ctx : ParseContext, n : Int) -> Double {
170
  for n = n.to_double() {
6✔
171
    match read_char(ctx) {
30✔
172
      Some(c) => {
28✔
173
        if (c >= '0' && c <= '9') ||
174
          (c >= 'a' && c <= 'f') ||
175
          (c >= 'A' && c <= 'F') {
176
          continue n * 16.0 + hex_digit_to_int(c).to_double()
24✔
177
        }
178
        ctx.offset -= 1
179
        return n
180
      }
181
      None => return n
2✔
182
    }
183
  }
184
}
185

186
///|
187
fn lex_number_end(
188
  ctx : ParseContext,
189
  start : Int,
190
  end : Int
191
) -> Double!ParseError {
192
  let s = ctx.input.substring(start~, end~)
49✔
193
  match @strconv.parse_double?(s) {
194
    Ok(d) => d
49✔
195
    Err(_) =>
UNCOV
196
      parse_error!(InvalidNumber(offset_to_position(ctx.input, start), s))
×
197
  }
198
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc