diff --git a/internal/parser/lexer.go b/internal/parser/lexer.go index 5da8719..7ae7e3b 100644 --- a/internal/parser/lexer.go +++ b/internal/parser/lexer.go @@ -6,7 +6,7 @@ import ( var lex = lexer.MustSimple([]lexer.SimpleRule{ {Name: "header", Pattern: `TAF (AMD|COR)? ?`}, - {Name: "Remark", Pattern: `RMK .*`}, + {Name: "Remark", Pattern: `RMK[^\n]*`}, {Name: "Number", Pattern: `\d+`}, {Name: "Modifier", Pattern: `[+-]|VC`}, {Name: "Prob", Pattern: "PROB"}, diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 73fcdb4..575e3e4 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -34,8 +34,8 @@ type ValidPair struct { type Probability struct { Pos lexer.Position - Value string `Prob @Number` - Time string `(WS @Number)?` + Value string `Prob @Number` + Valid ValidPair `(WS @@)?` } type WindSpeed struct { diff --git a/taf.go b/taf.go index 73098d0..2b7dea5 100644 --- a/taf.go +++ b/taf.go @@ -322,7 +322,7 @@ func DecodeWithOptions(r io.Reader, opts Options) (*Forecast, error) { case item.Probability != nil: // If the time is empty, this probability belongs to the // next change. - if item.Probability.Time == "" { + if item.Probability.Valid.Start == "" { prob, err := strconv.Atoi(item.Probability.Value) if err != nil { return nil, participle.Errorf(item.Probability.Pos, "prob: %s", err) @@ -332,34 +332,11 @@ func DecodeWithOptions(r io.Reader, opts Options) (*Forecast, error) { } else { pr := &Probability{} - // The probability time string must have 4 characters, - // 2 for starting time and 2 for ending time - if len(item.Probability.Time) < 4 { - return nil, participle.Errorf(item.Probability.Pos, "prob: invalid time %q", item.Probability.Time) - } - - startStr := item.Probability.Time[:2] - endStr := item.Probability.Time[2:] - - start, err := strconv.Atoi(startStr) + pr.Valid, err = parseValid(&item.Probability.Valid, opts.Month, opts.Year) if err != nil { return nil, participle.Errorf(item.Probability.Pos, "prob: %s", err) } - end, err := strconv.Atoi(endStr) - if err != nil { - return nil, participle.Errorf(item.Probability.Pos, "prob: %s", err) - } - - t := fc.PublishTime - pr.Valid = ValidPair{ - From: time.Date(t.Year(), t.Month(), t.Day(), start, 0, 0, 0, time.UTC), - To: time.Date(t.Year(), t.Month(), t.Day(), end, 0, 0, 0, time.UTC), - } - - // Get the duration by subtracting the from time from the to time - pr.Valid.Duration = pr.Valid.To.Sub(pr.Valid.From) - fc.Probabilities = append(fc.Probabilities, pr) // Set out to the probability value so that future mutations @@ -367,7 +344,7 @@ func DecodeWithOptions(r io.Reader, opts Options) (*Forecast, error) { out = reflect.ValueOf(pr).Elem() } case item.Remark != nil: - fc.Remark = *item.Remark + fc.Remark = strings.TrimSpace(strings.TrimPrefix(*item.Remark, "RMK")) } }