Logo

index : blog

---

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/blog.git/html/src/gen/parser.jai blob: c6431ba13cd73e446d9c681a20ced15e34281524 [raw] [clear marker]

        
0
1
2PARSER_ERROR_MSG :: #string STR_END
3-- YOUR STRING ---------------------------
4%1
5------------------------------------------
6Parsing error at: %3
7Could not find termination character: »%2«
8
9STR_END;
10
11
12parser_is_at_end :: inline (idx: int, s: string) -> bool {
13 return idx > s.count-1;
14}
15
16parser_peek :: inline (idx: int, s: string) -> u8 {
17 if idx > s.count-1 return "\0";
18 return s[idx];
19}
20
21parser_peek_forward :: inline (idx: int, s: string, offset: int) -> u8 {
22 if idx+offset > s.count-1 return "\0";
23 return s[idx+offset];
24}
25
26parser_pop :: inline (idx: *int, s: string) -> u8 {
27 value := s[idx.*];
28 idx.* += 1;
29 return value;
30}
31
32parser_advance_by :: inline (idx: *int, offset: int) {
33 idx.* += offset;
34}
35
36parser_advance_by :: inline (idx: *int, s: string) {
37 idx.* += s.count;
38}
39
40parser_match :: (s: string, substring: string, offset := 0) -> bool {
41 if substring.count + offset > s.count return false;
42
43 for substring if it != s[it_index + offset] return false;
44
45 return true;
46}
47
48parser_consume_including :: inline (
49 match_index: int,
50 data: string,
51 char: u8
52)
53 -> string
54{
55 return parser_consume_till(match_index, data, char, true);
56}
57
58parser_consume_including :: inline (
59 match_index: int,
60 data: string,
61 substring: string
62)
63 -> string
64{
65 return parser_consume_till(match_index, data, substring, true);
66}
67
68parser_consume_till :: (
69 match_index: int,
70 data: string,
71 char: u8,
72 include := false,
73)
74 -> string
75{
76 count := find_index_from_left(data, char, match_index);
77 if count == -1 return "";
78
79 offset := ifx include then 1 else 0;
80 my_slice := slice(data, match_index, count - match_index + offset);
81 return copy_string(my_slice);
82}
83
84parser_consume_till :: (
85 match_index: int,
86 data: string,
87 substring: string,
88 include := false
89)
90 -> string
91{
92 last := substring[substring.count-1];
93 count := find_index_from_left(data, last, match_index);
94 if count == -1 return "";
95
96 if substring.count > 1 {
97 for < i: 0..substring.count-2 {
98 if substring[i] != data[count+i-1] return "";
99 }
100 }
101
102 offset := ifx include then 1 else 0;
103 my_slice := slice(data, match_index, count - match_index + offset);
104 return copy_string(my_slice);
105}
106
107parse_to_int :: (s: string, $T: Type = int, loc := #caller_location) -> T #expand {
108 for s if !is_digit(it) {
109 log_error("%", loc);
110 log_error(
111 "Your input is not a digit: '%'. Invalid character: %",
112 s, string.{ 1, *it }
113 );
114 exit(1);
115 }
116
117 maybe_integer, ok := parse_int(*s, T);
118
119 if !ok {
120 log_error("%: Could not parse digits: '%'", loc, s);
121 exit(1);
122 }
123
124 return maybe_integer;
125}
126
127
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit