%PDF- %PDF-
Direktori : /usr/share/doc/re2c/examples/go/submatch/ |
Current File : //usr/share/doc/re2c/examples/go/submatch/03_posix.go |
// Code generated by re2c, DO NOT EDIT. //line "go/submatch/03_posix.re":1 //go:generate re2go $INPUT -o $OUTPUT package main import "reflect" // Maximum number of capturing groups among all rules. //line "go/submatch/03_posix.go":10 var YYMAXNMATCH int = 4 //line "go/submatch/03_posix.re":7 type SemVer struct { major, minor, patch int } func s2n(s string) int { // convert pre-parsed string to a number n := 0 for _, c := range s { n = n*10 + int(c-'0') } return n } func parse(str string) *SemVer { var cur, mar int // Allocate memory for capturing parentheses (twice the number of groups). yypmatch := make([]int, YYMAXNMATCH*2) var yynmatch int // Autogenerated tag variables used by the lexer to track tag values. //line "go/submatch/03_posix.go":32 var yyt1 int var yyt2 int var yyt3 int var yyt4 int var yyt5 int //line "go/submatch/03_posix.re":25 //line "go/submatch/03_posix.go":42 { var yych byte yych = str[cur] switch (yych) { case '0','1','2','3','4','5','6','7','8','9': yyt1 = cur goto yy3 default: goto yy1 } yy1: cur += 1 yy2: //line "go/submatch/03_posix.re":54 { return nil } //line "go/submatch/03_posix.go":58 yy3: cur += 1 mar = cur yych = str[cur] switch (yych) { case '.': goto yy4 case '0','1','2','3','4','5','6','7','8','9': goto yy6 default: goto yy2 } yy4: cur += 1 yych = str[cur] switch (yych) { case '0','1','2','3','4','5','6','7','8','9': yyt2 = cur goto yy7 default: goto yy5 } yy5: cur = mar goto yy2 yy6: cur += 1 yych = str[cur] switch (yych) { case '.': goto yy4 case '0','1','2','3','4','5','6','7','8','9': goto yy6 default: goto yy5 } yy7: cur += 1 yych = str[cur] switch (yych) { case 0x00: yyt3 = cur yyt4 = -1 yyt5 = -1 goto yy8 case '.': yyt3 = cur yyt5 = cur goto yy9 case '0','1','2','3','4','5','6','7','8','9': goto yy7 default: goto yy5 } yy8: cur += 1 yynmatch = 4 yypmatch[2] = yyt1 yypmatch[4] = yyt2 yypmatch[5] = yyt3 yypmatch[6] = yyt5 yypmatch[7] = yyt4 yypmatch[0] = yyt1 yypmatch[1] = cur yypmatch[3] = yyt2 yypmatch[3] += -1 //line "go/submatch/03_posix.re":41 { // `yynmatch` is the number of capturing groups if yynmatch != 4 { panic("expected 4 submatch groups") } // Even `yypmatch` values are for opening parentheses, odd values // are for closing parentheses, the first group is the whole match. major := s2n(str[yypmatch[2]:yypmatch[3]]) minor := s2n(str[yypmatch[4]:yypmatch[5]]) patch := 0 if yypmatch[6] != -1 { patch = s2n(str[yypmatch[6]+1:yypmatch[7]]) } return &SemVer{major, minor, patch} } //line "go/submatch/03_posix.go":139 yy9: cur += 1 yych = str[cur] if (yych <= 0x00) { goto yy5 } goto yy11 yy10: cur += 1 yych = str[cur] yy11: switch (yych) { case 0x00: yyt4 = cur goto yy8 case '0','1','2','3','4','5','6','7','8','9': goto yy10 default: goto yy5 } } //line "go/submatch/03_posix.re":55 } func main() { assert_eq := func(x, y *SemVer) { if !reflect.DeepEqual(x, y) { panic("error") } } assert_eq(parse("23.34\000"), &SemVer{23, 34, 0}) assert_eq(parse("1.2.9999\000"), &SemVer{1, 2, 9999}) assert_eq(parse("1.a\000"), nil) }