1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// Generated by re2zig
// re2zig $INPUT -o $OUTPUT
const std = @import("std");
const none = std.math.maxInt(usize);
const SemVer = struct {
major: u32,
minor: u32,
patch: u32,
};
fn s2n(str: []const u8) u32 { // convert pre-parsed string to a number
var n: u32 = 0;
for (str) |c| { n = n * 10 + (c - 48); }
return n;
}
fn parse(yyinput: [:0]const u8) ?SemVer {
var yycursor: usize = 0;
var yymarker: usize = 0;
// Final tag variables available in semantic action.
var yytl0: usize = none;var yytl1: usize = none;var yytl2: usize = none;var yytl3: usize = none;var yytr0: usize = none;var yytr1: usize = none;var yytr2: usize = none;var yytr3: usize = none;
// Intermediate tag variables used by the lexer (must be autogenerated).
var yyt1: usize = none;var yyt2: usize = none;var yyt3: usize = none;var yyt4: usize = none;var yyt5: usize = none;
var yych: u8 = 0;
var yystate: u32 = 0;
yyl: while (true) {
switch (yystate) {
0 => {
yych = yyinput[yycursor];
switch (yych) {
0x30...0x39 => {
yyt1 = yycursor;
yycursor += 1;
yystate = 3;
continue :yyl;
},
else => {
yycursor += 1;
yystate = 1;
continue :yyl;
},
}
},
1 => {
yystate = 2;
continue :yyl;
},
2 => { return null; },
3 => {
yymarker = yycursor;
yych = yyinput[yycursor];
switch (yych) {
0x2E => {
yycursor += 1;
yystate = 4;
continue :yyl;
},
0x30...0x39 => {
yycursor += 1;
yystate = 6;
continue :yyl;
},
else => {
yystate = 2;
continue :yyl;
},
}
},
4 => {
yych = yyinput[yycursor];
switch (yych) {
0x30...0x39 => {
yyt2 = yycursor;
yycursor += 1;
yystate = 7;
continue :yyl;
},
else => {
yystate = 5;
continue :yyl;
},
}
},
5 => {
yycursor = yymarker;
yystate = 2;
continue :yyl;
},
6 => {
yych = yyinput[yycursor];
switch (yych) {
0x2E => {
yycursor += 1;
yystate = 4;
continue :yyl;
},
0x30...0x39 => {
yycursor += 1;
yystate = 6;
continue :yyl;
},
else => {
yystate = 5;
continue :yyl;
},
}
},
7 => {
yych = yyinput[yycursor];
switch (yych) {
0x00 => {
yyt3 = yycursor;
yyt4 = std.math.maxInt(usize);
yyt5 = std.math.maxInt(usize);
yycursor += 1;
yystate = 8;
continue :yyl;
},
0x2E => {
yyt3 = yycursor;
yyt5 = yycursor;
yycursor += 1;
yystate = 9;
continue :yyl;
},
0x30...0x39 => {
yycursor += 1;
yystate = 7;
continue :yyl;
},
else => {
yystate = 5;
continue :yyl;
},
}
},
8 => {
yytl1 = yyt1;
yytl2 = yyt2;
yytr2 = yyt3;
yytl3 = yyt5;
yytr3 = yyt4;
yytl0 = yyt1;
yytr0 = yycursor;
yytr1 = yyt2;
yytr1 -= 1;
return SemVer {
.major = s2n(yyinput[yytl1..yytr1]),
.minor = s2n(yyinput[yytl2..yytr2]),
.patch = if (yytl3 == none) 0 else s2n(yyinput[yytl3 + 1..yytr3])
};
},
9 => {
yych = yyinput[yycursor];
if (yych <= 0x00) {
yystate = 5;
continue :yyl;
}
yystate = 11;
continue :yyl;
},
10 => {
yych = yyinput[yycursor];
yystate = 11;
continue :yyl;
},
11 => {
switch (yych) {
0x00 => {
yyt4 = yycursor;
yycursor += 1;
yystate = 8;
continue :yyl;
},
0x30...0x39 => {
yycursor += 1;
yystate = 10;
continue :yyl;
},
else => {
yystate = 5;
continue :yyl;
},
}
},
else => { @panic("internal lexer error"); },
}
}
}
test {
try std.testing.expectEqual(parse("23.34"), SemVer{.major = 23, .minor = 34, .patch = 0});
try std.testing.expectEqual(parse("1.2.99999"), SemVer{.major = 1, .minor = 2, .patch = 99999});
try std.testing.expectEqual(parse("1.a"), null);
}
|